sonic/executor/flushb.rs
1// Sonic
2//
3// Fast, lightweight and schema-less search backend
4// Copyright: 2019, Valerian Saliou <valerian@valeriansaliou.name>
5// Copyright: 2026, Rémi Bardon <remi@remibardon.name>
6// License: Mozilla Public License v2.0 (MPL v2.0)
7
8use crate::store::StoreItem;
9use crate::store::kv::{StoreKVAcquireMode, StoreKVActionBuilder};
10
11impl super::Executor {
12 pub fn flushb(&self, item: StoreItem) -> Result<u32, ()> {
13 if let StoreItem(collection, Some(bucket), None) = item {
14 // Important: acquire database access read lock, and reference it in context. This \
15 // prevents the database from being erased while using it in this block.
16 // Notice: acquire FST lock in write mode, as we will erase it.
17 let _kv_read_guard = self.kv_pool.lock_read_access();
18 let _fst_write_guard = self.fst_pool.lock_write_access();
19
20 if let Ok(kv_store) = self
21 .kv_pool
22 .acquire(StoreKVAcquireMode::OpenOnly, collection)
23 {
24 // Important: acquire bucket store write lock
25 executor_kv_lock_write!(kv_store);
26
27 if kv_store.is_some() {
28 // Store exists, proceed erasure.
29 tracing::debug!(
30 "collection store exists, erasing: {} from {}",
31 bucket.as_str(),
32 collection.as_str()
33 );
34
35 let kv_action = StoreKVActionBuilder::access(bucket, kv_store);
36
37 // Notice: we cannot use the provided KV bucket erasure helper there, as \
38 // erasing a bucket requires a database lock, which would incur a dead-lock, \
39 // thus we need to perform the erasure from there.
40 if let Ok(erase_count) = kv_action.batch_erase_bucket() {
41 if self.fst_pool.erase(collection, Some(bucket)).is_ok() {
42 tracing::debug!("done with bucket erasure");
43
44 return Ok(erase_count);
45 }
46 }
47 } else {
48 // Store does not exist, consider as already erased.
49 tracing::debug!(
50 "collection store does not exist, consider {} from {} already erased",
51 bucket.as_str(),
52 collection.as_str()
53 );
54
55 return Ok(0);
56 }
57 }
58 }
59
60 Err(())
61 }
62}