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
impl NoDb
Sourcepub fn new<P: AsRef<Path>>(
db_path: P,
policy: DumpPolicy,
ser_method: SerializationMethod,
) -> Self
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);
Sourcepub fn load<P: AsRef<Path>>(
db_path: P,
policy: DumpPolicy,
ser_method: SerializationMethod,
) -> Result<Self>
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();
Sourcepub fn dump(&mut self) -> Result<()>
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.
Sourcepub fn set<K: AsRef<str>, V: Serialize>(
&mut self,
key: K,
value: V,
) -> Result<()>
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).
Sourcepub fn get<K: AsRef<str>, V: DeserializeOwned>(&self, key: K) -> Option<V>
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.
Sourcepub fn exists<K: AsRef<str>>(&self, key: K) -> bool
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.
Sourcepub fn get_all(&self) -> Vec<String>
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.
Sourcepub fn total_keys(&self) -> usize
pub fn total_keys(&self) -> usize
Get the total number of keys in the DB.
Sourcepub fn rem<K: AsRef<str>>(&mut self, key: K) -> Result<bool>
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).
Sourcepub fn lcreate<N: AsRef<str>>(&mut self, name: N) -> Result<NoDbExt<'_>>
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.
Sourcepub fn ladd<K: AsRef<str>, V: Serialize>(
&mut self,
name: K,
value: &V,
) -> Option<NoDbExt<'_>>
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).
Sourcepub fn lextend<'a, N: AsRef<str>, V, I>(
&mut self,
name: N,
seq: I,
) -> Option<NoDbExt<'_>>
pub fn lextend<'a, N: AsRef<str>, V, I>( &mut self, name: N, seq: I, ) -> Option<NoDbExt<'_>>
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).
Sourcepub fn lget<V: DeserializeOwned, N: AsRef<str>>(
&self,
name: N,
pos: usize,
) -> Option<V>
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.
Sourcepub fn llen<N: AsRef<str>>(&self, name: N) -> usize
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.
Sourcepub fn lrem_list<N: AsRef<str>>(&mut self, name: N) -> Result<usize>
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).
Sourcepub fn lpop<V: DeserializeOwned, N: AsRef<str>>(
&mut self,
name: N,
pos: usize,
) -> Option<V>
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.
Sourcepub fn lrem_value<V: Serialize, N: AsRef<str>>(
&mut self,
name: N,
value: &V,
) -> Result<bool>
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.
Trait Implementations§
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> 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
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
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
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
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
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.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
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.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
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.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
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.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
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.