reddb-io-engine 1.28.1

Shared RedDB query execution and storage primitives for native and WebAssembly runtimes.
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
//! Fallible, ordered reads over a published collection and a frozen write overlay.
//!
//! Hosts own publication: a base tree must remain immutable while a view uses it.
//! No reader hydrates the complete collection or treats failed I/O as a missing row.

use super::btree::{BTree, BTreeCursor, BTreeError};
use super::entity::{EntityId, UnifiedEntity};
use super::error::{StorageIntegrityError, StoreError};
use super::metadata::Metadata;
use super::metadata_codec::deserialize_entity_record;
use super::page_store::PageStore;
use std::collections::BTreeMap;
use std::sync::Arc;

pub struct EncodedRecord {
    pub id: EntityId,
    pub bytes: Vec<u8>,
}

impl EncodedRecord {
    /// The allowance covers encoded input plus retained decoded allocations.
    /// A host must reserve the same allowance in its global scratch pool.
    pub fn decode_with_budget(
        &self,
        version: u32,
        limit_bytes: usize,
    ) -> Result<(UnifiedEntity, Option<Metadata>), StoreError> {
        let mut budget = reddb_file::native_value::DecodeBudget::new(limit_bytes);
        budget
            .charge(self.bytes.len())
            .map_err(super::metadata_codec::decode_error)?;
        let record = super::metadata_codec::deserialize_entity_record_with_budget(
            &self.bytes,
            version,
            &mut budget,
        )?;
        self.validate_identity(record)
    }

    /// Typed decoding is separate from byte admission; hosts must also reserve
    /// space for decoded values in their shared query memory account.
    pub fn decode(&self, version: u32) -> Result<(UnifiedEntity, Option<Metadata>), StoreError> {
        let record =
            deserialize_entity_record(&self.bytes, version).map_err(StoreError::Serialization)?;
        self.validate_identity(record)
    }

    fn validate_identity(
        &self,
        record: (UnifiedEntity, Option<Metadata>),
    ) -> Result<(UnifiedEntity, Option<Metadata>), StoreError> {
        if record.0.id != self.id {
            return Err(StoreError::StorageIntegrity(StorageIntegrityError::new(
                "collection-record",
                self.id.raw().to_string(),
                None,
                "entity identity differs from its physical key",
            )));
        }
        Ok(record)
    }
}

pub trait CollectionCursor {
    fn peek_id(&mut self) -> Result<Option<EntityId>, StoreError>;
    fn next_record(&mut self, limit_bytes: usize) -> Result<Option<EncodedRecord>, StoreError>;
    fn skip_record(&mut self) -> Result<bool, StoreError>;
}

pub trait CollectionRead {
    fn get_record(
        &self,
        id: EntityId,
        limit_bytes: usize,
    ) -> Result<Option<EncodedRecord>, StoreError>;
    fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError>;
    /// The cursor retains its page-retirement fence independently of this handle.
    fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError>;
    /// Candidate index for this exact immutable base, or None for an unindexed
    /// legacy/fixture source. Callers still verify each decoded logical key.
    fn key_cursor(&self, _key: &str) -> Result<Option<Box<dyn CollectionCursor + '_>>, StoreError> {
        Ok(None)
    }
    fn owned_key_cursor(
        &self,
        _key: &str,
    ) -> Result<Option<Box<dyn CollectionCursor>>, StoreError> {
        Ok(None)
    }
}

/// A published generation; the host retains its existing page-retirement fence.
pub struct PagedCollection<P: PageStore> {
    tree: Arc<BTree<P>>,
    keys: Option<Arc<BTree<P>>>,
}

impl<P: PageStore> PagedCollection<P> {
    pub fn new(tree: Arc<BTree<P>>) -> Self {
        Self { tree, keys: None }
    }

    /// Hosts publish both roots atomically and retain their retirement fences.
    pub fn with_key_index(tree: Arc<BTree<P>>, keys: Arc<BTree<P>>) -> Self {
        Self {
            tree,
            keys: Some(keys),
        }
    }

    pub fn scan(&self) -> Result<PagedCursor<P>, StoreError> {
        Ok(PagedCursor {
            cursor: self
                .tree
                .cursor_first()
                .map_err(read_error)?
                .with_immutable_leaves(),
        })
    }
}

