Struct Database

Source
pub struct Database {
    pub path: String,
    /* private fields */
}
Expand description

The main struct of Dino. The Database struct is responsible for creating the storage instance that will store this database’s documents, managing the database tables as well as providing access to the default table.

Fields§

§path: String

The path of the file in a String format

Implementations§

Source§

impl Database

Source

pub fn new(path: &str) -> Database

Create a new instance of the Database

Examples found in repository?
examples/database.rs (line 7)
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}
Source

pub fn load(&mut self)

Load the database from the file and initialize variables

Examples found in repository?
examples/database.rs (line 10)
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}
Source

pub fn insert_tree(&self, key: &str, value: Tree)

Insert a key with a subtree in the database

Examples found in repository?
examples/database.rs (line 26)
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}
Source

pub fn insert(&self, key: &str, value: &str)

Insert a key and a value in the database

Examples found in repository?
examples/database.rs (line 13)
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}
Source

pub fn insert_number(&self, key: &str, value: usize)

Insert a key and a value in the database

Examples found in repository?
examples/database.rs (line 52)
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}
Source

pub fn remove(&self, key: &str)

Remove a key in the database with its value

Examples found in repository?
examples/database.rs (line 42)
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}
Source

pub fn find(&self, key: &str) -> Result<Value, String>

Find a value in the db

Examples found in repository?
examples/database.rs (line 29)
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}
Source

pub fn contains_key(&self, key: &str) -> bool

Check if the key exists in the database

Examples found in repository?
examples/database.rs (line 45)
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}
Source

pub fn len(&self) -> usize

Return the length of items that are in the main tree

Examples found in repository?
examples/database.rs (line 49)
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}

Trait Implementations§

Source§

impl Display for Database

impl Display for database So we can print the whole tree to the display

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.