interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
//! Property arena allocator for variable-length property storage.
//!
//! Properties are stored as linked lists in a separate arena section of the file.
//! This module provides allocation and writing functions for property data.
//!
//! # Arena Layout
//!
//! The property arena is a contiguous region of the file that grows upward
//! (toward higher offsets). Properties are stored as linked lists:
//!
//! ```text
//! [PropertyEntry header (17 bytes)][value data (N bytes)]
//! [PropertyEntry header (17 bytes)][value data (M bytes)]
//! ...
//! ```
//!
//! Each property entry contains:
//! - `key_id` (4 bytes): String table ID for the property key
//! - `value_type` (1 byte): Value discriminant
//! - `value_len` (4 bytes): Length of serialized value
//! - `next` (8 bytes): Offset to next property, or `u64::MAX` if last
//!
//! # Allocation Strategy
//!
//! The arena uses a simple bump allocator. New allocations are appended at the
//! current arena end position. There is no compaction or free list for the arena;
//! deleted properties simply leave gaps that are not reclaimed.

use crate::error::StorageError;
use crate::storage::mmap::records::{PropertyEntry, PROPERTY_ENTRY_HEADER_SIZE};
use crate::value::Value;
use hashbrown::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

/// Tracks the current allocation position in the property arena.
///
/// This is a simple bump allocator that appends new property entries
/// at the end of the arena.
pub struct ArenaAllocator {
    /// Current write position (absolute file offset)
    current_offset: AtomicU64,
    /// Start of the arena (from header.property_arena_offset)
    arena_start: u64,
    /// End of the arena (start of string table or end of arena space)
    arena_end: u64,
}

impl ArenaAllocator {
    /// Create a new arena allocator.
    ///
    /// # Arguments
    ///
    /// * `arena_start` - Absolute file offset where arena begins
    /// * `arena_end` - Absolute file offset where arena ends (e.g., string table start)
    /// * `current_offset` - Current write position (arena_start for new database)
    pub fn new(arena_start: u64, arena_end: u64, current_offset: u64) -> Self {
        Self {
            current_offset: AtomicU64::new(current_offset),
            arena_start,
            arena_end,
        }
    }

    /// Get the current allocation offset.
    #[inline]
    pub fn current_offset(&self) -> u64 {
        self.current_offset.load(Ordering::SeqCst)
    }

    /// Get the arena start offset.
    #[inline]
    pub fn arena_start(&self) -> u64 {
        self.arena_start
    }

    /// Get the arena end offset.
    #[inline]
    pub fn arena_end(&self) -> u64 {
        self.arena_end
    }

    /// Calculate the total size needed for a property entry.
    ///
    /// Returns the size of the header plus the serialized value data.
    #[inline]
    pub fn entry_size(value_len: usize) -> usize {
        PROPERTY_ENTRY_HEADER_SIZE + value_len
    }

    /// Check if there's enough space for an allocation.
    ///
    /// # Arguments
    ///
    /// * `size` - Number of bytes needed
    ///
    /// # Returns
    ///
    /// `true` if the allocation can fit, `false` otherwise.
    pub fn has_space(&self, size: usize) -> bool {
        let current = self.current_offset.load(Ordering::SeqCst);
        current + size as u64 <= self.arena_end
    }

    /// Reserve space in the arena.
    ///
    /// Atomically allocates `size` bytes and returns the starting offset.
    /// Uses compare-and-swap to ensure atomic allocation without race conditions.
    ///
    /// # Arguments
    ///
    /// * `size` - Number of bytes to allocate
    ///
    /// # Returns
    ///
    /// The absolute file offset where the allocation starts.
    ///
    /// # Errors
    ///
    /// Returns [`StorageError::OutOfSpace`] if there isn't enough room.
    ///
    /// # Thread Safety
    ///
    /// This method is safe to call concurrently from multiple threads.
    /// Allocation is performed atomically using compare-and-swap.
    pub fn allocate(&self, size: usize) -> Result<u64, StorageError> {
        let size = size as u64;

        // Use compare-and-swap loop for atomic allocation without TOCTOU race
        loop {
            let current = self.current_offset.load(Ordering::SeqCst);
            let new_offset = current + size;

            // Check if we would exceed the arena
            if new_offset > self.arena_end {
                return Err(StorageError::OutOfSpace);
            }

            // Atomically try to update the offset
            match self.current_offset.compare_exchange(
                current,
                new_offset,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(_) => return Ok(current), // Successfully allocated
                Err(_) => continue,          // Contention, retry
            }
        }
    }

    /// Update the arena end (e.g., after file growth).
    pub fn set_arena_end(&mut self, new_end: u64) {
        self.arena_end = new_end;
    }
}

