Skip to main content

GrumpyDb

Struct GrumpyDb 

Source
pub struct GrumpyDb { /* private fields */ }
👎Deprecated since 5.0.0:

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

Source

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.

Source

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).

Source

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.

Source

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.

Source

pub fn update(&mut self, key: &Uuid, value: Value) -> Result<()>

Updates an existing document.

Returns KeyNotFound if the key does not exist.

Source

pub fn delete(&mut self, key: &Uuid) -> Result<()>

Deletes a document by its UUID key.

Returns KeyNotFound if the key does not exist.

Source

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.

Source

pub fn flush(&mut self) -> Result<()>

Flushes all data to disk and writes a WAL checkpoint.

Source

pub fn close(self) -> Result<()>

Closes the database, flushing all pending data.

Source

pub fn document_count(&self) -> u64

Returns the number of documents in the database.

Source

pub fn compact(&mut self) -> Result<CompactResult>

Compacts the database: defragments data pages and rebuilds the B+Tree index.

Source

pub fn pool_stats(&self) -> (u64, u64, usize, usize)

Returns buffer pool statistics: (read_count, write_count, cached_count, capacity).

Source

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 target Database to insert documents into
  • collection — The collection name within that database
§Returns

The number of documents migrated.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.