genegraph-storage 0.72.0

vector database: Lance and Zarr storage backends with a shared metadata catalog
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
//! Arrow-IPC interop path (#142): `.arrow` file format, `.arrows` streaming
//! format, generation scoping, and byte-layout conformance. Runs only with
//! the `arrow-ipc` Cargo feature (off by default).

use std::sync::Arc;

use arrow::array::{FixedSizeListArray, Float64Array};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::ipc::reader::FileReader;
use arrow::record_batch::RecordBatch;
use rand::rngs::StdRng;
use rand::{RngExt, SeedableRng};
use smartcore::linalg::basic::arrays::Array;
use smartcore::linalg::basic::matrix::DenseMatrix;

use crate::graph::{GraphEdge, StoredGraph};
use crate::lance_storage_graph::LanceStorageGraph;
use crate::metadata::{CollectionKind as MetaKind, GeneMetadata};
use crate::tests::tmp_dir;
use crate::traits::backend::StorageBackend;
use crate::traits::metadata::Metadata;

async fn seeded_storage(name: &str) -> (std::path::PathBuf, LanceStorageGraph) {
    let base = tmp_dir(name).await;
    let storage = LanceStorageGraph::new(base.to_string_lossy().to_string(), name.to_string())
        .expect("valid instance name");
    GeneMetadata::seed_metadata(name, 4, 4, &storage)
        .await
        .expect("seed metadata");
    (base, storage)
}

fn sample_matrix(rows: usize, cols: usize) -> DenseMatrix<f64> {
    let mut rng = StdRng::seed_from_u64(3407);
    let data: Vec<f64> = (0..rows * cols)
        .map(|_| rng.random::<f64>() - 0.5)
        .collect();
    DenseMatrix::new(rows, cols, data, true).unwrap()
}

fn assert_matrix_exact(a: &DenseMatrix<f64>, b: &DenseMatrix<f64>) {
    assert_eq!(a.shape(), b.shape(), "shape mismatch");
    let (rows, cols) = a.shape();
    for r in 0..rows {
        for c in 0..cols {
            assert_eq!(
                *a.get((r, c)),
                *b.get((r, c)),
                "value mismatch at ({r},{c}): IPC must round-trip f64 exactly"
            );
        }
    }
}

fn vector_batch(dim: usize, values: Vec<f64>) -> RecordBatch {
    let child = Arc::new(Field::new("item", DataType::Float64, false));
    let list = FixedSizeListArray::new(
        child.clone(),
        dim as i32,
        Arc::new(Float64Array::from(values)),
        None,
    );
    let schema = Schema::new(vec![Field::new(
        "vector",
        DataType::FixedSizeList(child, dim as i32),
        false,
    )]);
    RecordBatch::try_new(Arc::new(schema), vec![Arc::new(list)]).unwrap()
}

/// The vector-layout batch for a matrix, hand-built exactly as the crate's
/// dense conversion builds it (schema name included).
fn dense_batch(data: &DenseMatrix<f64>) -> RecordBatch {
    let (rows, cols) = data.shape();
    let values: Vec<f64> = (0..rows)
        .flat_map(|r| (0..cols).map(move |c| *data.get((r, c))))
        .collect();
    vector_batch(cols, values)
}

/// `save_dense_to_ipc` / `load_dense_from_ipc`: exact `DenseMatrix<f64>`
/// round-trip through the Arrow IPC file format, registered in the
/// metadata registry as an `arrow-ipc` artifact.
#[tokio::test(flavor = "multi_thread")]
async fn save_load_dense_ipc_roundtrip() {
    let (_base, storage) = seeded_storage("ipc_dense").await;
    let md_path = storage.metadata_path();
    let data = sample_matrix(5, 7);

    let path = storage
        .save_dense_to_ipc("dense_ipc", &data, &md_path)
        .await
        .expect("save_dense_to_ipc");

    assert!(
        path.to_string_lossy()
            .ends_with("ipc_dense_dense_ipc.arrow"),
        "IPC file artifacts live at {{instance}}_{{key}}.arrow: {path:?}"
    );
    assert!(path.is_file(), "IPC artifact must exist after save");

    let loaded = storage
        .load_dense_from_ipc("dense_ipc")
        .await
        .expect("load_dense_from_ipc");
    assert_matrix_exact(&data, &loaded);

    // registry: filetype stays semantic ("dense"), the format fact names
    // arrow-ipc, and the collection kind is the vector-space kind.
    let md = storage.load_metadata().await.unwrap();
    let info = md.files.get("dense_ipc").expect("registered dense_ipc");
    assert_eq!(info.filetype, "dense");
    assert_eq!(info.storage_format, "arrow-ipc");
    assert_eq!(info.kind, Some(MetaKind::VectorSpace));
    assert_eq!(info.filename, "ipc_dense_dense_ipc.arrow");
}

