|
|
@@ -1,27 +1,21 @@
|
|
|
-use std::path::Path;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
+use crate::rocks::{columns, IteratorMode, Rocks};
|
|
|
use crate::serial::{deserialize, serialize};
|
|
|
use crate::{slab::Slab, Result};
|
|
|
|
|
|
-use rocksdb::{IteratorMode, Options, DB};
|
|
|
-
|
|
|
pub struct SlabStore {
|
|
|
- db: DB,
|
|
|
+ rocks: Rocks,
|
|
|
}
|
|
|
|
|
|
impl SlabStore {
|
|
|
- pub fn new(path: &Path) -> Result<Arc<Self>> {
|
|
|
- let mut opt = Options::default();
|
|
|
- opt.create_if_missing(true);
|
|
|
-
|
|
|
- let db = DB::open(&opt, path)?;
|
|
|
-
|
|
|
- Ok(Arc::new(SlabStore { db }))
|
|
|
+ pub fn new(rocks: Rocks) -> Result<Arc<Self>> {
|
|
|
+ Ok(Arc::new(SlabStore { rocks }))
|
|
|
}
|
|
|
|
|
|
pub fn get(&self, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
|
|
|
- let value = self.db.get(key)?;
|
|
|
+ let cf = self.rocks.cf_handle::<columns::Slabs>()?;
|
|
|
+ let value = self.rocks.get_cf(cf, key)?;
|
|
|
Ok(value)
|
|
|
}
|
|
|
|
|
|
@@ -29,9 +23,13 @@ impl SlabStore {
|
|
|
let slab: Slab = deserialize(&value)?;
|
|
|
let last_index = self.get_last_index()?;
|
|
|
let key = last_index + 1;
|
|
|
+
|
|
|
if slab.get_index() == key {
|
|
|
let key = serialize(&key);
|
|
|
- self.db.put(key.clone(), value)?;
|
|
|
+
|
|
|
+ let cf = self.rocks.cf_handle::<columns::Slabs>()?;
|
|
|
+ self.rocks.put_cf(cf, key.clone(), value)?;
|
|
|
+
|
|
|
Ok(Some(key))
|
|
|
} else {
|
|
|
Ok(None)
|
|
|
@@ -39,7 +37,7 @@ impl SlabStore {
|
|
|
}
|
|
|
|
|
|
pub fn get_value_deserialized(&self, key: Vec<u8>) -> Result<Option<Slab>> {
|
|
|
- let value = self.db.get(key)?;
|
|
|
+ let value = self.get(key)?;
|
|
|
match value {
|
|
|
Some(v) => {
|
|
|
let v: Slab = deserialize(&v)?;
|
|
|
@@ -50,7 +48,8 @@ impl SlabStore {
|
|
|
}
|
|
|
|
|
|
pub fn get_last_index(&self) -> Result<u64> {
|
|
|
- let last_index = self.db.iterator(IteratorMode::End).next();
|
|
|
+ let cf = self.rocks.cf_handle::<columns::Slabs>()?;
|
|
|
+ let last_index = self.rocks.iterator(cf, IteratorMode::End).next();
|
|
|
match last_index {
|
|
|
Some((index, _)) => Ok(deserialize(&index)?),
|
|
|
None => Ok(0),
|
|
|
@@ -58,15 +57,11 @@ impl SlabStore {
|
|
|
}
|
|
|
|
|
|
pub fn get_last_index_as_bytes(&self) -> Result<Vec<u8>> {
|
|
|
- let last_index = self.db.iterator(IteratorMode::End).next();
|
|
|
+ let cf = self.rocks.cf_handle::<columns::Slabs>()?;
|
|
|
+ let last_index = self.rocks.iterator(cf, IteratorMode::End).next();
|
|
|
match last_index {
|
|
|
Some((index, _)) => Ok(index.to_vec()),
|
|
|
None => Ok(serialize::<u64>(&0)),
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- pub fn destroy(path: &Path) -> Result<()> {
|
|
|
- DB::destroy(&Options::default(), path)?;
|
|
|
- Ok(())
|
|
|
- }
|
|
|
}
|