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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! ZE-66 S3: typed `Store::get_nodes`/`get_relationships`.
//!
//! Both calls admit exactly one native read lease (`Store::with_native_read`)
//! for the whole batch of requested identities, so every returned entity was
//! read against the same generation: this is the "one admitted generation for
//! the whole call" contract, not N independent point reads. The copy shape
//! mirrors ZE-53's `query::completed::{Node, Relationship, Property, Value}`
//! pooled representation (label/type names, key, properties, and for nodes,
//! explicitly selected text/vector) rather than inventing a new one; unlike a
//! query row this is a flat `Option<_>` per requested id, so there is no
//! column/cell/search apparatus to copy.
//!
//! Labels and properties are always copied; a node's stored text and vector
//! are opt-in through [`GraphGetOptions`] because reading them copies payload
//! bytes callers may not want. Copying preserves the exact absent-vs-empty
//! text distinction and the typed-empty-list-vs-`EmptyList`-vs-absent-property
//! distinction the stored canonical encoding already carries.

use super::GraphStoreError;
use crate::lifecycle::QueryControl;
use crate::lifecycle::Store;
use crate::lifecycle::native_graph::NativeReadConsumer;
use crate::property_graph::catalog::Symbol;
use crate::property_graph::query::completed::{
    GraphQueryOptions, Key, ListKind, Node, Pools, Property, Relationship, Span, Value, ValueIndex,
};
use crate::property_graph::query::runtime::RuntimeContext;
use crate::property_graph::storage::GraphReadView;
use crate::property_graph::storage::records::{RecordShape, RecordView, StoredKey, StoredVector};
use crate::property_graph::storage::stream::{PayloadCursor, PayloadSlice};
use crate::property_graph::storage::tree::directory::{BlockSource, TreeError, TreeResources};
use crate::property_graph::{GraphGeneration, MAX_GRAPH_CHANGES, NodeId, RelId};

/// Which optional node fields [`Store::get_nodes`] copies. Labels, the
/// application key and properties are always copied; text and vector are
/// opt-in because reading them copies payload bytes, not just index rows.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct GraphGetOptions {
    /// Copy stored text, preserving absent versus present-empty.
    pub text: bool,
    /// Copy the stored vector's original finite f32 bits, when present.
    pub vector: bool,
}

/// Growable owned backing pools shared by one `get_nodes`/`get_relationships`
/// call. Unlike the internal query engine's `QueryArena`, these are ordinary
/// heap `Vec`s: a typed get is a bounded caller-sized batch, not an operator
/// in the admitted query budget, and the same trade-off already applies to
/// `GraphWriteResult`'s receipts (ZE-66 S1).
#[derive(Default)]
struct EntityPool {
    bytes: Vec<u8>,
    names: Vec<Span>,
    properties: Vec<Property>,
    values: Vec<Value>,
    children: Vec<ValueIndex>,
    vectors: Vec<u32>,
}

impl EntityPool {
    fn pools(&self) -> Pools<'_> {
        Pools {
            bytes: &self.bytes,
            names: &self.names,
            properties: &self.properties,
            values: &self.values,
            children: &self.children,
            vectors: &self.vectors,
            ..Pools::default()
        }
    }
}

fn range(span: Span) -> std::ops::Range<usize> {
    let start = span.start as usize;
    start..start.saturating_add(span.len as usize)
}

fn span_slice<T>(pool: &[T], span: Span) -> &[T] {
    pool.get(range(span)).unwrap_or(&[])
}

/// Owned result of [`Store::get_nodes`]. Nothing here borrows the store,
/// its admitted lease, or its generation; values stay valid after close.
pub struct GraphNodesResult {
    generation: GraphGeneration,
    nodes: Vec<Option<Node>>,
    pool: EntityPool,
}

