sqlitegraph 2.0.7

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Storage module for V2 backend
//!
//! This module provides:
//! - Delta index for committed-but-not-checkpointed changes
//! - JSON input validation for memory safety and DoS prevention
//! - Free space manager for contiguous region allocation (advisory, allocation-aware optimization)
//!
//! # JSON Validation
//!
//! ```rust
//! use sqlitegraph::backend::native::v2::storage::{JsonLimits, parse_and_validate_json};
//!
//! let limits = JsonLimits::default(); // 10MB size, 128 depth
//! let input = br#"{"key": "value"}"#;
//! let json = parse_and_validate_json(input, &limits)?;
//! ```
//!
//! # Delta Index
//!
//! ```rust
//! use sqlitegraph::backend::native::v2::storage::{SharedDeltaIndex, DeltaIndex};
//! use std::sync::Arc;
//!
//! // Create a new delta index
//! let delta_index = DeltaIndex::new();
//! // For thread-safe sharing across backend
//! let shared_index: SharedDeltaIndex = Arc::new(delta_index.into());
//! // Use shared_index.apply_commit() at commit time
//! // Use shared_index.checkpoint_completed() after checkpoint
//! ```
//!
//! # Free Space Manager (Allocation-Aware Optimization)
//!
//! ```rust
//! use sqlitegraph::backend::native::v2::storage::{FreeSpaceManager, Region};
//!
//! let mut fsm = FreeSpaceManager::new(1024 * 1024); // 1MB file
//!
//! // Try to reserve contiguous space for sequential clusters
//! if let Some(region) = fsm.try_reserve_contiguous(10 * 4096, 4096) {
//!     // Use region for sequential cluster writes
//!     fsm.commit_contiguous(&region, tx_id)?;
//! } else {
//!     // Fall back to normal allocation
//! }
//! ```
//!
//! The free space manager is **advisory only** - failures gracefully fall back
//! to normal allocation without affecting correctness. It's used for allocation-aware
//! sequential cluster optimization to achieve IO-12 target (Chain(500) <= 75ms).
//! ```

pub mod adjacency_writer;
pub mod delta_index;
pub mod free_space;

pub use adjacency_writer::{AdjacencyWriter, WrittenOffset};
pub use delta_index::{DeltaIndex, DeltaRecord, SharedDeltaIndex};
pub use free_space::{
    CHAIN_THRESHOLD, ChainAllocationTrigger, ContiguousAllocation, FreeSpaceError,
    FreeSpaceManager, Region, WalRecoveryState,
};

// JSON validation exports
pub use self::json_validation::{
    JsonLimits, JsonValidationError, parse_and_validate_json, parse_and_validate_json_str,
};

mod json_validation {
    use serde_json::Value;

    /// Default maximum JSON payload size (10MB)
    const DEFAULT_MAX_JSON_SIZE: usize = 10 * 1024 * 1024;

    /// Default maximum JSON nesting depth (128 levels)
    const DEFAULT_MAX_JSON_DEPTH: usize = 128;

    /// Configurable limits for JSON input validation
    ///
    /// These limits prevent DoS attacks through:
    /// - Large payloads that exhaust memory
    /// - Deeply nested structures that cause stack overflow
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct JsonLimits {
        /// Maximum JSON payload size in bytes
        pub max_size: usize,

        /// Maximum nesting depth of JSON structures
        pub max_depth: usize,
    }

    impl Default for JsonLimits {
        fn default() -> Self {
            Self {
                max_size: DEFAULT_MAX_JSON_SIZE,
                max_depth: DEFAULT_MAX_JSON_DEPTH,
            }
        }
    }

    impl JsonLimits {
        /// Create custom JSON limits
        pub fn new(max_size: usize, max_depth: usize) -> Self {
            Self {
                max_size,
                max_depth,
            }
        }

        /// Create limits for maximum size only (use default depth)
        pub fn with_max_size(max_size: usize) -> Self {
            Self {
                max_size,
                ..Default::default()
            }
        }

        /// Create limits for maximum depth only (use default size)
        pub fn with_max_depth(max_depth: usize) -> Self {
            Self {
                max_depth,
                ..Default::default()
            }
        }

