pub struct SharedDatabase { /* private fields */ }Expand description
Thread-safe database wrapper enabling concurrent read queries.
SharedDatabase wraps a Database in Arc<RwLock<...>> to enable:
- Concurrent reads: Multiple
query()calls can execute simultaneously - Exclusive writes: Mutations acquire exclusive access via
write()
§Performance
Using SharedDatabase enables significant throughput improvements for read-heavy
workloads. On a multi-core system, concurrent read queries can achieve near-linear
scaling with the number of cores.
| Metric | Sequential | Concurrent (4 cores) |
|---|---|---|
| Read QPS | 1x | ~4x |
| P99 latency | baseline | ~1.5x baseline |
§Example
use vibesql_executor::SharedDatabase;
use vibesql_storage::Database;
// Create shared database
let db = SharedDatabase::new(Database::new());
// Concurrent reads (acquire read lock internally)
let result = db.query("SELECT * FROM users").await?;
// Exclusive writes
let mut guard = db.write().await;
guard.insert_row("users", row)?;
// guard dropped, releasing write lockImplementations§
Sourcepub fn from_arc(inner: Arc<RwLock<Database>>) -> Self
pub fn from_arc(inner: Arc<RwLock<Database>>) -> Self
Create a SharedDatabase from an existing Arc<RwLock<Database>>.
This is useful when integrating with existing code that already uses
the Arc<RwLock
Sourcepub fn into_inner(self) -> Arc<RwLock<Database>>
pub fn into_inner(self) -> Arc<RwLock<Database>>
Get the inner Arc<RwLock<Database>>.
This is useful when you need to pass the database to code that expects
the raw Arc<RwLock<Database>> type.
Sourcepub fn as_arc(&self) -> &Arc<RwLock<Database>>
pub fn as_arc(&self) -> &Arc<RwLock<Database>>
Get a reference to the inner Arc<RwLock<Database>>.
Sourcepub async fn read(&self) -> RwLockReadGuard<'_, Database>
pub async fn read(&self) -> RwLockReadGuard<'_, Database>
Acquire a read lock for concurrent read access.
Multiple readers can hold read locks simultaneously. Use this for SELECT queries or any read-only operations.
Sourcepub async fn write(&self) -> RwLockWriteGuard<'_, Database>
pub async fn write(&self) -> RwLockWriteGuard<'_, Database>
Acquire a write lock for exclusive write access.
Only one writer can hold the write lock at a time, and no readers can acquire read locks while a write lock is held.
Sourcepub async fn query(&self, sql: &str) -> Result<SelectResult, ReadOnlyError>
pub async fn query(&self, sql: &str) -> Result<SelectResult, ReadOnlyError>
Execute a read-only SQL query with automatic read lock management.
This is a convenience method that:
- Acquires a read lock on the database
- Parses and executes the SQL query
- Returns the result, releasing the lock
Only SELECT statements are allowed. Other statement types return
ReadOnlyError::NotReadOnly.
§Example
let db = SharedDatabase::new(Database::new());
// ... setup tables and data ...
// Execute concurrent queries from multiple tasks
let result = db.query("SELECT * FROM users WHERE active = true").await?;Trait Implementations§
Source§fn clone(&self) -> SharedDatabase
fn clone(&self) -> SharedDatabase
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more