impl GraphNodesResult {
    /// Bulk backing payloads with the same spans and value indices as the
    /// individual accessors. Entities remain in the request-ordered Option
    /// slice; entity and row pools in this view are empty.
    #[must_use]
    pub fn pools(&self) -> Pools<'_> {
        self.pool.pools()
    }

    /// The single generation this call's one read admission observed. Every
    /// returned node was read against this same generation.
    #[must_use]
    pub const fn generation(&self) -> GraphGeneration {
        self.generation
    }
    /// One entry per requested id, in request order. `None` is a missing or
    /// deleted node, indistinguishable at this seam.
    #[must_use]
    pub fn nodes(&self) -> &[Option<Node>] {
        &self.nodes
    }
    /// A node's label names, in ascending UTF-8 byte order.
    #[must_use]
    pub fn labels(&self, node: &Node) -> &[Span] {
        span_slice(&self.pool.names, node.labels)
    }
    /// A node's or relationship's properties, in ascending name order.
    #[must_use]
    pub fn properties(&self, span: Span) -> &[Property] {
        span_slice(&self.pool.properties, span)
    }
    /// A copied scalar value, or a list's own descriptor.
    #[must_use]
    pub fn value(&self, index: ValueIndex) -> Option<&Value> {
        self.pool.values.get(index.0 as usize)
    }
    /// A list value's ordered child value indices.
    #[must_use]
    pub fn children(&self, span: Span) -> &[ValueIndex] {
        span_slice(&self.pool.children, span)
    }
    /// Exact owned UTF-8 bytes at `span`, including embedded NUL; a present
    /// empty span returns `Some("")`.
    #[must_use]
    pub fn string(&self, span: Span) -> Option<&str> {
        std::str::from_utf8(span_slice(&self.pool.bytes, span)).ok()
    }
    /// Original finite f32 coordinate bits at `span`, one `u32` per dimension.
    #[must_use]
    pub fn vector(&self, span: Span) -> &[u32] {
        span_slice(&self.pool.vectors, span)
    }
}

/// Owned result of [`Store::get_relationships`]. See
/// [`GraphNodesResult`]; relationships never carry text or a vector.
pub struct GraphRelationshipsResult {
    generation: GraphGeneration,
    relationships: Vec<Option<Relationship>>,
    pool: EntityPool,
}

impl GraphRelationshipsResult {
    /// Bulk backing payloads with the same spans and value indices as the
    /// individual accessors. Entities remain in the request-ordered Option
    /// slice; entity and row pools in this view are empty.
    #[must_use]
    pub fn pools(&self) -> Pools<'_> {
        self.pool.pools()
    }

    /// The single generation this call's one read admission observed.
    #[must_use]
    pub const fn generation(&self) -> GraphGeneration {
        self.generation
    }
    /// One entry per requested id, in request order. `None` is a missing or
    /// deleted relationship, indistinguishable at this seam.
    #[must_use]
    pub fn relationships(&self) -> &[Option<Relationship>] {
        &self.relationships
    }
    /// A relationship's properties, in ascending name order.
    #[must_use]
    pub fn properties(&self, span: Span) -> &[Property] {
        span_slice(&self.pool.properties, span)
    }
    /// A copied scalar value, or a list's own descriptor.
    #[must_use]
    pub fn value(&self, index: ValueIndex) -> Option<&Value> {
        self.pool.values.get(index.0 as usize)
    }
    /// A list value's ordered child value indices.
    #[must_use]
    pub fn children(&self, span: Span) -> &[ValueIndex] {
        span_slice(&self.pool.children, span)
    }
    /// Exact owned UTF-8 bytes at `span`, including embedded NUL.
    #[must_use]
    pub fn string(&self, span: Span) -> Option<&str> {
        std::str::from_utf8(span_slice(&self.pool.bytes, span)).ok()
    }
}

fn append_text(text: &str, bytes: &mut Vec<u8>) -> Result<Span, TreeError> {
    let start = u32::try_from(bytes.len()).map_err(|_| TreeError::Memory)?;
    bytes.extend_from_slice(text.as_bytes());
    Ok(Span::new(
        start,
        u32::try_from(text.len()).map_err(|_| TreeError::Memory)?,
    ))
}

fn append_payload<S: BlockSource>(
    payload: PayloadSlice<'_, S>,
    bytes: &mut Vec<u8>,
    resources: &mut TreeResources<'_>,
) -> Result<Span, TreeError> {
    let start = u32::try_from(bytes.len()).map_err(|_| TreeError::Memory)?;
    let len = usize::try_from(payload.len()).map_err(|_| TreeError::Memory)?;
    let base = bytes.len();
    bytes.resize(base.checked_add(len).ok_or(TreeError::Memory)?, 0);
    let target = bytes.get_mut(base..).ok_or(TreeError::Memory)?;
    let written = payload.read_at(0, target, resources)?;
    if written != len {
        return Err(TreeError::Invalid("typed get: short payload copy"));
    }
    Ok(Span::new(
        start,
        u32::try_from(len).map_err(|_| TreeError::Memory)?,
    ))
}

