zeppelin-embed 0.8.0

In-process vector, lexical, and hybrid search for macOS and Apple silicon
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! One private native-record/OUT/IN candidate from the authentic staged batch.
use super::*;
use crate::epoch::EmbeddingTower;
use crate::property_graph::storage::{
    memory::{StorageBuffer, StorageMemory, StorageReservation},
    participant::{
        BatchCatalog, DirectoryBase, NativeDirectoryCandidate, PreparationCatalog,
        prepare_directories,
    },
    records::sort_by_symbol,
    tree::{TreeKind, directory::*},
};
use crate::property_graph::{
    EntityId, GraphDeleteMode, GraphGeneration,
    staging::{BaseIdentity, StagedBatch},
    wal::{self, CommitState, WalGraphRoots},
};
mod ranges;
#[cfg(any(test, feature = "test-seams"))]
pub(crate) use ranges::QUALIFICATION_RANGES;
pub(crate) use ranges::relocate_ranges;

/// Exact metadata borrowed from the sole coordinator's retained base. The
/// catalog/root-envelope token and this WAL state must have one admission owner.
#[derive(Clone, Copy, Debug)]
pub struct NativeGraphBase<'a> {
    /// Coherent native roots and complete logical base identity used by staging.
    pub directories: DirectoryBase,
    /// Real committed sequence and exact required root descriptors from that view.
    pub committed: CommitState<'a>,
}
/// Successful whole native participant. All record and directional changes are
/// private until this one result exists; it has no publication/sync authority.
pub struct NativeGraphCandidate<'a> {
    directories: NativeDirectoryCandidate<'a>,
    roots: GraphRoots,
    expected_sequence: u64,
    expected_roots: WalGraphRoots,
    expected_catalog: wal::RequiredRef,
    expected_vector: Option<wal::RequiredRef>,
    expected_text: Option<wal::RequiredRef>,
    expected_reclaim: Option<wal::RequiredRef>,
    expected_high_waters: wal::HighWaters,
    expected_prepared_inventories: StorageBuffer<'a, wal::RequiredRef>,
    target_generation: GraphGeneration,
    sequence: u64,
    _charge: StorageReservation<'a>,
}
impl NativeGraphCandidate<'_> {
    /// Complete staging token retained for the coordinator's publication recheck.
    pub const fn expected_base(&self) -> BaseIdentity {
        self.directories.expected_base()
    }
    /// Independent committed WAL cutoff the coordinator must still observe.
    pub const fn expected_sequence(&self) -> u64 {
        self.expected_sequence
    }
    /// Every optional required root, including complete object metadata.
    pub const fn expected_roots(&self) -> WalGraphRoots {
        self.expected_roots
    }
    /// Exact admitted catalog descriptor required at handoff.
    pub const fn expected_catalog(&self) -> wal::RequiredRef {
        self.expected_catalog
    }
    /// Exact optional admitted vector descriptor required at handoff.
    pub const fn expected_vector(&self) -> Option<wal::RequiredRef> {
        self.expected_vector
    }
    /// Exact optional admitted text descriptor required at handoff.
    pub const fn expected_text(&self) -> Option<wal::RequiredRef> {
        self.expected_text
    }
    /// Exact optional admitted reclaim descriptor required at handoff.
    pub const fn expected_reclaim(&self) -> Option<wal::RequiredRef> {
        self.expected_reclaim
    }
    /// Complete admitted allocation high-water identities required at handoff.
    pub const fn expected_high_waters(&self) -> wal::HighWaters {
        self.expected_high_waters
    }
    /// Exact admitted prepared inventory descriptors required at handoff.
    pub fn expected_prepared_inventories(&self) -> &[wal::RequiredRef] {
        self.expected_prepared_inventories.as_slice()
    }
    /// Generation assigned to every newly prepared artifact.
    pub const fn target_generation(&self) -> GraphGeneration {
        self.target_generation
    }
    /// All eight proposed native roots, including both adjacency directions.
    pub const fn roots(&self) -> GraphRoots {
        self.roots
    }
    /// One target sequence, or the original cutoff for an empty/no-op batch.
    pub const fn sequence(&self) -> u64 {
        self.sequence
    }
}

