selene-db-graph 1.3.0

In-memory property-graph storage core (ArcSwap + imbl CoW, label/typed indexes, write funnel) for selene-db.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Post-snapshot WAL index-intent accumulation and registration-set replay.
//!
//! Split out of `recovery_state.rs` (700-LOC cap). `RecoveryState::apply_change`
//! distills each property/composite-index `SchemaChange` into a [`PendingIndex`]
//! / [`PendingCompositeIndex`] intent; `into_graph` then replays those intents
//! against the **registration set only** (empty `TypedIndex` placeholders). The
//! single bitmap rebuild runs downstream in
//! `SharedGraph::from_graph_parts_and_snapshot` (GRAPH-06 dedup).

use selene_core::{
    DbString, HnswIndexConfig, IvfIndexConfig, SchemaChange, SchemaPropertyIndexKind,
    SchemaVectorIndexKind,
};
use smallvec::SmallVec;

use crate::core_provider::sections::SchemaEntityKind;
use crate::graph::{
    CompositePropertyIndexEntry, PropertyIndexEntry, SeleneGraph, TextIndexEntry, VectorIndexEntry,
};
use crate::typed_index::TypedIndex;
use crate::typed_index::TypedIndexKind;
use crate::vector_index::{VectorIndex, VectorIndexKind};

/// A distilled, replayable property-index intent from the WAL.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum PendingIndex {
    /// Register an index for `(label, property)` of the declared `kind`.
    Create {
        /// Entity family the index applies to.
        entity: SchemaEntityKind,
        /// Indexed node label.
        label: DbString,
        /// Indexed property key.
        property: DbString,
        /// Declared indexable value kind.
        kind: TypedIndexKind,
        /// Optional explicit catalog name.
        name: Option<DbString>,
    },
    /// Drop the index registration for `(label, property)`.
    Drop {
        /// Entity family the index applies to.
        entity: SchemaEntityKind,
        /// Indexed node label.
        label: DbString,
        /// Indexed property key.
        property: DbString,
    },
}

/// A distilled, replayable composite-property-index intent from the WAL.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum PendingCompositeIndex {
    /// Register a composite index over `(label, properties...)`.
    Create {
        /// Indexed node label.
        label: DbString,
        /// Indexed property keys in declaration order.
        properties: SmallVec<[DbString; 4]>,
        /// Declared indexable value kinds in declaration order.
        kinds: SmallVec<[TypedIndexKind; 4]>,
        /// Optional explicit catalog name.
        name: Option<DbString>,
    },
    /// Drop the composite index registration over `(label, properties...)`.
    Drop {
        /// Indexed node label.
        label: DbString,
        /// Indexed property keys in declaration order.
        properties: SmallVec<[DbString; 4]>,
    },
}

/// A distilled, replayable vector-index intent from the WAL.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum PendingVectorIndex {
    /// Register a vector index for `(label, property)`.
    Create {
        /// Indexed node label.
        label: DbString,
        /// Indexed vector property key.
        property: DbString,
        /// Declared vector index kind.
        kind: VectorIndexKind,
        /// Required vector dimensionality.
        dimension: u32,
        /// Optional HNSW construction config.
        hnsw_config: Option<HnswIndexConfig>,
        /// Optional IVF construction config.
        ivf_config: Option<IvfIndexConfig>,
        /// Optional explicit catalog name.
        name: Option<DbString>,
    },
    /// Drop the vector index registration for `(label, property)`.
    Drop {
        /// Indexed node label.
        label: DbString,
        /// Indexed vector property key.
        property: DbString,
    },
}

/// A distilled, replayable text-index intent from the WAL.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum PendingTextIndex {
    /// Register a text index for `(label, property)`.
    Create {
        /// Indexed node label.
        label: DbString,
        /// Indexed text property key.
        property: DbString,
        /// Optional explicit catalog name.
        name: Option<DbString>,
    },
    /// Drop the text index registration for `(label, property)`.
    Drop {
        /// Indexed node label.
        label: DbString,
        /// Indexed text property key.
        property: DbString,
    },
}

/// Distill a property-index `SchemaChange` into a replayable intent, or `None`
/// when the change is not a property-index change.
pub(super) fn pending_property_index_change(change: &SchemaChange) -> Option<PendingIndex> {
    match change {
        SchemaChange::PropertyIndexCreated {
            label,
            property,
            kind,
        } => Some(PendingIndex::Create {
            entity: SchemaEntityKind::Node,
            label: label.clone(),
            property: property.clone(),
            kind: typed_kind_from(*kind),
            name: None,
        }),
        SchemaChange::PropertyIndexCreatedNamed {
            label,
            property,
            kind,
            name,
        } => Some(PendingIndex::Create {
            entity: SchemaEntityKind::Node,
            label: label.clone(),
            property: property.clone(),
            kind: typed_kind_from(*kind),
            name: name.clone(),
        }),
        SchemaChange::PropertyIndexDropped { label, property } => Some(PendingIndex::Drop {
            entity: SchemaEntityKind::Node,
            label: label.clone(),
            property: property.clone(),
        }),
        SchemaChange::EdgePropertyIndexCreated {
            label,
            property,
            kind,
            name,
        } => Some(PendingIndex::Create {
            entity: SchemaEntityKind::Edge,
            label: label.clone(),
            property: property.clone(),
            kind: typed_kind_from(*kind),
            name: name.clone(),
        }),
        SchemaChange::EdgePropertyIndexDropped { label, property } => Some(PendingIndex::Drop {
            entity: SchemaEntityKind::Edge,
            label: label.clone(),
            property: property.clone(),
        }),
        _ => None,
    }
}

