From cec5075777b3fff0bf8a0f8584c05115911ffdc3 Mon Sep 17 00:00:00 2001 From: Tpt Date: Thu, 13 Sep 2018 16:25:04 +0200 Subject: [PATCH] Makes sure that all data written to RocksDB use network endian --- src/store/rocksdb.rs | 20 ++++++++++---------- src/utils.rs | 19 ------------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/store/rocksdb.rs b/src/store/rocksdb.rs index be492519..c23a73f3 100644 --- a/src/store/rocksdb.rs +++ b/src/store/rocksdb.rs @@ -1,3 +1,5 @@ +use byteorder::ByteOrder; +use byteorder::NetworkEndian; use errors::*; use rocksdb::ColumnFamily; use rocksdb::DBRawIterator; @@ -6,16 +8,12 @@ use rocksdb::Options; use rocksdb::WriteBatch; use rocksdb::DB; use std::io::Cursor; -use std::mem::size_of; use std::path::Path; use std::str; use std::sync::Mutex; use store::numeric_encoder::*; use store::store::EncodedQuadsStore; use store::store::StoreDataset; -use utils::from_bytes; -use utils::from_bytes_slice; -use utils::to_bytes; pub type RocksDbDataset = StoreDataset; @@ -77,7 +75,7 @@ impl BytesStore for RocksDbStore { fn insert_bytes(&self, value: &[u8]) -> Result { Ok(match self.db.get_cf(self.str2id_cf, value)? { - Some(id) => from_bytes_slice(&id), + Some(id) => NetworkEndian::read_u64(&id), None => { let id = self.str_id_counter.lock()?.get_and_increment(&self.db)? as u64; let id_bytes = to_bytes(id); @@ -353,11 +351,7 @@ impl RocksDBCounter { fn get_and_increment(&self, db: &DB) -> Result { let value = db .get(self.name.as_bytes())? - .map(|b| { - let mut buf = [0 as u8; size_of::()]; - buf.copy_from_slice(&b); - from_bytes(buf) - }) + .map(|b| NetworkEndian::read_u64(&b)) .unwrap_or(0); db.put(self.name.as_bytes(), &to_bytes(value + 1))?; Ok(value) @@ -527,3 +521,9 @@ impl>> Iterator for InGraphQuadsIterator< }) } } + +fn to_bytes(int: u64) -> [u8; 8] { + let mut buf = [0 as u8; 8]; + NetworkEndian::write_u64(&mut buf, int); + buf +} diff --git a/src/utils.rs b/src/utils.rs index ec61d49f..5f1a00fb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,3 @@ -use std::mem::size_of; -use std::mem::transmute; - pub trait Escaper { fn escape(&self) -> String; } @@ -80,19 +77,3 @@ impl ExactSizeIterator for EscapeRDF { } } } - -pub fn to_bytes(int: u64) -> [u8; size_of::()] { - //TODO: remove when next rust version stabilize this method - unsafe { transmute(int) } -} - -pub fn from_bytes(bytes: [u8; size_of::()]) -> 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::()]; - buf.copy_from_slice(bytes); - from_bytes(buf) -}