nusy-arrow-git 0.14.2

Graph-native git on Arrow RecordBatches — commit, checkout, merge, diff, blame, rebase, cherry-pick, revert
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
//! # Remote — Push/Pull Graph State Over Any Transport
//!
//! ## GRAPH MINDSET: NO FILES, NO SERIALIZATION TO DISK
//!
//! Everything in this module operates on in-memory Arrow RecordBatches.
//! When we "push" graph state, we convert Arrow tables to Parquet bytes
//! **in memory** and send those bytes over the wire. When we "pull", we
//! receive bytes and load them **directly into Arrow tables**. At no point
//! do we write to the filesystem. The graph store IS the data — Parquet
//! is just the wire encoding, like protobuf but columnar.
//!
//! This works for ANY Arrow graph state: being knowledge, code graph
//! objects, kanban items, research data. The `GitObjectStore` doesn't
//! care what's in the RecordBatches — it versions them all the same way.
//!
//! ## Wire Format
//!
//! A length-prefixed JSON manifest followed by raw Parquet segments.
//! The manifest contains commit history (JSON), ref pointers (JSON),
//! and namespace segment offsets. Transport-agnostic — works over NATS,
//! HTTP, TCP, or even carrier pigeon.

use crate::commit::CommitsTable;
use crate::object_store::GitObjectStore;
use crate::refs::RefsTable;
use crate::save::{deserialize_commits, deserialize_refs, serialize_commits, serialize_refs};
use bytes::Bytes;
use nusy_arrow_core::Namespace;
use parquet::arrow::ArrowWriter;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use std::io::Cursor;

// Note: Zero file I/O in this module. All serialization is to/from in-memory
// byte buffers. The Parquet format is used for namespace data (efficient columnar
// encoding), and lightweight JSON for commits/refs metadata.

