shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
//! API memory management validation tests
//!
//! This test suite validates proper resource management, cleanup, and memory safety
//! of the ApiThing-based Shardex API.

use apithing::ApiOperation;
use shardex::api::{
    AddPostings, AddPostingsParams, CreateIndex, CreateIndexParams, Flush, FlushParams, GetStats, GetStatsParams,
    Search, SearchParams, ShardexContext, StoreDocumentText, StoreDocumentTextParams,
};
use shardex::{DocumentId, Posting};
use tempfile::TempDir;

/// Create a temporary directory for testing
fn create_temp_directory() -> TempDir {
    tempfile::tempdir().expect("Failed to create temporary directory")
}

/// Generate test postings for memory testing
fn generate_test_postings(count: usize, vector_size: usize, base_id: u128) -> Vec<Posting> {
    (0..count)
        .map(|i| {
            let document_id = DocumentId::from_raw(base_id + i as u128);
            let mut vector = vec![0.0; vector_size];

            // Create deterministic vectors
            for (j, item) in vector.iter_mut().enumerate().take(vector_size) {
                *item = ((i * 13 + j * 17) as f32 % 100.0) / 100.0;
            }

            // Normalize vector
            let magnitude: f32 = vector.iter().map(|x| x * x).sum::<f32>().sqrt();
            if magnitude > 0.0 {
                for value in &mut vector {
                    *value /= magnitude;
                }
            }

            Posting {
                document_id,
                start: 0,
                length: 100,
                vector,
            }
        })
        .collect()
}

#[test]
fn test_context_lifecycle_management() {
    // Test that contexts properly clean up resources when dropped

    for iteration in 0..5 {
        let temp_dir = create_temp_directory();
        let mut context = ShardexContext::new();

        // Create index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(128)
            .shard_size(1000)
            .build()
            .expect("Failed to build CreateIndexParams");

        CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

        // Add some data
        let postings = generate_test_postings(50, 128, (iteration * 1000) as u128);
        let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

        AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

        // Flush to ensure data is written
        let flush_params = FlushParams::new();
        Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

        // Get memory usage before drop
        let stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

        println!(
            "Iteration {}: Memory usage: {:.2} MB",
            iteration,
            stats.memory_usage as f64 / 1024.0 / 1024.0
        );

        // Context should clean up when dropped
        drop(context);

        // Clean up temp directory
        drop(temp_dir);
    }

    // If we get here without running out of memory, resource cleanup is working
    println!("Context lifecycle test completed - no memory leaks detected");
}

#[test]
fn test_repeated_operations_memory_stability() {
    // Test that repeated operations don't cause memory leaks

    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(64)
        .shard_size(2000)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    let mut previous_memory_usage = 0usize;

    // Perform repeated operations and monitor memory
    for batch in 0..10 {
        // Add postings
        let postings = generate_test_postings(20, 64, (batch * 100) as u128);
        let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

        AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

        // Perform searches
        for _ in 0..5 {
            let query_vector = vec![0.1 * batch as f32; 64];
            let search_params = SearchParams::builder()
                .query_vector(query_vector)
                .k(5)
                .build()
                .expect("Failed to build SearchParams");

            let _results = Search::execute(&mut context, &search_params).expect("Search should succeed");
        }

        // Flush operations
        let flush_params = FlushParams::new();
        Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

        // Check memory usage
        let stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

        println!(
            "Batch {}: Memory usage: {:.2} MB, Postings: {}",
            batch,
            stats.memory_usage as f64 / 1024.0 / 1024.0,
            stats.active_postings
        );

        // Memory should grow gradually with data, not exponentially
        if batch > 0 {
            let memory_growth = stats.memory_usage - previous_memory_usage;
            let growth_ratio = memory_growth as f64 / previous_memory_usage as f64;

            // Memory growth should be reasonable (less than 50% per batch after initial growth)
            if batch > 2 {
                // Allow larger growth in first few batches
                assert!(
                    growth_ratio < 0.5,
                    "Memory growth of {:.2}% in batch {} suggests a memory leak",
                    growth_ratio * 100.0,
                    batch
                );
            }
        }

        previous_memory_usage = stats.memory_usage;
    }
}

#[test]
fn test_large_document_memory_handling() {
    // Test memory handling with large documents

    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(128)
        .shard_size(1000)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Store several large documents
    let large_text = "A".repeat(100_000); // 100KB document

    for i in 0..10 {
        let document_id = DocumentId::from_raw((i + 1) as u128);
        let postings = vec![Posting {
            document_id,
            start: 0,
            length: large_text.len() as u32,
            vector: vec![0.1 * i as f32; 128],
        }];

        let store_params = StoreDocumentTextParams {
            document_id,
            text: large_text.clone(),
            postings,
        };

        StoreDocumentText::execute(&mut context, &store_params).expect("StoreDocumentText should succeed");

        // Check memory usage periodically
        if i % 3 == 0 {
            let stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

            println!(
                "After {} large documents: {:.2} MB memory",
                i + 1,
                stats.memory_usage as f64 / 1024.0 / 1024.0
            );
        }
    }

    // Flush to ensure everything is persisted
    let flush_params = FlushParams::new();
    Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

    // Final memory check
    let final_stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

    println!(
        "Final memory after large documents: {:.2} MB",
        final_stats.memory_usage as f64 / 1024.0 / 1024.0
    );

    // Memory usage should be reasonable for the amount of data stored
    let total_text_size = large_text.len() * 10;
    let memory_efficiency = total_text_size as f64 / final_stats.memory_usage as f64;

    println!("Memory efficiency: {:.2} (data size / memory usage)", memory_efficiency);

    // Should not use more than 10x the actual data size in memory
    assert!(
        memory_efficiency > 0.1,
        "Memory usage seems excessive compared to data size"
    );
}

