Struct NoDb

Source
pub struct NoDb {
    pub map: HashMap<String, Vec<u8>>,
    pub list_map: HashMap<String, Vec<Vec<u8>>>,
    pub path: PathBuf,
    pub policy: DumpPolicy,
    pub last_dump: Instant,
    /* private fields */
}
Expand description

A struct that represents a NoDb object.

Fields§

§map: HashMap<String, Vec<u8>>§list_map: HashMap<String, Vec<Vec<u8>>>§path: PathBuf§policy: DumpPolicy§last_dump: Instant

Implementations§

Source§

impl NoDb

Source

pub fn new<P: AsRef<Path>>( db_path: P, policy: DumpPolicy, ser_method: SerializationMethod, ) -> Self

Constructs a new NoDb instance.

§Examples
use nodb::{NoDb, DumpPolicy, SerializationMethod};

let mut db = NoDb::new("example.db", DumpPolicy::AutoDump, SerializationMethod::Json);
Source

pub fn load<P: AsRef<Path>>( db_path: P, policy: DumpPolicy, ser_method: SerializationMethod, ) -> Result<Self>

Loads a NoDb instance from a file.

This method tries to load a DB from a file. Upon success an instance of Ok(NoDb) is returned, otherwise an anyhow::Error object is returned.

§Examples
use nodb::{NoDb, DumpPolicy, SerializationMethod};
let nodb = NoDb::load("example.db", DumpPolicy::Auto, SerializationMethod::Json).unwrap();
Source

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

Dump the data to the file.

Calling this method is necessary only if the DB is loaded or created with a dump policy other than DumpPolicy::Auto, otherwise the data is dumped to the file upon every change unless the dump policy is DumpPolicy::Never.

This method returns Ok(()) if dump is successful, Or an anyhow::Error otherwise.

Source

pub fn set<K: AsRef<str>, V: Serialize>( &mut self, key: K, value: V, ) -> Result<()>

Set a key-value pair.

The key has to be a string but the value can be of any type that is serializable. That includes all primitive types, vectors, tuples, enums and every struct that has the #[derive(Serialize, Deserialize) attribute.

This method returns Ok(()) if set is successful, Or an anyhow::Error otherwise. An error is not likely to happen but may occur mostly in cases where this action triggers a DB dump (which is decided according to the dump policy).

Source

pub fn get<K: AsRef<str>, V: DeserializeOwned>(&self, key: K) -> Option<V>

Get a value of a key.

The key is always a string but the value can be of any type. It’s the developer’s responsibility to know the value type and give it while calling this method. If the key doesn’t exist or if the type is wrong, None will be returned. Otherwise Some(V) will be returned.

Since the values are stored in a serialized way the returned object is not a reference to the value stored in a DB but actually a new instance of it.

Source

pub fn exists<K: AsRef<str>>(&self, key: K) -> bool

Check if a key exists.

This method returns true if the key exists and false otherwise.

Source

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

Get a vector of all the keys in the DB.

The keys returned in the vector are not references to the actual key string objects but rather a clone of them.

Source

pub fn total_keys(&self) -> usize

Get the total number of keys in the DB.

Source

pub fn rem<K: AsRef<str>>(&mut self, key: K) -> Result<bool>

Remove a key-value pair or a list from the DB.

This methods returns Ok(true) if the key was found in the DB or Ok(false) if it wasn’t found. It may also return anyhow::Error if key was found but removal failed. Removal error is not likely to happen but may occur mostly in cases where this action triggers a DB dump (which is decided according to the dump policy).

Source

pub fn lcreate<N: AsRef<str>>(&mut self, name: N) -> Result<NoDbExt<'_>>

Create a new list.

This method just creates a new list, it doesn’t add any elements to it. If another list or value is already set under this key, they will be overridden, meaning the new list will override the old list or value.

Upon success, the method returns an object of type NoDbExt that enables to add items to the newly created list. Alternatively you can use ladd() or lextend() to add items to the list.

Source

pub fn lexists<N: AsRef<str>>(&self, name: N) -> bool

Check if a list exists.

This method returns true if the list name exists and false otherwise. The difference between this method and exists() is that this methods checks only for lists with that name (key) and exists() checks for both values and lists.

Source

pub fn ladd<K: AsRef<str>, V: Serialize>( &mut self, name: K, value: &V, ) -> Option<NoDbExt<'_>>

Add a single item to an existing list.

As mentioned before, the lists are heterogeneous, meaning a single list can contain items of different types. That means that the item can be of any type that is serializable. That includes all primitive types, vectors, tuples and every struct that has the #[derive(Serialize, Deserialize) attribute.

