Skip to main content

SharedDB

Struct SharedDB 

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

A thread-safe, cheaply-cloneable handle to an OverDriveDB.

Internally wraps the database in an Arc<Mutex<OverDriveDB>>. Multiple SharedDB instances pointing to the same database can be safely sent across threads.

§Locking

Each .with() call acquires the mutex for the duration of the closure. Keep closures short to avoid blocking other threads.

Implementations§

Source§

impl SharedDB

Source

pub fn open(path: &str) -> SdkResult<Self>

Open (or create) a database wrapped in a thread-safe handle.

File permissions are hardened automatically on open.

Source

pub fn open_encrypted(path: &str, key_env_var: &str) -> SdkResult<Self>

Open with an encryption key from an environment variable.

use overdrive::shared::SharedDB;
// $env:ODB_KEY="my-aes-256-key"
let db = SharedDB::open_encrypted("app.odb", "ODB_KEY").unwrap();
Source

pub fn with<F, T>(&self, f: F) -> SdkResult<T>
where F: FnOnce(&mut OverDriveDB) -> T,

Execute a closure with exclusive access to the database.

The mutex is acquired for the duration of f and released when it returns. Returns SecurityError if the mutex has been poisoned by a panicking thread.

let count = db.with(|d| d.count("users")).unwrap();
Source

pub fn query(&self, sql: &str) -> SdkResult<QueryResult>

Convenience: execute an SQL query.

Source

pub fn query_safe( &self, sql_template: &str, params: &[&str], ) -> SdkResult<QueryResult>

Convenience: execute a safe parameterized SQL query.

See OverDriveDB::query_safe() for full documentation.

Source

pub fn insert(&self, table: &str, doc: &Value) -> SdkResult<String>

Convenience: insert a document into a table.

Source

pub fn get(&self, table: &str, id: &str) -> SdkResult<Option<Value>>

Convenience: get a document by _id.

Source

pub fn backup(&self, dest_path: &str) -> SdkResult<()>

Convenience: create a backup at dest_path.

Source

pub fn sync(&self) -> SdkResult<()>

Convenience: sync to disk.

Source

pub fn handle_count(&self) -> usize

Number of SharedDB handles pointing to this same database (Arc strong count).

Trait Implementations§

Source§

impl Clone for SharedDB

Source§

fn clone(&self) -> SharedDB

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

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

Performs copy-assignment from source. 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> 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, 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.