aegis-document 0.3.0

Document store engine for Aegis database
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Aegis Document Collection
//!
//! Collection management for document storage.
//!
//! @version 0.1.0
//! @author AutomataNexus Development Team

use crate::index::{DocumentIndex, IndexType};
use crate::query::{Filter, Query, QueryResult};
use crate::types::{Document, DocumentId};
use crate::validation::{Schema, ValidationResult};
use std::collections::{HashMap, HashSet};
use std::sync::RwLock;

// =============================================================================
// Collection
// =============================================================================

/// A collection of documents.
pub struct Collection {
    name: String,
    documents: RwLock<HashMap<DocumentId, Document>>,
    indexes: RwLock<Vec<DocumentIndex>>,
    schema: Option<Schema>,
}

impl Collection {
    /// Create a new collection.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            documents: RwLock::new(HashMap::new()),
            indexes: RwLock::new(Vec::new()),
            schema: None,
        }
    }

    /// Create a collection with schema validation.
    pub fn with_schema(name: impl Into<String>, schema: Schema) -> Self {
        Self {
            name: name.into(),
            documents: RwLock::new(HashMap::new()),
            indexes: RwLock::new(Vec::new()),
            schema: Some(schema),
        }
    }

    /// Get the collection name.
    pub fn name(&self) -> &str {
        &self.name
    }

    // -------------------------------------------------------------------------
    // Document Operations
    // -------------------------------------------------------------------------

    /// Insert a document.
    pub fn insert(&self, doc: Document) -> Result<DocumentId, CollectionError> {
        if let Some(ref schema) = self.schema {
            let result = schema.validate(&doc);
            if !result.is_valid {
                return Err(CollectionError::ValidationFailed(result.errors));
            }
        }

        let id = doc.id.clone();

        {
            let mut docs = self.documents.write().expect("documents RwLock poisoned");
            if docs.contains_key(&id) {
                return Err(CollectionError::DuplicateId(id));
            }
            docs.insert(id.clone(), doc.clone());
        }

        self.index_document(&doc);

        Ok(id)
    }

    /// Insert multiple documents.
    pub fn insert_many(&self, docs: Vec<Document>) -> Result<Vec<DocumentId>, CollectionError> {
        let mut ids = Vec::with_capacity(docs.len());

        for doc in docs {
            let id = self.insert(doc)?;
            ids.push(id);
        }

        Ok(ids)
    }

    /// Get a document by ID.
    pub fn get(&self, id: &DocumentId) -> Option<Document> {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.get(id).cloned()
    }

    /// Update a document.
    pub fn update(&self, id: &DocumentId, doc: Document) -> Result<(), CollectionError> {
        if let Some(ref schema) = self.schema {
            let result = schema.validate(&doc);
            if !result.is_valid {
                return Err(CollectionError::ValidationFailed(result.errors));
            }
        }

        {
            let mut docs = self.documents.write().expect("documents RwLock poisoned");
            if !docs.contains_key(id) {
                return Err(CollectionError::NotFound(id.clone()));
            }

            if let Some(old_doc) = docs.get(id) {
                self.unindex_document(old_doc);
            }

            docs.insert(id.clone(), doc.clone());
        }

        self.index_document(&doc);

        Ok(())
    }

    /// Delete a document.
    pub fn delete(&self, id: &DocumentId) -> Result<Document, CollectionError> {
        let mut docs = self.documents.write().expect("documents RwLock poisoned");

        match docs.remove(id) {
            Some(doc) => {
                self.unindex_document(&doc);
                Ok(doc)
            }
            None => Err(CollectionError::NotFound(id.clone())),
        }
    }

    /// Check if a document exists.
    pub fn contains(&self, id: &DocumentId) -> bool {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.contains_key(id)
    }

    /// Get the number of documents.
    pub fn count(&self) -> usize {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.len()
    }

    /// Get all document IDs.
    pub fn ids(&self) -> Vec<DocumentId> {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.keys().cloned().collect()
    }

    /// Get all documents.
    pub fn all(&self) -> Vec<Document> {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.values().cloned().collect()
    }

    /// Clear all documents.
    pub fn clear(&self) {
        let mut docs = self.documents.write().expect("documents RwLock poisoned");
        docs.clear();

        let mut indexes = self.indexes.write().expect("indexes RwLock poisoned");
        for index in indexes.iter_mut() {
            index.clear();
        }
    }

    // -------------------------------------------------------------------------
    // Query Operations
    // -------------------------------------------------------------------------

    /// Find documents matching a query.
    ///
    /// When possible, uses indexes on fields with `Eq` filters to narrow
    /// the candidate set before applying the full filter chain, reducing
    /// the number of documents scanned.
    pub fn find(&self, query: &Query) -> QueryResult {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        let indexes = self.indexes.read().expect("indexes RwLock poisoned");
        let start = std::time::Instant::now();

        // Try to find an Eq filter whose field has a hash/unique/btree index.
        // We pick the first matching index we find and use it to get candidate IDs.
        let candidate_ids = self.find_indexed_candidates(&query.filters, &indexes);

        let (mut matching, total_scanned) = if let Some(ids) = candidate_ids {
            // Index path: only scan the candidate documents
            let scanned = ids.len();
            let results: Vec<Document> = ids
                .iter()
                .filter_map(|id| docs.get(id))
                .filter(|doc| query.matches(doc))
                .cloned()
                .collect();
            (results, scanned)
        } else {
            // Full scan fallback
            let results: Vec<Document> = docs
                .values()
                .filter(|doc| query.matches(doc))
                .cloned()
                .collect();
            (results, docs.len())
        };

        // Apply sort
        if let Some(ref sort) = query.sort {
            matching.sort_by(|a, b| {
                let va = a.get(&sort.field);
                let vb = b.get(&sort.field);
                let ord = crate::types::compare_values(va, vb);
                if sort.ascending {
                    ord
                } else {
                    ord.reverse()
                }
            });
        }

        // Apply skip
        if let Some(skip) = query.skip {
            if skip < matching.len() {
                matching = matching.split_off(skip);
            } else {
                matching.clear();
            }
        }

        // Apply limit
        if let Some(limit) = query.limit {
            matching.truncate(limit);
        }

        // Apply projection
        if let Some(ref fields) = query.projection {
            for doc in &mut matching {
                doc.data.retain(|key, _| fields.contains(key));
            }
        }

        QueryResult {
            documents: matching,
            total_scanned,
            execution_time_ms: start.elapsed().as_millis() as u64,
        }
    }

    /// Find one document matching a query.
    pub fn find_one(&self, query: &Query) -> Option<Document> {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.values().find(|doc| query.matches(doc)).cloned()
    }

    /// Count documents matching a query.
    pub fn count_matching(&self, query: &Query) -> usize {
        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.values().filter(|doc| query.matches(doc)).count()
    }

    // -------------------------------------------------------------------------
    // Index-Accelerated Lookup (private)
    // -------------------------------------------------------------------------

    /// Scan filters for `Eq` conditions on indexed fields and return candidate
    /// document IDs from the first matching index. When multiple `Eq` filters
    /// have indexes, intersect their candidate sets for tighter filtering.
    /// Returns `None` if no applicable index is found (triggers full scan).
    fn find_indexed_candidates(
        &self,
        filters: &[Filter],
        indexes: &[DocumentIndex],
    ) -> Option<HashSet<DocumentId>> {
        let mut result: Option<HashSet<DocumentId>> = None;

        for filter in filters {
            if let Filter::Eq { field, value } = filter {
                // Find an index on this field (Hash, Unique, or BTree all use hash_index)
                if let Some(index) = indexes.iter().find(|idx| {
                    idx.field() == field.as_str()
                        && matches!(
                            idx.index_type(),
                            IndexType::Hash | IndexType::Unique | IndexType::BTree
                        )
                }) {
                    let ids: HashSet<DocumentId> = index.find_eq(value).into_iter().collect();
                    result = Some(match result {
                        Some(current) => current.intersection(&ids).cloned().collect(),
                        None => ids,
                    });
                }
            }
        }

        result
    }

    // -------------------------------------------------------------------------
    // Index Operations
    // -------------------------------------------------------------------------

    /// Create an index on a field.
    pub fn create_index(&self, field: impl Into<String>, index_type: IndexType) {
        let field = field.into();
        let mut index = DocumentIndex::new(field.clone(), index_type);

        let docs = self.documents.read().expect("documents RwLock poisoned");
        for doc in docs.values() {
            index.index_document(doc);
        }

        let mut indexes = self.indexes.write().expect("indexes RwLock poisoned");
        indexes.push(index);
    }

    /// Drop an index.
    pub fn drop_index(&self, field: &str) {
        let mut indexes = self.indexes.write().expect("indexes RwLock poisoned");
        indexes.retain(|idx| idx.field() != field);
    }

    /// Get all index names.
    pub fn index_names(&self) -> Vec<String> {
        let indexes = self.indexes.read().expect("indexes RwLock poisoned");
        indexes.iter().map(|idx| idx.field().to_string()).collect()
    }

    fn index_document(&self, doc: &Document) {
        let mut indexes = self.indexes.write().expect("indexes RwLock poisoned");
        for index in indexes.iter_mut() {
            index.index_document(doc);
        }
    }

    fn unindex_document(&self, doc: &Document) {
        let mut indexes = self.indexes.write().expect("indexes RwLock poisoned");
        for index in indexes.iter_mut() {
            index.unindex_document(doc);
        }
    }

    // -------------------------------------------------------------------------
    // Schema Operations
    // -------------------------------------------------------------------------

    /// Set the collection schema.
    pub fn set_schema(&mut self, schema: Schema) {
        self.schema = Some(schema);
    }

    /// Get the collection schema.
    pub fn schema(&self) -> Option<&Schema> {
        self.schema.as_ref()
    }

    /// Validate all documents against the schema.
    pub fn validate_all(&self) -> Vec<(DocumentId, ValidationResult)> {
        let Some(ref schema) = self.schema else {
            return Vec::new();
        };

        let docs = self.documents.read().expect("documents RwLock poisoned");
        docs.iter()
            .map(|(id, doc)| (id.clone(), schema.validate(doc)))
            .filter(|(_, result)| !result.is_valid)
            .collect()
    }
}

