AvlonDB

Struct AvlonDB 

Source
pub struct AvlonDB {
    pub db_name: String,
    pub client: Db,
}
Expand description

A struct representing a wrapper around a Sled database.

Fields§

§db_name: String§client: Db

Implementations§

Source§

impl AvlonDB

Source

pub fn new(db_name: String) -> Self

Creates a new instance of AvlonDB with the specified database name.

§Arguments
  • db_name - The name of the database file.
§Returns

A new AvlonDB instance.

Examples found in repository?
examples/range_usage.rs (line 11)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let db = AvlonDB::new("test_db".to_string());
12
13    // Insert some data into the database
14    for i in 0..10 {
15        let data = Data {
16            name: format!("name_{}", i),
17            value: i,
18        };
19        db.save(format!("key_{}", i), data)?;
20    }
21
22    let data = db.load::<Data>("key_0")?;
23    println!("{:?}", data);
24
25    // Load a range of data from the database
26    let results = db.load_range::<Data>("key_0", "key_9")?;
27    for data in results {
28        println!("{:?}", data);
29    }
30
31    Ok(())
32}
More examples
Hide additional examples
examples/basic_usage.rs (line 24)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Define the database name and will create a same named directory in the current directory.
14    let db_name = String::from("test_db");
15
16    // Create an instance of Account
17    let account = Account {
18        username: String::from("johndoe"),
19        password: String::from("secretpassword"),
20        age: 30,
21    };
22
23    // Initialize the database
24    let db = AvlonDB::new(db_name);
25
26    // Save the account to the database
27    db.save(account.username.clone(), account)?;
28    println!("Account saved to database!");
29
30    // Load the account from the database
31    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
32        println!("Account loaded from database: {:?}", loaded_account);
33    } else {
34        println!("Account not found in the database.");
35    }
36
37    // Update the account in the database
38    let updated_account = Account {
39        username: String::from("joker"),
40        password: String::from("123654987"),
41        age: 99,
42    };
43    db.update("johndoe", updated_account)?;
44
45    // Load the updated account from the database
46    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
47        println!("Updated account loaded from database: {:?}", loaded_account);
48    } else {
49        println!("Account not found in the database.");
50    }
51
52    // Remove the account from the database
53    db.remove("johndoe")?;
54
55    // Attempt to load the account again after removal
56    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
57        println!(
58            "Account loaded from database after removal: {:?}",
59            loaded_account
60        );
61    } else {
62        println!("Account successfully removed from the database.");
63    }
64
65    Ok(())
66}
Source

pub fn save<T>(&self, key: String, value: T) -> Result<(), Box<dyn Error>>
where T: Serialize,

Saves a value to the database under the given key.

§Arguments
  • key - The key to associate with the value.
  • value - The value to store in the database.
§Returns

A Result indicating success or failure.

Examples found in repository?
examples/range_usage.rs (line 19)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let db = AvlonDB::new("test_db".to_string());
12
13    // Insert some data into the database
14    for i in 0..10 {
15        let data = Data {
16            name: format!("name_{}", i),
17            value: i,
18        };
19        db.save(format!("key_{}", i), data)?;
20    }
21
22    let data = db.load::<Data>("key_0")?;
23    println!("{:?}", data);
24
25    // Load a range of data from the database
26    let results = db.load_range::<Data>("key_0", "key_9")?;
27    for data in results {
28        println!("{:?}", data);
29    }
30
31    Ok(())
32}
More examples
Hide additional examples
examples/basic_usage.rs (line 27)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Define the database name and will create a same named directory in the current directory.
14    let db_name = String::from("test_db");
15
16    // Create an instance of Account
17    let account = Account {
18        username: String::from("johndoe"),
19        password: String::from("secretpassword"),
20        age: 30,
21    };
22
23    // Initialize the database
24    let db = AvlonDB::new(db_name);
25
26    // Save the account to the database
27    db.save(account.username.clone(), account)?;
28    println!("Account saved to database!");
29
30    // Load the account from the database
31    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
32        println!("Account loaded from database: {:?}", loaded_account);
33    } else {
34        println!("Account not found in the database.");
35    }
36
37    // Update the account in the database
38    let updated_account = Account {
39        username: String::from("joker"),
40        password: String::from("123654987"),
41        age: 99,
42    };
43    db.update("johndoe", updated_account)?;
44
45    // Load the updated account from the database
46    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
47        println!("Updated account loaded from database: {:?}", loaded_account);
48    } else {
49        println!("Account not found in the database.");
50    }
51
52    // Remove the account from the database
53    db.remove("johndoe")?;
54
55    // Attempt to load the account again after removal
56    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
57        println!(
58            "Account loaded from database after removal: {:?}",
59            loaded_account
60        );
61    } else {
62        println!("Account successfully removed from the database.");
63    }
64
65    Ok(())
66}
Source

pub fn load<T>(&self, key: &str) -> Result<Option<T>, Box<dyn Error>>
where T: for<'de> Deserialize<'de>,

Loads a value from the database associated with the given key.

§Arguments
  • key - The key associated with the value to load.
§Returns

A Result containing an Option with the value, or None if the key does not exist.