impl<P: PageStore + 'static> CollectionRead for PagedCollection<P> {
    fn get_record(
        &self,
        id: EntityId,
        limit_bytes: usize,
    ) -> Result<Option<EncodedRecord>, StoreError> {
        self.tree
            .get_with_budget(&id.raw().to_be_bytes(), limit_bytes)
            .map(|record| record.map(|bytes| EncodedRecord { id, bytes }))
            .map_err(read_error)
    }

    fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
        Ok(Box::new(self.scan()?))
    }

    fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
        Ok(Box::new(self.scan()?))
    }

    fn key_cursor(&self, key: &str) -> Result<Option<Box<dyn CollectionCursor + '_>>, StoreError> {
        let Some(keys) = &self.keys else {
            return Ok(None);
        };
        Ok(Some(Box::new(super::collection_key_read::KeyCursor::new(
            Arc::clone(&self.tree),
            keys,
            key,
        )?)))
    }

    fn owned_key_cursor(&self, key: &str) -> Result<Option<Box<dyn CollectionCursor>>, StoreError> {
        let Some(keys) = &self.keys else {
            return Ok(None);
        };
        Ok(Some(Box::new(super::collection_key_read::KeyCursor::new(
            Arc::clone(&self.tree),
            keys,
            key,
        )?)))
    }
}

pub struct PagedCursor<P: PageStore> {
    cursor: BTreeCursor<P>,
}

impl<P: PageStore> CollectionCursor for PagedCursor<P> {
    fn peek_id(&mut self) -> Result<Option<EntityId>, StoreError> {
        self.cursor
            .peek_key()
            .map_err(read_error)?
            .map(|key| physical_id(&key))
            .transpose()
    }

    fn next_record(&mut self, limit_bytes: usize) -> Result<Option<EncodedRecord>, StoreError> {
        self.cursor
            .next_with_budget(limit_bytes)
            .map_err(read_error)?
            .map(|(key, bytes)| {
                Ok(EncodedRecord {
                    id: physical_id(&key)?,
                    bytes,
                })
            })
            .transpose()
    }

    fn skip_record(&mut self) -> Result<bool, StoreError> {
        self.cursor.skip_next().map_err(read_error)
    }
}

/// `None` is a physical removal. MVCC delete stamps remain encoded records.
/// A host freezes this map by ownership transfer, not by cloning its payloads.
pub type FrozenOverlay = BTreeMap<EntityId, Option<Arc<[u8]>>>;

pub struct MergedCollection<'a> {
    base: &'a dyn CollectionRead,
    overlay: Arc<FrozenOverlay>,
}

impl<'a> MergedCollection<'a> {
    pub fn new(base: &'a dyn CollectionRead, overlay: Arc<FrozenOverlay>) -> Self {
        Self { base, overlay }
    }
}

impl CollectionRead for MergedCollection<'_> {
    fn get_record(
        &self,
        id: EntityId,
        limit_bytes: usize,
    ) -> Result<Option<EncodedRecord>, StoreError> {
        match self.overlay.get(&id) {
            Some(Some(bytes)) => {
                check_budget(bytes.len(), limit_bytes)?;
                Ok(Some(EncodedRecord {
                    id,
                    bytes: bytes.to_vec(),
                }))
            }
            Some(None) => Ok(None),
            None => self.base.get_record(id, limit_bytes),
        }
    }

    fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
        Ok(Box::new(MergedCursor {
            base: self.base.cursor()?,
            overlay: self.overlay.iter().peekable(),
        }))
    }

    fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
        let next = self
            .overlay
            .first_key_value()
            .map(|(id, bytes)| (*id, bytes.clone()));
        Ok(Box::new(OwnedMergedCursor {
            base: self.base.owned_cursor()?,
            overlay: Arc::clone(&self.overlay),
            next,
        }))
    }
    fn owned_key_cursor(&self, key: &str) -> Result<Option<Box<dyn CollectionCursor>>, StoreError> {
        let Some(base) = self.base.owned_key_cursor(key)? else {
            return Ok(None);
        };
        let next = self
            .overlay
            .first_key_value()
            .map(|(id, bytes)| (*id, bytes.clone()));
        Ok(Some(Box::new(OwnedMergedCursor {
            base,
            overlay: Arc::clone(&self.overlay),
            next,
        })))
    }
}

struct MergedCursor<'a> {
    base: Box<dyn CollectionCursor + 'a>,
    overlay:
        std::iter::Peekable<std::collections::btree_map::Iter<'a, EntityId, Option<Arc<[u8]>>>>,
}