/// Replay post-snapshot WAL property-index intents against the **registration
/// set only** — create inserts an empty `TypedIndex` placeholder of the declared
/// kind, drop removes the registration. The bitmap contents are (re)built once,
/// downstream, by `SharedGraph::from_graph_parts_and_snapshot`'s single
/// `rebuild_property_indexes` pass (which reads this registration set), so
/// `into_graph` no longer builds index contents that the downstream rebuild
/// immediately discards (GRAPH-06).
pub(super) fn replay_property_index_changes(
    graph: &mut SeleneGraph,
    changes: &[PendingIndex],
) -> crate::GraphResult<()> {
    for change in changes {
        match change {
            PendingIndex::Create {
                entity,
                label,
                property,
                kind,
                name,
            } => {
                let target = match entity {
                    SchemaEntityKind::Node => &mut graph.property_index,
                    SchemaEntityKind::Edge => &mut graph.edge_property_index,
                };
                target.insert(
                    (label.clone(), property.clone()),
                    PropertyIndexEntry::new(TypedIndex::new(*kind), name.clone()),
                );
            }
            PendingIndex::Drop {
                entity,
                label,
                property,
            } => {
                let target = match entity {
                    SchemaEntityKind::Node => &mut graph.property_index,
                    SchemaEntityKind::Edge => &mut graph.edge_property_index,
                };
                target.remove(&(label.clone(), property.clone()));
            }
        }
    }
    Ok(())
}

/// Distill a composite-property-index `SchemaChange` into a replayable intent,
/// or `None` when the change is not a composite-index change.
pub(super) fn pending_composite_property_index_change(
    change: &SchemaChange,
) -> Option<PendingCompositeIndex> {
    match change {
        SchemaChange::CompositePropertyIndexCreated {
            label,
            properties,
            kinds,
            name,
        } => Some(PendingCompositeIndex::Create {
            label: label.clone(),
            properties: properties.clone(),
            kinds: kinds.iter().copied().map(typed_kind_from).collect(),
            name: name.clone(),
        }),
        SchemaChange::CompositePropertyIndexDropped { label, properties } => {
            Some(PendingCompositeIndex::Drop {
                label: label.clone(),
                properties: properties.clone(),
            })
        }
        _ => None,
    }
}

/// Replay post-snapshot WAL composite-index intents against the **registration
/// set only** (see [`replay_property_index_changes`]); the downstream
/// `rebuild_composite_property_indexes` pass fills the bitmaps once.
pub(super) fn replay_composite_property_index_changes(
    graph: &mut SeleneGraph,
    changes: &[PendingCompositeIndex],
) -> crate::GraphResult<()> {
    for change in changes {
        match change {
            PendingCompositeIndex::Create {
                label,
                properties,
                kinds,
                name,
            } => {
                let key = crate::graph::composite_property_key(properties);
                graph.composite_property_index.insert(
                    (label.clone(), key),
                    CompositePropertyIndexEntry::new(
                        crate::CompositeTypedIndex::new(kinds.clone()),
                        properties.clone(),
                        name.clone(),
                    ),
                );
            }
            PendingCompositeIndex::Drop { label, properties } => {
                let key = crate::graph::composite_property_key(properties);
                graph.composite_property_index.remove(&(label.clone(), key));
            }
        }
    }
    Ok(())
}

/// Distill a vector-index `SchemaChange` into a replayable intent, or `None`
/// when the change is not a vector-index change.
pub(super) fn pending_vector_index_change(change: &SchemaChange) -> Option<PendingVectorIndex> {
    match change {
        SchemaChange::VectorIndexCreated {
            label,
            property,
            kind,
            dimension,
            name,
            hnsw_config,
            ivf_config,
        } => Some(PendingVectorIndex::Create {
            label: label.clone(),
            property: property.clone(),
            kind: vector_kind_from(*kind),
            dimension: *dimension,
            hnsw_config: *hnsw_config,
            ivf_config: *ivf_config,
            name: name.clone(),
        }),
        SchemaChange::VectorIndexDropped { label, property } => Some(PendingVectorIndex::Drop {
            label: label.clone(),
            property: property.clone(),
        }),
        _ => None,
    }
}

