Makes sure that all data written to RocksDB use network endian

pull/10/head
Tpt 6 years ago
parent 38a8f97254
commit cec5075777
  1. 20
      src/store/rocksdb.rs
  2. 19
      src/utils.rs

@ -1,3 +1,5 @@
use byteorder::ByteOrder;
use byteorder::NetworkEndian;
use errors::*; use errors::*;
use rocksdb::ColumnFamily; use rocksdb::ColumnFamily;
use rocksdb::DBRawIterator; use rocksdb::DBRawIterator;
@ -6,16 +8,12 @@ use rocksdb::Options;
use rocksdb::WriteBatch; use rocksdb::WriteBatch;
use rocksdb::DB; use rocksdb::DB;
use std::io::Cursor; use std::io::Cursor;
use std::mem::size_of;
use std::path::Path; use std::path::Path;
use std::str; use std::str;
use std::sync::Mutex; use std::sync::Mutex;
use store::numeric_encoder::*; use store::numeric_encoder::*;
use store::store::EncodedQuadsStore; use store::store::EncodedQuadsStore;
use store::store::StoreDataset; use store::store::StoreDataset;
use utils::from_bytes;
use utils::from_bytes_slice;
use utils::to_bytes;
pub type RocksDbDataset = StoreDataset<RocksDbStore>; pub type RocksDbDataset = StoreDataset<RocksDbStore>;
@ -77,7 +75,7 @@ impl BytesStore for RocksDbStore {
fn insert_bytes(&self, value: &[u8]) -> Result<u64> { fn insert_bytes(&self, value: &[u8]) -> Result<u64> {
Ok(match self.db.get_cf(self.str2id_cf, value)? { Ok(match self.db.get_cf(self.str2id_cf, value)? {
Some(id) => from_bytes_slice(&id), Some(id) => NetworkEndian::read_u64(&id),
None => { None => {
let id = self.str_id_counter.lock()?.get_and_increment(&self.db)? as u64; let id = self.str_id_counter.lock()?.get_and_increment(&self.db)? as u64;
let id_bytes = to_bytes(id); let id_bytes = to_bytes(id);
@ -353,11 +351,7 @@ impl RocksDBCounter {
fn get_and_increment(&self, db: &DB) -> Result<u64> { fn get_and_increment(&self, db: &DB) -> Result<u64> {
let value = db let value = db
.get(self.name.as_bytes())? .get(self.name.as_bytes())?
.map(|b| { .map(|b| NetworkEndian::read_u64(&b))
let mut buf = [0 as u8; size_of::<usize>()];
buf.copy_from_slice(&b);
from_bytes(buf)
})
.unwrap_or(0); .unwrap_or(0);
db.put(self.name.as_bytes(), &to_bytes(value + 1))?; db.put(self.name.as_bytes(), &to_bytes(value + 1))?;
Ok(value) Ok(value)
@ -527,3 +521,9 @@ impl<I: Iterator<Item = Result<EncodedQuad>>> Iterator for InGraphQuadsIterator<
}) })
} }
} }
fn to_bytes(int: u64) -> [u8; 8] {
let mut buf = [0 as u8; 8];
NetworkEndian::write_u64(&mut buf, int);
buf
}

@ -1,6 +1,3 @@
use std::mem::size_of;
use std::mem::transmute;
pub trait Escaper { pub trait Escaper {
fn escape(&self) -> String; fn escape(&self) -> String;
} }
@ -80,19 +77,3 @@ impl ExactSizeIterator for EscapeRDF {
} }
} }
} }
pub fn to_bytes(int: u64) -> [u8; size_of::<u64>()] {
//TODO: remove when next rust version stabilize this method
unsafe { transmute(int) }
}
pub fn from_bytes(bytes: [u8; size_of::<u64>()]) -> u64 {
//TODO: remove when next rust version stabilize this method
unsafe { transmute(bytes) }
}
pub fn from_bytes_slice(bytes: &[u8]) -> u64 {
let mut buf = [0 as u8; size_of::<u64>()];
buf.copy_from_slice(bytes);
from_bytes(buf)
}

Loading…
Cancel
Save