Skip to main content

sonic/executor/
flusho.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 flusho(&self, item: StoreItem) -> Result<u32, ()> {
13        if let StoreItem(collection, Some(bucket), Some(object)) = 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            let _kv_read_guard = self.kv_pool.lock_read_access();
17
18            if let Ok(kv_store) = self
19                .kv_pool
20                .acquire(StoreKVAcquireMode::OpenOnly, collection)
21            {
22                // Important: acquire bucket store write lock
23                executor_kv_lock_write!(kv_store);
24
25                let kv_action = StoreKVActionBuilder::access(bucket, kv_store);
26
27                // Try to resolve existing OID to IID (if it does not exist, there is nothing to \
28                //   be flushed)
29                let oid = object.as_str();
30
31                if let Ok(iid_value) = kv_action.get_oid_to_iid(oid) {
32                    let mut count_flushed = 0;
33
34                    if let Some(iid) = iid_value {
35                        // Resolve terms associated to IID
36                        let iid_terms = {
37                            if let Ok(iid_terms_value) = kv_action.get_iid_to_terms(iid) {
38                                iid_terms_value.unwrap_or_default()
39                            } else {
40                                tracing::error!("failed getting flusho executor iid-to-terms");
41
42                                Vec::new()
43                            }
44                        };
45
46                        // Flush bucket (batch operation, as it is shared w/ other executors)
47                        if let Ok(batch_count) = kv_action.batch_flush_bucket(iid, oid, &iid_terms)
48                        {
49                            count_flushed += batch_count;
50                        } else {
51                            tracing::error!(
52                                "failed executing batch-flush-bucket in flusho executor"
53                            );
54                        }
55                    }
56
57                    return Ok(count_flushed);
58                } else {
59                    tracing::error!("failed getting flusho executor oid-to-iid");
60                }
61            }
62        }
63
64        Err(())
65    }
66}