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§
Sourcepub fn open(path: &str) -> SdkResult<Self>
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.
Sourcepub fn open_encrypted(path: &str, key_env_var: &str) -> SdkResult<Self>
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();Sourcepub fn with<F, T>(&self, f: F) -> SdkResult<T>where
F: FnOnce(&mut OverDriveDB) -> T,
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();Sourcepub fn query(&self, sql: &str) -> SdkResult<QueryResult>
pub fn query(&self, sql: &str) -> SdkResult<QueryResult>
Convenience: execute an SQL query.
Sourcepub fn query_safe(
&self,
sql_template: &str,
params: &[&str],
) -> SdkResult<QueryResult>
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.
Sourcepub fn insert(&self, table: &str, doc: &Value) -> SdkResult<String>
pub fn insert(&self, table: &str, doc: &Value) -> SdkResult<String>
Convenience: insert a document into a table.
Sourcepub fn get(&self, table: &str, id: &str) -> SdkResult<Option<Value>>
pub fn get(&self, table: &str, id: &str) -> SdkResult<Option<Value>>
Convenience: get a document by _id.
Sourcepub fn backup(&self, dest_path: &str) -> SdkResult<()>
pub fn backup(&self, dest_path: &str) -> SdkResult<()>
Convenience: create a backup at dest_path.
Sourcepub fn handle_count(&self) -> usize
pub fn handle_count(&self) -> usize
Number of SharedDB handles pointing to this same database (Arc strong count).