redb_extras/roaring/mod.rs
1//! Roaring bitmap handling module.
2//!
3//! This module provides roaring-specific value handling including encoding,
4//! decoding, and operations that require bitmap knowledge.
5
6use crate::Result;
7use roaring::RoaringTreemap;
8use std::fmt;
9
10/// Errors specific to the roaring layer.
11/// These are concerned with bitmap operations and value-specific semantics.
12#[derive(Debug)]
13pub enum RoaringError {
14 /// Failed to serialize/deserialize RoaringTreemap
15 SerializationFailed(String),
16
17 /// Compaction operation failed
18 CompactionFailed(String),
19
20 /// Invalid roaring bitmap data
21 InvalidBitmap(String),
22
23 /// Size query failed
24 SizeQueryFailed(String),
25}
26
27impl fmt::Display for RoaringError {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 RoaringError::SerializationFailed(msg) => {
31 write!(f, "Roaring serialization failed: {}", msg)
32 }
33 RoaringError::CompactionFailed(msg) => {
34 write!(f, "Compaction failed: {}", msg)
35 }
36 RoaringError::InvalidBitmap(msg) => {
37 write!(f, "Invalid roaring bitmap: {}", msg)
38 }
39 RoaringError::SizeQueryFailed(msg) => {
40 write!(f, "Size query failed: {}", msg)
41 }
42 }
43 }
44}
45
46impl std::error::Error for RoaringError {
47 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48 None
49 }
50}
51
52pub trait RoaringValueReadOnlyTable<'txn, K> {
53 /// Gets complete roaring bitmap for the given base key.
54 ///
55 /// # Arguments
56 /// * `base_key` - The base key to retrieve (any type that implements redb::Key)
57 ///
58 /// # Returns
59 /// The complete RoaringTreemap or empty if not found
60 fn get_bitmap(&self, base_key: K) -> Result<RoaringTreemap>;
61
62 fn iter_members(&self, base_key: K) -> Result<impl Iterator<Item = u64> + '_> {
63 // Get complete bitmap and return iterator
64 let bitmap = self.get_bitmap(base_key)?;
65 Ok(bitmap.into_iter())
66 }
67}
68
69pub trait RoaringValueTable<'txn, K> {
70 /// Inserts a single member ID into the bitmap for the given base key.
71 ///
72 /// This method handles shard selection, head segment discovery, segment rolling,
73 /// and bitmap serialization automatically.
74 ///
75 /// # Arguments
76 /// * `base_key` - The base key to modify (any type that implements redb::Key)
77 /// * `member` - The member to insert
78 ///
79 /// # Returns
80 /// Result indicating success or failure
81 fn insert_member(&mut self, base_key: K, member: u64) -> Result<()>;
82}
83
84mod facade;
85mod value;
86
87// Re-export main types for public API
88pub use value::RoaringValue;