        /// Create limits for testing (small values for faster test execution)
        #[cfg(test)]
        pub fn test_limits() -> Self {
            Self {
                max_size: 1000,
                max_depth: 10,
            }
        }
    }

    /// Errors that can occur during JSON validation
    #[derive(Debug, thiserror::Error)]
    pub enum JsonValidationError {
        /// JSON payload size exceeds maximum allowed
        #[error("JSON size {actual} bytes exceeds maximum {max} bytes")]
        SizeTooLarge { actual: usize, max: usize },

        /// JSON nesting depth exceeds maximum allowed
        #[error("JSON depth {actual} exceeds maximum {max}")]
        DepthTooLarge { actual: usize, max: usize },

        /// JSON parsing error from serde_json
        #[error("JSON parsing error: {0}")]
        ParseError(#[from] serde_json::Error),
    }

    /// Validate JSON input size before parsing
    ///
    /// This check happens BEFORE serde_json processes the data,
    /// preventing memory allocation for oversized payloads.
    pub fn validate_json_size(
        input: &[u8],
        limits: &JsonLimits,
    ) -> Result<(), JsonValidationError> {
        if input.len() > limits.max_size {
            return Err(JsonValidationError::SizeTooLarge {
                actual: input.len(),
                max: limits.max_size,
            });
        }
        Ok(())
    }

    /// Calculate the maximum nesting depth of a JSON value
    fn calculate_json_depth(value: &Value, current: usize) -> usize {
        match value {
            Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => current,
            Value::Array(arr) => arr
                .iter()
                .map(|v| calculate_json_depth(v, current + 1))
                .max()
                .unwrap_or(current),
            Value::Object(obj) => obj
                .values()
                .map(|v| calculate_json_depth(v, current + 1))
                .max()
                .unwrap_or(current),
        }
    }

    /// Validate JSON depth after parsing
    pub fn validate_json_depth(
        value: &Value,
        limits: &JsonLimits,
    ) -> Result<(), JsonValidationError> {
        let depth = calculate_json_depth(value, 0);
        if depth > limits.max_depth {
            return Err(JsonValidationError::DepthTooLarge {
                actual: depth,
                max: limits.max_depth,
            });
        }
        Ok(())
    }

    /// Parse and validate JSON with enforced limits
    ///
    /// This is the preferred way to parse JSON in SQLiteGraph. It enforces
    /// size limits BEFORE parsing (preventing memory exhaustion) and
    /// depth limits AFTER parsing (preventing stack overflow).
    ///
    /// # Example
    ///
    /// ```rust
    /// use sqlitegraph::backend::native::v2::storage::{JsonLimits, parse_and_validate_json};
    ///
    /// let limits = JsonLimits::default();
    /// let input = br#"{"name": "test"}"#;
    /// let json = parse_and_validate_json(input, &limits)?;
    /// ```
    pub fn parse_and_validate_json(
        input: &[u8],
        limits: &JsonLimits,
    ) -> Result<Value, JsonValidationError> {
        // Validate size FIRST (before any parsing)
        validate_json_size(input, limits)?;

        // Parse JSON
        let value: Value = serde_json::from_slice(input)?;

        // Validate depth after parsing
        validate_json_depth(&value, limits)?;

        Ok(value)
    }

