pub struct GrumpyDb { /* private fields */ }use Database (with the _default collection if you only need a single-collection store). GrumpyDb will be removed in v6.
Expand description
The main GrumpyDB storage engine.
Provides CRUD operations on schema-less documents identified by UUID keys. Documents are stored in page-based files with B+Tree indexing. Data pages are cached in a buffer pool for reduced disk I/O.
§Example
use grumpydb::{GrumpyDb, Value};
use uuid::Uuid;
let mut db = GrumpyDb::open(std::path::Path::new("./mydb")).unwrap();
let key = Uuid::new_v4();
db.insert(key, Value::String("hello".into())).unwrap();
assert_eq!(db.get(&key).unwrap(), Some(Value::String("hello".into())));
db.close().unwrap();Implementations§
Source§impl GrumpyDb
impl GrumpyDb
Sourcepub fn open(path: &Path) -> Result<Self>
pub fn open(path: &Path) -> Result<Self>
Opens or creates a database at the given directory path.
Creates data.db for document storage and primary.idx for the B+Tree index.
Data pages are cached in a buffer pool (256 frames = 2 MiB by default).
If the files already exist, they are opened and the engine resumes.
Sourcepub fn open_with_pool_capacity(
path: &Path,
pool_capacity: usize,
) -> Result<Self>
pub fn open_with_pool_capacity( path: &Path, pool_capacity: usize, ) -> Result<Self>
Opens a database with a custom buffer pool capacity (number of frames).
Sourcepub fn insert(&mut self, key: Uuid, value: Value) -> Result<()>
pub fn insert(&mut self, key: Uuid, value: Value) -> Result<()>
Inserts a document with the given UUID key.
Returns DuplicateKey if the key already exists.
Sourcepub fn get(&mut self, key: &Uuid) -> Result<Option<Value>>
pub fn get(&mut self, key: &Uuid) -> Result<Option<Value>>
Retrieves a document by its UUID key.
Returns None if the key does not exist.
Uses the buffer pool — repeated reads of the same page hit the cache.
Sourcepub fn update(&mut self, key: &Uuid, value: Value) -> Result<()>
pub fn update(&mut self, key: &Uuid, value: Value) -> Result<()>
Updates an existing document.
Returns KeyNotFound if the key does not exist.
Sourcepub fn delete(&mut self, key: &Uuid) -> Result<()>
pub fn delete(&mut self, key: &Uuid) -> Result<()>
Deletes a document by its UUID key.
Returns KeyNotFound if the key does not exist.
Sourcepub fn scan(
&mut self,
range: impl RangeBounds<Uuid>,
) -> Result<Vec<(Uuid, Value)>>
pub fn scan( &mut self, range: impl RangeBounds<Uuid>, ) -> Result<Vec<(Uuid, Value)>>
Scans documents in a UUID key range.
Returns all documents whose keys fall within the given range, sorted by key.
Sourcepub fn document_count(&self) -> u64
pub fn document_count(&self) -> u64
Returns the number of documents in the database.
Sourcepub fn compact(&mut self) -> Result<CompactResult>
pub fn compact(&mut self) -> Result<CompactResult>
Compacts the database: defragments data pages and rebuilds the B+Tree index.
Sourcepub fn pool_stats(&self) -> (u64, u64, usize, usize)
pub fn pool_stats(&self) -> (u64, u64, usize, usize)
Returns buffer pool statistics: (read_count, write_count, cached_count, capacity).
Sourcepub fn migrate_to_database(
&mut self,
target: &mut Database,
collection: &str,
) -> Result<u64>
pub fn migrate_to_database( &mut self, target: &mut Database, collection: &str, ) -> Result<u64>
Migrates all documents from this v1 GrumpyDb into a v2 Database collection.
Reads every document via scan(..) and inserts them into the target
database/collection. The original v1 data is not modified.
§Arguments
target— The targetDatabaseto insert documents intocollection— The collection name within that database
§Returns
The number of documents migrated.