Skip to main content

LocalDatabase

Struct LocalDatabase 

Source
pub struct LocalDatabase<T> { /* private fields */ }
Expand description

A local database for storing key-value pairs.

The database is stored in a JSON file, which is updated every time a key-value pair is added, updated, or removed. Writes are atomic (write to temporary file, then rename) to prevent corruption.

§Example

use blueprint_store_local_database::LocalDatabase;

let db = LocalDatabase::<u64>::open("data.json")?;

db.set("key", 42)?;
assert_eq!(db.get("key")?, Some(42));

Implementations§

Source§

impl<T> LocalDatabase<T>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Reads a LocalDatabase from the given path.

If the file does not exist, an empty database is created.

§Examples
use blueprint_store_local_database::LocalDatabase;

let db = LocalDatabase::<u64>::open("data.json")?;
assert!(db.is_empty()?);
§Errors
  • The parent of path is not a directory
  • Unable to write to path
Source

pub fn len(&self) -> Result<usize, Error>

Returns the number of key-value pairs in the database.

Source

pub fn is_empty(&self) -> Result<bool, Error>

Checks if the database is empty.

Source

pub fn set(&self, key: &str, value: T) -> Result<(), Error>

Adds or updates a key-value pair in the database.

§Errors
  • Unable to serialize the data
  • Unable to write to disk
Source

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

Retrieves a value associated with the given key.

Source

pub fn remove(&self, key: &str) -> Result<Option<T>, Error>

Removes a key-value pair from the database.

Returns the removed value if the key existed.

Source

pub fn values(&self) -> Result<Vec<T>, Error>

Returns a clone of all values in the database.

Source

pub fn entries(&self) -> Result<Vec<(String, T)>, Error>

Returns a clone of all key-value pairs in the database.

Source

pub fn find<F>(&self, predicate: F) -> Result<Option<T>, Error>
where F: Fn(&T) -> bool,

Finds the first value matching a predicate.

Source

pub fn update<F>(&self, key: &str, f: F) -> Result<bool, Error>
where F: FnOnce(&mut T),

Atomically gets a value by key, applies a mutation, and flushes.

Returns true if the key was found and updated.

Source

pub fn replace(&self, new_data: HashMap<String, T>) -> Result<(), Error>

Replaces the entire database contents with a new map.

Source

pub fn contains_key(&self, key: &str) -> Result<bool, Error>

Checks if a key exists in the database.

Trait Implementations§

Source§

impl<T: Debug> Debug for LocalDatabase<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for LocalDatabase<T>

§

impl<T> RefUnwindSafe for LocalDatabase<T>

§

impl<T> Send for LocalDatabase<T>
where T: Send,

§

impl<T> Sync for LocalDatabase<T>
where T: Send,

§

impl<T> Unpin for LocalDatabase<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for LocalDatabase<T>

§

impl<T> UnwindSafe for LocalDatabase<T>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V