    /// Parse JSON string with enforced limits
    ///
    /// Convenience function for string input.
    pub fn parse_and_validate_json_str(
        input: &str,
        limits: &JsonLimits,
    ) -> Result<Value, JsonValidationError> {
        parse_and_validate_json(input.as_bytes(), limits)
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_json_size_within_limit() {
            let limits = JsonLimits {
                max_size: 1000,
                max_depth: 128,
            };
            let input = br#"{"test": "data"}"#;
            let result = parse_and_validate_json(input, &limits);
            assert!(result.is_ok());
            assert_eq!(result.unwrap()["test"], "data");
        }

        #[test]
        fn test_json_size_exceeds_limit() {
            let limits = JsonLimits {
                max_size: 10,
                max_depth: 128,
            };
            let input = br#"{"large": "this value exceeds limit"}"#;
            let result = parse_and_validate_json(input, &limits);
            assert!(matches!(
                result,
                Err(JsonValidationError::SizeTooLarge { .. })
            ));
        }

        #[test]
        fn test_json_depth_within_limit() {
            let limits = JsonLimits {
                max_size: 10000,
                max_depth: 64,
            };
            let mut json_str = String::from("null");
            for _ in 0..10 {
                json_str = format!("[{}]", json_str);
            }
            let result = parse_and_validate_json(json_str.as_bytes(), &limits);
            assert!(result.is_ok());
        }

        #[test]
        fn test_json_depth_exceeds_limit() {
            let limits = JsonLimits {
                max_size: 10000,
                max_depth: 10,
            };
            let mut json_str = String::from("null");
            for _ in 0..20 {
                json_str = format!("[{}]", json_str);
            }
            let result = parse_and_validate_json(json_str.as_bytes(), &limits);
            assert!(matches!(
                result,
                Err(JsonValidationError::DepthTooLarge { .. })
            ));
        }

        #[test]
        fn test_json_depth_exactly_at_limit() {
            let limits = JsonLimits {
                max_size: 10000,
                max_depth: 10,
            };
            // Create JSON with exactly 10 levels of nesting
            let mut json_str = String::from("null");
            for _ in 0..10 {
                json_str = format!("[{}]", json_str);
            }
            let result = parse_and_validate_json(json_str.as_bytes(), &limits);
            // depth=10 should pass (max_depth=10, depth is 0-indexed from root)
            assert!(result.is_ok());
        }

        #[test]
        fn test_empty_json() {
            let limits = JsonLimits::default();
            let result = parse_and_validate_json(b"{}", &limits);
            assert!(result.is_ok());
        }

        #[test]
        fn test_empty_array() {
            let limits = JsonLimits::default();
            let result = parse_and_validate_json(b"[]", &limits);
            assert!(result.is_ok());
        }

        #[test]
        fn test_invalid_json_syntax() {
            let limits = JsonLimits::default();
            let result = parse_and_validate_json(b"{invalid json}", &limits);
            assert!(matches!(result, Err(JsonValidationError::ParseError(_))));
        }

        #[test]
        fn test_nested_object_depth() {
            let limits = JsonLimits {
                max_size: 10000,
                max_depth: 5,
            };
            // Create nested objects: {"a": {"a": {"a": {"a": {"a": null}}}}}
            let json_str = r#"{"a":{"a":{"a":{"a":{"a":null}}}}}"#;
            let result = parse_and_validate_json(json_str.as_bytes(), &limits);
            // 5 levels of nesting should pass
            assert!(result.is_ok());
        }

        #[test]
        fn test_mixed_nesting_depth() {
            let limits = JsonLimits {
                max_size: 10000,
                max_depth: 4,
            };
            // Create mixed nesting: [{"a": [null]}]
            // Root array -> object -> array -> null = 4 levels
            let json_str = r#"[{"a": [null]}]"#;
            let result = parse_and_validate_json(json_str.as_bytes(), &limits);
            assert!(result.is_ok());
        }

        #[test]
        fn test_default_limits() {
            let limits = JsonLimits::default();
            assert_eq!(limits.max_size, 10 * 1024 * 1024);
            assert_eq!(limits.max_depth, 128);
        }

        #[test]
        fn test_custom_limits() {
            let limits = JsonLimits::new(100, 10);
            assert_eq!(limits.max_size, 100);
            assert_eq!(limits.max_depth, 10);
        }

        #[test]
        fn test_with_max_size() {
            let limits = JsonLimits::with_max_size(500);
            assert_eq!(limits.max_size, 500);
            assert_eq!(limits.max_depth, 128); // default depth
        }

        #[test]
        fn test_with_max_depth() {
            let limits = JsonLimits::with_max_depth(64);
            assert_eq!(limits.max_size, 10 * 1024 * 1024); // default size
            assert_eq!(limits.max_depth, 64);
        }

        #[test]
        fn test_string_input() {
            let limits = JsonLimits::default();
            let result = parse_and_validate_json_str(r#"{"key": "value"}"#, &limits);
            assert!(result.is_ok());
            assert_eq!(result.unwrap()["key"], "value");
        }

        #[test]
        fn test_zero_size_input() {
            let limits = JsonLimits::default();
            let result = parse_and_validate_json(b"", &limits);
            assert!(matches!(result, Err(JsonValidationError::ParseError(_))));
        }
    } // mod json_validation
} // mod storage