Struct avlon_db::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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = AvlonDB::new("test_db".to_string());

    // Insert some data into the database
    for i in 0..10 {
        let data = Data {
            name: format!("name_{}", i),
            value: i,
        };
        db.save(format!("key_{}", i), data)?;
    }

    let data = db.load::<Data>("key_0")?;
    println!("{:?}", data);

    // Load a range of data from the database
    let results = db.load_range::<Data>("key_0", "key_9")?;
    for data in results {
        println!("{:?}", data);
    }

    Ok(())
}
More examples
Hide additional examples
examples/basic_usage.rs (line 24)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}
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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = AvlonDB::new("test_db".to_string());

    // Insert some data into the database
    for i in 0..10 {
        let data = Data {
            name: format!("name_{}", i),
            value: i,
        };
        db.save(format!("key_{}", i), data)?;
    }

    let data = db.load::<Data>("key_0")?;
    println!("{:?}", data);

    // Load a range of data from the database
    let results = db.load_range::<Data>("key_0", "key_9")?;
    for data in results {
        println!("{:?}", data);
    }

    Ok(())
}
More examples
Hide additional examples
examples/basic_usage.rs (line 27)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}
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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = AvlonDB::new("test_db".to_string());

    // Insert some data into the database
    for i in 0..10 {
        let data = Data {
            name: format!("name_{}", i),
            value: i,
        };
        db.save(format!("key_{}", i), data)?;
    }

    let data = db.load::<Data>("key_0")?;
    println!("{:?}", data);

    // Load a range of data from the database
    let results = db.load_range::<Data>("key_0", "key_9")?;
    for data in results {
        println!("{:?}", data);
    }

    Ok(())
}
More examples
Hide additional examples
examples/basic_usage.rs (line 31)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}
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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = AvlonDB::new("test_db".to_string());

    // Insert some data into the database
    for i in 0..10 {
        let data = Data {
            name: format!("name_{}", i),
            value: i,
        };
        db.save(format!("key_{}", i), data)?;
    }

    let data = db.load::<Data>("key_0")?;
    println!("{:?}", data);

    // Load a range of data from the database
    let results = db.load_range::<Data>("key_0", "key_9")?;
    for data in results {
        println!("{:?}", data);
    }

    Ok(())
}
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)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}
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)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}

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.