nitrite 0.3.1

An embedded NoSQL document database for Rust with collections, repositories, indexing, and ACID transactions
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
use crate::collection::{Document, FindPlan, NitriteId};
use crate::common::processor::ProcessorChain;
use crate::common::stream::joined_cursor::{JoinedDocumentCursor, Lookup};
use crate::common::stream::projected_cursor::ProjectedDocumentCursor;
use crate::common::{ReadExecutor, WriteExecutor};
use crate::errors::NitriteResult;
use crate::ProcessorProvider;

/// Rebuilds a cursor's underlying document stream. A streaming cursor uses this to replay on
/// reset without retaining every yielded document in memory.
pub(crate) type StreamFactory =
    Box<dyn Fn() -> NitriteResult<Box<dyn Iterator<Item = NitriteResult<Document>>>>>;

pub struct DocumentCursor {
    underlying: Option<Box<dyn Iterator<Item = NitriteResult<Document>>>>,
    /// Rebuilds `underlying` for a streaming cursor on reset. `None` for cursors built from a
    /// fixed, non-rebuildable iterator (those are rewindable and replay from `cache`).
    factory: Option<StreamFactory>,
    cache: Vec<NitriteResult<Document>>,
    /// When `true`, yielded documents are cached so the cursor replays from memory on reset
    /// (raw-iterator cursors and join foreign cursors). When `false`, the cursor is *streaming*:
    /// it retains nothing and rebuilds the stream via `factory` on reset, so a forward-only scan
    /// never holds the whole result set in memory.
    rewindable: bool,
    current_index: usize,
    processor_chain: ProcessorChain,
    find_plan: Option<FindPlan>,
    /// Number of matching documents when the query is fully answered by an index (an index
    /// scan with no post-filter, skip, limit, or OR-union). When set, `count()`/`size()` return
    /// it directly instead of fetching and deserializing every matching document.
    covered_count: Option<usize>,
}

impl DocumentCursor {
    /// Builds a cursor over a fixed, non-rebuildable iterator. Such a cursor is **rewindable**:
    /// it caches yielded documents so it can replay them on `reset()` (tests, vec-backed cursors,
    /// and join foreign cursors).
    pub fn new(
        iter: Box<dyn Iterator<Item = NitriteResult<Document>>>,
        processor_chain: ProcessorChain,
    ) -> Self {
        DocumentCursor {
            underlying: Some(iter),
            factory: None,
            cache: Vec::new(),
            rewindable: true,
            current_index: 0,
            processor_chain,
            find_plan: None,
            covered_count: None,
        }
    }

    /// Builds a **streaming** cursor that retains nothing: documents flow straight through, and
    /// `reset()` rebuilds the stream via `factory` rather than replaying a cache. This keeps the
    /// common forward-only scan from holding the entire result set in memory.
    pub(crate) fn streaming(
        iter: Box<dyn Iterator<Item = NitriteResult<Document>>>,
        factory: StreamFactory,
        processor_chain: ProcessorChain,
    ) -> Self {
        DocumentCursor {
            underlying: Some(iter),
            factory: Some(factory),
            cache: Vec::new(),
            rewindable: false,
            current_index: 0,
            processor_chain,
            find_plan: None,
            covered_count: None,
        }
    }

    /// Switches a streaming cursor into caching mode so it can be replayed cheaply — used by a
    /// join, which scans the foreign side once per local row. Call before iteration begins.
    pub(crate) fn make_rewindable(&mut self) {
        self.rewindable = true;
    }

    /// Records the index-covered match count so `count()`/`size()` can answer without fetching.
    pub(crate) fn with_covered_count(mut self, covered_count: Option<usize>) -> Self {
        self.covered_count = covered_count;
        self
    }

    /// Resets the cursor so that it can be iterated from the beginning.
    ///
    /// A rewindable cursor replays from its cache; a streaming cursor that has advanced rebuilds
    /// its underlying stream (re-running the query) so it never has to retain documents to be
    /// replayable.
    pub fn reset(&mut self) {
        if !self.rewindable && (self.current_index > 0 || self.underlying.is_none()) {
            self.underlying = self.factory.as_ref().and_then(|factory| factory().ok());
        }
        self.current_index = 0;
    }

    /// Returns the number of matching documents.
    ///
    /// When the query is fully answered by an index, this is the **fast path**: the count comes
    /// straight from the index id set without fetching or deserializing a single document (the
    /// common "unread count in a folder" case). Otherwise the cursor is drained.
    pub fn count(mut self) -> usize {
        if let Some(count) = self.covered_count {
            return count;
        }
        let mut count = 0;
        while self.next().is_some() {
            count += 1;
        }
        count
    }