fn append_vector<S: BlockSource>(
    vector: StoredVector<'_, S>,
    vectors: &mut Vec<u32>,
    resources: &mut TreeResources<'_>,
) -> Result<Span, TreeError> {
    let start = u32::try_from(vectors.len()).map_err(|_| TreeError::Memory)?;
    for index in 0..vector.dimensions() {
        vectors.push(vector.coordinate(index, resources)?.to_bits());
    }
    Ok(Span::new(start, vector.dimensions()))
}

fn copy_key<S: BlockSource>(
    key: Option<StoredKey<'_, S>>,
    bytes: &mut Vec<u8>,
    resources: &mut TreeResources<'_>,
) -> Result<Option<Key>, TreeError> {
    key.map(|key| {
        Ok(Key {
            kind: key.kind(),
            namespace: append_payload(key.namespace(), bytes, resources)?,
            value: append_payload(key.key(), bytes, resources)?,
        })
    })
    .transpose()
}

fn push_value(pool: &mut EntityPool, value: Value) -> Result<ValueIndex, TreeError> {
    let index = ValueIndex(u32::try_from(pool.values.len()).map_err(|_| TreeError::Memory)?);
    pool.values.push(value);
    Ok(index)
}

/// Decodes one ZGCIv1 stored property value: tag 1 string, 2 bool, 3 i64, 4
/// f64, 5 the untyped `EmptyList` sentinel (count must be zero), 6..9 typed
/// lists. This mirrors `query::completed::native::values::fill_stored`'s
/// decode exactly, over an owned `Vec` pool instead of a charged `QueryArena`.
fn decode_value<S: BlockSource>(
    payload: PayloadSlice<'_, S>,
    pool: &mut EntityPool,
    resources: &mut TreeResources<'_>,
) -> Result<ValueIndex, TreeError> {
    let mut cursor = PayloadCursor::new(payload);
    let tag = u8::from_le_bytes(cursor.read_array(resources)?);
    let value = match tag {
        1 => {
            let text = cursor.blob(resources)?;
            Value::String(append_payload(text, &mut pool.bytes, resources)?)
        }
        2 => {
            let raw = u8::from_le_bytes(cursor.read_array(resources)?);
            if raw > 1 {
                return Err(TreeError::Invalid("typed get: stored bool tag"));
            }
            Value::Bool(raw == 1)
        }
        3 => Value::I64(i64::from_le_bytes(cursor.read_array(resources)?)),
        4 => Value::F64(u64::from_le_bytes(cursor.read_array(resources)?)),
        5..=9 => {
            let count = usize::try_from(u64::from_le_bytes(cursor.read_array(resources)?))
                .map_err(|_| TreeError::Memory)?;
            if tag == 5 && count != 0 {
                return Err(TreeError::Invalid("typed get: nonempty untyped list"));
            }
            let start = u32::try_from(pool.children.len()).map_err(|_| TreeError::Memory)?;
            for _ in 0..count {
                let child = match tag {
                    6 => {
                        let text = cursor.blob(resources)?;
                        let span = append_payload(text, &mut pool.bytes, resources)?;
                        push_value(pool, Value::String(span))?
                    }
                    7 => {
                        let raw = u8::from_le_bytes(cursor.read_array(resources)?);
                        if raw > 1 {
                            return Err(TreeError::Invalid("typed get: stored list bool tag"));
                        }
                        push_value(pool, Value::Bool(raw == 1))?
                    }
                    8 => push_value(
                        pool,
                        Value::I64(i64::from_le_bytes(cursor.read_array(resources)?)),
                    )?,
                    9 => push_value(
                        pool,
                        Value::F64(u64::from_le_bytes(cursor.read_array(resources)?)),
                    )?,
                    _ => return Err(TreeError::Invalid("typed get: stored list tag")),
                };
                pool.children.push(child);
            }
            let element = match tag {
                5 => ListKind::Empty,
                6 => ListKind::String,
                7 => ListKind::Bool,
                8 => ListKind::I64,
                9 => ListKind::F64,
                _ => return Err(TreeError::Invalid("typed get: stored list element tag")),
            };
            Value::List {
                children: Span::new(start, u32::try_from(count).map_err(|_| TreeError::Memory)?),
                element,
            }
        }
        _ => return Err(TreeError::Invalid("typed get: stored property tag")),
    };
    cursor.finish(resources)?;
    push_value(pool, value)
}

