pub struct Tree { /* private fields */ }
Expand description
The struct that allows you to create sub trees in the main tree in the database Sub trees do not auto insert in the main tree of the database You can do that by doing
§Example
// Create the database instance
let mut db = Database::new("./hello.dino");
// Load and create the database if does not exist
db.load();
// Create a new sub Tree in the main Tree of the db
let mut data_tree = Tree::new();
// Insert the [data_tree] under the main tree
db.insert_tree("id", data_tree);
Where the key always need to be a String
Implementations§
Source§impl Tree
impl Tree
Sourcepub fn new() -> Tree
pub fn new() -> Tree
Create a new sub tree
Examples found in repository?
examples/database.rs (line 16)
5fn main() {
6 // Create the database instance
7 let mut db = Database::new("./hello.dino");
8
9 // Load and create the database if does not exist
10 db.load();
11
12 // Insert values in the db in the format of key, value
13 db.insert("key", "q");
14
15 // Create a new sub Tree in the main Tree of the db
16 let mut data_tree = Tree::new();
17
18 // Insert the key and value in the sub tree
19 data_tree.insert("b", "c");
20
21 // The length of items in the sub tree in the database
22 // This also shows almost all of the functions in Database are also avaliable in Tree
23 println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25 // Insert the [data_tree] under the main tree
26 db.insert_tree("id", data_tree);
27
28 // Print the value of id
29 println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31 match db.find("not_exists") {
32 Ok(_value) => {
33 println!("This is unfortunate :(")
34 }
35
36 Err(error) => {
37 println!("Everting works! Here is the error for reference: {}", error)
38 }
39 }
40
41 // Remove a key in the database with its value
42 db.remove("id");
43
44 // Now here it wont print that it exists as it does not we removed it ^^^^^
45 if db.contains_key("id") {
46 println!("The key `id` exists!")
47 };
48
49 println!("The length of items in the database is: {}", db.len());
50
51 // Insert a number in the database
52 db.insert_number("test", 1);
53
54 println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Sourcepub fn insert(&mut self, key: &str, value: &str)
pub fn insert(&mut self, key: &str, value: &str)
Insert data with String value type in the sub tree
Examples found in repository?
examples/database.rs (line 19)
5fn main() {
6 // Create the database instance
7 let mut db = Database::new("./hello.dino");
8
9 // Load and create the database if does not exist
10 db.load();
11
12 // Insert values in the db in the format of key, value
13 db.insert("key", "q");
14
15 // Create a new sub Tree in the main Tree of the db
16 let mut data_tree = Tree::new();
17
18 // Insert the key and value in the sub tree
19 data_tree.insert("b", "c");
20
21 // The length of items in the sub tree in the database
22 // This also shows almost all of the functions in Database are also avaliable in Tree
23 println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25 // Insert the [data_tree] under the main tree
26 db.insert_tree("id", data_tree);
27
28 // Print the value of id
29 println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31 match db.find("not_exists") {
32 Ok(_value) => {
33 println!("This is unfortunate :(")
34 }
35
36 Err(error) => {
37 println!("Everting works! Here is the error for reference: {}", error)
38 }
39 }
40
41 // Remove a key in the database with its value
42 db.remove("id");
43
44 // Now here it wont print that it exists as it does not we removed it ^^^^^
45 if db.contains_key("id") {
46 println!("The key `id` exists!")
47 };
48
49 println!("The length of items in the database is: {}", db.len());
50
51 // Insert a number in the database
52 db.insert_number("test", 1);
53
54 println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Sourcepub fn insert_number(&mut self, key: &str, value: usize)
pub fn insert_number(&mut self, key: &str, value: usize)
Insert data with usize value type in the sub tree
Sourcepub fn len(&mut self) -> usize
pub fn len(&mut self) -> usize
Return the length of items that are in the sub tree
Examples found in repository?
examples/database.rs (line 23)
5fn main() {
6 // Create the database instance
7 let mut db = Database::new("./hello.dino");
8
9 // Load and create the database if does not exist
10 db.load();
11
12 // Insert values in the db in the format of key, value
13 db.insert("key", "q");
14
15 // Create a new sub Tree in the main Tree of the db
16 let mut data_tree = Tree::new();
17
18 // Insert the key and value in the sub tree
19 data_tree.insert("b", "c");
20
21 // The length of items in the sub tree in the database
22 // This also shows almost all of the functions in Database are also avaliable in Tree
23 println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25 // Insert the [data_tree] under the main tree
26 db.insert_tree("id", data_tree);
27
28 // Print the value of id
29 println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31 match db.find("not_exists") {
32 Ok(_value) => {
33 println!("This is unfortunate :(")
34 }
35
36 Err(error) => {
37 println!("Everting works! Here is the error for reference: {}", error)
38 }
39 }
40
41 // Remove a key in the database with its value
42 db.remove("id");
43
44 // Now here it wont print that it exists as it does not we removed it ^^^^^
45 if db.contains_key("id") {
46 println!("The key `id` exists!")
47 };
48
49 println!("The length of items in the database is: {}", db.len());
50
51 // Insert a number in the database
52 db.insert_number("test", 1);
53
54 println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Sourcepub fn remove(&mut self, key: &str)
pub fn remove(&mut self, key: &str)
Remove a key in the sub tree in the database with its value
Sourcepub fn find(&self, key: &str) -> Result<Value, String>
pub fn find(&self, key: &str) -> Result<Value, String>
Find a value in the sub tree in the database
Sourcepub fn contains_key(&mut self, key: &str) -> bool
pub fn contains_key(&mut self, key: &str) -> bool
Check if the key exists in the sub tree of the main database
Sourcepub fn insert_tree(&mut self, key: &str, value: Tree)
pub fn insert_tree(&mut self, key: &str, value: Tree)
Insert a key with a subtree in the subtree!
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tree
impl RefUnwindSafe for Tree
impl Send for Tree
impl Sync for Tree
impl Unpin for Tree
impl UnwindSafe for Tree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more