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?
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
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(())
}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?
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
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(())
}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?
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
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(())
}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?
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(())
}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?
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(())
}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?
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(())
}