fn copy_properties<S: BlockSource>(
    view: &GraphReadView<'_, '_, '_, '_>,
    record: &RecordView<'_, S>,
    pool: &mut EntityPool,
    resources: &mut TreeResources<'_>,
) -> Result<Span, TreeError> {
    let count =
        usize::try_from(record.canonical().property_count()).map_err(|_| TreeError::Memory)?;
    let mut items = Vec::with_capacity(count);
    for index in 0..count as u64 {
        let (key, _) = record.property_at(index, resources)?;
        let name = view
            .expression_symbol_name(Symbol::Property(key), resources)?
            .ok_or(TreeError::Invalid("typed get: property symbol is missing"))?;
        items.push((name.as_str(), index));
    }
    items.sort_by(|left, right| left.0.cmp(right.0));
    let start = u32::try_from(pool.properties.len()).map_err(|_| TreeError::Memory)?;
    for (name, index) in items {
        let (_, payload) = record.property_at(index, resources)?;
        let name_span = append_text(name, &mut pool.bytes)?;
        let value = decode_value(payload, pool, resources)?;
        pool.properties.push(Property {
            name: name_span,
            value,
        });
    }
    Ok(Span::new(
        start,
        u32::try_from(count).map_err(|_| TreeError::Memory)?,
    ))
}

fn copy_node(
    view: &GraphReadView<'_, '_, '_, '_>,
    id: NodeId,
    options: GraphGetOptions,
    pool: &mut EntityPool,
    resources: &mut TreeResources<'_>,
) -> Result<Option<Node>, TreeError> {
    let Some(node) = view.lookup_node(id, resources)? else {
        return Ok(None);
    };
    let record = node.record();
    let RecordShape::Node {
        labels: label_count,
        ..
    } = record.shape()
    else {
        return Err(TreeError::Invalid("typed get: node directory role"));
    };
    let mut labels = Vec::with_capacity(label_count as usize);
    for index in 0..label_count {
        let label = record.label(index, resources)?;
        let name = view
            .expression_symbol_name(Symbol::Label(label), resources)?
            .ok_or(TreeError::Invalid("typed get: label symbol is missing"))?;
        labels.push(name.as_str());
    }
    labels.sort_unstable();
    let label_start = u32::try_from(pool.names.len()).map_err(|_| TreeError::Memory)?;
    for name in labels {
        let span = append_text(name, &mut pool.bytes)?;
        pool.names.push(span);
    }
    let labels_span = Span::new(label_start, label_count);
    let properties_span = copy_properties(view, record, pool, resources)?;
    let key = copy_key(record.provenance().key(), &mut pool.bytes, resources)?;
    let text = if options.text {
        record
            .canonical()
            .stored_text()
            .map(|payload| append_payload(payload, &mut pool.bytes, resources))
            .transpose()?
    } else {
        None
    };
    let vector = if options.vector {
        record
            .canonical()
            .stored_vector()
            .map(|vector| append_vector(vector, &mut pool.vectors, resources))
            .transpose()?
    } else {
        None
    };
    Ok(Some(Node {
        id,
        revision: record.revision(),
        generation: record.provenance().original_generation(),
        key,
        labels: labels_span,
        properties: properties_span,
        text,
        vector,
    }))
}

fn copy_relationship(
    view: &GraphReadView<'_, '_, '_, '_>,
    id: RelId,
    pool: &mut EntityPool,
    resources: &mut TreeResources<'_>,
) -> Result<Option<Relationship>, TreeError> {
    let Some(relationship) = view.lookup_relationship(id, resources)? else {
        return Ok(None);
    };
    let record = relationship.record();
    let RecordShape::Relationship {
        source,
        target,
        relationship_type,
        ..
    } = record.shape()
    else {
        return Err(TreeError::Invalid("typed get: relationship directory role"));
    };
    let type_name = view
        .expression_symbol_name(Symbol::RelationshipType(relationship_type), resources)?
        .ok_or(TreeError::Invalid(
            "typed get: relationship type symbol is missing",
        ))?;
    let relationship_type_span = append_text(type_name.as_str(), &mut pool.bytes)?;
    let properties_span = copy_properties(view, record, pool, resources)?;
    let key = copy_key(record.provenance().key(), &mut pool.bytes, resources)?;
    Ok(Some(Relationship {
        id,
        revision: record.revision(),
        generation: record.provenance().original_generation(),
        key,
        source,
        target,
        relationship_type: relationship_type_span,
        properties: properties_span,
    }))
}

struct GetNodes<'a> {
    ids: &'a [NodeId],
    options: GraphGetOptions,
}