/// Errors from remote operations.
#[derive(Debug, thiserror::Error)]
pub enum RemoteError {
    #[error("Parquet error: {0}")]
    Parquet(#[from] parquet::errors::ParquetError),

    #[error("Arrow error: {0}")]
    Arrow(#[from] arrow::error::ArrowError),

    #[error("Invalid snapshot: {0}")]
    InvalidSnapshot(String),
}

/// A serialized snapshot of the full git state, ready for transport.
///
/// Contains all namespace data + commit history + refs as Parquet bytes.
/// Self-contained — can be deserialized without any other context.
pub struct Snapshot {
    /// Per-namespace Parquet bytes. Key = namespace name (e.g., "world").
    pub namespaces: Vec<(String, Vec<u8>)>,
    /// CommitsTable serialized as JSON.
    pub commits_json: String,
    /// RefsTable serialized as JSON.
    pub refs_json: String,
}

/// Serialize the current git state to a transportable Snapshot.
///
/// Purely in-memory — no file I/O. Namespaces are serialized to Parquet
/// bytes in memory. Commits and refs use lightweight JSON serialization.
pub fn snapshot_state(
    obj_store: &GitObjectStore,
    commits_table: &CommitsTable,
    refs_table: &RefsTable,
) -> Result<Snapshot, RemoteError> {
    let mut namespaces = Vec::new();

    for ns in Namespace::ALL {
        let batches = obj_store.store.get_namespace_batches(ns);
        if batches.is_empty() {
            continue;
        }

        let schema = obj_store.store.schema().clone();
        let mut buf = Vec::new();
        {
            let cursor = Cursor::new(&mut buf);
            let mut writer = ArrowWriter::try_new(cursor, schema, None)?;
            for batch in batches {
                writer.write(batch)?;
            }
            writer.close()?;
        }

        namespaces.push((ns.as_str().to_string(), buf));
    }

    // Serialize commits and refs directly to JSON strings — no file I/O
    let commits_json = serialize_commits(commits_table);
    let refs_json = serialize_refs(refs_table);

    Ok(Snapshot {
        namespaces,
        commits_json,
        refs_json,
    })
}

/// Restore git state from a Snapshot received over the wire.
///
/// Purely in-memory — no file I/O. Clears the local store and loads all
/// namespace data from in-memory Parquet bytes. Commits and refs are
/// deserialized from JSON strings.
pub fn restore_snapshot(
    obj_store: &mut GitObjectStore,
    snapshot: &Snapshot,
) -> Result<(CommitsTable, RefsTable), RemoteError> {
    obj_store.store.clear();

    // Restore each namespace from in-memory Parquet bytes
    for (ns_name, parquet_bytes) in &snapshot.namespaces {
        let ns = Namespace::from_str_loose(ns_name)
            .ok_or_else(|| RemoteError::InvalidSnapshot(format!("Unknown namespace: {ns_name}")))?;

        let bytes = Bytes::from(parquet_bytes.clone());
        let reader = ParquetRecordBatchReaderBuilder::try_new(bytes)?.build()?;

        let mut batches = Vec::new();
        for batch_result in reader {
            batches.push(batch_result?);
        }

        obj_store.store.set_namespace_batches(ns, batches);
    }

    // Deserialize commits and refs directly from JSON — no file I/O
    let commits = if snapshot.commits_json.is_empty() {
        CommitsTable::new()
    } else {
        deserialize_commits(&snapshot.commits_json)
    };

    let refs = if snapshot.refs_json.is_empty() {
        RefsTable::new()
    } else {
        deserialize_refs(&snapshot.refs_json)
    };

    Ok((commits, refs))
}

/// Serialize a Snapshot to a flat byte vector for NATS transport.
///
/// Format: JSON manifest (length-prefixed) followed by raw Parquet segments.
/// The manifest contains: commits_json, refs_json, and namespace offsets.
pub fn snapshot_to_bytes(snapshot: &Snapshot) -> Vec<u8> {
    // Build manifest
    let mut ns_entries = Vec::new();
    let mut offset = 0u64;
    for (name, data) in &snapshot.namespaces {
        ns_entries.push(format!(
            "{{\"name\":\"{}\",\"offset\":{},\"len\":{}}}",
            name,
            offset,
            data.len()
        ));
        offset += data.len() as u64;
    }

    let manifest = format!(
        "{{\"commits\":{},\"refs\":{},\"namespaces\":[{}]}}",
        &snapshot.commits_json,
        &snapshot.refs_json,
        ns_entries.join(",")
    );

    let manifest_bytes = manifest.as_bytes();
    let manifest_len = (manifest_bytes.len() as u64).to_le_bytes();

    let mut result = Vec::new();
    result.extend_from_slice(&manifest_len);
    result.extend_from_slice(manifest_bytes);
    for (_, data) in &snapshot.namespaces {
        result.extend_from_slice(data);
    }

    result
}

/// Deserialize a Snapshot from bytes received over NATS.
pub fn bytes_to_snapshot(bytes: &[u8]) -> Result<Snapshot, RemoteError> {
    if bytes.len() < 8 {
        return Err(RemoteError::InvalidSnapshot("Too short".into()));
    }

    let manifest_len = u64::from_le_bytes(bytes[..8].try_into().unwrap()) as usize;
    if bytes.len() < 8 + manifest_len {
        return Err(RemoteError::InvalidSnapshot("Manifest truncated".into()));
    }

    let manifest_str = std::str::from_utf8(&bytes[8..8 + manifest_len])
        .map_err(|e| RemoteError::InvalidSnapshot(format!("Invalid UTF-8: {e}")))?;

    // Minimal JSON parsing for the manifest
    let commits_json = extract_json_value(manifest_str, "commits").unwrap_or_default();
    let refs_json = extract_json_value(manifest_str, "refs").unwrap_or_default();

    // Parse namespace entries
    let data_start = 8 + manifest_len;
    let ns_section = extract_json_value(manifest_str, "namespaces").unwrap_or_default();

    let mut namespaces = Vec::new();
    // Parse each namespace entry: {"name":"world","offset":0,"len":1234}
    for entry in extract_json_objects(&ns_section) {
        let name = extract_json_string_field(&entry, "name").unwrap_or_default();
        let offset = extract_json_number_field(&entry, "offset").unwrap_or(0) as usize;
        let len = extract_json_number_field(&entry, "len").unwrap_or(0) as usize;

        if data_start + offset + len <= bytes.len() {
            let data = bytes[data_start + offset..data_start + offset + len].to_vec();
            namespaces.push((name, data));
        }
    }

    Ok(Snapshot {
        namespaces,
        commits_json,
        refs_json,
    })
}

// --- Minimal JSON helpers (no serde dependency) ---

fn extract_json_value(json: &str, key: &str) -> Option<String> {
    let pattern = format!("\"{}\":", key);
    let start = json.find(&pattern)? + pattern.len();
    let rest = json[start..].trim_start();

    if rest.starts_with('[') {
        // Array value — find matching ]
        let mut depth = 0;
        let mut end = 0;
        for (i, ch) in rest.char_indices() {
            match ch {
                '[' => depth += 1,
                ']' => {
                    depth -= 1;
                    if depth == 0 {
                        end = i + 1;
                        break;
                    }
                }
                _ => {}
            }
        }
        Some(rest[..end].to_string())
    } else if rest.starts_with('{') {
        // Object value — find matching }
        let mut depth = 0;
        let mut end = 0;
        for (i, ch) in rest.char_indices() {
            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        end = i + 1;
                        break;
                    }
                }
                _ => {}
            }
        }
        Some(rest[..end].to_string())
    } else {
        // Primitive — read until , or }
        let end = rest.find([',', '}']).unwrap_or(rest.len());
        Some(rest[..end].trim().to_string())
    }
}

fn extract_json_objects(json: &str) -> Vec<String> {
    let mut objects = Vec::new();
    let mut depth = 0;
    let mut start = None;
    for (i, ch) in json.char_indices() {
        match ch {
            '{' => {
                if depth == 0 {
                    start = Some(i);
                }
                depth += 1;
            }
            '}' => {
                depth -= 1;
                if depth == 0 {
                    if let Some(s) = start {
                        objects.push(json[s..=i].to_string());
                    }
                    start = None;
                }
            }
            _ => {}
        }
    }
    objects
}

