change to rocksdb

This commit is contained in:
open-trade
2021-03-19 11:32:28 +08:00
parent 06309ac068
commit 482b5e0a66
5 changed files with 167 additions and 160 deletions

View File

@@ -65,7 +65,7 @@ impl PeerMap {
fn new() -> ResultType<Self> {
Ok(Self {
map: Default::default(),
db: super::SledAsync::new("./sled.db", true)?,
db: super::SledAsync::new("./hbbs.db", true)?,
})
}

View File

@@ -3,49 +3,50 @@ use hbb_common::{
tokio::{self, sync::mpsc},
ResultType,
};
use rocksdb::DB;
#[derive(Debug)]
enum Action {
Insert((String, Vec<u8>)),
Get((String, mpsc::Sender<Option<sled::IVec>>)),
Get((String, mpsc::Sender<Option<Vec<u8>>>)),
_Close,
}
#[derive(Clone)]
pub struct SledAsync {
db: sled::Db,
tx: Option<mpsc::UnboundedSender<Action>>,
path: String,
}
impl SledAsync {
pub fn new(path: &str, run: bool) -> ResultType<Self> {
let mut res = Self {
db: sled::open(path)?,
tx: None,
path: path.to_owned(),
};
if run {
res.run();
res.run()?;
}
Ok(res)
}
pub fn run(&mut self) -> std::thread::JoinHandle<()> {
pub fn run(&mut self) -> ResultType<std::thread::JoinHandle<()>> {
let (tx, rx) = mpsc::unbounded_channel::<Action>();
self.tx = Some(tx);
let db = self.db.clone();
std::thread::spawn(move || {
let db = DB::open_default(&self.path)?;
Ok(std::thread::spawn(move || {
Self::io_loop(db, rx);
log::debug!("Exit SledAsync loop");
})
}))
}
#[tokio::main(basic_scheduler)]
async fn io_loop(db: sled::Db, rx: mpsc::UnboundedReceiver<Action>) {
async fn io_loop(db: DB, rx: mpsc::UnboundedReceiver<Action>) {
let mut rx = rx;
while let Some(x) = rx.recv().await {
match x {
Action::Insert((key, value)) => {
allow_err!(db.insert(key, value));
allow_err!(db.put(&key, &value));
}
Action::Get((key, sender)) => {
let mut sender = sender;
@@ -67,9 +68,9 @@ impl SledAsync {
allow_err!(j.join());
}
pub async fn get(&mut self, key: String) -> Option<sled::IVec> {
pub async fn get(&mut self, key: String) -> Option<Vec<u8>> {
if let Some(tx) = &self.tx {
let (tx_once, mut rx) = mpsc::channel::<Option<sled::IVec>>(1);
let (tx_once, mut rx) = mpsc::channel::<Option<Vec<u8>>>(1);
allow_err!(tx.send(Action::Get((key, tx_once))));
if let Some(v) = rx.recv().await {
return v;
@@ -79,7 +80,7 @@ impl SledAsync {
}
#[inline]
pub fn deserialize<'a, T: serde::Deserialize<'a>>(v: &'a Option<sled::IVec>) -> Option<T> {
pub fn deserialize<'a, T: serde::Deserialize<'a>>(v: &'a Option<Vec<u8>>) -> Option<T> {
if let Some(v) = v {
if let Ok(v) = std::str::from_utf8(v) {
if let Ok(v) = serde_json::from_str::<T>(&v) {