2023-07-13 23:09:11 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-03-09 06:58:58 +00:00
|
|
|
use rocksdb::{
|
2024-03-21 01:44:53 +00:00
|
|
|
DBCompressionType, ThreadMode, SingleThreaded, LogLevel, WriteOptions,
|
|
|
|
Transaction as RocksTransaction, Options, OptimisticTransactionDB,
|
2024-03-09 06:58:58 +00:00
|
|
|
};
|
2023-07-13 23:09:11 +00:00
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
2024-07-16 23:42:15 +00:00
|
|
|
#[must_use]
|
2024-03-21 01:44:53 +00:00
|
|
|
pub struct Transaction<'a, T: ThreadMode>(
|
|
|
|
RocksTransaction<'a, OptimisticTransactionDB<T>>,
|
|
|
|
&'a OptimisticTransactionDB<T>,
|
|
|
|
);
|
|
|
|
|
|
|
|
impl<T: ThreadMode> Get for Transaction<'_, T> {
|
2023-07-13 23:09:11 +00:00
|
|
|
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
|
2024-03-21 01:44:53 +00:00
|
|
|
self.0.get(key).expect("couldn't read from RocksDB via transaction")
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 01:44:53 +00:00
|
|
|
impl<T: ThreadMode> DbTxn for Transaction<'_, T> {
|
2023-07-13 23:09:11 +00:00
|
|
|
fn put(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) {
|
2024-03-21 01:44:53 +00:00
|
|
|
self.0.put(key, value).expect("couldn't write to RocksDB via transaction")
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
fn del(&mut self, key: impl AsRef<[u8]>) {
|
2024-03-21 01:44:53 +00:00
|
|
|
self.0.delete(key).expect("couldn't delete from RocksDB via transaction")
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
fn commit(self) {
|
2024-03-21 01:44:53 +00:00
|
|
|
self.0.commit().expect("couldn't commit to RocksDB via transaction");
|
|
|
|
self.1.flush_wal(true).expect("couldn't flush RocksDB WAL");
|
|
|
|
self.1.flush().expect("couldn't flush RocksDB");
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 01:44:53 +00:00
|
|
|
impl<T: ThreadMode> Get for Arc<OptimisticTransactionDB<T>> {
|
2023-07-13 23:09:11 +00:00
|
|
|
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
|
2024-03-21 01:44:53 +00:00
|
|
|
OptimisticTransactionDB::get(self, key).expect("couldn't read from RocksDB")
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 01:44:53 +00:00
|
|
|
impl<T: Send + ThreadMode + 'static> Db for Arc<OptimisticTransactionDB<T>> {
|
|
|
|
type Transaction<'a> = Transaction<'a, T>;
|
2023-07-13 23:09:11 +00:00
|
|
|
fn txn(&mut self) -> Self::Transaction<'_> {
|
2024-03-09 08:18:52 +00:00
|
|
|
let mut opts = WriteOptions::default();
|
|
|
|
opts.set_sync(true);
|
2024-03-21 01:44:53 +00:00
|
|
|
Transaction(self.transaction_opt(&opts, &Default::default()), &**self)
|
2023-07-13 23:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-26 01:39:29 +00:00
|
|
|
|
2024-03-21 01:44:53 +00:00
|
|
|
pub type RocksDB = Arc<OptimisticTransactionDB<SingleThreaded>>;
|
2023-07-26 01:39:29 +00:00
|
|
|
pub fn new_rocksdb(path: &str) -> RocksDB {
|
|
|
|
let mut options = Options::default();
|
2023-07-26 03:00:10 +00:00
|
|
|
options.create_if_missing(true);
|
2024-03-09 06:58:58 +00:00
|
|
|
options.set_compression_type(DBCompressionType::Zstd);
|
|
|
|
|
|
|
|
options.set_wal_compression_type(DBCompressionType::Zstd);
|
2024-03-09 10:05:43 +00:00
|
|
|
// 10 MB
|
|
|
|
options.set_max_total_wal_size(10 * 1024 * 1024);
|
|
|
|
options.set_wal_size_limit_mb(10);
|
2024-03-09 06:58:58 +00:00
|
|
|
|
|
|
|
options.set_log_level(LogLevel::Warn);
|
2024-03-09 10:05:43 +00:00
|
|
|
// 1 MB
|
2024-03-09 06:58:58 +00:00
|
|
|
options.set_max_log_file_size(1024 * 1024);
|
|
|
|
options.set_recycle_log_file_num(1);
|
|
|
|
|
2024-03-21 01:44:53 +00:00
|
|
|
Arc::new(OptimisticTransactionDB::open(&options, path).unwrap())
|
2023-07-26 01:39:29 +00:00
|
|
|
}
|