|
23 | 23 |
|
24 | 24 | namespace lsm::db { |
25 | 25 |
|
| 26 | +using internal::operator""_seqno; |
| 27 | + |
26 | 28 | class memtable::iterator : public internal::iterator { |
27 | 29 | public: |
28 | 30 | // The dummy iterator is only a place holder for the linked list in the |
@@ -140,19 +142,50 @@ class memtable::iterator : public internal::iterator { |
140 | 142 | _it; |
141 | 143 | }; |
142 | 144 |
|
143 | | -void memtable::apply(internal::write_batch batch) { |
144 | | - if (_last_seqno) { |
145 | | - dassert( |
146 | | - _last_seqno < batch.last_seqno(), |
147 | | - "expected new batch seqno to be greater than what is applied: {} < " |
148 | | - "{}", |
149 | | - _last_seqno.value(), |
150 | | - batch.last_seqno()); |
151 | | - } |
| 145 | +void memtable::put(internal::key key, iobuf value) { |
| 146 | + internal::key_view::parts parts = internal::key_view{key}.decode(); |
| 147 | + vassert( |
| 148 | + parts.type == internal::value_type::value, |
| 149 | + "when add a put to a memtable, keys much be of value type", |
| 150 | + key); |
| 151 | + vassert( |
| 152 | + parts.seqno >= _last_seqno, |
| 153 | + "seqno should only go up: {} >= {}", |
| 154 | + parts.seqno); |
| 155 | + invalidate_iterators(); |
| 156 | + _memory_usage += key.memory_usage() + value.memory_usage(); |
| 157 | + _last_seqno = parts.seqno; |
| 158 | + _table.emplace(std::move(key), std::move(value)); |
| 159 | +} |
| 160 | + |
| 161 | +void memtable::remove(internal::key key) { |
| 162 | + internal::key_view::parts parts = internal::key_view{key}.decode(); |
| 163 | + vassert( |
| 164 | + parts.type == internal::value_type::tombstone, |
| 165 | + "when add a tombstone to a memtable, keys much be of tombstone type", |
| 166 | + key); |
| 167 | + vassert( |
| 168 | + parts.seqno >= _last_seqno, |
| 169 | + "seqno should only go up: {} >= {}", |
| 170 | + parts.seqno); |
| 171 | + invalidate_iterators(); |
| 172 | + iobuf value; |
| 173 | + _memory_usage += key.memory_usage() + value.memory_usage(); |
| 174 | + _last_seqno = parts.seqno; |
| 175 | + _table.emplace(std::move(key), std::move(value)); |
| 176 | +} |
| 177 | + |
| 178 | +void memtable::merge(ss::lw_shared_ptr<memtable> other) { |
| 179 | + vassert( |
| 180 | + _last_seqno < other->last_seqno(), |
| 181 | + "expected new batch seqno to be greater than what is applied: {} < " |
| 182 | + "{}", |
| 183 | + _last_seqno.value_or(0_seqno), |
| 184 | + other->last_seqno().value_or(0_seqno)); |
152 | 185 | invalidate_iterators(); |
153 | | - _memory_usage += batch.memory_usage(); |
154 | | - _last_seqno = batch.last_seqno(); |
155 | | - _table.merge(std::move(batch.entries())); |
| 186 | + _memory_usage += other->approximate_memory_usage(); |
| 187 | + _last_seqno = other->last_seqno(); |
| 188 | + _table.merge(std::move(other->_table)); |
156 | 189 | } |
157 | 190 |
|
158 | 191 | lookup_result memtable::get(internal::key_view key) { |
|
0 commit comments