/// Prepare one complete native participant. Any failure yields no candidate;
/// the caller retains every private pack and its abort inventory for writes.
pub fn prepare_native_graph<'a, S: BlockSink>(
    sink: &mut S,
    batch: &StagedBatch<'_>,
    base: NativeGraphBase<'_>,
    catalog: &impl PreparationCatalog<S>,
    document: Option<&EmbeddingTower>,
    memory: &'a StorageMemory<'a>,
    r: &mut TreeResources<'_>,
) -> Result<NativeGraphCandidate<'a>, TreeError> {
    r.require_preparation(memory)?;
    memory.require_batch(batch)?;
    bind_base(sink, base, r)?;
    let charge = memory.reserve(std::mem::size_of::<NativeGraphCandidate<'_>>())?;
    let inventory_count = base
        .committed
        .prepared_inventories
        .len()
        .map_err(TreeError::WalMetadata)?;
    let mut expected_prepared_inventories = StorageBuffer::new(memory, inventory_count)?;
    let mut no_cancel = || false;
    let mut wal_resources =
        wal::WalResources::new(u64::MAX, wal::STACK_RESERVATION_BYTES, &mut no_cancel)
            .map_err(TreeError::WalMetadata)?;
    for index in 0..inventory_count {
        expected_prepared_inventories.push(
            base.committed
                .prepared_inventories
                .get(index, &mut wal_resources)
                .map_err(TreeError::WalMetadata)?,
        )?;
    }
    let unchanged = batch.disposition() != crate::property_graph::BatchDisposition::Changed;
    let context = if unchanged {
        None
    } else {
        Some(RangeEditContext::at_generation(
            base.directories.identity.generation,
            base.committed.sequence,
            batch.target_generation(),
        )?)
    };
    // This uses the admitted pre-batch roots even when both nodes are pending
    // deletion. DETACH never calls the incident reader or enumerates its degree.
    check_restrict(sink, batch, base, catalog, document, memory, r)?;
    let directories =
        prepare_directories(sink, batch, base.directories, catalog, document, memory, r)?;
    let mut roots = directories.roots();
    if let Some(context) = context {
        let merged = BatchCatalog {
            base: catalog,
            additions: batch.symbols(),
        };
        let mut changes = collect_changes(
            sink,
            batch,
            base.directories.roots,
            roots,
            base.committed.sequence,
            context.target_sequence(),
            &merged,
            document,
            memory,
            r,
        )?;
        sort_by_symbol(changes.as_mut_slice(), Change::sort_key, r)?;
        for pair in changes.as_slice().windows(2) {
            r.step(1)?;
            if pair.first().map(Change::sort_key) == pair.last().map(Change::sort_key) {
                return Err(invalid("duplicate directed normalized change"));
            }
        }
        if !changes.as_slice().is_empty() {
            ranges::apply(
                sink,
                &mut roots,
                changes.as_slice(),
                base.committed.sequence,
                context,
                memory,
                r,
            )?;
        }
    }
    r.step(0)?;
    Ok(NativeGraphCandidate {
        directories,
        roots,
        expected_sequence: base.committed.sequence,
        expected_roots: base.committed.graph,
        expected_catalog: base.committed.catalog,
        expected_vector: base.committed.vector,
        expected_text: base.committed.text,
        expected_reclaim: base.committed.reclaim,
        expected_high_waters: base.committed.high_waters,
        expected_prepared_inventories,
        target_generation: roots.generation(),
        sequence: context.map_or(base.committed.sequence, RangeEditContext::target_sequence),
        _charge: charge,
    })
}

fn bind_base(
    source: &impl BlockSource,
    base: NativeGraphBase<'_>,
    r: &mut TreeResources<'_>,
) -> Result<(), TreeError> {
    r.step(1)?;
    let identity = base.directories.identity;
    let state = base.committed;
    if state.store != identity.store
        || state.generation != identity.generation
        || base.directories.roots.store() != identity.store
        || base.directories.roots.generation() != identity.generation
    {
        return Err(invalid("native WAL/base identity mismatch"));
    }
    for (reference, required) in base
        .directories
        .roots
        .references()
        .into_iter()
        .zip(state.graph.slots)
    {
        r.step(std::mem::size_of::<Option<wal::RequiredRef>>() as u64)?;
        if reference != required.map(|required| required.block) {
            return Err(invalid("native WAL root position mismatch"));
        }
        if let Some(required) = required {
            wal::validate_graph_root_reference(state, required).map_err(TreeError::WalMetadata)?;
            let block = source.resolve(required.block, r)?;
            let object = required.object;
            let identity = block.identity();
            if block.reference() != required.block
                || identity.store != object.store
                || identity.artifact != object.artifact
                || identity.generation != object.generation
                || identity.creation_serial != object.serial
                || block.file_length() != object.bytes as usize
                || block.file_checksum() != object.checksum
            {
                return Err(invalid("native WAL root source identity mismatch"));
            }
        }
    }
    r.step(0)
}

#[derive(Clone, Copy)]
struct Change {
    node: NodeId,
    relationship_type: RelTypeId,
    direction: Direction,
    delta: DeltaEntry,
}
impl Change {
    fn sort_key(&self) -> (u8, u128, u64, u128) {
        (
            self.direction as u8,
            self.node.get(),
            self.relationship_type.get(),
            self.delta.edge.rel.get(),
        )
    }
    fn group(&self) -> (u8, u128, u64) {
        (
            self.direction as u8,
            self.node.get(),
            self.relationship_type.get(),
        )
    }
}