/// Serialize properties into arena format.
///
/// Converts a property map into a series of bytes ready to be written
/// to the arena. Returns the serialized data and a list of entry offsets
/// for linking.
///
/// # Arguments
///
/// * `properties` - The properties to serialize
/// * `intern_key` - Function to intern a property key and get its ID
///
/// # Returns
///
/// A tuple of:
/// - `Vec<u8>` - The serialized property data
/// - `Vec<usize>` - Offsets within the data where each entry's `next` field is located
///
/// # Example
///
/// ```ignore
/// let props = HashMap::from([("name".to_string(), Value::String("Alice".to_string()))]);
/// let (data, next_offsets) = serialize_properties(&props, |key| interner.intern(key));
/// ```
pub fn serialize_properties<F>(
    properties: &HashMap<String, Value>,
    mut intern_key: F,
) -> (Vec<u8>, Vec<usize>)
where
    F: FnMut(&str) -> u32,
{
    let mut data = Vec::new();
    let mut next_offsets = Vec::new();

    let entries: Vec<_> = properties.iter().collect();

    for (key, value) in entries.iter() {
        // Serialize the value
        let mut value_data = Vec::new();
        value.serialize(&mut value_data);

        // Get key ID from interner
        let key_id = intern_key(key);

        // Determine the next pointer (will be patched later with actual offsets)
        // For now, use u64::MAX to indicate "not yet linked"
        let next = u64::MAX;

        // Create property entry header
        let entry = PropertyEntry::new(key_id, value.discriminant(), value_data.len() as u32, next);

        // Record where the `next` field is in our data buffer
        // The `next` field is at offset 9 within PropertyEntry (after key_id + value_type + value_len)
        let next_field_offset = data.len() + 9;
        next_offsets.push(next_field_offset);

        // Write entry header
        data.extend_from_slice(&entry.to_bytes());

        // Write value data
        data.extend_from_slice(&value_data);
    }

    (data, next_offsets)
}

/// Link property entries by patching their `next` pointers.
///
/// After writing property entries to the arena, this function patches
/// the `next` fields to create a linked list.
///
/// # Arguments
///
/// * `data` - Mutable reference to the serialized property data
/// * `next_offsets` - Offsets within `data` where `next` fields are located
/// * `base_offset` - The absolute file offset where `data` will be written
/// * `entry_sizes` - Size of each entry (header + value data)
///
/// The last entry's `next` is set to `u64::MAX` to indicate end of list.
pub fn link_property_entries(
    data: &mut [u8],
    next_offsets: &[usize],
    base_offset: u64,
    entry_sizes: &[usize],
) {
    if next_offsets.is_empty() {
        return;
    }

    // Calculate the absolute offset of each entry
    let mut entry_start = base_offset;
    let mut entry_offsets = Vec::with_capacity(next_offsets.len());

    for size in entry_sizes {
        entry_offsets.push(entry_start);
        entry_start += *size as u64;
    }

    // Link each entry to the next
    for i in 0..next_offsets.len() {
        let next_value = if i + 1 < entry_offsets.len() {
            entry_offsets[i + 1]
        } else {
            u64::MAX // Last entry
        };

        let offset = next_offsets[i];
        data[offset..offset + 8].copy_from_slice(&next_value.to_le_bytes());
    }
}

