pub struct TransactionDB<T: ThreadMode = SingleThreaded> { /* private fields */ }Expand description
RocksDB TransactionDB.
Please read the official guide to learn more about RocksDB TransactionDB.
The default thread mode for TransactionDB is SingleThreaded
if feature multi-threaded-cf is not enabled.
use rust_rocksdb::{DB, Options, TransactionDB, SingleThreaded};
let tempdir = tempfile::Builder::new()
.prefix("_path_for_transaction_db")
.tempdir()
.expect("Failed to create temporary path for the _path_for_transaction_db");
let path = tempdir.path();
{
let db: TransactionDB = TransactionDB::open_default(path).unwrap();
db.put(b"my key", b"my value").unwrap();
// create transaction
let txn = db.transaction();
txn.put(b"key2", b"value2");
txn.put(b"key3", b"value3");
txn.commit().unwrap();
}
let _ = DB::destroy(&Options::default(), path);A Snapshot must not outlive the TransactionDB it was created from:
use rust_rocksdb::{SingleThreaded, TransactionDB};
let _snapshot = {
let db = TransactionDB::<SingleThreaded>::open_default("foo").unwrap();
db.snapshot()
};Implementations§
Source§impl<T: ThreadMode> TransactionDB<T>
impl<T: ThreadMode> TransactionDB<T>
Sourcepub fn open_default<P: AsRef<Path>>(path: P) -> Result<Self, Error>
pub fn open_default<P: AsRef<Path>>(path: P) -> Result<Self, Error>
Opens a database with default options.
Sourcepub fn open<P: AsRef<Path>>(
opts: &Options,
txn_db_opts: &TransactionDBOptions,
path: P,
) -> Result<Self, Error>
pub fn open<P: AsRef<Path>>( opts: &Options, txn_db_opts: &TransactionDBOptions, path: P, ) -> Result<Self, Error>
Opens the database with the specified options.
Sourcepub fn open_cf<P, I, N>(
opts: &Options,
txn_db_opts: &TransactionDBOptions,
path: P,
cfs: I,
) -> Result<Self, Error>
pub fn open_cf<P, I, N>( opts: &Options, txn_db_opts: &TransactionDBOptions, path: P, cfs: I, ) -> Result<Self, Error>
Opens a database with the given database options and column family names.
Column families opened using this function will be created with default Options.
Sourcepub fn open_cf_descriptors<P, I>(
opts: &Options,
txn_db_opts: &TransactionDBOptions,
path: P,
cfs: I,
) -> Result<Self, Error>
pub fn open_cf_descriptors<P, I>( opts: &Options, txn_db_opts: &TransactionDBOptions, path: P, cfs: I, ) -> Result<Self, Error>
Opens a database with the given database options and column family descriptors.
pub fn list_cf<P: AsRef<Path>>( opts: &Options, path: P, ) -> Result<Vec<String>, Error>
pub fn destroy<P: AsRef<Path>>(opts: &Options, path: P) -> Result<(), Error>
pub fn repair<P: AsRef<Path>>(opts: &Options, path: P) -> Result<(), Error>
pub fn path(&self) -> &Path
Sourcepub fn flush_wal(&self, sync: bool) -> Result<(), Error>
pub fn flush_wal(&self, sync: bool) -> Result<(), Error>
Flushes the WAL buffer. If sync is set to true, also syncs
the data to disk.
Sourcepub fn flush_opt(&self, flushopts: &FlushOptions) -> Result<(), Error>
pub fn flush_opt(&self, flushopts: &FlushOptions) -> Result<(), Error>
Flushes database memtables to SST files on the disk.
Sourcepub fn flush(&self) -> Result<(), Error>
pub fn flush(&self) -> Result<(), Error>
Flushes database memtables to SST files on the disk using default options.
Sourcepub fn flush_cf_opt(
&self,
cf: &impl AsColumnFamilyRef,
flushopts: &FlushOptions,
) -> Result<(), Error>
pub fn flush_cf_opt( &self, cf: &impl AsColumnFamilyRef, flushopts: &FlushOptions, ) -> Result<(), Error>
Flushes database memtables to SST files on the disk for a given column family.
Sourcepub fn flush_cfs_opt(
&self,
cfs: &[&impl AsColumnFamilyRef],
opts: &FlushOptions,
) -> Result<(), Error>
pub fn flush_cfs_opt( &self, cfs: &[&impl AsColumnFamilyRef], opts: &FlushOptions, ) -> Result<(), Error>
Flushes multiple column families.
If atomic flush is not enabled, it is equivalent to calling flush_cf multiple times.
If atomic flush is enabled, it will flush all column families specified in cfs up to the latest sequence
number at the time when flush is requested.
Sourcepub fn flush_cf(&self, cf: &impl AsColumnFamilyRef) -> Result<(), Error>
pub fn flush_cf(&self, cf: &impl AsColumnFamilyRef) -> Result<(), Error>
Flushes database memtables to SST files on the disk for a given column family using default options.
Sourcepub fn transaction(&self) -> Transaction<'_, Self>
pub fn transaction(&self) -> Transaction<'_, Self>
Creates a transaction with default options.
Sourcepub fn transaction_opt<'a>(
&'a self,
write_opts: &WriteOptions,
txn_opts: &TransactionOptions,
) -> Transaction<'a, Self>
pub fn transaction_opt<'a>( &'a self, write_opts: &WriteOptions, txn_opts: &TransactionOptions, ) -> Transaction<'a, Self>
Creates a transaction with options.
Sourcepub fn prepared_transactions(&self) -> Vec<Transaction<'_, Self>>
pub fn prepared_transactions(&self) -> Vec<Transaction<'_, Self>>
Get all prepared transactions for recovery.
This function is expected to call once after open database. User should commit or rollback all transactions before start other transactions.
Sourcepub fn get<K: AsRef<[u8]>>(&self, key: K) -> Result<Option<Vec<u8>>, Error>
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Result<Option<Vec<u8>>, Error>
Returns the bytes associated with a key value.
Sourcepub fn get_cf<K: AsRef<[u8]>>(
&self,
cf: &impl AsColumnFamilyRef,
key: K,
) -> Result<Option<Vec<u8>>, Error>
pub fn get_cf<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, ) -> Result<Option<Vec<u8>>, Error>
Returns the bytes associated with a key value and the given column family.
Sourcepub fn get_opt<K: AsRef<[u8]>>(
&self,
key: K,
readopts: &ReadOptions,
) -> Result<Option<Vec<u8>>, Error>
pub fn get_opt<K: AsRef<[u8]>>( &self, key: K, readopts: &ReadOptions, ) -> Result<Option<Vec<u8>>, Error>
Returns the bytes associated with a key value with read options.
Sourcepub fn get_cf_opt<K: AsRef<[u8]>>(
&self,
cf: &impl AsColumnFamilyRef,
key: K,
readopts: &ReadOptions,
) -> Result<Option<Vec<u8>>, Error>
pub fn get_cf_opt<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, readopts: &ReadOptions, ) -> Result<Option<Vec<u8>>, Error>
Returns the bytes associated with a key value and the given column family with read options.
pub fn get_pinned<K: AsRef<[u8]>>( &self, key: K, ) -> Result<Option<DBPinnableSlice<'_>>, Error>
Sourcepub fn get_pinned_cf<K: AsRef<[u8]>>(
&self,
cf: &impl AsColumnFamilyRef,
key: K,
) -> Result<Option<DBPinnableSlice<'_>>, Error>
pub fn get_pinned_cf<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, ) -> Result<Option<DBPinnableSlice<'_>>, Error>
Returns the bytes associated with a key value and the given column family.
Sourcepub fn get_pinned_opt<K: AsRef<[u8]>>(
&self,
key: K,
readopts: &ReadOptions,
) -> Result<Option<DBPinnableSlice<'_>>, Error>
pub fn get_pinned_opt<K: AsRef<[u8]>>( &self, key: K, readopts: &ReadOptions, ) -> Result<Option<DBPinnableSlice<'_>>, Error>
Returns the bytes associated with a key value with read options.
Sourcepub fn get_pinned_cf_opt<K: AsRef<[u8]>>(
&self,
cf: &impl AsColumnFamilyRef,
key: K,
readopts: &ReadOptions,
) -> Result<Option<DBPinnableSlice<'_>>, Error>
pub fn get_pinned_cf_opt<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, readopts: &ReadOptions, ) -> Result<Option<DBPinnableSlice<'_>>, Error>
Returns the bytes associated with a key value and the given column family with read options.
Sourcepub fn multi_get<K, I>(&self, keys: I) -> Vec<Result<Option<Vec<u8>>, Error>>
pub fn multi_get<K, I>(&self, keys: I) -> Vec<Result<Option<Vec<u8>>, Error>>
Return the values associated with the given keys.
Sourcepub fn multi_get_opt<K, I>(
&self,
keys: I,
readopts: &ReadOptions,
) -> Vec<Result<Option<Vec<u8>>, Error>>
pub fn multi_get_opt<K, I>( &self, keys: I, readopts: &ReadOptions, ) -> Vec<Result<Option<Vec<u8>>, Error>>
Return the values associated with the given keys using read options.
Sourcepub fn multi_get_cf<'a, 'b: 'a, K, I, W>(
&'a self,
keys: I,
) -> Vec<Result<Option<Vec<u8>>, Error>>
pub fn multi_get_cf<'a, 'b: 'a, K, I, W>( &'a self, keys: I, ) -> Vec<Result<Option<Vec<u8>>, Error>>
Return the values associated with the given keys and column families.
Sourcepub fn multi_get_cf_opt<'a, 'b: 'a, K, I, W>(
&'a self,
keys: I,
readopts: &ReadOptions,
) -> Vec<Result<Option<Vec<u8>>, Error>>
pub fn multi_get_cf_opt<'a, 'b: 'a, K, I, W>( &'a self, keys: I, readopts: &ReadOptions, ) -> Vec<Result<Option<Vec<u8>>, Error>>
Return the values associated with the given keys and column families using read options.
pub fn put<K, V>(&self, key: K, value: V) -> Result<(), Error>
pub fn put_cf<K, V>( &self, cf: &impl AsColumnFamilyRef, key: K, value: V, ) -> Result<(), Error>
pub fn put_opt<K, V>( &self, key: K, value: V, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn put_cf_opt<K, V>( &self, cf: &impl AsColumnFamilyRef, key: K, value: V, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn write( &self, batch: &WriteBatchWithTransaction<true>, ) -> Result<(), Error>
pub fn write_opt( &self, batch: &WriteBatchWithTransaction<true>, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn merge<K, V>(&self, key: K, value: V) -> Result<(), Error>
pub fn merge_cf<K, V>( &self, cf: &impl AsColumnFamilyRef, key: K, value: V, ) -> Result<(), Error>
pub fn merge_opt<K, V>( &self, key: K, value: V, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn merge_cf_opt<K, V>( &self, cf: &impl AsColumnFamilyRef, key: K, value: V, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn delete<K: AsRef<[u8]>>(&self, key: K) -> Result<(), Error>
pub fn delete_cf<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, ) -> Result<(), Error>
pub fn delete_opt<K: AsRef<[u8]>>( &self, key: K, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn delete_cf_opt<K: AsRef<[u8]>>( &self, cf: &impl AsColumnFamilyRef, key: K, writeopts: &WriteOptions, ) -> Result<(), Error>
pub fn iterator<'a: 'b, 'b>( &'a self, mode: IteratorMode<'_>, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn iterator_opt<'a: 'b, 'b>( &'a self, mode: IteratorMode<'_>, readopts: ReadOptions, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
Sourcepub fn iterator_cf_opt<'a: 'b, 'b>(
&'a self,
cf_handle: &impl AsColumnFamilyRef,
readopts: ReadOptions,
mode: IteratorMode<'_>,
) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn iterator_cf_opt<'a: 'b, 'b>( &'a self, cf_handle: &impl AsColumnFamilyRef, readopts: ReadOptions, mode: IteratorMode<'_>, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
Opens an iterator using the provided ReadOptions. This is used when you want to iterate over a specific ColumnFamily with a modified ReadOptions
Sourcepub fn full_iterator<'a: 'b, 'b>(
&'a self,
mode: IteratorMode<'_>,
) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn full_iterator<'a: 'b, 'b>( &'a self, mode: IteratorMode<'_>, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
Opens an iterator with set_total_order_seek enabled.
This must be used to iterate across prefixes when set_memtable_factory has been called
with a Hash-based implementation.
pub fn prefix_iterator<'a: 'b, 'b, P: AsRef<[u8]>>( &'a self, prefix: P, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn iterator_cf<'a: 'b, 'b>( &'a self, cf_handle: &impl AsColumnFamilyRef, mode: IteratorMode<'_>, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn full_iterator_cf<'a: 'b, 'b>( &'a self, cf_handle: &impl AsColumnFamilyRef, mode: IteratorMode<'_>, ) -> DBIteratorWithThreadMode<'b, Self> ⓘ
pub fn prefix_iterator_cf<'a, P: AsRef<[u8]>>( &'a self, cf_handle: &impl AsColumnFamilyRef, prefix: P, ) -> DBIteratorWithThreadMode<'a, Self> ⓘ
Sourcepub fn raw_iterator<'a: 'b, 'b>(
&'a self,
) -> DBRawIteratorWithThreadMode<'b, Self>
pub fn raw_iterator<'a: 'b, 'b>( &'a self, ) -> DBRawIteratorWithThreadMode<'b, Self>
Opens a raw iterator over the database, using the default read options
Sourcepub fn raw_iterator_cf<'a: 'b, 'b>(
&'a self,
cf_handle: &impl AsColumnFamilyRef,
) -> DBRawIteratorWithThreadMode<'b, Self>
pub fn raw_iterator_cf<'a: 'b, 'b>( &'a self, cf_handle: &impl AsColumnFamilyRef, ) -> DBRawIteratorWithThreadMode<'b, Self>
Opens a raw iterator over the given column family, using the default read options
Sourcepub fn raw_iterator_opt<'a: 'b, 'b>(
&'a self,
readopts: ReadOptions,
) -> DBRawIteratorWithThreadMode<'b, Self>
pub fn raw_iterator_opt<'a: 'b, 'b>( &'a self, readopts: ReadOptions, ) -> DBRawIteratorWithThreadMode<'b, Self>
Opens a raw iterator over the database, using the given read options
Sourcepub fn raw_iterator_cf_opt<'a: 'b, 'b>(
&'a self,
cf_handle: &impl AsColumnFamilyRef,
readopts: ReadOptions,
) -> DBRawIteratorWithThreadMode<'b, Self>
pub fn raw_iterator_cf_opt<'a: 'b, 'b>( &'a self, cf_handle: &impl AsColumnFamilyRef, readopts: ReadOptions, ) -> DBRawIteratorWithThreadMode<'b, Self>
Opens a raw iterator over the given column family, using the given read options
pub fn snapshot(&self) -> SnapshotWithThreadMode<'_, Self>
Source§impl TransactionDB<SingleThreaded>
impl TransactionDB<SingleThreaded>
Source§impl TransactionDB<MultiThreaded>
impl TransactionDB<MultiThreaded>
Sourcepub fn create_cf<N: AsRef<str>>(
&self,
name: N,
opts: &Options,
) -> Result<(), Error>
pub fn create_cf<N: AsRef<str>>( &self, name: N, opts: &Options, ) -> Result<(), Error>
Creates column family with given name and options.
Sourcepub fn cf_handle(&self, name: &str) -> Option<Arc<BoundColumnFamily<'_>>>
pub fn cf_handle(&self, name: &str) -> Option<Arc<BoundColumnFamily<'_>>>
Returns the underlying column family handle.
Sourcepub fn drop_cf(&self, name: &str) -> Result<(), Error>
pub fn drop_cf(&self, name: &str) -> Result<(), Error>
Drops the column family with the given name by internally locking the inner column
family map. This avoids needing &mut self reference