pub struct Database<S> { /* private fields */ }
Expand description
The primary interface to the database.
It allows creating read and write transactions from multiple threads. It is also safe to create multiple Database instances for the same database (from one or multiple processes).
Sqlite is configured to be in WAL mode. The effect of this mode is that there can be any number of readers with one concurrent writer. What is nice about this is that a Transaction can always be made immediately. Making a TransactionMut has to wait until all other TransactionMuts are finished.
Sqlite is also configured with synchronous=NORMAL
. This gives better performance by fsyncing less.
The database will not lose transactions due to application crashes, but it might due to system crashes or power loss.
§Creating transactions
Creating a transaction requires access to a ThreadToken. This makes it impossible to create two transactions on the same thread, making it impossible to accidentally share a TableRow outside of the transaction that it was created in.
Implementations§
Source§impl<S> Database<S>
impl<S> Database<S>
Sourcepub fn read<'a>(&self, token: &'a mut ThreadToken) -> Transaction<'a, S>
pub fn read<'a>(&self, token: &'a mut ThreadToken) -> Transaction<'a, S>
Create a Transaction. This operation always completes immediately as it does not need to wait on other transactions.
Sourcepub fn write_lock<'a>(
&self,
token: &'a mut ThreadToken,
) -> TransactionMut<'a, S>
pub fn write_lock<'a>( &self, token: &'a mut ThreadToken, ) -> TransactionMut<'a, S>
Create a TransactionMut. This operation needs to wait for all other TransactionMuts for this database to be finished.
The implementation uses the unlock_notify feature of sqlite. This makes it work across processes.