/// Calculate entry sizes for a list of properties.
///
/// # Arguments
///
/// * `properties` - The properties to measure
///
/// # Returns
///
/// A vector of sizes, one per property entry (header + value data).
pub fn calculate_entry_sizes(properties: &HashMap<String, Value>) -> Vec<usize> {
    properties
        .values()
        .map(|value| {
            let mut buf = Vec::new();
            value.serialize(&mut buf);
            PROPERTY_ENTRY_HEADER_SIZE + buf.len()
        })
        .collect()
}

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

    #[test]
    fn test_arena_allocator_new() {
        let allocator = ArenaAllocator::new(1000, 2000, 1000);

        assert_eq!(allocator.arena_start(), 1000);
        assert_eq!(allocator.arena_end(), 2000);
        assert_eq!(allocator.current_offset(), 1000);
    }

    #[test]
    fn test_arena_allocator_has_space() {
        let allocator = ArenaAllocator::new(1000, 2000, 1000);

        // Should have space for 1000 bytes
        assert!(allocator.has_space(1000));
        assert!(allocator.has_space(999));

        // Should not have space for more than 1000 bytes
        assert!(!allocator.has_space(1001));
    }

    #[test]
    fn test_arena_allocator_allocate() {
        let allocator = ArenaAllocator::new(1000, 2000, 1000);

        // First allocation
        let offset1 = allocator.allocate(100).unwrap();
        assert_eq!(offset1, 1000);
        assert_eq!(allocator.current_offset(), 1100);

        // Second allocation
        let offset2 = allocator.allocate(200).unwrap();
        assert_eq!(offset2, 1100);
        assert_eq!(allocator.current_offset(), 1300);
    }

    #[test]
    fn test_arena_allocator_out_of_space() {
        let allocator = ArenaAllocator::new(1000, 1100, 1000);

        // First allocation succeeds
        let offset1 = allocator.allocate(50).unwrap();
        assert_eq!(offset1, 1000);

        // Second allocation that would exceed arena fails
        let result = allocator.allocate(100);
        assert!(matches!(result, Err(StorageError::OutOfSpace)));

        // Offset should be rolled back
        assert_eq!(allocator.current_offset(), 1050);
    }

    #[test]
    fn test_serialize_properties_empty() {
        let props: HashMap<String, Value> = HashMap::new();
        let (data, next_offsets) = serialize_properties(&props, |_| 0);

        assert!(data.is_empty());
        assert!(next_offsets.is_empty());
    }

    #[test]
    fn test_serialize_properties_single() {
        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Alice".to_string()));

        let (data, next_offsets) = serialize_properties(&props, |_| 42);

        // Should have one entry
        assert_eq!(next_offsets.len(), 1);

        // Data should contain header + value
        // Header: 17 bytes
        // Value: 1 (discriminant) + 4 (length) + 5 ("Alice") = 10 bytes
        assert_eq!(data.len(), 17 + 10);

        // Verify the key_id is at the start (little-endian u32 = 42)
        let key_id = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
        assert_eq!(key_id, 42);
    }

    #[test]
    fn test_serialize_properties_multiple() {
        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Bob".to_string()));
        props.insert("age".to_string(), Value::Int(30));

        let (data, next_offsets) = serialize_properties(&props, |key| match key {
            "name" => 1,
            "age" => 2,
            _ => 0,
        });

        // Should have two entries
        assert_eq!(next_offsets.len(), 2);

        // Data should contain both entries
        assert!(!data.is_empty());
    }

    #[test]
    fn test_link_property_entries_single() {
        let mut data = vec![0u8; 30]; // Dummy data
        let next_offsets = vec![9]; // next field at offset 9
        let entry_sizes = vec![30];

        link_property_entries(&mut data, &next_offsets, 1000, &entry_sizes);

        // Single entry should have next = u64::MAX
        let next = u64::from_le_bytes([
            data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16],
        ]);
        assert_eq!(next, u64::MAX);
    }

    #[test]
    fn test_link_property_entries_multiple() {
        // Create data for two entries
        let mut data = vec![0u8; 60]; // Two 30-byte entries
        let next_offsets = vec![9, 39]; // next fields at offset 9 and 39
        let entry_sizes = vec![30, 30];

        link_property_entries(&mut data, &next_offsets, 1000, &entry_sizes);

        // First entry's next should point to second entry (offset 1030)
        let next1 = u64::from_le_bytes([
            data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16],
        ]);
        assert_eq!(next1, 1030);

        // Second entry's next should be u64::MAX
        let next2 = u64::from_le_bytes([
            data[39], data[40], data[41], data[42], data[43], data[44], data[45], data[46],
        ]);
        assert_eq!(next2, u64::MAX);
    }

    #[test]
    fn test_link_property_entries_empty() {
        let mut data = vec![];
        let next_offsets = vec![];
        let entry_sizes = vec![];

        // Should not panic
        link_property_entries(&mut data, &next_offsets, 1000, &entry_sizes);
    }

    #[test]
    fn test_calculate_entry_sizes() {
        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Alice".to_string()));
        props.insert("age".to_string(), Value::Int(30));

        let sizes = calculate_entry_sizes(&props);

        assert_eq!(sizes.len(), 2);

        // Each size should be header (17) + value size
        for size in &sizes {
            assert!(*size > PROPERTY_ENTRY_HEADER_SIZE);
        }
    }

    #[test]
    fn test_entry_size() {
        assert_eq!(ArenaAllocator::entry_size(0), PROPERTY_ENTRY_HEADER_SIZE);
        assert_eq!(
            ArenaAllocator::entry_size(10),
            PROPERTY_ENTRY_HEADER_SIZE + 10
        );
        assert_eq!(
            ArenaAllocator::entry_size(100),
            PROPERTY_ENTRY_HEADER_SIZE + 100
        );
    }

    #[test]
    fn test_serialize_all_value_types() {
        let mut props = HashMap::new();
        props.insert("null".to_string(), Value::Null);
        props.insert("bool_true".to_string(), Value::Bool(true));
        props.insert("bool_false".to_string(), Value::Bool(false));
        props.insert("int".to_string(), Value::Int(42));
        props.insert("float".to_string(), Value::Float(3.14));
        props.insert("string".to_string(), Value::String("hello".to_string()));

        let (data, next_offsets) = serialize_properties(&props, |_| 0);

        assert_eq!(next_offsets.len(), 6);
        assert!(!data.is_empty());
    }
}