Skip to content

Commit 3d94036

Browse files
committed
feat: Tree::clear
1 parent 3c952c9 commit 3d94036

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/abstract.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,16 @@ pub trait AbstractTree {
161161
///
162162
/// # Errors
163163
///
164-
/// Will return `Err` only if an IO error occurs during compaction.
164+
/// Will return `Err` only if an IO error occurs.
165165
fn drop_range<K: AsRef<[u8]>, R: RangeBounds<K>>(&self, range: R) -> crate::Result<()>;
166166

167+
/// Drops all tables and clears all memtables atomically.
168+
///
169+
/// # Errors
170+
///
171+
/// Will return `Err` only if an IO error occurs.
172+
fn clear(&self) -> crate::Result<()>;
173+
167174
/// Performs major compaction, blocking the caller until it's done.
168175
///
169176
/// # Errors

src/blob_tree/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ impl AbstractTree for BlobTree {
269269
self.index.drop_range(range)
270270
}
271271

272+
fn clear(&self) -> crate::Result<()> {
273+
self.index.clear()
274+
}
275+
272276
fn major_compact(&self, target_size: u64, seqno_threshold: SeqNo) -> crate::Result<()> {
273277
self.index.major_compact(target_size, seqno_threshold)
274278
}

src/tree/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ impl AbstractTree for Tree {
208208
self.inner_compact(strategy, 0)
209209
}
210210

211+
fn clear(&self) -> crate::Result<()> {
212+
let mut versions = self.get_version_history_lock();
213+
214+
versions.upgrade_version(
215+
&self.config.path,
216+
|v| {
217+
let mut copy = v.clone();
218+
copy.active_memtable = Arc::new(Memtable::new(self.memtable_id_counter.next()));
219+
copy.sealed_memtables = Arc::default();
220+
copy.version = Version::new(v.version.id() + 1, self.tree_type());
221+
Ok(copy)
222+
},
223+
&self.config.seqno,
224+
)
225+
}
226+
211227
#[doc(hidden)]
212228
fn major_compact(&self, target_size: u64, seqno_threshold: SeqNo) -> crate::Result<()> {
213229
let strategy = Arc::new(crate::compaction::major::Strategy::new(target_size));

0 commit comments

Comments
 (0)