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
//! # Versioned Data Structures for Mutable Engrams
//!
//! This module implements versioned, concurrency-safe data structures that enable
//! mutable engrams with optimistic locking. The key design principle is to layer
//! versioning and concurrency control over the mathematically pure VSA operations.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ VersionedEngram (Top Level) │
//! │ - Global version tracking │
//! │ - Transaction coordination │
//! │ - CAS-based root updates │
//! └─────────────────────────────────────────────────────────────┘
//! ↓
//! ┌───────────────┴───────────────┬─────────────────┐
//! ↓ ↓ ↓
//! ┌───────────────────┐ ┌──────────────────┐ ┌──────────────────┐
//! │VersionedChunk │ │VersionedManifest │ │VersionedCorr... │
//! │ Store │ │ - File-level │ │ - Chunk-level │
//! │ - Chunk-level │ │ versioning │ │ versioning │
//! │ versioning │ │ - RwLock │ │ - RwLock │
//! │ - RwLock │ │ - Per-file ver. │ │ - Arc<Corr> │
//! │ - Arc<SparseVec> │ │ │ │ │
//! │ (NOT VSA codebook)│ │ │ │ │
//! └───────────────────┘ └──────────────────┘ └──────────────────┘
//! ```
//!
//! ## Key Concepts
//!
//! ### Optimistic Locking
//!
//! Instead of preventing concurrent access, we allow it and detect conflicts:
//!
//! 1. **Read Phase**: Reader captures data + version number
//! 2. **Computation**: Reader processes data (no locks held)
//! 3. **Write Phase**: Writer checks if version unchanged, updates if valid
//! 4. **Retry**: If version mismatch, retry from step 1
//!
//! ### VSA Codebook vs Chunk Store
//!
//! **Important distinction:**
//! - **VSA Codebook** (in embeddenator-vsa): Static base vectors used for encoding/decoding.
//! This is the "dictionary" or "basis" that the VSA uses. Typically not versioned.
//! - **Chunk Store** (this module): Maps file chunk IDs to their VSA-encoded representations.
//! This is what gets versioned for mutable engrams.
//!
//! The engram's transparent compression comes from the VSA encoding itself, not from
//! explicit compression as a separate layer. Future layers (signatures, encryption) build on top.
//!
//! ### Multi-Level Versioning
//!
//! - **Component-Level**: Chunk Store, Manifest, Corrections each have global version
//! - **Entry-Level**: Individual chunks/files have local versions
//! - **Engram-Level**: Overall engram version coordinates components
//!
//! ### Structural Sharing with Arc
//!
//! Immutable data wrapped in `Arc` allows zero-copy sharing:
//!
//! ```rust,no_run
//! # use std::sync::Arc;
//! # struct SparseVec;
//! // Old version
//! let old_chunk = Arc::new(SparseVec { /* ... */ });
//!
//! // New version - only new data is allocated
//! let new_chunk = Arc::new(SparseVec { /* ... */ });
//!
//! // Old readers still have valid Arc::clone(old_chunk)
//! // New readers get Arc::clone(new_chunk)
//! // No copying of underlying data!
//! ```
//!
//! ## Usage Example
//!
//! ```rust,ignore
//! # use embeddenator_fs::versioned::*;
//! # use embeddenator_fs::VersionedEmbrFS;
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create versioned filesystem
//! let fs = VersionedEmbrFS::new();
//!
//! // Write a file
//! let version = fs.write_file("path/to/file", b"hello world", None)?;
//!
//! // Read with snapshot isolation (non-blocking)
//! let (data, version) = fs.read_file("path/to/file")?;
//!
//! // Process data...
//! let modified_data = process(&data);
//!
//! // Write with optimistic locking
//! match fs.write_file("path/to/file", &modified_data, Some(version)) {
//! Ok(new_version) => println!("Write succeeded: v{}", new_version),
//! Err(e) => {
//! println!("Write failed: {:?}", e);
//! // Retry logic here
//! },
//! }
//! # Ok(())
//! # }
//! # fn process(data: &[u8]) -> Vec<u8> { data.to_vec() }
//! ```
pub use VersionedChunk;
pub use VersionedChunkStore;
pub use VersionedCorrectionStore;
pub use VersionedEngram;
pub use ;
pub use ;
pub use ;
pub use ;