    pub fn size(&mut self) -> usize {
        // Index-covered queries know their size without materializing any document.
        if let Some(count) = self.covered_count {
            self.reset();
            return count;
        }
        // Count from the beginning by draining the cursor, then reset so it stays usable. A
        // streaming cursor counts without caching; a rewindable cursor fills its cache.
        self.reset();
        let mut count = 0;
        while self.next().is_some() {
            count += 1;
        }
        self.reset();
        count
    }

    pub fn first(&mut self) -> Option<NitriteResult<Document>> {
        self.reset();
        self.next()
    }

    pub fn find_plan(&self) -> Option<&FindPlan> {
        self.find_plan.as_ref()
    }

    pub(crate) fn set_find_plan(mut self, find_plan: FindPlan) -> Self {
        self.find_plan = Some(find_plan);
        self
    }

    pub fn join<'a>(
        &'a mut self,
        foreign_cursor: &'a mut DocumentCursor,
        lookup: &'a Lookup,
    ) -> NitriteResult<JoinedDocumentCursor<'a>> {
        Ok(JoinedDocumentCursor::new(self, foreign_cursor, lookup))
    }

    pub fn project<'a>(&'a mut self, projection: Document) -> NitriteResult<ProjectedDocumentCursor<'a>> {
        Ok(ProjectedDocumentCursor::new(self, projection))
    }

    /// Returns an iterator that yields `(NitriteId, Document)` pairs.
    /// This is useful when you need to update documents after retrieving them,
    /// as it provides the NitriteId needed for efficient O(1) updates via
    /// `update_by_id`.
    pub fn iter_with_id(&mut self) -> DocumentCursorWithId<'_> {
        DocumentCursorWithId { cursor: self }
    }
}

/// An iterator adapter that yields `(NitriteId, Document)` pairs from a DocumentCursor.
/// This enables efficient updates after retrieval by providing the NitriteId.
pub struct DocumentCursorWithId<'a> {
    cursor: &'a mut DocumentCursor,
}

impl<'a> Iterator for DocumentCursorWithId<'a> {
    type Item = NitriteResult<(NitriteId, Document)>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.cursor.next() {
            Some(Ok(mut doc)) => {
                // Get the NitriteId from the document
                match doc.id() {
                    Ok(id) => Some(Ok((id, doc))),
                    Err(e) => Some(Err(e)),
                }
            }
            Some(Err(e)) => Some(Err(e)),
            None => None,
        }
    }
}

impl Iterator for DocumentCursor {
    type Item = NitriteResult<Document>;

