use WriteBatchWithTransaction for batched insertions.
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
fcca352795
commit
10336f9af6
1 changed files with 13 additions and 4 deletions
|
@ -4,7 +4,10 @@ use std::{
|
||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
};
|
};
|
||||||
|
|
||||||
use rust_rocksdb::LogLevel::{Debug, Error, Fatal, Info, Warn};
|
use rust_rocksdb::{
|
||||||
|
LogLevel::{Debug, Error, Fatal, Info, Warn},
|
||||||
|
WriteBatchWithTransaction,
|
||||||
|
};
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
|
|
||||||
use super::{super::Config, watchers::Watchers, KeyValueDatabaseEngine, KvTree};
|
use super::{super::Config, watchers::Watchers, KeyValueDatabaseEngine, KvTree};
|
||||||
|
@ -223,11 +226,13 @@ impl KvTree for RocksDbEngineTree<'_> {
|
||||||
fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
|
fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
|
||||||
let writeoptions = rust_rocksdb::WriteOptions::default();
|
let writeoptions = rust_rocksdb::WriteOptions::default();
|
||||||
|
|
||||||
|
let mut batch = WriteBatchWithTransaction::<false>::default();
|
||||||
|
|
||||||
for (key, value) in iter {
|
for (key, value) in iter {
|
||||||
self.db.rocks.put_cf_opt(&self.cf(), key, value, &writeoptions)?;
|
batch.put_cf(&self.cf(), key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(self.db.rocks.write_opt(batch, &writeoptions)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||||
|
@ -293,14 +298,18 @@ impl KvTree for RocksDbEngineTree<'_> {
|
||||||
readoptions.set_total_order_seek(true);
|
readoptions.set_total_order_seek(true);
|
||||||
let writeoptions = rust_rocksdb::WriteOptions::default();
|
let writeoptions = rust_rocksdb::WriteOptions::default();
|
||||||
|
|
||||||
|
let mut batch = WriteBatchWithTransaction::<false>::default();
|
||||||
|
|
||||||
let lock = self.write_lock.write().unwrap();
|
let lock = self.write_lock.write().unwrap();
|
||||||
|
|
||||||
for key in iter {
|
for key in iter {
|
||||||
let old = self.db.rocks.get_cf_opt(&self.cf(), &key, &readoptions)?;
|
let old = self.db.rocks.get_cf_opt(&self.cf(), &key, &readoptions)?;
|
||||||
let new = utils::increment(old.as_deref()).unwrap();
|
let new = utils::increment(old.as_deref()).unwrap();
|
||||||
self.db.rocks.put_cf_opt(&self.cf(), key, new, &writeoptions)?;
|
batch.put_cf(&self.cf(), key, new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.db.rocks.write_opt(batch, &writeoptions)?;
|
||||||
|
|
||||||
drop(lock);
|
drop(lock);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue