|
6 | 6 |
|
7 | 7 | use crate::store::fst::StoreFSTActionBuilder;
|
8 | 8 | use crate::store::item::StoreItem;
|
9 |
| -use crate::store::kv::StoreKVActionBuilder; |
| 9 | +use crate::store::kv::{StoreKVAcquireMode, StoreKVActionBuilder, StoreKVPool}; |
10 | 10 |
|
11 | 11 | pub struct ExecutorFlushB;
|
12 | 12 |
|
13 | 13 | impl ExecutorFlushB {
|
14 | 14 | pub fn execute<'a>(store: StoreItem<'a>) -> Result<u32, ()> {
|
15 |
| - // Important: do not acquire the store from there, as otherwise it will remain open \ |
16 |
| - // even if dropped in the inner function, as this caller would still own a reference to \ |
17 |
| - // it. |
18 | 15 | if let StoreItem(collection, Some(bucket), None) = store {
|
19 |
| - match ( |
20 |
| - StoreKVActionBuilder::erase(collection, Some(bucket)), |
21 |
| - StoreFSTActionBuilder::erase(collection, Some(bucket)), |
22 |
| - ) { |
23 |
| - (Ok(erase_count), Ok(_)) => Ok(erase_count), |
24 |
| - _ => Err(()), |
| 16 | + // Important: acquire database access read lock, and reference it in context. This \ |
| 17 | + // prevents the database from being erased while using it in this block. |
| 18 | + general_kv_access_lock_read!(); |
| 19 | + |
| 20 | + if let Ok(kv_store) = StoreKVPool::acquire(StoreKVAcquireMode::OpenOnly, collection) { |
| 21 | + // Important: acquire bucket store write lock |
| 22 | + executor_kv_lock_write!(kv_store); |
| 23 | + |
| 24 | + if kv_store.is_some() == true { |
| 25 | + // Store exists, proceed erasure. |
| 26 | + debug!( |
| 27 | + "collection store exists, erasing: {} from {}", |
| 28 | + bucket.as_str(), |
| 29 | + collection.as_str() |
| 30 | + ); |
| 31 | + |
| 32 | + let kv_action = StoreKVActionBuilder::access(bucket, kv_store); |
| 33 | + |
| 34 | + // Notice: we cannot use the provided KV bucket erasure helper there, as \ |
| 35 | + // erasing a bucket requires a database lock, which would incur a dead-lock, \ |
| 36 | + // thus we need to perform the erasure from there. |
| 37 | + if let Ok(erase_count) = kv_action.batch_erase_bucket() { |
| 38 | + if StoreFSTActionBuilder::erase(collection, Some(bucket)).is_ok() == true { |
| 39 | + debug!("done with bucket erasure"); |
| 40 | + |
| 41 | + return Ok(erase_count); |
| 42 | + } |
| 43 | + } |
| 44 | + } else { |
| 45 | + // Store does not exist, consider as already erased. |
| 46 | + debug!( |
| 47 | + "collection store does not exist, consider {} from {} already erased", |
| 48 | + bucket.as_str(), |
| 49 | + collection.as_str() |
| 50 | + ); |
| 51 | + |
| 52 | + return Ok(0); |
| 53 | + } |
25 | 54 | }
|
26 |
| - } else { |
27 |
| - Err(()) |
28 | 55 | }
|
| 56 | + |
| 57 | + Err(()) |
29 | 58 | }
|
30 | 59 | }
|
0 commit comments