impl NativeReadConsumer<GraphNodesResult> for GetNodes<'_> {
    fn consume<'s, 'lease, 'm, 'g>(
        &mut self,
        view: &GraphReadView<'s, 'lease, 'm, 'g>,
        runtime: &mut RuntimeContext<'lease, 'm, 'g>,
    ) -> Result<GraphNodesResult, TreeError> {
        let generation = view.generation();
        let mut pool = EntityPool::default();
        let mut nodes = Vec::with_capacity(self.ids.len());
        let mut resources = TreeResources::for_query(runtime)?;
        for id in self.ids {
            nodes.push(copy_node(
                view,
                *id,
                self.options,
                &mut pool,
                &mut resources,
            )?);
        }
        Ok(GraphNodesResult {
            generation,
            nodes,
            pool,
        })
    }
}

struct GetRelationships<'a> {
    ids: &'a [RelId],
}

impl NativeReadConsumer<GraphRelationshipsResult> for GetRelationships<'_> {
    fn consume<'s, 'lease, 'm, 'g>(
        &mut self,
        view: &GraphReadView<'s, 'lease, 'm, 'g>,
        runtime: &mut RuntimeContext<'lease, 'm, 'g>,
    ) -> Result<GraphRelationshipsResult, TreeError> {
        let generation = view.generation();
        let mut pool = EntityPool::default();
        let mut relationships = Vec::with_capacity(self.ids.len());
        let mut resources = TreeResources::for_query(runtime)?;
        for id in self.ids {
            relationships.push(copy_relationship(view, *id, &mut pool, &mut resources)?);
        }
        Ok(GraphRelationshipsResult {
            generation,
            relationships,
            pool,
        })
    }
}

impl Store {
    /// Reads a batch of nodes by identity under one admitted read generation.
    ///
    /// Returns one `Option<Node>` per requested id, in request order: `None`
    /// for an id that is missing or deleted. Labels, the application key and
    /// properties are always copied; `options` additionally selects stored
    /// text and the stored vector, each `None` unless explicitly requested,
    /// so absent text stays distinct from present empty text, and an absent
    /// vector stays distinct from a requested-but-unset one.
    ///
    /// # Errors
    ///
    /// The classified read rejection; nothing is partially returned.
    pub fn get_nodes(
        &self,
        ids: &[NodeId],
        options: GraphGetOptions,
        control: &QueryControl,
    ) -> Result<GraphNodesResult, GraphStoreError> {
        self.get_nodes_with_limits(ids, options, control, &GraphQueryOptions::default())
    }

    /// Reads nodes with request-local memory and work tightening.
    pub fn get_nodes_with_limits(
        &self,
        ids: &[NodeId],
        options: GraphGetOptions,
        control: &QueryControl,
        limits: &GraphQueryOptions,
    ) -> Result<GraphNodesResult, GraphStoreError> {
        if ids.len() > MAX_GRAPH_CHANGES {
            return Err(GraphStoreError::limit(
                "requested id count exceeds MAX_GRAPH_CHANGES",
            ));
        }
        Ok(self.with_native_read(
            control,
            limits.runtime_limits(),
            limits.memory_limit(),
            64,
            GetNodes { ids, options },
        )?)
    }

    /// Reads a batch of relationships by identity under one admitted read
    /// generation. See [`get_nodes`](Self::get_nodes); relationships never
    /// carry text or a vector, so there is no field selection to make.
    ///
    /// # Errors
    ///
    /// The classified read rejection; nothing is partially returned.
    pub fn get_relationships(
        &self,
        ids: &[RelId],
        control: &QueryControl,
    ) -> Result<GraphRelationshipsResult, GraphStoreError> {
        self.get_relationships_with_limits(ids, control, &GraphQueryOptions::default())
    }

    /// Reads relationships with request-local memory and work tightening.
    pub fn get_relationships_with_limits(
        &self,
        ids: &[RelId],
        control: &QueryControl,
        limits: &GraphQueryOptions,
    ) -> Result<GraphRelationshipsResult, GraphStoreError> {
        if ids.len() > MAX_GRAPH_CHANGES {
            return Err(GraphStoreError::limit(
                "requested id count exceeds MAX_GRAPH_CHANGES",
            ));
        }
        Ok(self.with_native_read(
            control,
            limits.runtime_limits(),
            limits.memory_limit(),
            64,
            GetRelationships { ids },
        )?)
    }
}

#[cfg(test)]
mod tests;