#[allow(clippy::too_many_arguments)]
fn collect_changes<'a, S: BlockSource>(
    source: &S,
    batch: &StagedBatch<'_>,
    old: GraphRoots,
    next: GraphRoots,
    base_sequence: u64,
    sequence: u64,
    catalog: &impl PreparationCatalog<S>,
    document: Option<&EmbeddingTower>,
    memory: &'a StorageMemory<'a>,
    r: &mut TreeResources<'_>,
) -> Result<StorageBuffer<'a, Change>, TreeError> {
    let mut count = 0usize;
    for delta in batch.deltas() {
        r.step(1)?;
        if matches!(
            delta.provenance().fields().incarnation,
            EntityId::Relationship(_)
        ) {
            count = count.checked_add(2).ok_or(TreeError::Memory)?;
        }
    }
    let mut changes = StorageBuffer::new(memory, count)?;
    let previous = NativeGraphReader::new(source, old, base_sequence, catalog, document);
    let current = NativeGraphReader::new(source, next, sequence, catalog, document);
    for delta in batch.deltas() {
        r.step(1)?;
        let EntityId::Relationship(id) = delta.provenance().fields().incarnation else {
            continue;
        };
        let old_roots = old;
        let old = previous.raw_relationship(id, r)?;
        let new = current.raw_relationship(id, r)?;
        let (row, action) = match (old, new) {
            (Some(old), Some(new)) if old == new => continue,
            (None, Some(new)) if current.visible(new, r)? => (new, Action::Insert),
            (Some(old), None) => (old, Action::Delete),
            (None, None) if delta.canonical().is_none() => {
                super::super::records::swept_delete(
                    source,
                    old_roots,
                    delta.provenance().fields(),
                    catalog.namespace_id(
                        delta
                            .provenance()
                            .fields()
                            .key
                            .ok_or(invalid("swept Delete key absent"))?
                            .namespace(),
                        r,
                    )?,
                    catalog,
                    document,
                    r,
                )?;
                continue;
            }
            _ => return Err(invalid("normalized relationship topology/endpoints")),
        };
        for (direction, node, neighbor) in [
            (Direction::Out, row.source, row.target),
            (Direction::In, row.target, row.source),
        ] {
            r.step(std::mem::size_of::<Change>() as u64)?;
            changes.push(Change {
                node,
                relationship_type: row.relationship_type,
                direction,
                delta: DeltaEntry {
                    edge: Edge {
                        rel: row.rel,
                        neighbor,
                    },
                    action,
                },
            })?;
        }
    }
    r.step(0)?;
    Ok(changes)
}

fn check_restrict<S: BlockSource>(
    source: &S,
    batch: &StagedBatch<'_>,
    base: NativeGraphBase<'_>,
    catalog: &impl PreparationCatalog<S>,
    document: Option<&EmbeddingTower>,
    memory: &StorageMemory<'_>,
    r: &mut TreeResources<'_>,
) -> Result<(), TreeError> {
    let mut restrict = false;
    let mut removed_count = 0usize;
    for delta in batch.deltas() {
        r.step(1)?;
        let fields = delta.provenance().fields();
        if delta.canonical().is_none() {
            match fields.incarnation {
                EntityId::Node(_) if fields.delete_mode == Some(GraphDeleteMode::Restrict) => {
                    restrict = true
                }
                EntityId::Relationship(_) => {
                    removed_count = removed_count.checked_add(1).ok_or(TreeError::Memory)?
                }
                _ => {}
            }
        }
    }
    if !restrict {
        return r.step(0);
    }
    let mut removed = StorageBuffer::new(memory, removed_count)?;
    for delta in batch.deltas() {
        r.step(1)?;
        if delta.canonical().is_none()
            && let EntityId::Relationship(id) = delta.provenance().fields().incarnation
        {
            removed.push(id)?;
        }
    }
    sort_by_symbol(removed.as_mut_slice(), |id| *id, r)?;
    let mut scratch = RangeScratch::for_prepare(memory, r)?;
    let reader = NativeGraphReader::new(
        source,
        base.directories.roots,
        base.committed.sequence,
        catalog,
        document,
    );
    for delta in batch.deltas() {
        r.step(1)?;
        let fields = delta.provenance().fields();
        if delta.canonical().is_none()
            && fields.delete_mode == Some(GraphDeleteMode::Restrict)
            && let EntityId::Node(id) = fields.incarnation
        {
            for direction in [Direction::Out, Direction::In] {
                reader.visit_adjacency(
                    AdjacencyQuery {
                        node: id,
                        direction,
                        relationship_type: None,
                        relationships: RelationshipRange {
                            lower: RelId::new(1).map_err(|_| invalid("minimum relationship ID"))?,
                            upper: UpperBound::Infinity,
                        },
                    },
                    &mut scratch,
                    r,
                    &mut |row, r| {
                        if removed.as_slice().binary_search(&row.edge.rel).is_ok() {
                            return Ok(true);
                        }
                        if catalog
                            .relationship_on_delete(row.relationship_type, r)?
                            .is_some()
                        {
                            if direction == Direction::Out {
                                return Ok(true);
                            }
                            for change in batch.deltas() {
                                r.step(1)?;
                                if change.canonical().is_none()
                                    && change.provenance().fields().incarnation
                                        == EntityId::Node(row.edge.neighbor)
                                {
                                    return Ok(true);
                                }
                            }
                        }
                        Err(invalid("plain node deletion retains a live incident"))
                    },
                )?;
            }
        }
    }
    r.step(0)
}
fn invalid(message: &'static str) -> TreeError {
    TreeError::Invalid(message)
}