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: DbImplementations§
Source§impl AvlonDB
impl AvlonDB
Sourcepub fn new(db_name: String) -> Self
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?
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
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}Sourcepub fn save<T>(&self, key: String, value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
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?
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
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}Sourcepub fn load<T>(&self, key: &str) -> Result<Option<T>, Box<dyn Error>>where
T: for<'de> Deserialize<'de>,
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?
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
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}Sourcepub fn load_range<T>(&self, start_key: &str, end_key: &str) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
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?
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}Sourcepub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>
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?
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}Sourcepub fn update<T>(&self, key: &str, new_value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
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?
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}