YourDb

Struct YourDb 

Source
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_fields set; empty set = index all),
  • helper methods: list_keys, count, compact, etc.

Implementations§

Source§

impl YourDb

Source

pub fn open(path: &str) -> Result<Self, DbError>

Open existing DB or create a new one if path missing.

Examples found in repository?
examples/basic.rs (line 5)
4fn main() -> Result<(), DbError> {
5    let mut db = YourDb::open("test.db")?;
6    db.put("hello", &json!({ "msg": "world" }))?;
7    db.flush()?;
8    let val = db.get("hello")?;
9    println!("{:?}", val);
10    Ok(())
11}
Source

pub fn put(&mut self, key: &str, value: &Value) -> Result<(), DbError>

Put key -> JSON value (replaces existing if present).

Examples found in repository?
examples/basic.rs (line 6)
4fn main() -> Result<(), DbError> {
5    let mut db = YourDb::open("test.db")?;
6    db.put("hello", &json!({ "msg": "world" }))?;
7    db.flush()?;
8    let val = db.get("hello")?;
9    println!("{:?}", val);
10    Ok(())
11}
Source

pub fn get(&self, key: &str) -> Result<Option<Value>, DbError>

Get full JSON value for a key.

Examples found in repository?
examples/basic.rs (line 8)
4fn main() -> Result<(), DbError> {
5    let mut db = YourDb::open("test.db")?;
6    db.put("hello", &json!({ "msg": "world" }))?;
7    db.flush()?;
8    let val = db.get("hello")?;
9    println!("{:?}", val);
10    Ok(())
11}
Source

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

Flush: write index to page 0 as length-prefixed JSON.

Examples found in repository?
examples/basic.rs (line 7)
4fn main() -> Result<(), DbError> {
5    let mut db = YourDb::open("test.db")?;
6    db.put("hello", &json!({ "msg": "world" }))?;
7    db.flush()?;
8    let val = db.get("hello")?;
9    println!("{:?}", val);
10    Ok(())
11}
Source

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

Delete a key and remove from secondary indexes.

Source

pub fn get_field( &self, key: &str, field: &str, ) -> Result<Option<Value>, DbError>

Return a specific field from the JSON stored at key.

Source

pub fn filter<F>(&self, predicate: F) -> Result<Vec<(String, Value)>, DbError>
where F: Fn(&Value) -> bool,

Filter all records with a predicate function. Be careful: this iterates records.

Source

pub fn query( &self, field: &str, value: impl Into<Value>, ) -> Result<Vec<String>, DbError>

Query by field = value (via secondary index)

Source

pub fn query_page( &self, field: &str, value: impl Into<Value>, limit: usize, offset: usize, ) -> Result<Vec<String>, DbError>

Query page (limit/offset applied)

Source

pub fn export_query( &self, field: &str, value: impl Into<Value>, path: &str, ) -> Result<(), DbError>

Export query results (key -> value) to a JSON file

Source

pub fn export_to_file(&self, path: &str) -> Result<(), DbError>

Export entire DB to a JSON file.

Source

pub fn show_all(&self) -> Result<(), DbError>

Print all keys->json to stdout (for debugging)

Source

pub fn range_query( &self, field: &str, min: Value, max: Value, ) -> Result<Vec<String>, DbError>

Range query (numeric) for a field between min..=max

Source

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

Source

pub fn search_contains( &self, field: &str, substring: &str, ) -> Result<Vec<String>, DbError>

Search text fields for substring (only works for string-valued indexed fields).

Source

pub fn list_keys(&self) -> Vec<String>

Return all keys (lightweight)

Source

pub fn count(&self) -> usize

Count records

Source

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

Compact DB: rewrite record pages sequentially, rebuild index and secondary indexes. Note: this rewrites pages starting at 1 and will update page ids 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> 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.