/// `open_ipc_stream_writer`: three batches flushed incrementally to a
/// `.arrows` stream file reload as one `DenseMatrix` in write order.
#[tokio::test(flavor = "multi_thread")]
async fn ipc_stream_writer_concatenates_batches_in_order() {
    let (_base, storage) = seeded_storage("ipc_stream").await;

    let dim = 4usize;
    let batch_values = |b: usize| -> Vec<f64> {
        (0..4)
            .flat_map(|r| (0..dim).map(move |c| (b * 4 + r) as f64 + c as f64 / 10.0))
            .collect()
    };
    let schema = vector_batch(dim, batch_values(0)).schema();

    let mut writer = storage
        .open_ipc_stream_writer("stream_dense", schema)
        .await
        .expect("open_ipc_stream_writer");
    for b in 0..3 {
        writer
            .write_batch(&vector_batch(dim, batch_values(b)))
            .expect("write_batch");
    }
    let path = writer.finish().expect("finish");

    assert!(
        path.to_string_lossy()
            .ends_with("ipc_stream_stream_dense.arrows"),
        "stream artifacts live at {{instance}}_{{key}}.arrows: {path:?}"
    );
    assert!(path.is_file(), "stream artifact must exist after finish");

    let loaded = storage
        .load_dense_from_ipc("stream_dense")
        .await
        .expect("reload stream as one dense matrix");
    assert_eq!(loaded.shape(), (12, dim), "three batches concatenate");
    for b in 0..3 {
        for r in 0..4 {
            for c in 0..dim {
                assert_eq!(
                    *loaded.get((b * 4 + r, c)),
                    (b * 4 + r) as f64 + c as f64 / 10.0,
                    "batch {b} row {r} must land at global row {} in write order",
                    b * 4 + r
                );
            }
        }
    }
}

/// Graph collections round-trip through the Arrow IPC file format and
/// agree with the Lance `save_graph`/`load_graph` path, CSR included.
#[tokio::test(flavor = "multi_thread")]
async fn graph_ipc_roundtrip_matches_lance_load_graph() {
    let (_base, storage) = seeded_storage("ipc_graph").await;
    let md_path = storage.metadata_path();

    let mut rng = StdRng::seed_from_u64(3407);
    let edges: Vec<GraphEdge> = (0..14)
        .map(|_| {
            GraphEdge::weighted(
                rng.random_range(0..6),
                rng.random_range(0..6),
                rng.random::<f64>() - 0.5,
            )
        })
        .collect();

    let ipc_path = storage
        .save_graph_to_ipc("g_ipc", &edges, &md_path)
        .await
        .expect("save_graph_to_ipc");
    assert!(
        ipc_path
            .to_string_lossy()
            .ends_with("ipc_graph_g_ipc.arrow"),
        "graph IPC artifacts live at {{instance}}_{{key}}.arrow: {ipc_path:?}"
    );

    // the same edges through the canonical Lance path, for comparison
    storage
        .save_graph("g_lance", &edges, &md_path)
        .await
        .expect("save_graph");

    let ipc_graph: StoredGraph = storage
        .load_graph_from_ipc("g_ipc")
        .await
        .expect("load_graph_from_ipc");
    let lance_graph = storage.load_graph("g_lance").await.expect("load_graph");

    assert_eq!(ipc_graph, lance_graph, "IPC and Lance graph paths agree");
    let ipc_csr = ipc_graph.to_csr().expect("CSR from IPC graph");
    let lance_csr = lance_graph.to_csr().expect("CSR from Lance graph");
    assert_eq!(ipc_csr.rows(), lance_csr.rows());
    assert_eq!(ipc_csr.cols(), lance_csr.cols());
    assert_eq!(ipc_csr.nnz(), lance_csr.nnz());
    for (v, (r, c)) in ipc_csr.iter() {
        assert_eq!(
            lance_csr.get(r, c),
            Some(v),
            "CSR value mismatch at ({r},{c})"
        );
    }

    let md = storage.load_metadata().await.unwrap();
    let info = md.files.get("g_ipc").expect("registered g_ipc");
    assert_eq!(info.filetype, "graph");
    assert_eq!(info.storage_format, "arrow-ipc");
    assert_eq!(info.kind, Some(MetaKind::Graph));
}