If the item was added successfully the method returns Some(NoDbExt) which enables to add more items to the list. Alternatively the method returns None if the list isn’t found in the DB or if a failure happened while extending the list. Failures are not likely to happen but may occur mostly in cases where this action triggers a DB dump (which is decided according to the dump policy).

Source

pub fn lextend<'a, N: AsRef<str>, V, I>( &mut self, name: N, seq: I, ) -> Option<NoDbExt<'_>>
where V: 'a + Serialize, I: IntoIterator<Item = &'a V>,

Add multiple items to an existing list.

As mentioned before, the lists are heterogeneous, meaning a single list can contain items of different types. That means that the item can be of any type that is serializable. That includes all primitive types, vectors, tuples and every struct that has the #[derive(Serialize, Deserialize) attribute. This method adds multiple items to the list, but since they’re in a vector that means all of them are of the same type. Of course it doesn’t mean that the list cannot contain items of other types as well, as you can see in the example below.

If all items were added successfully the method returns Some(NoDbExt) which enables to add more items to the list. Alternatively the method returns None if the list isn’t found in the DB or if a failure happened while extending the list. Failures are not likely to happen but may occur mostly in cases where this action triggers a DB dump (which is decided according to the dump policy).

Source

pub fn lget<V: DeserializeOwned, N: AsRef<str>>( &self, name: N, pos: usize, ) -> Option<V>

Get an item of of a certain list in a certain position.

This method takes a list name and a position inside the list and retrieves the item in this position. It’s the developer’s responsibility to know what is the correct type of the item and give it while calling this method. Since the item in the lists are stored in a serialized way the returned object is not a reference to the item stored in a DB but actually a new instance of it. If the list is not found in the DB or the given position is out of bounds of the list None will be returned. Otherwise Some(V) will be returned.

Source

pub fn llen<N: AsRef<str>>(&self, name: N) -> usize

Get the length of a list.

If the list is empty or if it doesn’t exist the value of 0 is returned.

Source

pub fn lrem_list<N: AsRef<str>>(&mut self, name: N) -> Result<usize>

Remove a list.

This method is somewhat similar to rem() but with 2 small differences:

  • This method only removes lists and not key-value pairs
  • The return value of this method is the number of items that were in the list that was removed. If the list doesn’t exist a value of zero (0) is returned. In case of a failure an anyhow::Error is returned. Failures are not likely to happen but may occur mostly in cases where this action triggers a DB dump (which is decided according to the dump policy).
Source

pub fn lpop<V: DeserializeOwned, N: AsRef<str>>( &mut self, name: N, pos: usize, ) -> Option<V>

Pop an item out of a list.

This method takes a list name and a position inside the list, removes the item in this position and returns it to the user. It’s the user’s responsibility to know what is the correct type of the item and give it while calling this method. Since the item in the lists are stored in a serialized way the returned object is not a reference to the item stored in a DB but actually a new instance of it.

If the list is not found in the DB or the given position is out of bounds no item will be removed and None will be returned. None may also be returned if removing the item fails, which may happen mostly in cases where this action triggers a DB dump (which is decided according to the dump policy). Otherwise the item will be removed and Some(V) will be returned.

This method is very similar to lrem_value(), the only difference is that this methods returns the value and lrem_value() returns only an indication whether the item was removed or not.

Source

pub fn lrem_value<V: Serialize, N: AsRef<str>>( &mut self, name: N, value: &V, ) -> Result<bool>

Remove an item out of a list.

This method takes a list name and a reference to a value, removes the first instance of the value if it exists in the list, and returns an indication whether the item was removed or not.

If the list is not found in the DB or the given value isn’t found in the list, no item will be removed and Ok(false) will be returned. If removing the item fails, which may happen mostly in cases where this action triggers a DB dump (which is decided according to the dump policy), an anyhow::Error is returned. Otherwise the item will be removed and Ok(true) will be returned.

This method is very similar to lpop(), the only difference is that this methods returns an indication and lpop() returns the actual item that was removed.

Source

pub fn iter(&self) -> NoDbIter<'_>

Return an iterator over the keys and values in the DB.

Source

pub fn liter<N: AsRef<str>>(&self, name: N) -> NoDbListIter<'_>

Return an iterator over the items in certain list.

Trait Implementations§

Source§

impl Drop for NoDb

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for NoDb

§

impl RefUnwindSafe for NoDb

§

impl Send for NoDb

§

impl Sync for NoDb

§

impl Unpin for NoDb

§

impl UnwindSafe for NoDb

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> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. 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> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
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