// =============================================================================
// Collection Error
// =============================================================================

/// Errors that can occur in collection operations.
#[derive(Debug, Clone)]
pub enum CollectionError {
    DuplicateId(DocumentId),
    NotFound(DocumentId),
    ValidationFailed(Vec<String>),
    IndexError(String),
}

impl std::fmt::Display for CollectionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::DuplicateId(id) => write!(f, "Document with ID {} already exists", id),
            Self::NotFound(id) => write!(f, "Document with ID {} not found", id),
            Self::ValidationFailed(errors) => {
                write!(f, "Validation failed: {}", errors.join(", "))
            }
            Self::IndexError(msg) => write!(f, "Index error: {}", msg),
        }
    }
}

impl std::error::Error for CollectionError {}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_collection_creation() {
        let collection = Collection::new("users");
        assert_eq!(collection.name(), "users");
        assert_eq!(collection.count(), 0);
    }

    #[test]
    fn test_insert_and_get() {
        let collection = Collection::new("test");

        let mut doc = Document::with_id("doc1");
        doc.set("name", "Alice");

        let id = collection.insert(doc).unwrap();
        assert_eq!(id.as_str(), "doc1");

        let retrieved = collection.get(&id).unwrap();
        assert_eq!(
            retrieved.get("name").and_then(|v| v.as_str()),
            Some("Alice")
        );
    }

    #[test]
    fn test_duplicate_id() {
        let collection = Collection::new("test");

        let doc1 = Document::with_id("same-id");
        let doc2 = Document::with_id("same-id");

        collection.insert(doc1).unwrap();
        let result = collection.insert(doc2);

        assert!(matches!(result, Err(CollectionError::DuplicateId(_))));
    }

    #[test]
    fn test_update() {
        let collection = Collection::new("test");

        let mut doc = Document::with_id("doc1");
        doc.set("count", 1i64);
        collection.insert(doc).unwrap();

        let mut updated = Document::with_id("doc1");
        updated.set("count", 2i64);
        collection
            .update(&DocumentId::new("doc1"), updated)
            .unwrap();

        let retrieved = collection.get(&DocumentId::new("doc1")).unwrap();
        assert_eq!(retrieved.get("count").and_then(|v| v.as_i64()), Some(2));
    }

    #[test]
    fn test_delete() {
        let collection = Collection::new("test");

        let doc = Document::with_id("doc1");
        collection.insert(doc).unwrap();

        assert!(collection.contains(&DocumentId::new("doc1")));

        collection.delete(&DocumentId::new("doc1")).unwrap();
        assert!(!collection.contains(&DocumentId::new("doc1")));
    }

    #[test]
    fn test_find() {
        let collection = Collection::new("test");

        for i in 0..10 {
            let mut doc = Document::new();
            doc.set("value", i as i64);
            doc.set("even", i % 2 == 0);
            collection.insert(doc).unwrap();
        }

        let query = QueryBuilder::new().eq("even", true).build();
        let result = collection.find(&query);

        assert_eq!(result.documents.len(), 5);
    }

    #[test]
    fn test_find_uses_index() {
        let collection = Collection::new("test");

        // Insert 100 documents with a "status" field
        for i in 0..100 {
            let mut doc = Document::new();
            doc.set("status", if i % 10 == 0 { "active" } else { "inactive" });
            doc.set("value", i as i64);
            collection.insert(doc).unwrap();
        }

        // Query without index => full scan
        let query = QueryBuilder::new().eq("status", "active").build();
        let result_no_index = collection.find(&query);
        assert_eq!(result_no_index.documents.len(), 10);
        assert_eq!(result_no_index.total_scanned, 100); // full scan

        // Create index on "status"
        collection.create_index("status", IndexType::Hash);

        // Query with index => should scan far fewer documents
        let result_with_index = collection.find(&query);
        assert_eq!(result_with_index.documents.len(), 10);
        assert!(
            result_with_index.total_scanned < 100,
            "Expected index scan to examine fewer than 100 docs, got {}",
            result_with_index.total_scanned
        );
        // The index should return exactly the 10 matching documents
        assert_eq!(result_with_index.total_scanned, 10);
    }

    #[test]
    fn test_find_index_with_additional_filters() {
        let collection = Collection::new("test");

        // Insert docs: 50 active, 50 inactive; half of each have value > 25
        for i in 0..100 {
            let mut doc = Document::new();
            doc.set("status", if i < 50 { "active" } else { "inactive" });
            doc.set("value", i as i64);
            collection.insert(doc).unwrap();
        }

        collection.create_index("status", IndexType::Hash);

        // Eq filter on indexed field + Gt filter on non-indexed field
        let query = QueryBuilder::new()
            .eq("status", "active")
            .gt("value", 25i64)
            .build();

        let result = collection.find(&query);

        // active docs are i=0..50, value>25 means i=26..49 => 24 docs
        assert_eq!(result.documents.len(), 24);
        // Should only scan the 50 "active" candidates from the index
        assert_eq!(result.total_scanned, 50);
    }

    #[test]
    fn test_find_no_matching_index_falls_back_to_scan() {
        let collection = Collection::new("test");

        for i in 0..20 {
            let mut doc = Document::new();
            doc.set("x", i as i64);
            collection.insert(doc).unwrap();
        }

        // Index on a different field
        collection.create_index("y", IndexType::Hash);

        let query = QueryBuilder::new().eq("x", 5i64).build();
        let result = collection.find(&query);

        assert_eq!(result.documents.len(), 1);
        // No index on "x", so full scan
        assert_eq!(result.total_scanned, 20);
    }
}