/// Generations stay independent on the IPC path: generation 0 and
/// generation 1 artifacts resolve separately, generation-scoped.
#[tokio::test(flavor = "multi_thread")]
async fn ipc_generation_scoping_resolves_independently() {
    let base = tmp_dir("ipc_generations").await;
    let logical = LanceStorageGraph::new(base.to_string_lossy().to_string(), "ds".to_string())
        .expect("valid instance name");

    let g0 = logical.scoped_generation(0);
    let g1 = logical.scoped_generation(1);
    GeneMetadata::seed_metadata("ds", 4, 4, &g0)
        .await
        .expect("seed generation 0");
    GeneMetadata::seed_metadata("ds", 4, 4, &g1)
        .await
        .expect("seed generation 1");

    let a = sample_matrix(3, 2);
    let b = sample_matrix(4, 3);
    let p0 = g0
        .save_dense_to_ipc("dense", &a, &g0.metadata_path())
        .await
        .expect("save generation 0");
    let p1 = g1
        .save_dense_to_ipc("dense", &b, &g1.metadata_path())
        .await
        .expect("save generation 1");

    assert!(
        p0.ends_with("ds__g0_dense.arrow"),
        "generation 0 artifact name carries the generation stamp: {p0:?}"
    );
    assert!(
        p1.ends_with("ds__g1_dense.arrow"),
        "generation 1 artifact name carries the generation stamp: {p1:?}"
    );

    assert_matrix_exact(
        &a,
        &g0.load_dense_from_ipc("dense").await.expect("load gen 0"),
    );
    assert_matrix_exact(
        &b,
        &g1.load_dense_from_ipc("dense").await.expect("load gen 1"),
    );
}

/// Golden-fixture conformance: the writer emits the Arrow IPC *file*
/// byte layout — `ARROW1\0\0` magic up front and the spec trailer
/// (footer flatbuffer, i32 footer-length prefix, `ARROW1` tail). The
/// standard arrow-crate reader opens the result, and a file written by
/// the standard crate loads through the backend reader.
#[tokio::test(flavor = "multi_thread")]
async fn ipc_file_byte_layout_conformance() {
    let (_base, storage) = seeded_storage("ipc_bytes").await;
    let md_path = storage.metadata_path();
    let data = sample_matrix(3, 5);

    let path = storage
        .save_dense_to_ipc("layout", &data, &md_path)
        .await
        .expect("save_dense_to_ipc");

    // byte-layout invariants of the Arrow IPC *file* format
    let bytes = std::fs::read(&path).expect("read IPC file");
    assert!(
        bytes.len() > 18,
        "IPC file must carry header, payload and trailer"
    );
    assert_eq!(
        &bytes[0..8],
        b"ARROW1\0\0",
        "file must start with the Arrow IPC file magic"
    );
    assert_eq!(
        &bytes[bytes.len() - 6..],
        b"ARROW1",
        "file must end with the ARROW1 trailer magic"
    );
    let footer_len = i32::from_le_bytes(
        bytes[bytes.len() - 10..bytes.len() - 6]
            .try_into()
            .expect("footer length prefix"),
    );
    assert!(
        footer_len > 0 && (footer_len as usize) + 10 <= bytes.len(),
        "footer length prefix {footer_len} must describe the footer before the trailer"
    );

    // standard-reader direction: the arrow crate opens our artifact
    let file = std::fs::File::open(&path).unwrap();
    let mut reader =
        FileReader::try_new(file, None).expect("standard FileReader opens the artifact");
    assert_eq!(reader.num_batches(), 1);
    let batch = reader.next().unwrap().expect("batch read");
    assert_eq!(batch.num_rows(), 3, "standard reader sees the payload");

    // interop direction: a file written by the plain arrow crate (no
    // genegraph writer involved) loads through the same IPC reader.
    let foreign = path.with_file_name("ipc_bytes_foreign.arrow");
    let file = std::fs::File::create(&foreign).unwrap();
    let mut writer =
        arrow::ipc::writer::FileWriter::try_new(file, dense_batch(&data).schema().as_ref())
            .expect("foreign FileWriter");
    writer.write(&dense_batch(&data)).expect("foreign write");
    writer.finish().expect("foreign finish");

    let loaded = storage
        .load_dense_from_ipc("foreign")
        .await
        .expect("foreign-written file loads through the IPC reader");
    assert_matrix_exact(&data, &loaded);
}

