Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

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

Implementations§

Source§

impl Client

Source

pub fn new<P: AsRef<Path> + Clone + Debug>( path: P, sync_interval: Option<Duration>, ) -> Result<Box<dyn DatabaseClient>, DatabaseError>

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

Source

pub fn open<P: AsRef<Path> + Clone + Debug>( path: P, ) -> Result<Box<dyn DatabaseClient>, DatabaseError>

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.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl DatabaseClient for Client

Source§

fn save(&mut self) -> Result<(), DatabaseError>

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();
Source§

fn create_table(&mut self, table: Table) -> Result<(), DatabaseError>

Creates a table within the database of the associated client

use persistent_keystore_rs::{Client, Table, FieldType};
use persistent_keystore_rs::prelude::*;
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();
Source§

fn list_tables(&mut self) -> Result<Vec<String>, DatabaseError>

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"));
Source§

fn drop_table(&mut self, table: &String) -> Result<(), DatabaseError>

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);
Source§

fn insert(&mut self, table: String, entry: Entry) -> Result<(), DatabaseError>

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();
Source§

fn insert_or_update( &mut self, table: String, entry: Entry, ) -> Result<(), DatabaseError>

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();
Source§

fn update(&mut self, table: String, entry: Entry) -> Result<(), DatabaseError>

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();
Source§

fn get( &mut self, table: String, primary_field: Field, ) -> Result<Entry, DatabaseError>

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();
Source§

fn delete( &mut self, table: String, primary_field: Field, ) -> Result<(), DatabaseError>

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();
Source§

fn delete_many( &mut self, table: String, criteria: HashMap<String, Field>, ) -> Result<u64, DatabaseError>

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();
Source§

fn scan(&mut self, table: String) -> Result<Vec<Entry>, DatabaseError>

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();
Source§

fn query( &mut self, table: String, criteria: HashMap<String, Field>, ) -> Result<Vec<Entry>, DatabaseError>

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();
Source§

fn prune(&mut self) -> Result<(), DatabaseError>

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")
};

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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