feat: Move DocumentKey/Attr to their own module

This commit is contained in:
Clément Renault 2018-12-03 15:16:18 +01:00
parent 3d0b334942
commit 8563c25d62
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 101 additions and 94 deletions

View File

@ -0,0 +1,96 @@
use std::io::{Cursor, Read, Write};
use std::mem::size_of;
use byteorder::{NativeEndian, WriteBytesExt, ReadBytesExt};
use crate::index::schema::SchemaAttr;
use crate::DocumentId;
const DOC_KEY_LEN: usize = 4 + size_of::<u64>();
const DOC_KEY_ATTR_LEN: usize = DOC_KEY_LEN + 1 + size_of::<u32>();
#[derive(Copy, Clone)]
pub struct DocumentKey([u8; DOC_KEY_LEN]);
impl DocumentKey {
pub fn new(id: DocumentId) -> DocumentKey {
let mut buffer = [0; DOC_KEY_LEN];
let mut wtr = Cursor::new(&mut buffer[..]);
wtr.write_all(b"doc-").unwrap();
wtr.write_u64::<NativeEndian>(id).unwrap();
DocumentKey(buffer)
}
pub fn from_bytes(mut bytes: &[u8]) -> DocumentKey {
assert!(bytes.len() >= DOC_KEY_LEN);
assert_eq!(&bytes[..4], b"doc-");
let mut buffer = [0; DOC_KEY_LEN];
bytes.read_exact(&mut buffer).unwrap();
DocumentKey(buffer)
}
pub fn with_attribute(&self, attr: SchemaAttr) -> DocumentKeyAttr {
DocumentKeyAttr::new(self.document_id(), attr)
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
}
}
impl AsRef<[u8]> for DocumentKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Copy, Clone)]
pub struct DocumentKeyAttr([u8; DOC_KEY_ATTR_LEN]);
impl DocumentKeyAttr {
pub fn new(id: DocumentId, attr: SchemaAttr) -> DocumentKeyAttr {
let mut buffer = [0; DOC_KEY_ATTR_LEN];
let DocumentKey(raw_key) = DocumentKey::new(id);
let mut wtr = Cursor::new(&mut buffer[..]);
wtr.write_all(&raw_key).unwrap();
wtr.write_all(b"-").unwrap();
wtr.write_u32::<NativeEndian>(attr.as_u32()).unwrap();
DocumentKeyAttr(buffer)
}
pub fn from_bytes(mut bytes: &[u8]) -> DocumentKeyAttr {
assert!(bytes.len() >= DOC_KEY_ATTR_LEN);
assert_eq!(&bytes[..4], b"doc-");
let mut buffer = [0; DOC_KEY_ATTR_LEN];
bytes.read_exact(&mut buffer).unwrap();
DocumentKeyAttr(buffer)
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
}
pub fn attribute(&self) -> SchemaAttr {
let offset = 4 + size_of::<u64>() + 1;
let value = (&self.0[offset..]).read_u32::<NativeEndian>().unwrap();
SchemaAttr::new(value)
}
pub fn into_document_key(self) -> DocumentKey {
DocumentKey::new(self.document_id())
}
}
impl AsRef<[u8]> for DocumentKeyAttr {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

View File

@ -1,27 +1,24 @@
use std::io::{Cursor, Read, Write};
use std::{fmt, marker}; use std::{fmt, marker};
use std::error::Error; use std::error::Error;
use std::mem::size_of;
use std::path::Path; use std::path::Path;
use rocksdb::rocksdb::{DB, Snapshot, DBVector}; use rocksdb::rocksdb::{DB, Snapshot};
use rocksdb::rocksdb_options::ReadOptions; use rocksdb::rocksdb_options::ReadOptions;
use byteorder::{NativeEndian, WriteBytesExt, ReadBytesExt};
use serde::de::{DeserializeOwned, Visitor}; use serde::de::{DeserializeOwned, Visitor};
use serde::de::value::MapDeserializer; use serde::de::value::MapDeserializer;
use serde::forward_to_deserialize_any; use serde::forward_to_deserialize_any;
use crate::index::schema::{Schema, SchemaAttr}; use crate::index::schema::Schema;
use crate::blob::positive::PositiveBlob; use crate::blob::positive::PositiveBlob;
use crate::index::update::Update; use crate::index::update::Update;
use crate::database::document_key::{DocumentKey, DocumentKeyAttr};
use crate::DocumentId; use crate::DocumentId;
pub mod document_key;
const DATA_INDEX: &[u8] = b"data-index"; const DATA_INDEX: &[u8] = b"data-index";
const DATA_SCHEMA: &[u8] = b"data-schema"; const DATA_SCHEMA: &[u8] = b"data-schema";
const DOC_KEY_LEN: usize = 4 + size_of::<u64>();
const DOC_KEY_ATTR_LEN: usize = DOC_KEY_LEN + 1 + size_of::<u32>();
// FIXME Do not panic! // FIXME Do not panic!
fn retrieve_data_schema(snapshot: &Snapshot<&DB>) -> Result<Schema, Box<Error>> { fn retrieve_data_schema(snapshot: &Snapshot<&DB>) -> Result<Schema, Box<Error>> {
match snapshot.get(DATA_SCHEMA)? { match snapshot.get(DATA_SCHEMA)? {
@ -37,92 +34,6 @@ fn retrieve_data_index(snapshot: &Snapshot<&DB>) -> Result<PositiveBlob, Box<Err
} }
} }
#[derive(Copy, Clone)]
pub struct DocumentKey([u8; DOC_KEY_LEN]);
impl DocumentKey {
pub fn new(id: DocumentId) -> DocumentKey {
let mut buffer = [0; DOC_KEY_LEN];
let mut wtr = Cursor::new(&mut buffer[..]);
wtr.write_all(b"doc-").unwrap();
wtr.write_u64::<NativeEndian>(id).unwrap();
DocumentKey(buffer)
}
pub fn from_bytes(mut bytes: &[u8]) -> DocumentKey {
assert!(bytes.len() >= DOC_KEY_LEN);
assert_eq!(&bytes[..4], b"doc-");
let mut buffer = [0; DOC_KEY_LEN];
bytes.read_exact(&mut buffer).unwrap();
DocumentKey(buffer)
}
pub fn with_attribute(&self, attr: SchemaAttr) -> DocumentKeyAttr {
DocumentKeyAttr::new(self.document_id(), attr)
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
}
}
impl AsRef<[u8]> for DocumentKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Copy, Clone)]
pub struct DocumentKeyAttr([u8; DOC_KEY_ATTR_LEN]);
impl DocumentKeyAttr {
pub fn new(id: DocumentId, attr: SchemaAttr) -> DocumentKeyAttr {
let mut buffer = [0; DOC_KEY_ATTR_LEN];
let DocumentKey(raw_key) = DocumentKey::new(id);
let mut wtr = Cursor::new(&mut buffer[..]);
wtr.write_all(&raw_key).unwrap();
wtr.write_all(b"-").unwrap();
wtr.write_u32::<NativeEndian>(attr.as_u32()).unwrap();
DocumentKeyAttr(buffer)
}
pub fn from_bytes(mut bytes: &[u8]) -> DocumentKeyAttr {
assert!(bytes.len() >= DOC_KEY_ATTR_LEN);
assert_eq!(&bytes[..4], b"doc-");
let mut buffer = [0; DOC_KEY_ATTR_LEN];
bytes.read_exact(&mut buffer).unwrap();
DocumentKeyAttr(buffer)
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
}
pub fn attribute(&self) -> SchemaAttr {
let offset = 4 + size_of::<u64>() + 1;
let value = (&self.0[offset..]).read_u32::<NativeEndian>().unwrap();
SchemaAttr::new(value)
}
pub fn into_document_key(self) -> DocumentKey {
DocumentKey::new(self.document_id())
}
}
impl AsRef<[u8]> for DocumentKeyAttr {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
pub struct Database(DB); pub struct Database(DB);
impl Database { impl Database {