/// `load_dense_from_ipc` on a missing key surfaces a typed error naming
/// both candidate artifacts instead of a bare IO error.
#[tokio::test(flavor = "multi_thread")]
async fn ipc_missing_artifact_is_a_typed_error() {
    let (_base, storage) = seeded_storage("ipc_missing").await;

    let err = storage
        .load_dense_from_ipc("absent")
        .await
        .expect_err("missing artifact must error");
    assert!(
        matches!(err, crate::StorageError::Invalid(_)),
        "expected Invalid for a missing IPC artifact, got {err:?}"
    );
    let msg = err.to_string();
    assert!(msg.contains(".arrow"), "error names the file format: {msg}");
    assert!(
        msg.contains(".arrows"),
        "error names the stream format: {msg}"
    );
}
/// One rule, every format (#142 + the layer rules): the Zarr backend gets
/// the dense and stream IPC paths (dense fits Zarr roots), and keeps its
/// typed rejection for graph collections.
#[tokio::test(flavor = "multi_thread")]
async fn zarr_ipc_dense_roundtrip_graph_rejected_and_stream_writes() {
    let base = tmp_dir("ipc_zarr").await;
    let storage = {
        use crate::zarr_storage::ZarrStorage;
        let s = ZarrStorage::new(&base, "zarr_ipc").expect("valid label");
        GeneMetadata::seed_metadata("zarr_ipc", 4, 4, &s)
            .await
            .expect("seed registry");
        s
    };
    let md_path = storage.metadata_path();

    // dense round trip through the file format, registered as arrow-ipc
    let data = sample_matrix(4, 6);
    let path = storage
        .save_dense_to_ipc("dense_ipc", &data, &md_path)
        .await
        .expect("zarr save_dense_to_ipc");
    assert!(
        path.to_string_lossy().ends_with("dense_ipc.arrow"),
        "Zarr IPC artifacts live at {{root}}/{{key}}.arrow: {path:?}"
    );
    assert_matrix_exact(
        &data,
        &storage
            .load_dense_from_ipc("dense_ipc")
            .await
            .expect("zarr reload"),
    );

    let md = storage.load_metadata().await.unwrap();
    let info = md.files.get("dense_ipc.arrow").expect("registered rel key");
    assert_eq!(info.filetype, "dense");
    assert_eq!(info.storage_format, "arrow-ipc");
    drop(md);

    // stream: three flushes reload concatenated in order
    let dim = 3usize;
    let batch_values = |b: usize| -> Vec<f64> {
        (0..2)
            .flat_map(|r| (0..dim).map(move |c| (b * 2 + r) as f64 + c as f64 / 10.0))
            .collect()
    };
    let schema = vector_batch(dim, batch_values(0)).schema();
    let mut writer = storage
        .open_ipc_stream_writer("stream_dense", schema)
        .await
        .expect("zarr open_ipc_stream_writer");
    for b in 0..3 {
        writer
            .write_batch(&vector_batch(dim, batch_values(b)))
            .expect("write_batch");
    }
    writer.finish().expect("finish");
    let loaded = storage
        .load_dense_from_ipc("stream_dense")
        .await
        .expect("zarr stream reload");
    assert_eq!(loaded.shape(), (6, dim));
    for b in 0..3 {
        for r in 0..2 {
            for c in 0..dim {
                assert_eq!(
                    *loaded.get((b * 2 + r, c)),
                    (b * 2 + r) as f64 + c as f64 / 10.0
                );
            }
        }
    }

    // graph collections keep their typed rejection on Zarr
    let edges = vec![GraphEdge::weighted(0, 1, 0.5)];
    let err = storage
        .save_graph_to_ipc("g", &edges, &md_path)
        .await
        .expect_err("graph IPC stays rejected on Zarr");
    assert!(
        matches!(err, crate::StorageError::UnsupportedFiletype(_)),
        "expected UnsupportedFiletype, got {err:?}"
    );
    let err = storage
        .load_graph_from_ipc("g")
        .await
        .expect_err("graph IPC stays rejected on Zarr");
    assert!(matches!(err, crate::StorageError::UnsupportedFiletype(_)));
}