Struct Db

Source
pub struct Db {
    pub config: Config,
    pub indexes: Index,
    pub stats: RwLock<StatRecord>,
    pub values: RwLock<HashMap<Uuid, ValueRecord>>,
    /* private fields */
}

Fields§

§config: Config§indexes: Index§stats: RwLock<StatRecord>§values: RwLock<HashMap<Uuid, ValueRecord>>

Implementations§

Source§

impl Db

Source

pub fn new(config: Config) -> Self

Creates new DB.

§Examples
use alex_db_lib::{config::Config, db::Db};

let config = Config::default();
let db = Db::new(config);

assert_eq!(0, db.values.read().unwrap().len());
Source

pub fn api_key_exists( &self, api_key: Uuid, ) -> Result<bool, Box<dyn Error + Send + Sync>>

Source

pub fn api_key_init(&self) -> Result<Option<Uuid>, Box<dyn Error + Send + Sync>>

Source

pub fn gc(&self) -> Result<(), Box<dyn Error + Send + Sync>>

Source

pub fn get_stats(&self) -> Result<StatRecord, Box<dyn Error + Send + Sync>>

Source

pub fn restore(&mut self) -> Result<(), Box<dyn Error + Send + Sync>>

Source

pub fn save(&self) -> Result<(), Box<dyn Error + Send + Sync>>

Source

pub fn list( &self, direction: Direction, limit: Option<usize>, page: Option<usize>, sort: Sort, ) -> Result<Vec<ValueResponse>, Box<dyn Error + Send + Sync>>

Returns a list of records from the database.

§Examples
use alex_db_lib::{config::Config, db::{Db, Direction, Sort}, value_record::{Value, ValuePost}};

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().reads);

let value_responses = db.list(Direction::Asc, None, None, Sort::CreatedAt).unwrap();

assert_eq!(0, value_responses.len());
assert_eq!(0, db.stats.read().unwrap().reads);

let key = "test_key".to_string();
let value = Value::Boolean(true);
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
db.try_create(value_post);
let value_responses = db.list(Direction::Asc, None, None, Sort::CreatedAt).unwrap();

assert_eq!(1, value_responses.len());
assert_eq!(1, db.stats.read().unwrap().reads);
Source

pub fn try_append( &self, key: &str, value_append: ValueAppend, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to append a value to an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValueAppend, ValuePost}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value1 = Value::String("test_value".to_string());
let value1_array = Value::Array(VecDeque::from([value1.clone()]));
let value_post = ValuePost { key: key.clone(), ttl: None, value: value1_array.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value1_array);
assert_eq!(1, db.stats.read().unwrap().writes);

let value2 = Value::Integer(100);
let value2_array = Value::Array(VecDeque::from([value2.clone()]));
let value_append = ValueAppend { append: value2_array.clone()};
let value_response = db.try_append(&key, value_append).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Array(VecDeque::from([value1, value2])));
assert_eq!(2, db.stats.read().unwrap().writes);
Source

pub fn try_create( &self, value_post: ValuePost, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to create a new record containing a value in the database.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost}};

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.indexes.created_at.read().unwrap().len());
assert_eq!(0, db.indexes.delete_at.read().unwrap().len());
assert_eq!(0, db.indexes.key.read().unwrap().len());
assert_eq!(0, db.indexes.updated_at.read().unwrap().len());
assert_eq!(0, db.stats.read().unwrap().writes);
assert_eq!(0, db.values.read().unwrap().len());

let key = "test_key1".to_string();
let value = Value::String("test_value".to_string());
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.indexes.created_at.read().unwrap().len());
assert_eq!(0, db.indexes.delete_at.read().unwrap().len());
assert_eq!(1, db.indexes.key.read().unwrap().len());
assert_eq!(1, db.indexes.updated_at.read().unwrap().len());
assert_eq!(1, db.stats.read().unwrap().writes);
assert_eq!(1, db.values.read().unwrap().len());

let key = "test_key2".to_string();
let value = Value::Integer(10);
let value_post = ValuePost { key: key.clone(), ttl: Some(100), value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(2, db.indexes.created_at.read().unwrap().len());
assert_eq!(1, db.indexes.delete_at.read().unwrap().len());
assert_eq!(2, db.indexes.key.read().unwrap().len());
assert_eq!(2, db.indexes.updated_at.read().unwrap().len());
assert_eq!(2, db.stats.read().unwrap().writes);
assert_eq!(2, db.values.read().unwrap().len());
Source

pub fn try_decrement( &self, key: &str, value_decrement: ValueDecrement, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to decrement a value of an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValueDecrement, ValuePost}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value = Value::Integer(5000);
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.stats.read().unwrap().writes);

let value_decrement = ValueDecrement { decrement: None };
let value_response = db.try_decrement(&key, value_decrement).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Integer(4999));
assert_eq!(2, db.stats.read().unwrap().writes);

let value_decrement = ValueDecrement { decrement: Some(10) };
let value_response = db.try_decrement(&key, value_decrement).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Integer(4989));
assert_eq!(3, db.stats.read().unwrap().writes);
Source

pub fn try_delete( &self, key: &str, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to delete an existing record from the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value = Value::Boolean(false);
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.stats.read().unwrap().writes);

let value_response = db.try_delete(&key).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(2, db.stats.read().unwrap().writes);

let value_response = db.try_read(&key).unwrap();

