pub struct YourDb { /* private fields */ }Expand description
Small file-backed key/value JSON DB with optional secondary indexes.
- Page 0 is reserved for the index (key -> page_id) serialized as JSON.
- Pages start at 1 for records.
- Each record page layout: [u32 little-endian length][json bytes][padding…]
Improvements vs original:
- safer page length checks (no panics),
- selective secondary indexing (only index fields present in
indexed_fieldsset; empty set = index all), - helper methods: list_keys, count, compact, etc.
Implementations§
Source§impl YourDb
impl YourDb
Sourcepub fn open(path: &str) -> Result<Self, DbError>
pub fn open(path: &str) -> Result<Self, DbError>
Open existing DB or create a new one if path missing.
Sourcepub fn put(&mut self, key: &str, value: &Value) -> Result<(), DbError>
pub fn put(&mut self, key: &str, value: &Value) -> Result<(), DbError>
Put key -> JSON value (replaces existing if present).
Sourcepub fn flush(&mut self) -> Result<(), DbError>
pub fn flush(&mut self) -> Result<(), DbError>
Flush: write index to page 0 as length-prefixed JSON.
Sourcepub fn delete(&mut self, key: &str) -> Result<(), DbError>
pub fn delete(&mut self, key: &str) -> Result<(), DbError>
Delete a key and remove from secondary indexes.
Sourcepub fn get_field(
&self,
key: &str,
field: &str,
) -> Result<Option<Value>, DbError>
pub fn get_field( &self, key: &str, field: &str, ) -> Result<Option<Value>, DbError>
Return a specific field from the JSON stored at key.
Sourcepub fn filter<F>(&self, predicate: F) -> Result<Vec<(String, Value)>, DbError>
pub fn filter<F>(&self, predicate: F) -> Result<Vec<(String, Value)>, DbError>
Filter all records with a predicate function. Be careful: this iterates records.
Sourcepub fn query(
&self,
field: &str,
value: impl Into<Value>,
) -> Result<Vec<String>, DbError>
pub fn query( &self, field: &str, value: impl Into<Value>, ) -> Result<Vec<String>, DbError>
Query by field = value (via secondary index)
Sourcepub fn query_page(
&self,
field: &str,
value: impl Into<Value>,
limit: usize,
offset: usize,
) -> Result<Vec<String>, DbError>
pub fn query_page( &self, field: &str, value: impl Into<Value>, limit: usize, offset: usize, ) -> Result<Vec<String>, DbError>
Query page (limit/offset applied)
Sourcepub fn export_query(
&self,
field: &str,
value: impl Into<Value>,
path: &str,
) -> Result<(), DbError>
pub fn export_query( &self, field: &str, value: impl Into<Value>, path: &str, ) -> Result<(), DbError>
Export query results (key -> value) to a JSON file
Sourcepub fn export_to_file(&self, path: &str) -> Result<(), DbError>
pub fn export_to_file(&self, path: &str) -> Result<(), DbError>
Export entire DB to a JSON file.
Sourcepub fn range_query(
&self,
field: &str,
min: Value,
max: Value,
) -> Result<Vec<String>, DbError>
pub fn range_query( &self, field: &str, min: Value, max: Value, ) -> Result<Vec<String>, DbError>
Range query (numeric) for a field between min..=max
Sourcepub fn update_field(
&mut self,
key: &str,
field: &str,
new_value: Value,
) -> Result<(), DbError>
pub fn update_field( &mut self, key: &str, field: &str, new_value: Value, ) -> Result<(), DbError>
Update a single field on a record (updates secondary indexes accordingly).
Auto Trait Implementations§
impl Freeze for YourDb
impl RefUnwindSafe for YourDb
impl Send for YourDb
impl Sync for YourDb
impl Unpin for YourDb
impl UnwindSafe for YourDb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more