#[test]
fn test_error_handling_memory_safety() {
    // Test that memory is properly cleaned up even when operations fail

    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(128)
        .shard_size(1000)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    let initial_stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

    println!(
        "Initial memory usage: {:.2} MB",
        initial_stats.memory_usage as f64 / 1024.0 / 1024.0
    );

    // Try to cause various errors and ensure memory is handled properly
    for attempt in 0..5 {
        // Try to store a document that might cause issues
        let large_text = "X".repeat(1024); // 1KB document (reasonable size)
        let document_id = DocumentId::from_raw((attempt + 1) as u128);
        let postings = vec![Posting {
            document_id,
            start: 0,
            length: large_text.len() as u32,
            vector: vec![0.1; 128],
        }];

        let store_params = StoreDocumentTextParams {
            document_id,
            text: large_text,
            postings,
        };

        let result = StoreDocumentText::execute(&mut context, &store_params);

        if result.is_err() {
            println!("Attempt {}: Error occurred: {:?}", attempt, result.err());
        }

        // Check memory usage after operation
        let stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

        println!(
            "After attempt {}: Memory usage: {:.2} MB",
            attempt,
            stats.memory_usage as f64 / 1024.0 / 1024.0
        );

        // Memory should not grow excessively due to failed operations
        let memory_growth = stats.memory_usage as i64 - initial_stats.memory_usage as i64;
        assert!(
            memory_growth < 10 * 1024 * 1024, // Less than 10MB growth
            "Memory growth of {} bytes suggests operations are using excessive memory",
            memory_growth
        );
    }
}

#[test]
fn test_resource_cleanup_on_drop() {
    // Test that resources are properly cleaned up when context is dropped

    let temp_dir_path = {
        let temp_dir = create_temp_directory();
        let path = temp_dir.path().to_path_buf();

        {
            let mut context = ShardexContext::new();

            let create_params = CreateIndexParams::builder()
                .directory_path(path.clone())
                .vector_size(64)
                .shard_size(1000)
                .build()
                .expect("Failed to build CreateIndexParams");

            CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

            // Add some data
            let postings = generate_test_postings(20, 64, 1);
            let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

            AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

            // Flush data
            let flush_params = FlushParams::new();
            Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

            // Context will be dropped here
        }

        // Verify that index files were created (indicating proper persistence)
        assert!(path.exists(), "Index directory should exist after context drop");

        path
    };

    // Directory should still exist with index files after temp_dir is dropped
    // (The context should have properly flushed and closed files)
    if temp_dir_path.exists() {
        let entries: Vec<_> = std::fs::read_dir(&temp_dir_path)
            .expect("Should be able to read directory")
            .collect();

        println!("Index directory contains {} entries after context drop", entries.len());

        // Should have some index files
        assert!(!entries.is_empty(), "Index directory should not be empty");
    }
}

#[test]
fn test_memory_pressure_handling() {
    // Test behavior under memory pressure conditions

    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index with smaller shard size to force more frequent flushes
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(256)
        .shard_size(100) // Small shards to force frequent operations
        .batch_write_interval_ms(50) // Frequent batching
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Add many small batches to create memory pressure
    let mut total_postings = 0;
    let mut max_memory_usage = 0usize;

    for batch in 0..50 {
        let postings = generate_test_postings(25, 256, (batch * 100) as u128);
        let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

        AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

        total_postings += 25;

        // Flush more frequently to trigger shard creation - every 100 postings (shard size)
        if total_postings % 100 == 0 {
            let flush_params = FlushParams::new();
            Flush::execute(&mut context, &flush_params).expect("Flush should succeed to trigger shard creation");
        }

        // Periodically check memory usage
        if batch % 10 == 0 {
            let stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

            max_memory_usage = max_memory_usage.max(stats.memory_usage);

            println!(
                "Batch {}: {} total postings, {:.2} MB memory, {} shards",
                batch,
                total_postings,
                stats.memory_usage as f64 / 1024.0 / 1024.0,
                stats.total_shards
            );

            // Memory usage should not grow unbounded
            let memory_per_posting = stats.memory_usage as f64 / total_postings as f64;
            assert!(
                memory_per_posting < 50000.0, // Less than 50KB per posting
                "Memory per posting ({:.2} bytes) suggests memory is not being managed properly",
                memory_per_posting
            );
        }

        // Additional flush to test flush behavior under pressure
        if batch % 20 == 19 {
            let flush_params = FlushParams::new();
            Flush::execute(&mut context, &flush_params).expect("Flush should succeed under memory pressure");
        }
    }

    // Final flush and memory check
    let flush_params = FlushParams::with_stats();
    let flush_stats = Flush::execute(&mut context, &flush_params).expect("Final flush should succeed");

    let final_stats = GetStats::execute(&mut context, &GetStatsParams::new()).expect("GetStats should succeed");

    println!(
        "Final: {} postings, {:.2} MB memory, {} shards",
        total_postings,
        final_stats.memory_usage as f64 / 1024.0 / 1024.0,
        final_stats.total_shards
    );

    if let Some(stats) = flush_stats {
        println!("Final flush applied {} operations", stats.operations_applied);
    }

    // Verify the system handled memory pressure well
    // Note: The API layer creates a single shard for all operations, unlike the low-level
    // implementation which may create multiple shards automatically
    assert!(final_stats.total_shards >= 1, "Should have at least one shard");
    assert!(
        final_stats.memory_usage <= max_memory_usage * 2,
        "Final memory usage should not be excessive"
    );
}