/// Replay post-snapshot WAL vector-index intents against the registration set
/// only; the downstream `rebuild_vector_indexes` pass fills row membership.
pub(super) fn replay_vector_index_changes(
    graph: &mut SeleneGraph,
    changes: &[PendingVectorIndex],
) -> crate::GraphResult<()> {
    for change in changes {
        match change {
            PendingVectorIndex::Create {
                label,
                property,
                kind,
                dimension,
                hnsw_config,
                ivf_config,
                name,
            } => {
                graph.vector_index.insert(
                    (label.clone(), property.clone()),
                    VectorIndexEntry::new(
                        VectorIndex::new_with_configs(
                            *kind,
                            *dimension,
                            *hnsw_config,
                            *ivf_config,
                        )?,
                        name.clone(),
                    ),
                );
            }
            PendingVectorIndex::Drop { label, property } => {
                graph
                    .vector_index
                    .remove(&(label.clone(), property.clone()));
            }
        }
    }
    Ok(())
}

/// Distill a text-index `SchemaChange` into a replayable intent, or `None`
/// when the change is not a text-index change.
pub(super) fn pending_text_index_change(change: &SchemaChange) -> Option<PendingTextIndex> {
    match change {
        SchemaChange::TextIndexCreated {
            label,
            property,
            name,
        } => Some(PendingTextIndex::Create {
            label: label.clone(),
            property: property.clone(),
            name: name.clone(),
        }),
        SchemaChange::TextIndexDropped { label, property } => Some(PendingTextIndex::Drop {
            label: label.clone(),
            property: property.clone(),
        }),
        _ => None,
    }
}

/// Replay post-snapshot WAL text-index intents against the registration set
/// only; the downstream `rebuild_text_indexes` pass fills postings once.
pub(super) fn replay_text_index_changes(
    graph: &mut SeleneGraph,
    changes: &[PendingTextIndex],
) -> crate::GraphResult<()> {
    for change in changes {
        match change {
            PendingTextIndex::Create {
                label,
                property,
                name,
            } => {
                graph.text_index.insert(
                    (label.clone(), property.clone()),
                    TextIndexEntry::new(
                        crate::TextIndex::empty(label.clone(), property.clone()),
                        name.clone(),
                    ),
                );
            }
            PendingTextIndex::Drop { label, property } => {
                graph.text_index.remove(&(label.clone(), property.clone()));
            }
        }
    }
    Ok(())
}

/// Map a persisted `SchemaPropertyIndexKind` to the in-memory `TypedIndexKind`.
pub(super) const fn typed_kind_from(kind: SchemaPropertyIndexKind) -> TypedIndexKind {
    match kind {
        SchemaPropertyIndexKind::Bool => TypedIndexKind::Bool,
        SchemaPropertyIndexKind::I64 => TypedIndexKind::I64,
        SchemaPropertyIndexKind::U64 => TypedIndexKind::U64,
        SchemaPropertyIndexKind::I128 => TypedIndexKind::I128,
        SchemaPropertyIndexKind::U128 => TypedIndexKind::U128,
        SchemaPropertyIndexKind::Decimal => TypedIndexKind::Decimal,
        SchemaPropertyIndexKind::F32 => TypedIndexKind::F32,
        SchemaPropertyIndexKind::F64 => TypedIndexKind::F64,
        SchemaPropertyIndexKind::String => TypedIndexKind::String,
        SchemaPropertyIndexKind::Date => TypedIndexKind::Date,
        SchemaPropertyIndexKind::LocalDateTime => TypedIndexKind::LocalDateTime,
        SchemaPropertyIndexKind::ZonedDateTime => TypedIndexKind::ZonedDateTime,
        SchemaPropertyIndexKind::LocalTime => TypedIndexKind::LocalTime,
        SchemaPropertyIndexKind::ZonedTime => TypedIndexKind::ZonedTime,
        SchemaPropertyIndexKind::Duration => TypedIndexKind::Duration,
        SchemaPropertyIndexKind::Uuid => TypedIndexKind::Uuid,
    }
}

/// Map a persisted `SchemaVectorIndexKind` to the in-memory `VectorIndexKind`.
pub(super) const fn vector_kind_from(kind: SchemaVectorIndexKind) -> VectorIndexKind {
    match kind {
        SchemaVectorIndexKind::Flat => VectorIndexKind::Flat,
        SchemaVectorIndexKind::HnswSquaredEuclidean => VectorIndexKind::HnswSquaredEuclidean,
        SchemaVectorIndexKind::HnswCosine => VectorIndexKind::HnswCosine,
        SchemaVectorIndexKind::HnswNegativeInnerProduct => {
            VectorIndexKind::HnswNegativeInnerProduct
        }
        SchemaVectorIndexKind::IvfSquaredEuclidean => VectorIndexKind::IvfSquaredEuclidean,
        SchemaVectorIndexKind::IvfCosine => VectorIndexKind::IvfCosine,
        SchemaVectorIndexKind::IvfNegativeInnerProduct => VectorIndexKind::IvfNegativeInnerProduct,
        SchemaVectorIndexKind::TurboQuantCosine => VectorIndexKind::TurboQuantCosine,
    }
}