DataBase

Struct DataBase 

Source
pub struct DataBase { /* private fields */ }

Implementations§

Source§

impl DataBase

Source

pub fn new(path: &str) -> Self

Creates a new instance of the database or uses the existing db file, at the given path.

§Example
let db = mu_db::DataBase::new("./test.db");

Generates (./test.db) and (./index_test.db) if doesn’t exist.

Source

pub fn insert(&mut self, key: &str, value: &str)

Inserts a key-value pair into the database, replacing old value if key exists.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.insert("key", "before");
db.insert("key", "after");
assert_eq!(db.get("key"), Some("after".to_string()));
Source

pub fn get(&mut self, key: &str) -> Option<String>

Retrieves the value associated with the given key from the database.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.insert("key", "value");
assert_eq!(db.get("key"), Some("value".to_string()));
Source

pub fn remove(&mut self, key: &str)

Removes the entry associated with the given key from the index if the key exists. This method does not remove the value in the database file. To completely remove the value, you need to use (.shrink()) after removing the entry.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.insert("key", "value");
assert_eq!(db.get("key"), Some("value".to_string()));
db.remove("key");
assert_eq!(db.get("key"), None);
Source

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

Clears all data in the database.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.insert("key", "value");
assert!(!db.is_empty());
assert!(!db.is_buf_empty());
db.clear_all().unwrap();
assert!(db.is_empty());
assert!(db.is_buf_empty());
Source

pub fn shrink(&mut self)

Optimizes the database file by removing any unused space.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
db.insert("k1", "1".repeat(10).as_str());
db.insert("k2", "2".repeat(10).as_str());
assert_eq!(db.buf_len(), 20);
db.remove("k1");
assert_eq!(db.buf_len(), 20);
db.insert("k3", "3".repeat(5).as_str());
assert_eq!(db.buf_len(), 20);
db.shrink();
assert_eq!(db.buf_len(), 15);
db.remove("k2");
db.remove("k3");
assert_eq!(db.buf_len(), 15);
db.shrink();
assert_eq!(db.buf_len(), 0);
Source

pub fn read_at(&mut self, start: u64, size: usize) -> Result<String>

Reads data directly from the database file at the specified position (start) and size (size).

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
db.insert("k1", "hello");
db.insert("k2", "world");
assert_eq!(db.read_at(5, 5).unwrap(), "world".to_string());
Source

pub fn write_at(&mut self, start: u64, content: &str) -> Result<()>

Writes data directly to the database file at the specified position with any length.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
db.write_at(5, "world").unwrap();
assert_eq!(db.read_at(5, 5).unwrap(), "world".to_string());
Source

pub fn is_empty(&self) -> bool

Returns true if self.index.entries is empty, and false otherwise.

If you want to know if db file is empty, use (.is_buf_empty()).

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
db.insert("key", "value");
assert!(!db.is_empty());
assert!(!db.is_buf_empty());
db.remove("key");
assert!(db.is_empty());
assert!(!db.is_buf_empty());
db.shrink();
assert!(db.is_empty());
assert!(db.is_buf_empty());
Source

pub fn is_buf_empty(&self) -> bool

Returns true if db file has metadata length of 0, and false otherwise.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
assert!(db.is_buf_empty());
db.insert("key", "value");
assert!(!db.is_buf_empty());
Source

pub fn buf_len(&self) -> u64

Returns the length of the db file matadata.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
db.insert("key", "value");
assert_eq!(db.buf_len(), 5);
db.clear_all();
assert_eq!(db.buf_len(), 0);
Source

pub fn set_buf_len(&mut self, len: u64)

Sets the length of the database file directly, truncating or extending it as necessary.

§Example
let mut db = mu_db::DataBase::new("./test.db");
db.clear_all();
assert!(db.is_buf_empty());
assert_eq!(db.buf_len(), 0);
db.insert("key", "value");
assert_eq!(db.buf_len(), 5);
assert!(!db.is_buf_empty());
db.set_buf_len(0);
assert_eq!(db.buf_len(), 0);
assert!(db.is_buf_empty());

Auto Trait Implementations§

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.