assert!(value_response.is_none());
Source

pub fn try_increment( &self, key: &str, value_increment: ValueIncrement, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to increment a value of an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost, ValueIncrement}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value = Value::Integer(1000);
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.stats.read().unwrap().writes);

let value_increment = ValueIncrement { increment: None };
let value_response = db.try_increment(&key, value_increment).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Integer(1001));
assert_eq!(2, db.stats.read().unwrap().writes);

let value_increment = ValueIncrement { increment: Some(10) };
let value_response = db.try_increment(&key, value_increment).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Integer(1011));
assert_eq!(3, db.stats.read().unwrap().writes);
Source

pub fn try_pop_back( &self, key: &str, value_pop_back: ValuePopBack, ) -> Result<Option<Vec<Value>>, Box<dyn Error + Send + Sync>>

Tries to pop a value from the back of an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePopBack, ValuePost}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value1 = Value::String("test_value1".to_string());
let value2 = Value::String("test_value2".to_string());
let value3 = Value::Integer(100);
let value4 = Value::Integer(1000);
let value_array = Value::Array(VecDeque::from([value1.clone(), value2.clone(), value3.clone(), value4.clone()]));
let value_post = ValuePost { key: key.clone(), ttl: None, value: value_array.clone() };
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value_array);
assert_eq!(1, db.stats.read().unwrap().writes);

let value_pop_back = ValuePopBack { pop_back: None };
let value_response = db.try_pop_back(&key, value_pop_back).unwrap().unwrap();

assert_eq!(value_response, vec![value4]);
assert_eq!(2, db.stats.read().unwrap().writes);

let value_pop_back = ValuePopBack { pop_back: Some(2) };
let value_response = db.try_pop_back(&key, value_pop_back).unwrap().unwrap();

assert_eq!(value_response, vec![value3, value2]);
assert_eq!(3, db.stats.read().unwrap().writes);
Source

pub fn try_pop_front( &self, key: &str, value_pop_front: ValuePopFront, ) -> Result<Option<Vec<Value>>, Box<dyn Error + Send + Sync>>

Tries to pop a value from the front of an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePopFront, ValuePost}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value1 = Value::String("test_value1".to_string());
let value2 = Value::String("test_value2".to_string());
let value3 = Value::Integer(100);
let value4 = Value::Integer(1000);
let value_array = Value::Array(VecDeque::from([value1.clone(), value2.clone(), value3.clone(), value4.clone()]));
let value_post = ValuePost { key: key.clone(), ttl: None, value: value_array.clone() };
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value_array);
assert_eq!(1, db.stats.read().unwrap().writes);

let value_pop_front = ValuePopFront { pop_front: None };
let value_response = db.try_pop_front(&key, value_pop_front).unwrap().unwrap();

assert_eq!(value_response, vec![value1]);
assert_eq!(2, db.stats.read().unwrap().writes);

let value_pop_front = ValuePopFront { pop_front: Some(2) };
let value_response = db.try_pop_front(&key, value_pop_front).unwrap().unwrap();

assert_eq!(value_response, vec![value2, value3]);
assert_eq!(3, db.stats.read().unwrap().writes);
Source

pub fn try_prepend( &self, key: &str, value_prepend: ValuePrepend, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to prepend a value to an existing record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost, ValuePrepend}};
use std::collections::VecDeque;

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value1 = Value::String("test_value".to_string());
let value1_array = Value::Array(VecDeque::from([value1.clone()]));
let value_post = ValuePost { key: key.clone(), ttl: None, value: value1_array.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value1_array);
assert_eq!(1, db.stats.read().unwrap().writes);

let value2 = Value::Integer(100);
let value2_array = Value::Array(VecDeque::from([value2.clone()]));
let value_prepend = ValuePrepend { prepend: value2_array.clone()};
let value_response = db.try_prepend(&key, value_prepend).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, Value::Array(VecDeque::from([value2, value1])));
assert_eq!(2, db.stats.read().unwrap().writes);
Source

pub fn try_read( &self, key: &str, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to read a record from the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost}};

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().reads);

let key = "test_key".to_string();
let value = Value::Integer(10);
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
db.try_create(value_post);
let value_response = db.try_read(&key).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.stats.read().unwrap().reads);
Source

pub fn try_update( &self, key: &str, value_put: ValuePut, ) -> Result<Option<ValueResponse>, Box<dyn Error + Send + Sync>>

Tries to update a record in the database using the specified key.

§Examples
use alex_db_lib::{config::Config, db::Db, value_record::{Value, ValuePost, ValuePut}};

let config = Config::default();
let mut db = Db::new(config);

assert_eq!(0, db.stats.read().unwrap().writes);

let key = "test_key".to_string();
let value = Value::String("test_value".to_string());
let value_post = ValuePost { key: key.clone(), ttl: None, value: value.clone()};
let value_response = db.try_create(value_post).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(1, db.stats.read().unwrap().writes);

let value = Value::Integer(100);
let value_put = ValuePut { ttl: None, value: value.clone()};
let value_response = db.try_update(&key, value_put).unwrap().unwrap();

assert_eq!(value_response.key, key);
assert_eq!(value_response.value, value);
assert_eq!(2, db.stats.read().unwrap().writes);

Trait Implementations§

Source§

impl Debug for Db

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Db

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Db

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl !Freeze for Db

§

impl RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl UnwindSafe for Db

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T