    fn next(&mut self) -> Option<Self::Item> {
        // If we have cached items, return the next one.
        if self.current_index < self.cache.len() {
            let result = self.cache[self.current_index].clone();
            self.current_index += 1;
            return Some(result);
        }

        // Otherwise, try to pull from the underlying iterator.
        if let Some(ref mut iter) = self.underlying {
            if let Some(item) = iter.next() {
                // Process after read - combine Result<T, E> handling
                let processed = item.and_then(|doc| {
                    self.processor_chain.process_after_read(doc)
                });

                // Only retain documents for cursors that replay from memory; a streaming cursor
                // keeps nothing and rebuilds via its factory on reset.
                if self.rewindable {
                    self.cache.push(processed.clone());
                }
                self.current_index += 1;
                return Some(processed);
            }
            // Once exhausted, drop the underlying iterator.
            self.underlying = None;
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collection::Document;
    use crate::doc;
    use crate::errors::{ErrorKind, NitriteError};

    fn create_document(first: &str, last: &str) -> Document {
        let doc = doc!{
            first: first,
            last: last,
        };
        doc
    }

    #[test]
    fn test_new_document_cursor() {
        let docs = vec![
            Ok(create_document("John", "Doe")),
            Ok(create_document("Jane", "Doe")),
        ];
        let iter = Box::new(docs.into_iter());
        let cursor = DocumentCursor::new(iter, ProcessorChain::new());
        assert_eq!(cursor.count(), 2);
    }

    #[test]
    fn test_next() {
        let docs = [
            Ok(create_document("John", "Doe")),
            Ok(create_document("Jane", "Doe")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        assert_eq!(
            cursor
                .next()
                .unwrap()
                .unwrap()
                .get("first")
                .unwrap()
                .as_string()
                .unwrap(),
            "John"
        );
        assert_eq!(
            cursor
                .next()
                .unwrap()
                .unwrap()
                .get("first")
                .unwrap()
                .as_string()
                .unwrap(),
            "Jane"
        );
        assert!(cursor.next().is_none());
    }

    #[test]
    fn test_next_with_error() {
        let docs = [
            Ok(create_document("John", "Doe")),
            Err(NitriteError::new("Test Error", ErrorKind::IOError)),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        assert_eq!(
            cursor
                .next()
                .unwrap()
                .unwrap()
                .get("first")
                .unwrap()
                .as_string()
                .unwrap(),
            "John"
        );
        assert!(cursor.next().unwrap().is_err());
    }

    #[test]
    fn test_first() {
        let docs = [
            Ok(create_document("John", "Doe")),
            Ok(create_document("Jane", "Doe")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        assert_eq!(
            cursor
                .first()
                .unwrap()
                .unwrap()
                .get("first")
                .unwrap()
                .as_string()
                .unwrap(),
            "John"
        );
    }

    #[test]
    fn test_find_plan() {
        let docs = vec![Ok(create_document("John", "Doe"))];
        let iter = Box::new(docs.into_iter());
        let cursor = DocumentCursor::new(iter, ProcessorChain::new());
        assert!(cursor.find_plan().is_none());
    }

    #[test]
    fn test_set_find_plan() {
        let docs = vec![Ok(create_document("John", "Doe"))];
        let iter = Box::new(docs.into_iter());
        let find_plan = FindPlan::new(); // Assuming FindPlan has a default() method
        let cursor = DocumentCursor::new(iter, ProcessorChain::new()).set_find_plan(find_plan.clone());
        assert!(cursor.find_plan().is_some());
        assert_eq!(cursor.find_plan().unwrap().index_descriptor(), find_plan.index_descriptor());
    }

    #[test]
    fn test_join() {
        let docs1 = vec![Ok(create_document("John", "Doe"))];
        let docs2 = vec![Ok(create_document("Jane", "Doe"))];
        let iter1 = Box::new(docs1.into_iter());
        let iter2 = Box::new(docs2.into_iter());
        let mut cursor1 = DocumentCursor::new(iter1, ProcessorChain::new());
        let mut cursor2 = DocumentCursor::new(iter2, ProcessorChain::new());
        let lookup = Lookup {
            local_field: "last".to_string(),
            foreign_field: "last".to_string(),
            target_field: "surname".to_string(),
        };
        let joined_cursor = cursor1.join(&mut cursor2, &lookup).expect("Failed to join");
        assert_eq!(joined_cursor.count(), 1);
    }

    #[test]
    fn test_project() {
        let docs = vec![Ok(create_document("John", "Doe"))];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        let projection = doc!{ "first": "John" };
        let projected_cursor = cursor.project(projection).expect("Failed to project");
        assert_eq!(projected_cursor.into_iter().count(), 1);
    }

    #[test]
    fn bench_cursor_iteration() {
        let mut docs = Vec::new();
        for i in 0..1000 {
            docs.push(Ok(create_document(&format!("John{}", i), &format!("Doe{}", i))));
        }
        let iter = Box::new(docs.into_iter());
        let cursor = DocumentCursor::new(iter, ProcessorChain::new());

        let start = std::time::Instant::now();
        let count = cursor.count();
        let elapsed = start.elapsed();

        assert_eq!(count, 1000);
        println!("Cursor iteration (1000 docs): {:?} ({:.3}µs per doc)", 
                 elapsed, 
                 elapsed.as_micros() as f64 / 1000.0);
    }

    #[test]
    fn bench_cursor_cache_reuse() {
        let docs = vec![
            Ok(create_document("John", "Doe")),
            Ok(create_document("Jane", "Doe")),
            Ok(create_document("Bob", "Smith")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());

        let start = std::time::Instant::now();
        for _ in 0..1000 {
            cursor.reset();
            for _ in cursor.by_ref() {}
        }
        let elapsed = start.elapsed();

        println!("Cursor cache reuse (1000 iterations): {:?}", elapsed);
    }

    #[test]
    fn bench_cursor_size_operation() {
        let start = std::time::Instant::now();
        for _ in 0..100 {
            let mut cursor = DocumentCursor::new(Box::new(vec![
                Ok(create_document("John", "Doe")),
                Ok(create_document("Jane", "Doe")),
            ].into_iter()), ProcessorChain::new());
            let _ = cursor.size();
        }
        let elapsed = start.elapsed();

        println!("Cursor size operation (100 iterations): {:?}", elapsed);
    }

    fn create_document_with_id(first: &str, last: &str) -> Document {
        let mut doc = doc!{
            first: first,
            last: last,
        };
        // Assign a NitriteId to the document
        let _ = doc.id(); // This creates an ID if one doesn't exist
        doc
    }

    #[test]
    fn test_iter_with_id_basic() {
        let docs = vec![
            Ok(create_document_with_id("John", "Doe")),
            Ok(create_document_with_id("Jane", "Smith")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        
        let items: Vec<_> = cursor.iter_with_id().collect();
        assert_eq!(items.len(), 2);
        
        // Verify we got valid IDs and documents
        let (id1, doc1) = items[0].as_ref().unwrap();
        assert!(!id1.to_string().is_empty());
        assert_eq!(doc1.get("first").unwrap().as_string().unwrap(), "John");
        
        let (id2, doc2) = items[1].as_ref().unwrap();
        assert!(!id2.to_string().is_empty());
        assert_eq!(doc2.get("first").unwrap().as_string().unwrap(), "Jane");
        
        // IDs should be different
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_iter_with_id_empty() {
        let docs: Vec<NitriteResult<Document>> = vec![];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        
        let items: Vec<_> = cursor.iter_with_id().collect();
        assert!(items.is_empty());
    }

    #[test]
    fn test_iter_with_id_with_error() {
        let docs = vec![
            Ok(create_document_with_id("John", "Doe")),
            Err(NitriteError::new("Test Error", ErrorKind::IOError)),
            Ok(create_document_with_id("Jane", "Smith")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        
        let items: Vec<_> = cursor.iter_with_id().collect();
        assert_eq!(items.len(), 3);
        
        // First item should be Ok
        assert!(items[0].is_ok());
        
        // Second item should be an error
        assert!(items[1].is_err());
        
        // Third item should be Ok
        assert!(items[2].is_ok());
    }

    #[test]
    fn test_iter_with_id_preserves_document_content() {
        let mut doc = doc!{
            "name": "Test",
            "value": 42,
            "nested": {
                "field": "data"
            }
        };
        let _ = doc.id(); // Ensure ID is assigned
        
        let docs = vec![Ok(doc.clone())];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        
        let items: Vec<_> = cursor.iter_with_id().collect();
        assert_eq!(items.len(), 1);
        
        let (id, retrieved_doc) = items[0].as_ref().unwrap();
        
        // Verify ID matches original
        assert_eq!(*id, doc.id().unwrap());
        
        // Verify all fields are preserved
        assert_eq!(retrieved_doc.get("name").unwrap().as_string().unwrap(), "Test");
        assert_eq!(*retrieved_doc.get("value").unwrap().as_i32().unwrap(), 42);
    }

    #[test]
    fn test_iter_with_id_after_partial_iteration() {
        let docs = vec![
            Ok(create_document_with_id("John", "Doe")),
            Ok(create_document_with_id("Jane", "Smith")),
            Ok(create_document_with_id("Bob", "Brown")),
        ];
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());
        
        // Consume one item via regular next()
        let _ = cursor.next();
        
        // Now use iter_with_id for remaining items
        let items: Vec<_> = cursor.iter_with_id().collect();
        assert_eq!(items.len(), 2);
        
        let (_, doc1) = items[0].as_ref().unwrap();
        assert_eq!(doc1.get("first").unwrap().as_string().unwrap(), "Jane");
        
        let (_, doc2) = items[1].as_ref().unwrap();
        assert_eq!(doc2.get("first").unwrap().as_string().unwrap(), "Bob");
    }

    #[test]
    fn bench_iter_with_id() {
        let mut docs = Vec::new();
        for i in 0..1000 {
            docs.push(Ok(create_document_with_id(&format!("John{}", i), &format!("Doe{}", i))));
        }
        let iter = Box::new(docs.into_iter());
        let mut cursor = DocumentCursor::new(iter, ProcessorChain::new());

        let start = std::time::Instant::now();
        let items: Vec<_> = cursor.iter_with_id().collect();
        let elapsed = start.elapsed();

        assert_eq!(items.len(), 1000);
        assert!(items.iter().all(|r| r.is_ok()));
        
        println!("iter_with_id (1000 docs): {:?} ({:.3}µs per doc)", 
                 elapsed, 
                 elapsed.as_micros() as f64 / 1000.0);
    }
}