Debugs code

pull/10/head
Tpt 6 years ago
parent c865cb3ee9
commit 85556451e7
  1. 1
      src/errors.rs
  2. 7
      src/store/numeric_encoder.rs
  3. 18
      src/store/rocksdb/mod.rs
  4. 42
      src/store/rocksdb/storage.rs
  5. 5
      src/utils.rs

@ -4,5 +4,6 @@ error_chain! {
Uuid(::uuid::ParseError);
RocksDB(::rocksdb::Error);
Utf8(::std::str::Utf8Error);
Io(::std::io::Error);
}
}

@ -157,7 +157,7 @@ impl<S: BytesStore> Encoder<S> {
pub fn encode_blank_node(&self, blank_node: &BlankNode) -> Result<EncodedTerm> {
let mut bytes = [0 as u8; TERM_ENCODING_SIZE];
bytes[0] = TYPE_BLANK_NODE_ID;
bytes[TYPE_KEY_SIZE..TERM_ENCODING_SIZE].copy_from_slice(blank_node.as_bytes());
bytes[TYPE_KEY_SIZE..].copy_from_slice(blank_node.as_bytes());
Ok(EncodedTerm(bytes))
}
@ -332,10 +332,7 @@ mod test {
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::str::FromStr;
use store::numeric_encoder::BytesStore;
use store::numeric_encoder::Encoder;
use store::numeric_encoder::STRING_KEY_SIZE;
use store::numeric_encoder::TERM_ENCODING_SIZE;
use store::numeric_encoder::*;
use utils::to_bytes;
#[derive(Default)]

@ -2,18 +2,10 @@ mod storage;
use errors::*;
use model::*;
use std::ops::Deref;
use std::path::Path;
use std::slice;
use std::str;
use store::numeric_encoder::BytesStore;
use store::numeric_encoder::EncodedQuad;
use store::numeric_encoder::EncodedTerm;
use store::numeric_encoder::Encoder;
use store::numeric_encoder::STRING_KEY_SIZE;
use store::numeric_encoder::TERM_ENCODING_SIZE;
use store::rocksdb::storage::*;
use utils::to_bytes;
pub struct RocksDbDataset {
store: RocksDbStore,
@ -41,7 +33,7 @@ impl RocksDbDataset {
RocksDbUnionGraph { store: &self.store }
}
fn iter(&self) -> Result<QuadsIterator<SPOGIndexIterator>> {
pub fn iter(&self) -> Result<QuadsIterator<SPOGIndexIterator>> {
Ok(QuadsIterator {
iter: self.store.quads()?,
encoder: self.store.encoder(),
@ -59,16 +51,16 @@ impl RocksDbDataset {
})
}
fn contains(&self, quad: &Quad) -> Result<bool> {
pub fn contains(&self, quad: &Quad) -> Result<bool> {
self.store
.contains(&self.store.encoder().encode_quad(quad)?)
}
fn insert(&self, quad: &Quad) -> Result<()> {
pub fn insert(&self, quad: &Quad) -> Result<()> {
self.store.insert(&self.store.encoder().encode_quad(quad)?)
}
fn remove(&self, quad: &Quad) -> Result<()> {
pub fn remove(&self, quad: &Quad) -> Result<()> {
self.store.remove(&self.store.encoder().encode_quad(quad)?)
}
}
@ -86,7 +78,7 @@ struct RocksDbUnionGraph<'a> {
store: &'a RocksDbStore,
}
struct QuadsIterator<'a, I: Iterator<Item = Result<EncodedQuad>>> {
pub struct QuadsIterator<'a, I: Iterator<Item = Result<EncodedQuad>>> {
iter: I,
encoder: Encoder<RocksDbBytesStore<'a>>,
}

@ -2,20 +2,14 @@ use errors::*;
use rocksdb::ColumnFamily;
use rocksdb::DBRawIterator;
use rocksdb::DBVector;
use rocksdb::IteratorMode;
use rocksdb::Options;
use rocksdb::WriteBatch;
use rocksdb::DB;
use std::ops::Deref;
use std::path::Path;
use std::slice;
use std::str;
use store::numeric_encoder::BytesStore;
use store::numeric_encoder::EncodedQuad;
use store::numeric_encoder::EncodedTerm;
use store::numeric_encoder::Encoder;
use store::numeric_encoder::STRING_KEY_SIZE;
use store::numeric_encoder::TERM_ENCODING_SIZE;
use std::sync::Mutex;
use store::numeric_encoder::*;
use utils::from_bytes;
use utils::to_bytes;
const ID2STR_CF: &'static str = "id2str";
@ -32,6 +26,7 @@ const COLUMN_FAMILIES: [&'static str; 5] = [ID2STR_CF, STR2ID_CF, SPOG_CF, POSG_
pub struct RocksDbStore {
db: DB,
counter_mutex: Mutex<()>,
id2str_cf: ColumnFamily,
str2id_cf: ColumnFamily,
spog_cf: ColumnFamily,
@ -41,7 +36,9 @@ pub struct RocksDbStore {
impl RocksDbStore {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let options = Options::default();
let mut options = Options::default();
options.create_if_missing(true);
options.create_missing_column_families(true);
let db = DB::open_cf(&options, path, &COLUMN_FAMILIES)?;
let id2str_cf = get_cf(&db, STR2ID_CF)?;
@ -52,6 +49,7 @@ impl RocksDbStore {
Ok(Self {
db,
counter_mutex: Mutex::new(()),
id2str_cf,
str2id_cf,
spog_cf,
@ -174,6 +172,25 @@ pub fn get_cf(db: &DB, name: &str) -> Result<ColumnFamily> {
pub struct RocksDbBytesStore<'a>(&'a RocksDbStore);
impl<'a> RocksDbBytesStore<'a> {
fn new_id(&self) -> Result<[u8; STRING_KEY_SIZE]> {
let _ = self.0.counter_mutex.lock();
let id = self.0
.db
.get(b"bsc")?
.map(|v| {
let mut id = [0 as u8; STRING_KEY_SIZE];
id.copy_from_slice(&v);
id
})
.unwrap_or_else(|| [0 as u8; STRING_KEY_SIZE]);
self.0
.db
.put(b"bsc", &to_bytes(from_bytes(id.clone()) + 1))?;
Ok(id)
}
}
impl<'a> BytesStore for RocksDbBytesStore<'a> {
type BytesOutput = DBVector;
@ -181,9 +198,8 @@ impl<'a> BytesStore for RocksDbBytesStore<'a> {
match self.0.db.get_cf(self.0.str2id_cf, value)? {
Some(id) => id_buffer.copy_from_slice(&id),
None => {
let id = self.new_id()?;
let mut batch = WriteBatch::default();
// TODO: id allocation
let id = [0 as u8; STRING_KEY_SIZE];
batch.put_cf(self.0.id2str_cf, &id, value)?;
batch.put_cf(self.0.str2id_cf, value, &id)?;
self.0.db.write(batch)?;
@ -260,7 +276,7 @@ fn encode_term_triple(
let mut bytes = [0 as u8; 3 * TERM_ENCODING_SIZE];
bytes[0..TERM_ENCODING_SIZE].copy_from_slice(t1.as_ref());
bytes[TERM_ENCODING_SIZE..2 * TERM_ENCODING_SIZE].copy_from_slice(t2.as_ref());
bytes[2 * TERM_ENCODING_SIZE..3 * TERM_ENCODING_SIZE].copy_from_slice(t2.as_ref());
bytes[2 * TERM_ENCODING_SIZE..3 * TERM_ENCODING_SIZE].copy_from_slice(t3.as_ref());
bytes
}

@ -85,3 +85,8 @@ pub fn to_bytes(int: usize) -> [u8; size_of::<usize>()] {
//TODO: remove when next rust version stabilize this method
unsafe { transmute(int) }
}
pub fn from_bytes(bytes: [u8; size_of::<usize>()]) -> usize {
//TODO: remove when next rust version stabilize this method
unsafe { transmute(bytes) }
}

Loading…
Cancel
Save