Examples found in repository?
examples/range_usage.rs (line 22)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let db = AvlonDB::new("test_db".to_string());
12
13    // Insert some data into the database
14    for i in 0..10 {
15        let data = Data {
16            name: format!("name_{}", i),
17            value: i,
18        };
19        db.save(format!("key_{}", i), data)?;
20    }
21
22    let data = db.load::<Data>("key_0")?;
23    println!("{:?}", data);
24
25    // Load a range of data from the database
26    let results = db.load_range::<Data>("key_0", "key_9")?;
27    for data in results {
28        println!("{:?}", data);
29    }
30
31    Ok(())
32}
More examples
Hide additional examples
examples/basic_usage.rs (line 31)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Define the database name and will create a same named directory in the current directory.
14    let db_name = String::from("test_db");
15
16    // Create an instance of Account
17    let account = Account {
18        username: String::from("johndoe"),
19        password: String::from("secretpassword"),
20        age: 30,
21    };
22
23    // Initialize the database
24    let db = AvlonDB::new(db_name);
25
26    // Save the account to the database
27    db.save(account.username.clone(), account)?;
28    println!("Account saved to database!");
29
30    // Load the account from the database
31    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
32        println!("Account loaded from database: {:?}", loaded_account);
33    } else {
34        println!("Account not found in the database.");
35    }
36
37    // Update the account in the database
38    let updated_account = Account {
39        username: String::from("joker"),
40        password: String::from("123654987"),
41        age: 99,
42    };
43    db.update("johndoe", updated_account)?;
44
45    // Load the updated account from the database
46    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
47        println!("Updated account loaded from database: {:?}", loaded_account);
48    } else {
49        println!("Account not found in the database.");
50    }
51
52    // Remove the account from the database
53    db.remove("johndoe")?;
54
55    // Attempt to load the account again after removal
56    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
57        println!(
58            "Account loaded from database after removal: {:?}",
59            loaded_account
60        );
61    } else {
62        println!("Account successfully removed from the database.");
63    }
64
65    Ok(())
66}
Source

pub fn load_range<T>(&self, start_key: &str, end_key: &str) -> Result<Vec<T>>
where T: for<'de> Deserialize<'de>,

Loads range data from the database associated with the given start key and end key.

§Arguments
  • start_key - The start key of range data.
  • end_key - The end key of range data.
§Returns

A Result containing a Vec of the values, or an Err if an issue occurs.

Examples found in repository?
examples/range_usage.rs (line 26)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let db = AvlonDB::new("test_db".to_string());
12
13    // Insert some data into the database
14    for i in 0..10 {
15        let data = Data {
16            name: format!("name_{}", i),
17            value: i,
18        };
19        db.save(format!("key_{}", i), data)?;
20    }
21
22    let data = db.load::<Data>("key_0")?;
23    println!("{:?}", data);
24
25    // Load a range of data from the database
26    let results = db.load_range::<Data>("key_0", "key_9")?;
27    for data in results {
28        println!("{:?}", data);
29    }
30
31    Ok(())
32}
Source

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

Removes a value from the database associated with the given key.

§Arguments
  • key - The key associated with the value to remove.
§Returns

A Result indicating success or failure.

Examples found in repository?
examples/basic_usage.rs (line 53)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Define the database name and will create a same named directory in the current directory.
14    let db_name = String::from("test_db");
15
16    // Create an instance of Account
17    let account = Account {
18        username: String::from("johndoe"),
19        password: String::from("secretpassword"),
20        age: 30,
21    };
22
23    // Initialize the database
24    let db = AvlonDB::new(db_name);
25
26    // Save the account to the database
27    db.save(account.username.clone(), account)?;
28    println!("Account saved to database!");
29
30    // Load the account from the database
31    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
32        println!("Account loaded from database: {:?}", loaded_account);
33    } else {
34        println!("Account not found in the database.");
35    }
36
37    // Update the account in the database
38    let updated_account = Account {
39        username: String::from("joker"),
40        password: String::from("123654987"),
41        age: 99,
42    };
43    db.update("johndoe", updated_account)?;
44
45    // Load the updated account from the database
46    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
47        println!("Updated account loaded from database: {:?}", loaded_account);
48    } else {
49        println!("Account not found in the database.");
50    }
51
52    // Remove the account from the database
53    db.remove("johndoe")?;
54
55    // Attempt to load the account again after removal
56    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
57        println!(
58            "Account loaded from database after removal: {:?}",
59            loaded_account
60        );
61    } else {
62        println!("Account successfully removed from the database.");
63    }
64
65    Ok(())
66}
Source

pub fn update<T>(&self, key: &str, new_value: T) -> Result<(), Box<dyn Error>>
where T: Serialize,

Updates the value associated with the given key in the database.

§Arguments
  • key - The key associated with the value to update.
  • new_value - The new value to store in the database.
§Returns

A Result indicating success or failure. Returns an error if the key does not exist.

Examples found in repository?
examples/basic_usage.rs (line 43)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Define the database name and will create a same named directory in the current directory.
14    let db_name = String::from("test_db");
15
16    // Create an instance of Account
17    let account = Account {
18        username: String::from("johndoe"),
19        password: String::from("secretpassword"),
20        age: 30,
21    };
22
23    // Initialize the database
24    let db = AvlonDB::new(db_name);
25
26    // Save the account to the database
27    db.save(account.username.clone(), account)?;
28    println!("Account saved to database!");
29
30    // Load the account from the database
31    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
32        println!("Account loaded from database: {:?}", loaded_account);
33    } else {
34        println!("Account not found in the database.");
35    }
36
37    // Update the account in the database
38    let updated_account = Account {
39        username: String::from("joker"),
40        password: String::from("123654987"),
41        age: 99,
42    };
43    db.update("johndoe", updated_account)?;
44
45    // Load the updated account from the database
46    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
47        println!("Updated account loaded from database: {:?}", loaded_account);
48    } else {
49        println!("Account not found in the database.");
50    }
51
52    // Remove the account from the database
53    db.remove("johndoe")?;
54
55    // Attempt to load the account again after removal
56    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
57        println!(
58            "Account loaded from database after removal: {:?}",
59            loaded_account
60        );
61    } else {
62        println!("Account successfully removed from the database.");
63    }
64
65    Ok(())
66}

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.