fn extract_json_string_field(obj: &str, key: &str) -> Option<String> {
    let pattern = format!("\"{}\":\"", key);
    let start = obj.find(&pattern)? + pattern.len();
    let rest = &obj[start..];
    let end = rest.find('"')?;
    Some(rest[..end].to_string())
}

fn extract_json_number_field(obj: &str, key: &str) -> Option<i64> {
    let pattern = format!("\"{}\":", key);
    let start = obj.find(&pattern)? + pattern.len();
    let rest = obj[start..].trim_start();
    let end = rest
        .find(|c: char| !c.is_ascii_digit() && c != '-')
        .unwrap_or(rest.len());
    rest[..end].parse().ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CommitsTable, GitObjectStore, RefsTable, create_commit};
    use nusy_arrow_core::{Namespace, Triple, YLayer};

    fn make_triple(s: &str, p: &str, o: &str) -> Triple {
        Triple {
            subject: s.to_string(),
            predicate: p.to_string(),
            object: o.to_string(),
            graph: None,
            confidence: Some(1.0),
            source_document: None,
            source_chunk_id: None,
            extracted_by: None,
            caused_by: None,
            derived_from: None,
            consolidated_at: None,
            certifiability_class: None,
        }
    }

    #[test]
    fn test_snapshot_roundtrip() {
        let tmp = tempfile::tempdir().unwrap();
        let mut obj = GitObjectStore::with_snapshot_dir(tmp.path().join("snap"));
        let mut commits = CommitsTable::new();
        let mut refs = RefsTable::new();

        // Build state
        for i in 0..50 {
            obj.store
                .add_triple(
                    &make_triple(&format!("entity-{i}"), "rdf:type", "Thing"),
                    Namespace::World,
                    YLayer::Semantic,
                )
                .unwrap();
        }

        let c1 = create_commit(&obj, &mut commits, vec![], "init", "Mini").unwrap();
        refs.init_main(&c1.commit_id);

        assert_eq!(obj.store.len(), 50);

        // Snapshot
        let snapshot = snapshot_state(&obj, &commits, &refs).unwrap();
        assert!(!snapshot.namespaces.is_empty());
        assert!(!snapshot.commits_json.is_empty());

        // Serialize to bytes
        let bytes = snapshot_to_bytes(&snapshot);
        assert!(bytes.len() > 100);

        // Deserialize
        let restored_snapshot = bytes_to_snapshot(&bytes).unwrap();
        assert_eq!(
            restored_snapshot.namespaces.len(),
            snapshot.namespaces.len()
        );

        // Restore into fresh store
        let mut obj2 = GitObjectStore::with_snapshot_dir(tmp.path().join("snap2"));
        let (commits2, refs2) = restore_snapshot(&mut obj2, &restored_snapshot).unwrap();

        assert_eq!(obj2.store.len(), 50);
        assert_eq!(commits2.len(), 1);
        assert!(refs2.head().is_some());
    }

    #[test]
    fn test_snapshot_empty_store() {
        let tmp = tempfile::tempdir().unwrap();
        let obj = GitObjectStore::with_snapshot_dir(tmp.path());
        let commits = CommitsTable::new();
        let refs = RefsTable::new();

        let snapshot = snapshot_state(&obj, &commits, &refs).unwrap();
        assert!(snapshot.namespaces.is_empty());

        let bytes = snapshot_to_bytes(&snapshot);
        let restored = bytes_to_snapshot(&bytes).unwrap();
        assert!(restored.namespaces.is_empty());
    }

    #[test]
    fn test_snapshot_multiple_namespaces() {
        let tmp = tempfile::tempdir().unwrap();
        let mut obj = GitObjectStore::with_snapshot_dir(tmp.path().join("snap"));
        let mut commits = CommitsTable::new();
        let refs = RefsTable::new();

        obj.store
            .add_triple(
                &make_triple("w", "r", "1"),
                Namespace::World,
                YLayer::Semantic,
            )
            .unwrap();
        obj.store
            .add_triple(
                &make_triple("k", "r", "2"),
                Namespace::Work,
                YLayer::Procedural,
            )
            .unwrap();

        let _c1 = create_commit(&obj, &mut commits, vec![], "multi-ns", "test").unwrap();

        let snapshot = snapshot_state(&obj, &commits, &refs).unwrap();
        assert_eq!(snapshot.namespaces.len(), 2);

        let bytes = snapshot_to_bytes(&snapshot);
        let restored = bytes_to_snapshot(&bytes).unwrap();

        let mut obj2 = GitObjectStore::with_snapshot_dir(tmp.path().join("snap2"));
        let (_, _) = restore_snapshot(&mut obj2, &restored).unwrap();
        assert_eq!(obj2.store.len(), 2);
    }

    #[test]
    fn test_bytes_to_snapshot_invalid() {
        assert!(bytes_to_snapshot(&[]).is_err());
        assert!(bytes_to_snapshot(&[0; 4]).is_err());
    }
}