Skip to main content

reddb_server/storage/unified/store/
impl_entities.rs

1use super::*;
2
3impl UnifiedStore {
4    /// Vault versions are WAL-backed in memory until a committed checkpoint.
5    /// Preparing a commit must not dirty pager pages: eviction or an auth
6    /// flush could otherwise persist a version before its WAL batch exists.
7    pub(crate) fn insert_vault_version(
8        &self,
9        collection: &str,
10        entity: UnifiedEntity,
11        metadata: Metadata,
12    ) -> Result<(), StoreError> {
13        assert!(matches!(entity.data, EntityData::Row(_)));
14        let manager = self
15            .get_collection(collection)
16            .ok_or_else(|| StoreError::CollectionNotFound(collection.into()))?;
17        self.pending_vault_versions.write().insert(entity.id);
18        let id = manager.insert(entity)?;
19        self.register_entity_id(id);
20        if !metadata.fields.is_empty() {
21            manager.set_metadata(id, metadata)?;
22        }
23        let entity = manager
24            .get(id)
25            .expect("inserted vault version remains in memory");
26        self.persist_entities_to_pager_wal_only(collection, &[entity])
27    }
28
29    pub(crate) fn persist_entities_to_pager(
30        &self,
31        collection: &str,
32        entities: &[UnifiedEntity],
33    ) -> Result<(), StoreError> {
34        self.persist_entities_impl(collection, entities, /* skip_btree= */ false)
35    }
36
37    /// Same as `persist_entities_to_pager` but takes `&[&UnifiedEntity]` so
38    /// callers that already hold references (SQL UPDATE fast path) don't
39    /// have to clone a `Vec<UnifiedEntity>` just to satisfy the signature.
40    pub(crate) fn persist_entity_refs_to_pager(
41        &self,
42        collection: &str,
43        entities: &[&UnifiedEntity],
44    ) -> Result<(), StoreError> {
45        self.persist_entity_refs_impl(collection, entities, /* skip_btree= */ false)
46    }
47
48    /// Reference-taking WAL-only variant — see
49    /// `persist_entities_to_pager_wal_only` for semantics.
50    pub(crate) fn persist_entity_refs_to_pager_wal_only(
51        &self,
52        collection: &str,
53        entities: &[&UnifiedEntity],
54    ) -> Result<(), StoreError> {
55        self.persist_entity_refs_impl(collection, entities, /* skip_btree= */ true)
56    }
57
58    fn persist_entity_refs_impl(
59        &self,
60        collection: &str,
61        entities: &[&UnifiedEntity],
62        skip_btree: bool,
63    ) -> Result<(), StoreError> {
64        if entities.is_empty() {
65            return Ok(());
66        }
67        if self.pager.is_none() && self.config.embedded_wal_path.is_none() {
68            return Ok(());
69        }
70        let fv = self.format_version();
71        let manager = self
72            .get_collection(collection)
73            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
74        let mut serialized: Vec<(Vec<u8>, Vec<u8>)> = entities
75            .iter()
76            .map(|entity| {
77                let metadata = manager.get_metadata(entity.id);
78                (
79                    entity.id.raw().to_be_bytes().to_vec(),
80                    Self::serialize_entity_record(entity, metadata.as_ref(), fv),
81                )
82            })
83            .collect();
84        serialized.sort_by(|a, b| a.0.cmp(&b.0));
85
86        if !skip_btree {
87            let Some(pager) = &self.pager else {
88                let records: Vec<Vec<u8>> =
89                    serialized.into_iter().map(|(_id, record)| record).collect();
90                self.finish_paged_write([StoreWalAction::BulkUpsertEntityRecords {
91                    collection: collection.to_string(),
92                    records,
93                }])?;
94                return Ok(());
95            };
96            let mut btree_indices = self.btree_indices.write();
97            let btree = btree_indices
98                .entry(collection.to_string())
99                .or_insert_with(|| Arc::new(BTree::new(Arc::clone(pager))));
100            let root_before = btree.root_page_id();
101            btree.upsert_batch_sorted(&serialized).map_err(|e| {
102                StoreError::Io(std::io::Error::other(format!("B-tree upsert error: {}", e)))
103            })?;
104            let root_after = btree.root_page_id();
105            drop(btree_indices);
106            if root_before != root_after {
107                self.mark_paged_registry_dirty();
108            }
109        }
110        let records: Vec<Vec<u8>> = serialized.into_iter().map(|(_id, record)| record).collect();
111        self.finish_paged_write([StoreWalAction::BulkUpsertEntityRecords {
112            collection: collection.to_string(),
113            records,
114        }])?;
115        Ok(())
116    }
117
118    /// PG-HOT-like UPDATE persist: emit the WAL record so the update
119    /// survives a crash, but skip the in-line B-tree upsert. Reads
120    /// continue to see the new entity because `manager.get()` prefers
121    /// the segment over the B-tree; WAL replay re-applies the upsert
122    /// to the B-tree on recovery so the two views converge before
123    /// any stale B-tree page can be surfaced.
124    ///
125    /// Safe to use only when the caller has already updated the
126    /// segment in-place (so live reads see the new value). Cuts out
127    /// the dominant cost of single-row UPDATEs: per-entity serialize
128    /// + leaf descent + page decompress / compress / write_page.
129    pub(crate) fn persist_entities_to_pager_wal_only(
130        &self,
131        collection: &str,
132        entities: &[UnifiedEntity],
133    ) -> Result<(), StoreError> {
134        self.persist_entities_impl(collection, entities, /* skip_btree= */ true)
135    }
136
137    fn persist_entities_impl(
138        &self,
139        collection: &str,
140        entities: &[UnifiedEntity],
141        skip_btree: bool,
142    ) -> Result<(), StoreError> {
143        if entities.is_empty() {
144            return Ok(());
145        }
146
147        if self.pager.is_none() && self.config.embedded_wal_path.is_none() {
148            return Ok(());
149        }
150
151        let fv = self.format_version();
152        let manager = self
153            .get_collection(collection)
154            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
155        let mut serialized: Vec<(Vec<u8>, Vec<u8>)> = entities
156            .iter()
157            .map(|entity| {
158                let metadata = manager.get_metadata(entity.id);
159                (
160                    entity.id.raw().to_be_bytes().to_vec(),
161                    Self::serialize_entity_record(entity, metadata.as_ref(), fv),
162                )
163            })
164            .collect();
165        // u64 IDs encoded as big-endian — lex order = numeric order.
166        serialized.sort_by(|a, b| a.0.cmp(&b.0));
167
168        if !skip_btree {
169            let Some(pager) = &self.pager else {
170                let records: Vec<Vec<u8>> =
171                    serialized.into_iter().map(|(_id, record)| record).collect();
172                self.finish_paged_write([StoreWalAction::BulkUpsertEntityRecords {
173                    collection: collection.to_string(),
174                    records,
175                }])?;
176                return Ok(());
177            };
178            let mut btree_indices = self.btree_indices.write();
179            let btree = btree_indices
180                .entry(collection.to_string())
181                .or_insert_with(|| Arc::new(BTree::new(Arc::clone(pager))));
182            let root_before = btree.root_page_id();
183
184            // Walks each distinct leaf once, applies all in-place overwrites
185            // that belong there under one read+write. Keys that miss or grow
186            // fall back to the per-key `upsert` path internally.
187            btree.upsert_batch_sorted(&serialized).map_err(|e| {
188                StoreError::Io(std::io::Error::other(format!("B-tree upsert error: {}", e)))
189            })?;
190            let root_after = btree.root_page_id();
191            drop(btree_indices);
192            if root_before != root_after {
193                self.mark_paged_registry_dirty();
194            }
195        }
196        // One WAL action covering the whole bulk. Previously each entity
197        // produced its own `UpsertEntityRecord` action, which then became
198        // its own `WalRecord::PageWrite` triple inside
199        // `StoreCommitCoordinator::append_actions` — N WAL records per
200        // bulk. The batched variant is one WAL record per bulk.
201        //
202        // WAL is emitted even in the `skip_btree` variant — recovery
203        // replay reapplies the upsert to the B-tree, so durability
204        // is unchanged.
205        let records: Vec<Vec<u8>> = serialized.into_iter().map(|(_id, record)| record).collect();
206        self.finish_paged_write([StoreWalAction::BulkUpsertEntityRecords {
207            collection: collection.to_string(),
208            records,
209        }])?;
210
211        Ok(())
212    }
213
214    /// Insert a label→entity_id mapping into the graph label index.
215    pub(crate) fn update_graph_label_index(
216        &self,
217        collection: &str,
218        label: &str,
219        entity_id: EntityId,
220    ) {
221        let key = (collection.to_string(), label.to_string());
222        let mut idx = self.graph_label_index.write();
223        idx.entry(key).or_default().push(entity_id);
224    }
225
226    /// Remove a specific entity_id from the graph label index (called on delete).
227    pub(crate) fn remove_from_graph_label_index(&self, collection: &str, entity_id: EntityId) {
228        let mut idx = self.graph_label_index.write();
229        for ((col, _), ids) in idx.iter_mut() {
230            if col == collection {
231                ids.retain(|&id| id != entity_id);
232            }
233        }
234        // Prune empty entries to keep the index compact
235        idx.retain(|_, ids| !ids.is_empty());
236    }
237
238    pub(crate) fn remove_from_graph_label_index_batch(
239        &self,
240        collection: &str,
241        entity_ids: &[EntityId],
242    ) {
243        if entity_ids.is_empty() {
244            return;
245        }
246        let id_set: std::collections::HashSet<EntityId> = entity_ids.iter().copied().collect();
247        let mut idx = self.graph_label_index.write();
248        for ((col, _), ids) in idx.iter_mut() {
249            if col == collection {
250                ids.retain(|id| !id_set.contains(id));
251            }
252        }
253        idx.retain(|_, ids| !ids.is_empty());
254    }
255
256    /// Look up entity IDs for a graph node label across all collections.
257    pub fn lookup_graph_nodes_by_label(&self, label: &str) -> Vec<EntityId> {
258        let idx = self.graph_label_index.read();
259        idx.iter()
260            .filter(|((_, l), _)| l == label)
261            .flat_map(|(_, ids)| ids.iter().copied())
262            .collect()
263    }
264
265    /// Look up entity IDs for a graph node label scoped to a single collection.
266    pub fn lookup_graph_nodes_by_label_in(&self, collection: &str, label: &str) -> Vec<EntityId> {
267        let idx = self.graph_label_index.read();
268        idx.get(&(collection.to_string(), label.to_string()))
269            .cloned()
270            .unwrap_or_default()
271    }
272
273    pub fn create_collection(&self, name: impl Into<String>) -> Result<(), StoreError> {
274        let name = name.into();
275        let mut collections = self.collections.write();
276
277        if collections.contains_key(&name) {
278            return Err(StoreError::CollectionExists(name));
279        }
280
281        let manager = SegmentManager::with_config(&name, self.config.manager_config.clone());
282        collections.insert(name.clone(), Arc::new(manager));
283        drop(collections);
284
285        let create = match self.prepare_operational_collection_create(&name) {
286            Ok(create) => create,
287            Err(err) => {
288                let mut collections = self.collections.write();
289                collections.remove(&name);
290                return Err(err);
291            }
292        };
293        if let Some(create) = create {
294            if let Err(err) = create.publish().map_err(StoreError::Io) {
295                let mut collections = self.collections.write();
296                collections.remove(&name);
297                return Err(err);
298            }
299        }
300
301        self.mark_paged_registry_dirty();
302        self.finish_paged_write([StoreWalAction::CreateCollection { name }])?;
303
304        Ok(())
305    }
306
307    /// Get or create a collection
308    pub fn get_or_create_collection(&self, name: impl Into<String>) -> Arc<SegmentManager> {
309        let name = name.into();
310        // Fast path: shared read lock — zero contention for existing collections
311        {
312            let collections = self.collections.read();
313            if let Some(manager) = collections.get(&name) {
314                return Arc::clone(manager);
315            }
316        }
317        // Slow path: exclusive write lock — only when collection is missing
318        let mut collections = self.collections.write();
319        // Double-check after acquiring write lock (another thread may have created it)
320        if let Some(manager) = collections.get(&name) {
321            return Arc::clone(manager);
322        }
323        let manager = Arc::new(SegmentManager::with_config(
324            &name,
325            self.config.manager_config.clone(),
326        ));
327        collections.insert(name, Arc::clone(&manager));
328        self.mark_paged_registry_dirty();
329        manager
330    }
331
332    /// Get a collection
333    pub fn get_collection(&self, name: &str) -> Option<Arc<SegmentManager>> {
334        self.collections.read().get(name).map(Arc::clone)
335    }
336
337    /// Get the context index for cross-structure search.
338    pub fn context_index(&self) -> &ContextIndex {
339        &self.context_index
340    }
341
342    /// Drain WAL-replayed `VectorInsert` records for `collection`
343    /// (issue #694). Returns `None` when nothing was captured at open
344    /// time (in-memory mode, fresh database, or non-turbo collection).
345    /// One-shot: the caller owns the records after this call, the
346    /// store's internal buffer is cleared for that collection.
347    pub fn take_replayed_turbo_inserts(&self, collection: &str) -> Option<Vec<(u64, Vec<f32>)>> {
348        let mut map = self.replayed_turbo_inserts.lock();
349        map.remove(collection)
350    }
351
352    pub(crate) fn take_replayed_probabilistic_deltas(&self) -> Vec<(u8, u8, String, Vec<Vec<u8>>)> {
353        let mut deltas = self.replayed_probabilistic_deltas.lock();
354        std::mem::take(&mut *deltas)
355    }
356
357    /// Set multiple config KV pairs at once from a JSON tree.
358    /// Keys are flattened with dot-notation: `{"a":{"b":1}}` → `a.b = 1`.
359    ///
360    /// `red_config` is last-writer-wins (ADR 0014): each write replaces the
361    /// key's earlier rows. Appending instead grew the store on every boot,
362    /// since boot re-seeds keys such as `red.system.*`.
363    pub fn set_config_tree(&self, prefix: &str, json: &crate::serde_json::Value) -> usize {
364        let manager = self.get_or_create_collection("red_config");
365        let mut pairs = Vec::new();
366        flatten_config_json(prefix, json, &mut pairs);
367        // Existing rows per key, with the value each one holds.
368        let mut previous: HashMap<String, Vec<(EntityId, Option<reddb_types::Value>)>> = pairs
369            .iter()
370            .map(|(key, _)| (key.clone(), Vec::new()))
371            .collect();
372        for entity in manager.query_all(|_| true) {
373            if let EntityData::Row(row) = &entity.data {
374                let Some(named) = row.named.as_ref() else {
375                    continue;
376                };
377                if let Some(reddb_types::Value::Text(key)) = named.get("key") {
378                    if let Some(rows) = previous.get_mut(key.as_ref()) {
379                        rows.push((entity.id, named.get("value").cloned()));
380                    }
381                }
382            }
383        }
384        let mut saved = 0;
385        for (key, value) in pairs {
386            let stale = previous.remove(&key).unwrap_or_default();
387            // Rewriting a key with the value it already holds is a no-op.
388            // Boot re-seeds the same config every time, and each write is a
389            // durable WAL append with its own fsync.
390            if let [(_, Some(current))] = stale.as_slice() {
391                if *current == value {
392                    saved += 1;
393                    continue;
394                }
395            }
396            let stale: Vec<EntityId> = stale.into_iter().map(|(id, _)| id).collect();
397            // Insert before removing the old rows: a failure in between
398            // leaves a duplicate that readers resolve to the newest row,
399            // never a missing key.
400            let entity = UnifiedEntity::new(
401                EntityId::new(0),
402                EntityKind::TableRow {
403                    table: Arc::from("red_config"),
404                    row_id: 0,
405                },
406                EntityData::Row(RowData {
407                    columns: Vec::new(),
408                    named: Some(
409                        [
410                            ("key".to_string(), reddb_types::Value::text(key)),
411                            ("value".to_string(), value),
412                        ]
413                        .into_iter()
414                        .collect(),
415                    ),
416                    schema: None,
417                }),
418            );
419            if self.insert_auto("red_config", entity).is_ok() {
420                saved += 1;
421                if !stale.is_empty() {
422                    let _ = self.delete_batch("red_config", &stale);
423                }
424            }
425        }
426        saved
427    }
428
429    /// Read a single config value from `red_config` by dot-notation key.
430    /// When a key has several rows the newest one wins, like every other
431    /// `red_config` reader.
432    pub fn get_config(&self, key: &str) -> Option<reddb_types::Value> {
433        let manager = self.get_collection("red_config")?;
434        let mut latest: Option<(u64, reddb_types::Value)> = None;
435        for entity in manager.query_all(|_| true) {
436            if let EntityData::Row(row) = &entity.data {
437                if let Some(named) = &row.named {
438                    let key_matches = named
439                        .get("key")
440                        .and_then(|v| match v {
441                            reddb_types::Value::Text(s) => Some(s.as_ref() == key),
442                            _ => None,
443                        })
444                        .unwrap_or(false);
445                    if key_matches && latest.as_ref().is_none_or(|(id, _)| entity.id.raw() >= *id) {
446                        if let Some(value) = named.get("value") {
447                            latest = Some((entity.id.raw(), value.clone()));
448                        }
449                    }
450                }
451            }
452        }
453        latest.map(|(_, value)| value)
454    }
455
456    /// Replace the opaque store-level auxiliary metadata blob. Persisted
457    /// inside the binary dump (store format V10+); see the `aux_metadata`
458    /// field. RedDB uses it to carry collection contracts through the
459    /// single-file artifact.
460    pub fn set_aux_metadata(&self, bytes: Vec<u8>) {
461        *self.aux_metadata.write() = bytes;
462    }
463
464    /// Read the opaque store-level auxiliary metadata blob. Empty when unset.
465    pub fn aux_metadata(&self) -> Vec<u8> {
466        self.aux_metadata.read().clone()
467    }
468
469    /// List all collections
470    pub fn list_collections(&self) -> Vec<String> {
471        self.collections.read().keys().cloned().collect()
472    }
473
474    /// Drop a collection
475    pub fn drop_collection(&self, name: &str) -> Result<(), StoreError> {
476        let drop = self.prepare_operational_collection_drop(name)?;
477        let manager = {
478            let mut collections = self.collections.write();
479
480            collections
481                .remove(name)
482                .ok_or_else(|| StoreError::CollectionNotFound(name.to_string()))?
483        };
484
485        let entities = manager.query_all(|_| true);
486        let entity_ids: Vec<EntityId> = entities.iter().map(|entity| entity.id).collect();
487
488        for entity_id in &entity_ids {
489            self.context_index.remove_entity(*entity_id);
490            let _ = self.unindex_cross_refs(*entity_id);
491        }
492
493        self.btree_indices.write().remove(name);
494
495        self.entity_cache.retain(|entity_id, (collection, _)| {
496            collection != name && !entity_ids.iter().any(|id| id.raw() == entity_id)
497        });
498
499        self.cross_refs.write().retain(|source_id, refs| {
500            refs.retain(|(target_id, _, target_collection)| {
501                target_collection != name && !entity_ids.iter().any(|id| id == target_id)
502            });
503            !entity_ids.iter().any(|id| id == source_id)
504        });
505
506        self.reverse_refs.write().retain(|target_id, refs| {
507            refs.retain(|(source_id, _, source_collection)| {
508                source_collection != name && !entity_ids.iter().any(|id| id == source_id)
509            });
510            !entity_ids.iter().any(|id| id == target_id)
511        });
512
513        self.mark_paged_registry_dirty();
514        self.finish_paged_write([StoreWalAction::DropCollection {
515            name: name.to_string(),
516        }])?;
517        if let Some(drop) = drop {
518            drop.finish().map_err(StoreError::Io)?;
519        }
520
521        Ok(())
522    }
523
524    /// Insert an entity into a collection
525    pub fn insert(&self, collection: &str, entity: UnifiedEntity) -> Result<EntityId, StoreError> {
526        let manager = self
527            .get_collection(collection)
528            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
529
530        let mut entity = entity;
531        if entity.id.raw() == 0 {
532            entity.id = self.next_entity_id();
533        } else {
534            self.register_entity_id(entity.id);
535        }
536        // Assign per-table sequential row_id if not set
537        if let EntityKind::TableRow { ref mut row_id, .. } = entity.kind {
538            if *row_id == 0 {
539                *row_id = manager.next_row_id();
540            } else {
541                manager.register_row_id(*row_id);
542            }
543        }
544        entity.ensure_table_logical_id();
545        // Capture graph node label before entity is moved into the manager
546        let graph_node_label: Option<String> = if let EntityKind::GraphNode(ref node) = entity.kind
547        {
548            Some(node.label.clone())
549        } else {
550            None
551        };
552
553        let id = manager.insert(entity)?;
554        self.register_entity_id(id);
555
556        // Update graph label index for GraphNode entities
557        if let Some(ref label) = graph_node_label {
558            self.update_graph_label_index(collection, label, id);
559        }
560
561        // Also insert into B-tree index if pager is active
562        let mut registry_dirty = false;
563        if let Some(pager) = &self.pager {
564            if let Some(entity) = manager.get(id) {
565                let mut btree_indices = self.btree_indices.write();
566                let btree = btree_indices
567                    .entry(collection.to_string())
568                    .or_insert_with(|| Arc::new(BTree::new(Arc::clone(pager))));
569                let root_before = btree.root_page_id();
570
571                let key = id.raw().to_be_bytes();
572                let metadata = manager.get_metadata(id);
573                let value = Self::serialize_entity_record(
574                    &entity,
575                    metadata.as_ref(),
576                    self.format_version(),
577                );
578                // Ignore duplicate key errors (update scenario)
579                let _ = btree.insert(&key, &value);
580                registry_dirty = root_before != btree.root_page_id();
581            }
582        }
583
584        // Index cross-references if enabled
585        if self.config.auto_index_refs {
586            if let Some(entity) = manager.get(id) {
587                self.index_cross_refs(&entity, collection)?;
588            }
589        }
590
591        // Perf: skip WAL-action construction when the store is
592        // pagerless. For in-memory benchmarks this saved another
593        // `manager.get(id)` + `serialize_entity_record` per call.
594        if self.pager.is_some() || self.config.embedded_wal_path.is_some() {
595            let actions = manager
596                .get(id)
597                .map(|entity| {
598                    let metadata = manager.get_metadata(id);
599                    vec![StoreWalAction::upsert_entity(
600                        collection,
601                        &entity,
602                        metadata.as_ref(),
603                        self.format_version(),
604                    )]
605                })
606                .unwrap_or_default();
607            if registry_dirty {
608                self.mark_paged_registry_dirty();
609            }
610            self.finish_paged_write(actions)?;
611        }
612
613        Ok(id)
614    }
615
616    /// Turbo bulk insert — optimized fast path.
617    ///
618    /// Single lock for the entire batch. Skips bloom filter,
619    /// context index, and cross-ref indexing. B-tree writes are batched.
620    pub fn bulk_insert(
621        &self,
622        collection: &str,
623        mut entities: Vec<UnifiedEntity>,
624    ) -> Result<Vec<EntityId>, StoreError> {
625        // REDDB_BULK_TIMING=1 prints a per-call breakdown of the bulk
626        // insert path to stderr. Off by default — used by the reddb
627        // benchmark harness to locate ingest bottlenecks.
628        let trace = matches!(
629            std::env::var("REDDB_BULK_TIMING").ok().as_deref(),
630            Some("1") | Some("true") | Some("on")
631        );
632        let t_start = std::time::Instant::now();
633        let n = entities.len();
634        let manager = self.get_or_create_collection(collection);
635        let t_get_coll = t_start.elapsed();
636
637        // Assign IDs and per-table row_ids before serialization. Bulk
638        // insert must follow the same global ID semantics as
639        // insert()/insert_auto(). Reserve ID ranges in single fetch_add
640        // calls instead of one-per-entity. The overwhelming common case
641        // is "every entity comes in with id==0 and kind==TableRow with
642        // row_id==0" (the wire bulk insert path). Any entity that
643        // already carries an id or row_id falls back to the individual
644        // register_* path to preserve those semantics exactly.
645        let t0 = std::time::Instant::now();
646        let n_missing_entity_ids = entities.iter().filter(|e| e.id.raw() == 0).count() as u64;
647        let n_missing_row_ids = entities
648            .iter()
649            .filter(|e| matches!(e.kind, EntityKind::TableRow { row_id: 0, .. }))
650            .count() as u64;
651        let mut entity_id_range = if n_missing_entity_ids > 0 {
652            self.reserve_entity_ids(n_missing_entity_ids)
653        } else {
654            0..0
655        };
656        let mut row_id_range = if n_missing_row_ids > 0 {
657            manager.reserve_row_ids(n_missing_row_ids)
658        } else {
659            0..0
660        };
661        for entity in &mut entities {
662            if entity.id.raw() == 0 {
663                let next = entity_id_range
664                    .next()
665                    .expect("reserved entity-id range exhausted");
666                entity.id = EntityId::new(next);
667            } else {
668                self.register_entity_id(entity.id);
669            }
670            if let EntityKind::TableRow { ref mut row_id, .. } = entity.kind {
671                if *row_id == 0 {
672                    *row_id = row_id_range
673                        .next()
674                        .expect("reserved row-id range exhausted");
675                } else {
676                    manager.register_row_id(*row_id);
677                }
678            }
679            entity.ensure_table_logical_id();
680        }
681        let t_assign_ids = t0.elapsed();
682
683        // Capture graph node labels before entities are moved into the segment manager
684        let graph_labels: Vec<Option<(String, EntityId)>> = entities
685            .iter()
686            .map(|e| {
687                if let EntityKind::GraphNode(ref node) = e.kind {
688                    Some((node.label.clone(), e.id))
689                } else {
690                    None
691                }
692            })
693            .collect();
694
695        // Pre-serialize for B-tree while we still have references.
696        // `serialize_entity_record` is pure; with `n >= 1024` the
697        // rayon dispatch cost is amortised and the 16-core serialize
698        // fan-out becomes the biggest single win on insert_bulk. For
699        // smaller batches, stay serial — micro-batches pay more in
700        // work-stealing overhead than they save.
701        let t0 = std::time::Instant::now();
702        let serialized: Option<Vec<(Vec<u8>, Vec<u8>)>> =
703            if self.pager.is_some() || self.config.embedded_wal_path.is_some() {
704                let fv = self.format_version();
705                let serial_map = |e: &UnifiedEntity| {
706                    (
707                        e.id.raw().to_be_bytes().to_vec(),
708                        Self::serialize_entity_record(e, None, fv),
709                    )
710                };
711                // Gate chosen to match the bench's `BULK_BATCH_SIZE=1000`.
712                // Rayon's dispatch overhead is ~30µs/batch — on a 15-col
713                // row serializing for ~40µs the break-even is around
714                // 200-300 rows on a 16-core host. Keep a safety margin
715                // at 512.
716                if entities.len() >= 512 {
717                    use rayon::prelude::*;
718                    Some(entities.par_iter().map(serial_map).collect())
719                } else {
720                    Some(entities.iter().map(serial_map).collect())
721                }
722            } else {
723                None
724            };
725        let t_serialize = t0.elapsed();
726
727        // Move entities into segment
728        let t0 = std::time::Instant::now();
729        let ids = manager.bulk_insert(entities)?;
730        let t_manager = t0.elapsed();
731        for id in &ids {
732            self.register_entity_id(*id);
733        }
734
735        // Update graph label index for bulk-inserted GraphNode entities
736        for (label, entity_id) in graph_labels.iter().flatten() {
737            self.update_graph_label_index(collection, label, *entity_id);
738        }
739
740        // REDDB_BULK_SKIP_PERSIST_UNSAFE=1 skips the persistent B-tree index
741        // during bulk ingest.
742        //
743        // UNSAFE: for ephemeral benchmark containers ONLY.
744        // This flag is silently ignored when a pager (durable storage) is active.
745        // In persistent mode, bulk inserts ALWAYS write to the B-tree so the data
746        // survives a cold restart without any manual rebuild step.
747        //
748        // The flag is only honoured when self.pager is None (in-memory / ephemeral).
749        let skip_btree_requested = matches!(
750            std::env::var("REDDB_BULK_SKIP_PERSIST_UNSAFE")
751                .ok()
752                .as_deref(),
753            Some("1") | Some("true") | Some("on")
754        );
755        // Honour the flag only when there is no durable pager.
756        // If a pager exists we are in persistent mode → always persist.
757        let skip_btree = skip_btree_requested && self.pager.is_none();
758        if skip_btree_requested && !skip_btree {
759            // Flag was set but we ignored it because we have a real pager.
760            static IGNORED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
761            IGNORED.get_or_init(|| {
762                tracing::warn!(
763                    "REDDB_BULK_SKIP_PERSIST_UNSAFE set but durable pager is \
764                     active — flag ignored; bulk inserts will be persisted normally"
765                );
766            });
767        } else if skip_btree {
768            // Ephemeral mode and flag is active — warn once.
769            static WARNED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
770            WARNED.get_or_init(|| {
771                tracing::warn!(
772                    "REDDB_BULK_SKIP_PERSIST_UNSAFE set (ephemeral/no-pager mode) — \
773                     bulk inserts NOT durable; data will be lost on restart"
774                );
775            });
776        }
777
778        // Batch B-tree write from pre-serialized data.
779        // Uses sorted bulk insert: walks to a leaf once, appends many entries,
780        // writes each leaf exactly once per batch — O(N) instead of O(N²).
781        let mut t_btree_lock = std::time::Duration::ZERO;
782        let mut t_btree_insert = std::time::Duration::ZERO;
783        let mut t_flush = std::time::Duration::ZERO;
784        if !skip_btree {
785            if let (Some(pager), Some(batch)) = (&self.pager, serialized.as_ref()) {
786                let t0 = std::time::Instant::now();
787                let mut btree_indices = self.btree_indices.write();
788                let btree = btree_indices
789                    .entry(collection.to_string())
790                    .or_insert_with(|| Arc::new(BTree::new(Arc::clone(pager))));
791                let root_before = btree.root_page_id();
792                t_btree_lock = t0.elapsed();
793
794                let t0 = std::time::Instant::now();
795                let _ = btree.bulk_insert_sorted(batch);
796                t_btree_insert = t0.elapsed();
797                let registry_dirty = root_before != btree.root_page_id();
798
799                let t0 = std::time::Instant::now();
800                if registry_dirty {
801                    self.mark_paged_registry_dirty();
802                }
803                t_flush = t0.elapsed();
804            }
805        }
806
807        // Fold 25k per-entity `UpsertEntityRecord` actions into one
808        // `BulkUpsertEntityRecords` — same on-disk semantics (replay
809        // applies each record by id), one `WalRecord::PageWrite`
810        // instead of N, and `record.clone()` drops out because we
811        // consume `serialized`.
812        let actions = serialized
813            .map(|batch| {
814                let records: Vec<Vec<u8>> =
815                    batch.into_iter().map(|(_key, record)| record).collect();
816                vec![StoreWalAction::BulkUpsertEntityRecords {
817                    collection: collection.to_string(),
818                    records,
819                }]
820            })
821            .unwrap_or_default();
822        self.finish_paged_write(actions)?;
823
824        if trace {
825            tracing::debug!(
826                n,
827                total = ?t_start.elapsed(),
828                get_coll = ?t_get_coll,
829                assign = ?t_assign_ids,
830                serialize = ?t_serialize,
831                manager = ?t_manager,
832                btree_lock = ?t_btree_lock,
833                btree = ?t_btree_insert,
834                flush = ?t_flush,
835                "bulk_insert timing"
836            );
837        }
838
839        Ok(ids)
840    }
841
842    /// Insert an entity, creating collection if needed
843    pub fn insert_auto(
844        &self,
845        collection: &str,
846        entity: UnifiedEntity,
847    ) -> Result<EntityId, StoreError> {
848        let manager = self.get_or_create_collection(collection);
849        let mut entity = entity;
850        if entity.id.raw() == 0 {
851            entity.id = self.next_entity_id();
852        } else {
853            self.register_entity_id(entity.id);
854        }
855        // Assign per-table sequential row_id if not set
856        if let EntityKind::TableRow { ref mut row_id, .. } = entity.kind {
857            if *row_id == 0 {
858                *row_id = manager.next_row_id();
859            } else {
860                manager.register_row_id(*row_id);
861            }
862        }
863        entity.ensure_table_logical_id();
864
865        // Capture graph node label before entity is moved into the manager
866        let graph_node_label: Option<String> = if let EntityKind::GraphNode(ref node) = entity.kind
867        {
868            Some(node.label.clone())
869        } else {
870            None
871        };
872        // Index into context index before consuming the entity
873        self.context_index.index_entity(collection, &entity);
874
875        // Pre-serialize the entity record (B-tree image == WAL action body)
876        // and index cross-refs while we still own the entity. Mirrors the
877        // bulk_insert path (see line 559-580 above): segment.insert assigns
878        // a fresh `sequence_id`, but the bulk path already accepts a
879        // sequence_id=0 on-disk image (replay calls manager.insert which
880        // re-assigns sequence_id from the segment's allocator), so the
881        // single-row path can do the same. Fresh inserts never carry
882        // metadata — set_metadata is a separate call — so we pass None,
883        // matching how the bulk path serializes records.
884        //
885        // Eliminates the post-insert `manager.get(id)` clone of the just-
886        // inserted UnifiedEntity (HashMap<String, Value> + per-field
887        // Strings). Per `docs/perf/insert_sequential-2026-05-05.md` P2 this
888        // was the dominant clone in the insert_sequential hot path.
889        let id_for_serialize = entity.id;
890        let serialized_record: Option<Vec<u8>> =
891            if self.pager.is_some() || self.config.embedded_wal_path.is_some() {
892                Some(Self::serialize_entity_record(
893                    &entity,
894                    None,
895                    self.format_version(),
896                ))
897            } else {
898                None
899            };
900        if self.config.auto_index_refs {
901            self.index_cross_refs(&entity, collection)?;
902        }
903
904        let id = manager.insert(entity)?;
905        debug_assert_eq!(id, id_for_serialize);
906        // `register_entity_id` already advances the atomic counter on
907        // the allocation path above (`self.next_entity_id()`), so the
908        // second call here is a no-op CAS loop on the hot path. Only
909        // needed for the caller-supplied-id branch which happens via
910        // the `register_entity_id` call on line 573.
911
912        // Update graph label index for GraphNode entities
913        if let Some(ref label) = graph_node_label {
914            self.update_graph_label_index(collection, label, id);
915        }
916
917        let mut registry_dirty = false;
918        if let (Some(_pager), Some(record)) = (&self.pager, serialized_record.as_ref()) {
919            if let Some(btree) = self.get_or_create_btree(collection) {
920                let root_before = btree.root_page_id();
921
922                let key = id.raw().to_be_bytes();
923                btree.insert(&key, record).map_err(|e| {
924                    StoreError::Io(std::io::Error::other(format!(
925                        "B-tree insert error while inserting '{collection}'/{id}: {e}"
926                    )))
927                })?;
928                registry_dirty = root_before != btree.root_page_id();
929            }
930        }
931
932        // Perf: pagerless → skip WAL-action construction (saves a
933        // third manager.get + entity serialize per insert). For
934        // in-memory runtimes finish_paged_write is a no-op.
935        if self.pager.is_some() || self.config.embedded_wal_path.is_some() {
936            let actions = serialized_record
937                .map(|record| {
938                    vec![StoreWalAction::UpsertEntityRecord {
939                        collection: collection.to_string(),
940                        record,
941                    }]
942                })
943                .unwrap_or_default();
944            if registry_dirty {
945                self.mark_paged_registry_dirty();
946            }
947            self.finish_paged_write(actions)?;
948        }
949        Ok(id)
950    }
951
952    /// Get an entity from a collection
953    ///
954    /// Prefers the live SegmentManager view so reads after update/delete observe
955    /// the current in-memory state even when the paged B-tree image has not been
956    /// refreshed yet. Falls back to the B-tree image for recovery-oriented reads.
957    pub fn get(&self, collection: &str, id: EntityId) -> Option<UnifiedEntity> {
958        // Prefer the live manager state to avoid stale reads after manager.update().
959        if let Some(entity) = self
960            .get_collection(collection)
961            .and_then(|manager| manager.get(id))
962        {
963            return Some(entity);
964        }
965
966        // Fall back to the paged B-tree image if the manager does not currently hold the row.
967        if self.pager.is_some() {
968            let btree_indices = self.btree_indices.read();
969            if let Some(btree) = btree_indices.get(collection) {
970                let key = id.raw().to_be_bytes();
971                if let Ok(Some(value)) = btree.get(&key) {
972                    if let Ok((entity, _)) =
973                        Self::deserialize_entity_record(&value, self.format_version())
974                    {
975                        return Some(entity);
976                    }
977                }
978            }
979        }
980
981        None
982    }
983
984    /// Resolve a table row by stable logical identity.
985    ///
986    /// Legacy rows use `logical_id == id`, so the physical-id lookup remains
987    /// the fast path. Versioned rows may have a different physical id and fall
988    /// back to a scan until the history-store/index slices add a dedicated map.
989    pub fn get_table_row_by_logical_id(
990        &self,
991        collection: &str,
992        logical_id: EntityId,
993    ) -> Option<UnifiedEntity> {
994        if let Some(entity) = self.get(collection, logical_id) {
995            if matches!(entity.kind, EntityKind::TableRow { .. })
996                && entity.logical_id() == logical_id
997                && entity.xmax == 0
998            {
999                return Some(entity);
1000            }
1001        }
1002
1003        let manager = self.get_collection(collection)?;
1004        let mut matches = manager.query_all(|entity| {
1005            matches!(entity.kind, EntityKind::TableRow { .. }) && entity.logical_id() == logical_id
1006        });
1007        matches
1008            .iter()
1009            .find(|entity| entity.xmax == 0)
1010            .cloned()
1011            .or_else(|| matches.pop())
1012    }
1013
1014    pub fn table_row_versions_by_logical_id(
1015        &self,
1016        collection: &str,
1017        logical_id: EntityId,
1018    ) -> Vec<UnifiedEntity> {
1019        self.get_collection(collection)
1020            .map(|manager| {
1021                manager.query_all(|entity| {
1022                    matches!(entity.kind, EntityKind::TableRow { .. })
1023                        && entity.logical_id() == logical_id
1024                })
1025            })
1026            .unwrap_or_default()
1027    }
1028
1029    pub fn vacuum_mvcc_history(
1030        &self,
1031        collection: &str,
1032        cutoff_xid: u64,
1033    ) -> Result<MvccVacuumStats, StoreError> {
1034        let manager = self
1035            .get_collection(collection)
1036            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
1037        let entities =
1038            manager.query_all(|entity| matches!(entity.kind, EntityKind::TableRow { .. }));
1039        let mut logical = HashMap::<EntityId, (bool, u64)>::new();
1040        for entity in &entities {
1041            let entry = logical.entry(entity.logical_id()).or_insert((false, 0));
1042            if entity.xmax == 0 {
1043                entry.0 = true;
1044            }
1045            entry.1 = entry.1.max(entity.xmin);
1046        }
1047
1048        let mut stats = MvccVacuumStats::default();
1049        let mut reclaim_ids = Vec::new();
1050        for entity in entities {
1051            if entity.xmax == 0 {
1052                continue;
1053            }
1054            stats.scanned_versions += 1;
1055            let (has_live_version, max_xmin) = logical
1056                .get(&entity.logical_id())
1057                .copied()
1058                .unwrap_or((false, entity.xmin));
1059            let is_delete_tombstone = !has_live_version && entity.xmin == max_xmin;
1060            if entity.xmax < cutoff_xid {
1061                stats.reclaimed_versions += 1;
1062                if is_delete_tombstone {
1063                    stats.reclaimed_tombstones += 1;
1064                } else {
1065                    stats.reclaimed_history_versions += 1;
1066                }
1067                reclaim_ids.push(entity.id);
1068            } else {
1069                stats.retained_versions += 1;
1070                if is_delete_tombstone {
1071                    stats.retained_tombstones += 1;
1072                } else {
1073                    stats.retained_history_versions += 1;
1074                }
1075            }
1076        }
1077
1078        if !reclaim_ids.is_empty() {
1079            self.delete_batch(collection, &reclaim_ids)?;
1080        }
1081        Ok(stats)
1082    }
1083
1084    pub(crate) fn install_versioned_table_row_update(
1085        &self,
1086        collection: &str,
1087        old_version: UnifiedEntity,
1088        mut new_version: UnifiedEntity,
1089        metadata: Option<&Metadata>,
1090    ) -> Result<(), StoreError> {
1091        let manager = self
1092            .get_collection(collection)
1093            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
1094
1095        let old_id = old_version.id;
1096        let new_id = new_version.id;
1097        let inherited_metadata = metadata.cloned().or_else(|| manager.get_metadata(old_id));
1098
1099        self.entity_cache.remove(old_id.raw());
1100        self.entity_cache.remove(new_id.raw());
1101        manager.update(old_version.clone())?;
1102        self.context_index.remove_entity(old_id);
1103
1104        self.register_entity_id(new_version.id);
1105        if let EntityKind::TableRow { ref mut row_id, .. } = new_version.kind {
1106            if *row_id == 0 {
1107                *row_id = manager.next_row_id();
1108            } else {
1109                manager.register_row_id(*row_id);
1110            }
1111        }
1112        new_version.ensure_table_logical_id();
1113        manager.insert(new_version.clone())?;
1114        if let Some(metadata) = inherited_metadata {
1115            manager.set_metadata(new_id, metadata)?;
1116        }
1117        self.context_index.index_entity(collection, &new_version);
1118        if self.config.auto_index_refs {
1119            self.index_cross_refs(&new_version, collection)?;
1120        }
1121
1122        let old_metadata = manager.get_metadata(old_id);
1123        let new_metadata = manager.get_metadata(new_id);
1124        let fv = self.format_version();
1125        let records = vec![
1126            Self::serialize_entity_record(&old_version, old_metadata.as_ref(), fv),
1127            Self::serialize_entity_record(&new_version, new_metadata.as_ref(), fv),
1128        ];
1129        self.finish_paged_write([StoreWalAction::BulkUpsertEntityRecords {
1130            collection: collection.to_string(),
1131            records,
1132        }])?;
1133
1134        Ok(())
1135    }
1136
1137    /// Batch-fetch multiple entities from the same collection in minimal lock acquisitions.
1138    ///
1139    /// Preferred over N individual `get()` calls in indexed-scan loops (sorted index,
1140    /// bitmap, hash). Reduces lock acquisitions from N×3 to 2-3 total.
1141    /// Preserves input order: `result[i]` corresponds to `ids[i]`.
1142    pub fn get_batch(&self, collection: &str, ids: &[EntityId]) -> Vec<Option<UnifiedEntity>> {
1143        match self.get_collection(collection) {
1144            Some(manager) => manager.get_many(ids),
1145            None => vec![None; ids.len()],
1146        }
1147    }
1148
1149    /// Get an entity from any collection
1150    pub fn get_any(&self, id: EntityId) -> Option<(String, UnifiedEntity)> {
1151        // Sharded LRU probe: hits / misses are counted by `EntityCache::get`,
1152        // so external observers can read `entity_cache_hit_rate()` without
1153        // any extra wiring on the call site.
1154        if let Some(cached) = self.entity_cache.get(id.raw()) {
1155            return Some(cached);
1156        }
1157
1158        // Cache miss — fall back to the full collection scan, then memoise.
1159        let collections = self.collections.read();
1160        for (name, manager) in collections.iter() {
1161            if let Some(entity) = manager.get(id) {
1162                let result = (name.clone(), entity);
1163                // Drop the collections read guard before taking any cache
1164                // lock — the cache shards are independent, but releasing
1165                // here keeps `collections` contention low.
1166                drop(collections);
1167                self.entity_cache.insert(id.raw(), result.clone());
1168                return Some(result);
1169            }
1170        }
1171        None
1172    }
1173
1174    /// Hit rate of the store's entity cache (`get_any` lookups).
1175    ///
1176    /// Returns `None` until the cache has served at least one lookup.
1177    /// Exposed for observability — e.g. dashboards distinguishing graph
1178    /// workloads (high hit rate) from OLTP DML (≈ 0 % hit rate).
1179    pub fn entity_cache_hit_rate(&self) -> Option<f64> {
1180        self.entity_cache.hit_rate()
1181    }
1182
1183    /// Snapshot of cache hit / miss / eviction counters and current size.
1184    pub fn entity_cache_stats(&self) -> super::super::entity_cache::EntityCacheStats {
1185        self.entity_cache.stats()
1186    }
1187
1188    /// Delete an entity
1189    pub fn delete(&self, collection: &str, id: EntityId) -> Result<bool, StoreError> {
1190        // Invalidate entity cache (read-lock probe; no write lock when cold).
1191        self.entity_cache.remove(id.raw());
1192        let manager = self
1193            .get_collection(collection)
1194            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
1195
1196        let deleted = manager.delete(id)?;
1197        if !deleted {
1198            return Ok(false);
1199        }
1200
1201        // Remove from B-tree index if active
1202        let mut registry_dirty = false;
1203        if self.pager.is_some() {
1204            let btree_indices = self.btree_indices.read();
1205            if let Some(btree) = btree_indices.get(collection) {
1206                let root_before = btree.root_page_id();
1207                let key = id.raw().to_be_bytes();
1208                let _ = btree.delete(&key);
1209                registry_dirty = root_before != btree.root_page_id();
1210            }
1211        }
1212
1213        // Remove cross-references
1214        self.unindex_cross_refs(id)?;
1215
1216        // Remove from graph label index
1217        self.remove_from_graph_label_index(collection, id);
1218
1219        if registry_dirty {
1220            self.mark_paged_registry_dirty();
1221        }
1222        self.finish_paged_write([StoreWalAction::DeleteEntityRecord {
1223            collection: collection.to_string(),
1224            entity_id: id.raw(),
1225        }])?;
1226
1227        Ok(true)
1228    }
1229
1230    pub fn delete_batch(
1231        &self,
1232        collection: &str,
1233        ids: &[EntityId],
1234    ) -> Result<Vec<EntityId>, StoreError> {
1235        if ids.is_empty() {
1236            return Ok(Vec::new());
1237        }
1238
1239        // Sharded invalidation. The bounded LRU's shard write lock is
1240        // only acquired when the shard actually carries one of these ids,
1241        // so the bench-typical zero-hit `delete_sequential` workload never
1242        // takes a write lock here at all.
1243        self.entity_cache.remove_many(ids.iter().map(|id| id.raw()));
1244
1245        let manager = self
1246            .get_collection(collection)
1247            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
1248
1249        let deleted_ids = manager.delete_batch(ids)?;
1250        if deleted_ids.is_empty() {
1251            return Ok(deleted_ids);
1252        }
1253
1254        let mut registry_dirty = false;
1255        if self.pager.is_some() {
1256            let btree_indices = self.btree_indices.read();
1257            if let Some(btree) = btree_indices.get(collection) {
1258                let root_before = btree.root_page_id();
1259                for id in &deleted_ids {
1260                    let key = id.raw().to_be_bytes();
1261                    let _ = btree.delete(&key);
1262                }
1263                registry_dirty = root_before != btree.root_page_id();
1264            }
1265        }
1266
1267        self.unindex_cross_refs_batch(&deleted_ids)?;
1268        self.remove_from_graph_label_index_batch(collection, &deleted_ids);
1269        if registry_dirty {
1270            self.mark_paged_registry_dirty();
1271        }
1272        let actions = deleted_ids
1273            .iter()
1274            .map(|id| StoreWalAction::DeleteEntityRecord {
1275                collection: collection.to_string(),
1276                entity_id: id.raw(),
1277            })
1278            .collect::<Vec<_>>();
1279        self.finish_paged_write(actions)?;
1280
1281        Ok(deleted_ids)
1282    }
1283
1284    /// Set metadata for an entity
1285    pub fn set_metadata(
1286        &self,
1287        collection: &str,
1288        id: EntityId,
1289        metadata: Metadata,
1290    ) -> Result<(), StoreError> {
1291        let manager = self
1292            .get_collection(collection)
1293            .ok_or_else(|| StoreError::CollectionNotFound(collection.to_string()))?;
1294
1295        manager.set_metadata(id, metadata)?;
1296        if let Some(entity) = manager.get(id) {
1297            self.persist_entities_to_pager(collection, std::slice::from_ref(&entity))?;
1298        }
1299        Ok(())
1300    }
1301
1302    /// Get metadata for an entity
1303    pub fn get_metadata(&self, collection: &str, id: EntityId) -> Option<Metadata> {
1304        self.get_collection(collection)?.get_metadata(id)
1305    }
1306
1307    /// Add a cross-reference between entities
1308    pub fn add_cross_ref(
1309        &self,
1310        source_collection: &str,
1311        source_id: EntityId,
1312        target_collection: &str,
1313        target_id: EntityId,
1314        ref_type: RefType,
1315        weight: f32,
1316    ) -> Result<(), StoreError> {
1317        // Check source exists
1318        let source_manager = self
1319            .get_collection(source_collection)
1320            .ok_or_else(|| StoreError::CollectionNotFound(source_collection.to_string()))?;
1321
1322        if source_manager.get(source_id).is_none() {
1323            return Err(StoreError::EntityNotFound(source_id));
1324        }
1325
1326        // Check target exists
1327        let target_manager = self
1328            .get_collection(target_collection)
1329            .ok_or_else(|| StoreError::CollectionNotFound(target_collection.to_string()))?;
1330
1331        if target_manager.get(target_id).is_none() {
1332            return Err(StoreError::EntityNotFound(target_id));
1333        }
1334
1335        // Check limits
1336        let current_refs = self
1337            .cross_refs
1338            .read()
1339            .get(&source_id)
1340            .map_or(0, |v| v.len());
1341
1342        if current_refs >= self.config.max_cross_refs {
1343            return Err(StoreError::TooManyRefs(source_id));
1344        }
1345
1346        let mut registry_dirty = false;
1347        {
1348            let mut forward = self.cross_refs.write();
1349            let refs = forward.entry(source_id).or_default();
1350            let inserted = !refs.iter().any(|(id, kind, coll)| {
1351                *id == target_id && *kind == ref_type && coll == target_collection
1352            });
1353            if inserted {
1354                refs.push((target_id, ref_type, target_collection.to_string()));
1355                registry_dirty = true;
1356            }
1357        }
1358
1359        {
1360            let mut reverse = self.reverse_refs.write();
1361            let refs = reverse.entry(target_id).or_default();
1362            let inserted = !refs.iter().any(|(id, kind, coll)| {
1363                *id == source_id && *kind == ref_type && coll == source_collection
1364            });
1365            if inserted {
1366                refs.push((source_id, ref_type, source_collection.to_string()));
1367                registry_dirty = true;
1368            }
1369        }
1370
1371        if let Some(mut entity) = source_manager.get(source_id) {
1372            if !entity.cross_refs().iter().any(|xref| {
1373                xref.target == target_id
1374                    && xref.ref_type == ref_type
1375                    && xref.target_collection == target_collection
1376            }) {
1377                let cross_ref = CrossRef::with_weight(
1378                    source_id,
1379                    target_id,
1380                    target_collection,
1381                    ref_type,
1382                    weight,
1383                );
1384                entity.add_cross_ref(cross_ref);
1385                let _ = source_manager.update(entity.clone());
1386                registry_dirty = true;
1387                self.persist_entities_to_pager(source_collection, std::slice::from_ref(&entity))?;
1388            }
1389        }
1390
1391        if registry_dirty {
1392            self.mark_paged_registry_dirty();
1393            if matches!(
1394                self.config.durability_mode,
1395                crate::api::DurabilityMode::Strict
1396            ) {
1397                self.flush_paged_state()?;
1398            }
1399        }
1400
1401        Ok(())
1402    }
1403
1404    /// Get cross-references from an entity
1405    pub fn get_refs_from(&self, id: EntityId) -> Vec<(EntityId, RefType, String)> {
1406        self.cross_refs.read().get(&id).cloned().unwrap_or_default()
1407    }
1408
1409    /// Get cross-references to an entity
1410    pub fn get_refs_to(&self, id: EntityId) -> Vec<(EntityId, RefType, String)> {
1411        self.reverse_refs
1412            .read()
1413            .get(&id)
1414            .cloned()
1415            .unwrap_or_default()
1416    }
1417
1418    /// Expand cross-references to get related entities
1419    pub fn expand_refs(
1420        &self,
1421        id: EntityId,
1422        depth: u32,
1423        ref_types: Option<&[RefType]>,
1424    ) -> Vec<(UnifiedEntity, u32, RefType)> {
1425        let mut results = Vec::new();
1426        let mut visited = std::collections::HashSet::new();
1427        visited.insert(id);
1428
1429        self.expand_refs_recursive(id, depth, ref_types, &mut visited, &mut results, 1);
1430
1431        results
1432    }
1433
1434    fn expand_refs_recursive(
1435        &self,
1436        id: EntityId,
1437        max_depth: u32,
1438        ref_types: Option<&[RefType]>,
1439        visited: &mut std::collections::HashSet<EntityId>,
1440        results: &mut Vec<(UnifiedEntity, u32, RefType)>,
1441        current_depth: u32,
1442    ) {
1443        if current_depth > max_depth {
1444            return;
1445        }
1446
1447        for (target_id, ref_type, target_collection) in self.get_refs_from(id) {
1448            if visited.contains(&target_id) {
1449                continue;
1450            }
1451
1452            if let Some(types) = ref_types {
1453                if !types.contains(&ref_type) {
1454                    continue;
1455                }
1456            }
1457
1458            visited.insert(target_id);
1459
1460            if let Some(entity) = self.get(&target_collection, target_id) {
1461                results.push((entity, current_depth, ref_type));
1462
1463                // Recurse
1464                self.expand_refs_recursive(
1465                    target_id,
1466                    max_depth,
1467                    ref_types,
1468                    visited,
1469                    results,
1470                    current_depth + 1,
1471                );
1472            }
1473        }
1474    }
1475
1476    /// Index cross-references from an entity
1477    pub(crate) fn index_cross_refs(
1478        &self,
1479        entity: &UnifiedEntity,
1480        collection: &str,
1481    ) -> Result<(), StoreError> {
1482        let mut registry_dirty = false;
1483        for cross_ref in entity.cross_refs() {
1484            if cross_ref.target_collection.is_empty() {
1485                continue;
1486            }
1487            {
1488                let mut forward = self.cross_refs.write();
1489                let refs = forward.entry(cross_ref.source).or_default();
1490                let inserted = !refs.iter().any(|(id, kind, coll)| {
1491                    *id == cross_ref.target
1492                        && *kind == cross_ref.ref_type
1493                        && coll == &cross_ref.target_collection
1494                });
1495                if inserted {
1496                    refs.push((
1497                        cross_ref.target,
1498                        cross_ref.ref_type,
1499                        cross_ref.target_collection.clone(),
1500                    ));
1501                    registry_dirty = true;
1502                }
1503            }
1504
1505            {
1506                let mut reverse = self.reverse_refs.write();
1507                let refs = reverse.entry(cross_ref.target).or_default();
1508                let inserted = !refs.iter().any(|(id, kind, coll)| {
1509                    *id == cross_ref.source && *kind == cross_ref.ref_type && coll == collection
1510                });
1511                if inserted {
1512                    refs.push((cross_ref.source, cross_ref.ref_type, collection.to_string()));
1513                    registry_dirty = true;
1514                }
1515            }
1516        }
1517
1518        if registry_dirty {
1519            self.mark_paged_registry_dirty();
1520        }
1521
1522        Ok(())
1523    }
1524
1525    /// Remove cross-references for an entity
1526    pub(crate) fn unindex_cross_refs(&self, id: EntityId) -> Result<(), StoreError> {
1527        // Remove forward refs
1528        self.cross_refs.write().remove(&id);
1529
1530        // Remove from reverse refs (scan all)
1531        let mut reverse = self.reverse_refs.write();
1532        for refs in reverse.values_mut() {
1533            refs.retain(|(source, _, _)| *source != id);
1534        }
1535        reverse.remove(&id);
1536        self.mark_paged_registry_dirty();
1537
1538        Ok(())
1539    }
1540
1541    pub(crate) fn unindex_cross_refs_batch(&self, ids: &[EntityId]) -> Result<(), StoreError> {
1542        if ids.is_empty() {
1543            return Ok(());
1544        }
1545
1546        let id_set: std::collections::HashSet<EntityId> = ids.iter().copied().collect();
1547
1548        // Read-only probe: under steady-state delete workloads most rows have
1549        // no cross-refs at all, so both maps are entirely cold for the batch.
1550        // Acquiring the write lock to scan `reverse_refs.values_mut()` over an
1551        // unrelated dictionary was the dominant cost in #85's `delete_sequential`
1552        // profile. The read-lock probe below is O(|ids|) hashmap lookups against
1553        // each map, vs. O(|reverse_refs|) for the write-path scan it replaces.
1554        //
1555        // Decision: skip the write path iff
1556        //   - no deleted id is a key in `cross_refs`  (no outbound refs to drop,
1557        //     and so no `(deleted_id, _, _)` source entry exists anywhere in
1558        //     `reverse_refs.values()` either — the two maps are kept in sync by
1559        //     `add_cross_ref`), AND
1560        //   - no deleted id is a key in `reverse_refs` (no inbound refs to drop).
1561        let needs_forward_cleanup = {
1562            let forward = self.cross_refs.read();
1563            id_set.iter().any(|id| forward.contains_key(id))
1564        };
1565        let needs_reverse_cleanup = {
1566            let reverse = self.reverse_refs.read();
1567            id_set.iter().any(|id| reverse.contains_key(id))
1568        };
1569
1570        if !needs_forward_cleanup && !needs_reverse_cleanup {
1571            self.unindex_cross_refs_fast_path
1572                .fetch_add(1, Ordering::Relaxed);
1573            return Ok(());
1574        }
1575
1576        if needs_forward_cleanup {
1577            let mut forward = self.cross_refs.write();
1578            for id in &id_set {
1579                forward.remove(id);
1580            }
1581        }
1582
1583        if needs_reverse_cleanup || needs_forward_cleanup {
1584            // The inner `values_mut()` scan is only needed when at least one
1585            // deleted id was a *source* somewhere — which, by the invariant
1586            // above, can only happen when it had outbound forward refs.
1587            let mut reverse = self.reverse_refs.write();
1588            if needs_forward_cleanup {
1589                for refs in reverse.values_mut() {
1590                    refs.retain(|(source, _, _)| !id_set.contains(source));
1591                }
1592            }
1593            reverse.retain(|target, refs| !id_set.contains(target) && !refs.is_empty());
1594        }
1595        self.mark_paged_registry_dirty();
1596
1597        Ok(())
1598    }
1599
1600    /// Test/observability hook: number of times `unindex_cross_refs_batch`
1601    /// took the read-only fast path. Used to pin the early-exit in tests
1602    /// and as a cheap signal in delete-heavy benchmarks.
1603    pub fn unindex_cross_refs_fast_path_hits(&self) -> u64 {
1604        self.unindex_cross_refs_fast_path.load(Ordering::Relaxed)
1605    }
1606
1607    /// Query across all collections with a filter
1608    pub fn query_all<F>(&self, filter: F) -> Vec<(String, UnifiedEntity)>
1609    where
1610        F: Fn(&UnifiedEntity) -> bool + Clone + Send + Sync,
1611    {
1612        // Issue #815 — snapshot the (name, manager) handles under a brief
1613        // read lock, then release the `collections` lock *before* the
1614        // potentially long per-collection scan. Holding `collections.read()`
1615        // across a full-store scan let a large `/catalog` snapshot block (and
1616        // be blocked by) the replica apply loop's collection-creation writes:
1617        // under parking_lot a writer waiting behind the long read parks every
1618        // subsequent reader, so `/catalog` and readiness wedged during a big
1619        // WAL re-apply. The managers are `Arc`-shared with their own internal
1620        // locks, so scanning them off the map lock is safe and keeps the HTTP
1621        // surface responsive.
1622        let pairs: Vec<(String, Arc<SegmentManager>)> = {
1623            let collections = self.collections.read();
1624            collections
1625                .iter()
1626                .map(|(name, mgr)| (name.clone(), Arc::clone(mgr)))
1627                .collect()
1628        };
1629
1630        let use_parallel = pairs.len() > 1 && crate::runtime::SystemInfo::should_parallelize();
1631        if !use_parallel {
1632            // Single collection — no parallelism overhead
1633            return pairs
1634                .into_iter()
1635                .flat_map(|(name, mgr)| {
1636                    mgr.query_all(filter.clone())
1637                        .into_iter()
1638                        .map(move |e| (name.clone(), e))
1639                })
1640                .collect();
1641        }
1642
1643        // Multiple collections — scan in parallel
1644        let filter_ref = &filter;
1645        let collection_results: Vec<Vec<(String, UnifiedEntity)>> = std::thread::scope(|s| {
1646            pairs
1647                .iter()
1648                .map(|(name, manager)| {
1649                    let name = (*name).clone();
1650                    s.spawn(move || {
1651                        manager
1652                            .query_all(|e| filter_ref(e))
1653                            .into_iter()
1654                            .map(|e| (name.clone(), e))
1655                            .collect::<Vec<_>>()
1656                    })
1657                })
1658                .collect::<Vec<_>>()
1659                .into_iter()
1660                .map(|h| h.join().unwrap_or_default())
1661                .collect()
1662        });
1663
1664        collection_results.into_iter().flatten().collect()
1665    }
1666
1667    /// Scan every collection under an explicit MVCC snapshot.
1668    pub fn scan<F>(
1669        &self,
1670        snapshot: Option<&crate::runtime::impl_core::SnapshotContext>,
1671        filter: F,
1672    ) -> Vec<(String, UnifiedEntity)>
1673    where
1674        F: Fn(&UnifiedEntity) -> bool + Clone + Send + Sync,
1675    {
1676        let pairs: Vec<(String, Arc<SegmentManager>)> = {
1677            let collections = self.collections.read();
1678            collections
1679                .iter()
1680                .map(|(name, manager)| (name.clone(), Arc::clone(manager)))
1681                .collect()
1682        };
1683
1684        let use_parallel = pairs.len() > 1 && crate::runtime::SystemInfo::should_parallelize();
1685        if !use_parallel {
1686            return pairs
1687                .into_iter()
1688                .flat_map(|(name, manager)| {
1689                    manager
1690                        .scan(snapshot, filter.clone())
1691                        .into_iter()
1692                        .map(move |entity| (name.clone(), entity))
1693                })
1694                .collect();
1695        }
1696
1697        let filter_ref = &filter;
1698        let collection_results: Vec<Vec<(String, UnifiedEntity)>> = std::thread::scope(|scope| {
1699            pairs
1700                .iter()
1701                .map(|(name, manager)| {
1702                    let name = name.clone();
1703                    scope.spawn(move || {
1704                        manager
1705                            .scan(snapshot, |entity| filter_ref(entity))
1706                            .into_iter()
1707                            .map(|entity| (name.clone(), entity))
1708                            .collect::<Vec<_>>()
1709                    })
1710                })
1711                .collect::<Vec<_>>()
1712                .into_iter()
1713                .map(|handle| handle.join().unwrap_or_default())
1714                .collect()
1715        });
1716
1717        collection_results.into_iter().flatten().collect()
1718    }
1719
1720    /// Filter by metadata across all collections
1721    pub fn filter_metadata_all(
1722        &self,
1723        filters: &[(String, MetadataFilter)],
1724    ) -> Vec<(String, EntityId)> {
1725        let mut results = Vec::new();
1726        let collections = self.collections.read();
1727
1728        for (name, manager) in collections.iter() {
1729            for id in manager.filter_metadata(filters) {
1730                results.push((name.clone(), id));
1731            }
1732        }
1733
1734        results
1735    }
1736
1737    /// Get statistics
1738    pub fn stats(&self) -> StoreStats {
1739        // Issue #815 — snapshot the manager handles, drop the `collections`
1740        // read lock, then gather per-collection stats off the map lock. The
1741        // readiness probe (via `health()`) walks `stats()`, so holding the
1742        // store lock across a full-store stats scan was part of the same
1743        // wedge that froze `/catalog` during a large replica re-apply.
1744        let pairs: Vec<(String, Arc<SegmentManager>)> = {
1745            let collections = self.collections.read();
1746            collections
1747                .iter()
1748                .map(|(name, mgr)| (name.clone(), Arc::clone(mgr)))
1749                .collect()
1750        };
1751
1752        let mut stats = StoreStats {
1753            collection_count: pairs.len(),
1754            ..Default::default()
1755        };
1756
1757        for (name, manager) in &pairs {
1758            let manager_stats = manager.stats();
1759            stats.total_entities += manager_stats.total_entities;
1760            stats.total_memory_bytes += manager_stats.total_memory_bytes;
1761            stats.collections.insert(name.clone(), manager_stats);
1762        }
1763
1764        stats
1765    }
1766
1767    /// Run maintenance on all collections
1768    pub fn run_maintenance(&self) -> Result<(), StoreError> {
1769        let collections = self.collections.read();
1770        for manager in collections.values() {
1771            manager.run_maintenance()?;
1772        }
1773        Ok(())
1774    }
1775
1776    /// Bytes pressure consolidation could plausibly return across collections.
1777    pub fn reclaimable_segment_bytes(&self) -> u64 {
1778        let collections = self.collections.read();
1779        collections
1780            .values()
1781            .map(|manager| manager.reclaimable_bytes())
1782            .fold(0, u64::saturating_add)
1783    }
1784
1785    /// Advance pressure-triggered consolidation by one paced tick per
1786    /// collection. Returns true if any collection started or advanced a run.
1787    pub fn pressure_consolidation_tick(&self) -> bool {
1788        let managers: Vec<_> = self.collections.read().values().cloned().collect();
1789        // A plain loop, not `any()`: every collection must receive its tick —
1790        // short-circuiting on the first advanced run would starve the rest.
1791        let mut advanced = false;
1792        for manager in &managers {
1793            advanced |= manager.pressure_consolidation_tick();
1794        }
1795        advanced
1796    }
1797}
1798
1799/// Flatten a JSON value into dot-notation key-value pairs for red_config.
1800fn flatten_config_json(
1801    prefix: &str,
1802    value: &crate::serde_json::Value,
1803    out: &mut Vec<(String, reddb_types::Value)>,
1804) {
1805    use reddb_types::Value;
1806    match value {
1807        crate::serde_json::Value::Object(map) => {
1808            for (k, v) in map {
1809                let key = if prefix.is_empty() {
1810                    k.clone()
1811                } else {
1812                    format!("{prefix}.{k}")
1813                };
1814                flatten_config_json(&key, v, out);
1815            }
1816        }
1817        crate::serde_json::Value::String(s) => {
1818            out.push((prefix.to_string(), Value::text(s.clone())));
1819        }
1820        crate::serde_json::Value::Integer(n) => {
1821            out.push((prefix.to_string(), Value::Integer(*n)));
1822        }
1823        crate::serde_json::Value::Number(n) => {
1824            if n.fract().abs() < f64::EPSILON {
1825                out.push((prefix.to_string(), Value::UnsignedInteger(*n as u64)));
1826            } else {
1827                out.push((prefix.to_string(), Value::Float(*n)));
1828            }
1829        }
1830        crate::serde_json::Value::Decimal(n) => {
1831            out.push((prefix.to_string(), Value::DecimalText(n.clone())));
1832        }
1833        crate::serde_json::Value::Bool(b) => {
1834            out.push((prefix.to_string(), Value::Boolean(*b)));
1835        }
1836        crate::serde_json::Value::Null => {
1837            out.push((prefix.to_string(), Value::Null));
1838        }
1839        crate::serde_json::Value::Array(arr) => {
1840            let json_str = crate::serde_json::to_string(value).unwrap_or_default();
1841            out.push((prefix.to_string(), Value::text(json_str)));
1842        }
1843    }
1844}