impl CollectionCursor for MergedCursor<'_> {
    fn peek_id(&mut self) -> Result<Option<EntityId>, StoreError> {
        loop {
            let base = self.base.peek_id()?;
            let Some((id, bytes)) = self.overlay.peek() else {
                return Ok(base);
            };
            if base.is_some_and(|base| base < **id) {
                return Ok(base);
            }
            if bytes.is_some() {
                return Ok(Some(**id));
            }
            if base == Some(**id) {
                self.base.skip_record()?;
            }
            self.overlay.next();
        }
    }

    fn next_record(&mut self, limit_bytes: usize) -> Result<Option<EncodedRecord>, StoreError> {
        let Some(id) = self.peek_id()? else {
            return Ok(None);
        };
        if let Some((changed, Some(bytes))) = self.overlay.peek() {
            if **changed == id {
                check_budget(bytes.len(), limit_bytes)?;
                if self.base.peek_id()? == Some(id) {
                    self.base.skip_record()?;
                }
                let record = EncodedRecord {
                    id,
                    bytes: bytes.to_vec(),
                };
                self.overlay.next();
                return Ok(Some(record));
            }
        }
        self.base.next_record(limit_bytes)
    }

    fn skip_record(&mut self) -> Result<bool, StoreError> {
        let Some(id) = self.peek_id()? else {
            return Ok(false);
        };
        if self
            .overlay
            .peek()
            .is_some_and(|(changed, _)| **changed == id)
        {
            if self.base.peek_id()? == Some(id) {
                self.base.skip_record()?;
            }
            self.overlay.next();
            return Ok(true);
        }
        self.base.skip_record()
    }
}

struct OwnedMergedCursor {
    base: Box<dyn CollectionCursor>,
    overlay: Arc<FrozenOverlay>,
    next: Option<(EntityId, Option<Arc<[u8]>>)>,
}

impl OwnedMergedCursor {
    fn advance(&mut self) {
        let Some((id, _)) = self.next.take() else {
            return;
        };
        self.next = self
            .overlay
            .range((std::ops::Bound::Excluded(id), std::ops::Bound::Unbounded))
            .next()
            .map(|(id, bytes)| (*id, bytes.clone()));
    }
}

impl CollectionCursor for OwnedMergedCursor {
    fn peek_id(&mut self) -> Result<Option<EntityId>, StoreError> {
        loop {
            let base = self.base.peek_id()?;
            let Some((id, bytes)) = &self.next else {
                return Ok(base);
            };
            if base.is_some_and(|base| base < *id) {
                return Ok(base);
            }
            if bytes.is_some() {
                return Ok(Some(*id));
            }
            if base == Some(*id) {
                self.base.skip_record()?;
            }
            self.advance();
        }
    }
    fn next_record(&mut self, limit_bytes: usize) -> Result<Option<EncodedRecord>, StoreError> {
        let Some(id) = self.peek_id()? else {
            return Ok(None);
        };
        if let Some((changed, Some(bytes))) = &self.next {
            if *changed == id {
                check_budget(bytes.len(), limit_bytes)?;
                if self.base.peek_id()? == Some(id) {
                    self.base.skip_record()?;
                }
                let record = EncodedRecord {
                    id,
                    bytes: bytes.to_vec(),
                };
                self.advance();
                return Ok(Some(record));
            }
        }
        self.base.next_record(limit_bytes)
    }
    fn skip_record(&mut self) -> Result<bool, StoreError> {
        let Some(id) = self.peek_id()? else {
            return Ok(false);
        };
        if self
            .next
            .as_ref()
            .is_some_and(|(changed, _)| *changed == id)
        {
            if self.base.peek_id()? == Some(id) {
                self.base.skip_record()?;
            }
            self.advance();
            return Ok(true);
        }
        self.base.skip_record()
    }
}

fn physical_id(key: &[u8]) -> Result<EntityId, StoreError> {
    let bytes: [u8; 8] = key.try_into().map_err(|_| {
        StoreError::StorageIntegrity(StorageIntegrityError::new(
            "collection-key",
            "unknown",
            None,
            "physical entity key must contain eight bytes",
        ))
    })?;
    Ok(EntityId::new(u64::from_be_bytes(bytes)))
}

pub(super) fn read_error(error: BTreeError) -> StoreError {
    match error {
        BTreeError::ReadLimitExceeded {
            required_bytes,
            limit_bytes,
        } => StoreError::ReadLimitExceeded {
            required_bytes,
            limit_bytes,
        },
        BTreeError::Pager(message) => StoreError::Io(std::io::Error::other(message)),
        other => StoreError::StorageIntegrity(StorageIntegrityError::new(
            "collection-tree",
            "unknown",
            None,
            other.to_string(),
        )),
    }
}

fn check_budget(required_bytes: usize, limit_bytes: usize) -> Result<(), StoreError> {
    if required_bytes > limit_bytes {
        return Err(StoreError::ReadLimitExceeded {
            required_bytes,
            limit_bytes,
        });
    }
    Ok(())
}