A lightweight, embedded key-value database for mobile clients (i.e., iOS, Android), written in Rust.
ThetaDB is suitable for use on mobile clients with "High-Read, Low-Write" demands,
it uses B+ Tree as the foundational layer for index management.
Inspired by Go's BoltDB, ThetaDB uses mmap, relying
on the operating system to keep memory and database files in sync. ThetaDB also implements
shadow paging to guarantee atomicity and durability of transactions, preventing data loss
or damage to the internal structures of database.
Open Database
Use following way to open the database at the specified path. If the database file does not exist, ThetaDB will automatically create and initialize it.
use ;
#
#
ThetaDB will automatically close when the database instance is destroyed.
Get, Insert, Update, Delete
# use ;
#
#
Transaction
ThetaDB has two kinds of transactions: Read-Only Transaction and Read-Write Transaction.
The read-only transaction allows for read-only access and the read-write transaction allows
modification.
ThetaDB allows a number of read-only transactions at a time but allows at most one read-write
transaction at a time. When a read-write transaction is committing, it has exclusive access
to the database until the commit is completed, at which point other transactions trying to
access the database will be blocked. You can think of this situation as shared access and
exclusive access to reader-writer lock.
Read-Only Transaction
# use ;
#
#
Read-Write Transaction
ThetaBD's read-write transactions are designed to automatically rollback, and therefore any
changes made to the transaction will be discarded unless you explicity call the commit
method.
Or you can perform a read-write transaction using closure, if no errors occur, then the transaction will be commit automatically after the closure call.
# use ;
#
#
Attention
❗️ Transaction instances are nonsendable, which means it's not safe to send them to another
thread. Rust leverages Ownership system and the Send and Sync traits to enforce
requirements automatically, whereas Swift requires us to manually ensure these guarantees.
❗️ Read-only transactions and read-write transaction must not overlap, otherwise a deadlock will be occurred.
😺 So ThetaDB recommends that if you want to use transactions, use the APIs with closure
parameter (i.e., view, update).
Cursor
We can freely traverse the data in the ThetaDB using Cursor.
For instance, we can iterate over all the key-value pairs in the ThetaDB like this:
# use ;
#
#
Or we can perform range queries on ThetaDB in this way:
# use ;
#
#
Attention
❗️ Cursor is also a transaction (can be understood as a read-only transaction), so it alse follows the transaction considerations mentioned above.