2023-07-13 23:09:11 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-03-09 06:58:58 +00:00
|
|
|
use rocksdb::{
|
|
|
|
DBCompressionType, ThreadMode, SingleThreaded, LogLevel, Options, Transaction, TransactionDB,
|
|
|
|
};
|
2023-07-13 23:09:11 +00:00
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
impl<T: ThreadMode> Get for Transaction<'_, TransactionDB<T>> {
|
|
|
|
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
|
|
|
|
self.get(key).expect("couldn't read from RocksDB via transaction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T: ThreadMode> DbTxn for Transaction<'_, TransactionDB<T>> {
|
|
|
|
fn put(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) {
|
|
|
|
Transaction::put(self, key, value).expect("couldn't write to RocksDB via transaction")
|
|
|
|
}
|
|
|
|
fn del(&mut self, key: impl AsRef<[u8]>) {
|
|
|
|
self.delete(key).expect("couldn't delete from RocksDB via transaction")
|
|
|
|
}
|
|
|
|
fn commit(self) {
|
|
|
|
Transaction::commit(self).expect("couldn't commit to RocksDB via transaction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ThreadMode> Get for Arc<TransactionDB<T>> {
|
|
|
|
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
|
|
|
|
TransactionDB::get(self, key).expect("couldn't read from RocksDB")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T: ThreadMode + 'static> Db for Arc<TransactionDB<T>> {
|
|
|
|
type Transaction<'a> = Transaction<'a, TransactionDB<T>>;
|
|
|
|
fn txn(&mut self) -> Self::Transaction<'_> {
|
|
|
|
self.transaction()
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 01:39:29 +00:00
|
|
|
|
|
|
|
pub type RocksDB = Arc<TransactionDB<SingleThreaded>>;
|
|
|
|
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);
|
|
|
|
|
2024-03-08 14:19:34 +00:00
|
|
|
// 128 MB
|
2024-03-09 06:58:58 +00:00
|
|
|
options.set_wal_compression_type(DBCompressionType::Zstd);
|
|
|
|
options.set_max_total_wal_size(128 * 1024 * 1024);
|
|
|
|
|
|
|
|
// 1 MB
|
|
|
|
options.set_log_level(LogLevel::Warn);
|
|
|
|
options.set_max_log_file_size(1024 * 1024);
|
|
|
|
options.set_recycle_log_file_num(1);
|
|
|
|
|
2023-07-26 01:39:29 +00:00
|
|
|
Arc::new(TransactionDB::open(&options, &Default::default(), path).unwrap())
|
|
|
|
}
|