Struct persistent_keystore_rs::Client[][src]

pub struct Client { /* fields omitted */ }
Expand description

Thread-safe, optionally persistent client for interacting with a keystore database

Implementations

Creates a database at the supplied path

use std::path::Path;
let c = Client::new(Path::new("temp.db"), None);

This database will not sync on its own and will need to be saved with

c.save();

To create a persistent database that will create a thread attached to the lifetime of the client provide a duration

use std::path::Path;
use std::time::Duration;
let c = Client::new(Path::new("temp3.db"), Some(Duration::from_millis(30)));

This thread will prune (remove stale entries) and save the database every duration

Opens an existing database at the supplied path

use std::path::Path;
let c = Client::open(Path::new("existing.db"));

This database will resume the sync settings that were provided when the database was created.

Removes stale entries as defined by the expiration value per table and saves the database to disk; using lz4 compression

use std::path::Path;
let c = Client::new(Path::new("saved.db"), None);

This database will not sync on its own and will need to be saved with

c.save();

Creates a table within the database of the associated client

use persistent_keystore_rs::{Client, Table, FieldType};
use std::time::Duration;
let mut c = Client::new(Path::new("createtable.db"), None).unwrap();
let table = Table::new()
    .name(String::from("MyTable"))
    .primary_field(FieldType::String).unwrap()
    .add_field(String::from("TimeStamp"), FieldType::Date).unwrap()
    .add_expiration(Duration::from_secs(2592000))
    .build().unwrap();
c.create_table(table).unwrap();

Lists tables within the database of the associated client

let mut c = Client::new(Path::new("listtable.db"), None).unwrap();
let tables = c.list_tables().unwrap();
assert_eq!(tables.len(), 1);
assert_eq!(tables[0], String::from("MyTable"));

Drops the specified table from within the database of the associated client

let mut c = Client::new(Path::new("droptable.db"), None).unwrap();
let tables = c.list_tables().unwrap();
assert_eq!(tables.len(), 1);
assert_eq!(tables[0], String::from("MyTable"));
c.drop_table(&String::from("MyTable")).unwrap();
let current_tables = c.list_tables().unwrap();
assert_eq!(current_tables.len(), 0);

Inserts the provided entry into the specified table within the database of the associated client. If an entry with the same primary key exists, an DatabaseError::EntryExists is returned

use persistent_keystore_rs::{Entry, Field};
use std::time::SystemTime;
let mut c = Client::new(Path::new("insertentry.db"), None).unwrap();
let entry = Entry::new()
    .set_primary_field(Field::String("MyFirstEntry".to_string())).unwrap()
    .add_field("TimeStamp".to_string(), Field::Date(SystemTime::now())).unwrap()
    .build().unwrap();
c.insert("MyTable".to_string(), entry).unwrap();

Inserts the provided entry into the specified table within the database of the associated client. If an entry with the same primary key exists, the entry is updated.

use persistent_keystore_rs::{Entry, Field};
use std::time::SystemTime;
let mut c = Client::new(Path::new("insertorupdateentry.db"), None).unwrap();
let entry = Entry::new()
    .set_primary_field(Field::String("MyFirstEntry".to_string())).unwrap()
    .add_field("TimeStamp".to_string(), Field::Date(SystemTime::now())).unwrap()
    .build().unwrap();
c.insert("MyTable".to_string(), entry.clone()).unwrap();
c.insert_or_update("MyTable".to_string(), entry).unwrap();

Updates an existing entry in the specified table within the database of the associated client. If an entry does not exist, DatabaseError::EntryDoesNotExists is returned

use persistent_keystore_rs::{Entry, Field};
use std::time::SystemTime;
let mut c = Client::new(Path::new("updateentry.db"), None).unwrap();
let entry = Entry::new()
    .set_primary_field(Field::String("MyFirstEntry".to_string())).unwrap()
    .add_field("TimeStamp".to_string(), Field::Date(SystemTime::now())).unwrap()
    .build().unwrap();
c.update("MyTable".to_string(), entry).unwrap();

Get an existing entry from the specified table within the database of the associated client. If an entry does not exist, DatabaseError::EntryDoesNotExists is returned

use persistent_keystore_rs::Field;
let mut c = Client::new(Path::new("getentry.db"), None).unwrap();
let e = c.get("MyTable".to_string(), Field::String("MyFirstEntry".to_string())).unwrap();

Delete an existing entry from the specified table within the database of the associated client. If an entry does not exist, DatabaseError::EntryDoesNotExists is returned

use persistent_keystore_rs::Field;
let mut c = Client::new(Path::new("delete.db"), None).unwrap();
c.delete("MyTable".to_string(), Field::String("MyFirstEntry".to_string())).unwrap();

Delete all entries matching the supplied criteria.

use persistent_keystore_rs::Field;
use std::collections::HashMap;
let mut c = Client::new(Path::new("deletemany.db"), None).unwrap();
let mut criteria: HashMap<String, Field> = HashMap::new();
criteria.insert("OptionalField".to_string(), Field::String("MyTestingField".to_string()));
c.delete_many("MyTable".to_string(), criteria).unwrap();

Returns all entries from the specified table within the database of the associated client. If no entries exist, will return an empty vec

let mut c = Client::new(Path::new("scan.db"), None).unwrap();
let results = c.scan("MyTable".to_string()).unwrap();

Query for entries within a specified table meeting the supplied criteria.

use persistent_keystore_rs::Field;
use std::collections::HashMap;
let mut c = Client::new(Path::new("query.db"), None).unwrap();
let mut criteria: HashMap<String, Field> = HashMap::new();
criteria.insert("OptionalField".to_string(), Field::String("MyTestingField".to_string()));
let results = c.query("MyTable".to_string(), criteria).unwrap();

Removes entries that have expired by the specified TTL field in the table. This is done automatically before saves if a sync_interval is provided.

use persistent_keystore_rs::{Client, Table, FieldType, Entry};
use persistent_keystore_rs::Field;
use std::collections::HashMap;
let mut c = Client::new(Path::new("prune.db"), None).unwrap();
let table = Table::new()
    .name(String::from("MyTable"))
    .add_expiration(Duration::from_secs(1))
    .primary_field(FieldType::String).unwrap()
    .add_field("FirstKey".to_string(), FieldType::I64).unwrap()
    .add_optional_field("OptionalKey".to_string(), FieldType::String).unwrap()
    .build().unwrap();

c.create_table(table).unwrap();

let entry_first = Entry::new()
    .set_primary_field(Field::String("This should Succeed".to_string())).unwrap()
    .add_field("FirstKey".to_string(), Field::I64(123123)).unwrap()
    .add_field("OptionalKey".to_string(), Field::String("My first entry".to_string())).unwrap()
    .build().unwrap();

let result = c.insert("MyTable".to_string(), entry_first);

if let Err(e) = result {
    panic!("No error expected, received {}", e)
} 

let output = c.scan("MyTable".to_string()).unwrap();

if output.len() != 1 {
    panic!("Expected scan results to be 1")
};

sleep(Duration::from_secs(2));
c.prune().unwrap();

let output = c.scan("MyTable".to_string()).unwrap();
if output.len() != 0 {
    panic!("Expected scan results to be 0")
};

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more