2018-12-03 15:23:49 +01:00
|
|
|
use std::error::Error;
|
2018-12-10 11:09:09 +01:00
|
|
|
use std::path::Path;
|
2018-12-07 17:59:03 +01:00
|
|
|
use std::ops::Deref;
|
2018-12-03 22:26:24 +01:00
|
|
|
use std::{fmt, marker};
|
2018-12-03 15:23:49 +01:00
|
|
|
|
2018-12-10 11:09:09 +01:00
|
|
|
use rocksdb::rocksdb_options::{ReadOptions, EnvOptions, ColumnFamilyOptions};
|
|
|
|
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter};
|
2018-12-03 15:29:05 +01:00
|
|
|
use serde::de::DeserializeOwned;
|
2018-12-03 15:23:49 +01:00
|
|
|
|
2018-12-07 16:20:12 +01:00
|
|
|
use crate::database::{DocumentKey, DocumentKeyAttr};
|
|
|
|
use crate::database::{retrieve_data_schema, retrieve_data_index};
|
|
|
|
use crate::database::blob::positive::PositiveBlob;
|
2018-12-07 12:22:51 +01:00
|
|
|
use crate::database::deserializer::Deserializer;
|
|
|
|
use crate::database::schema::Schema;
|
2018-12-07 14:41:06 +01:00
|
|
|
use crate::rank::QueryBuilder;
|
2018-12-03 15:23:49 +01:00
|
|
|
use crate::DocumentId;
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
pub struct DatabaseView<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
snapshot: Snapshot<D>,
|
2018-12-07 16:20:12 +01:00
|
|
|
blob: PositiveBlob,
|
2018-12-03 15:23:49 +01:00
|
|
|
schema: Schema,
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<D> DatabaseView<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
pub fn new(snapshot: Snapshot<D>) -> Result<DatabaseView<D>, Box<Error>> {
|
2018-12-03 15:23:49 +01:00
|
|
|
let schema = retrieve_data_schema(&snapshot)?;
|
2018-12-07 16:20:12 +01:00
|
|
|
let blob = retrieve_data_index(&snapshot)?;
|
|
|
|
Ok(DatabaseView { snapshot, blob, schema })
|
2018-12-03 15:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:41:06 +01:00
|
|
|
pub fn schema(&self) -> &Schema {
|
|
|
|
&self.schema
|
|
|
|
}
|
|
|
|
|
2018-12-07 16:20:12 +01:00
|
|
|
pub fn blob(&self) -> &PositiveBlob {
|
|
|
|
&self.blob
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
pub fn into_snapshot(self) -> Snapshot<D> {
|
2018-12-03 15:23:49 +01:00
|
|
|
self.snapshot
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
pub fn snapshot(&self) -> &Snapshot<D> {
|
2018-12-07 14:41:06 +01:00
|
|
|
&self.snapshot
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:26:24 +01:00
|
|
|
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, Box<Error>> {
|
|
|
|
Ok(self.snapshot.get(key)?)
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:09:09 +01:00
|
|
|
pub fn dump_all<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<Error>> {
|
|
|
|
let path = path.as_ref().to_string_lossy();
|
|
|
|
|
|
|
|
let env_options = EnvOptions::new();
|
|
|
|
let column_family_options = ColumnFamilyOptions::new();
|
|
|
|
let mut file_writer = SstFileWriter::new(env_options, column_family_options);
|
|
|
|
file_writer.open(&path)?;
|
|
|
|
|
|
|
|
let mut iter = self.snapshot.iter();
|
|
|
|
iter.seek(SeekKey::Start);
|
|
|
|
|
|
|
|
for (key, value) in &mut iter {
|
|
|
|
file_writer.put(&key, &value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_writer.finish()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:14:16 +01:00
|
|
|
pub fn query_builder(&self) -> Result<QueryBuilder<D>, Box<Error>> {
|
2018-12-07 14:41:06 +01:00
|
|
|
QueryBuilder::new(self)
|
|
|
|
}
|
|
|
|
|
2018-12-25 12:26:38 +01:00
|
|
|
pub fn document_by_id<T>(&self, id: DocumentId) -> Result<T, Box<Error>>
|
2018-12-07 17:59:03 +01:00
|
|
|
where T: DeserializeOwned
|
2018-12-03 15:23:49 +01:00
|
|
|
{
|
|
|
|
let mut deserializer = Deserializer::new(&self.snapshot, &self.schema, id);
|
2018-12-07 17:59:03 +01:00
|
|
|
Ok(T::deserialize(&mut deserializer)?)
|
2018-12-03 15:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-25 12:26:38 +01:00
|
|
|
pub fn documents_by_id<T, I>(&self, ids: I) -> DocumentIter<D, T, I::IntoIter>
|
2018-12-07 17:59:03 +01:00
|
|
|
where T: DeserializeOwned,
|
2018-12-03 15:23:49 +01:00
|
|
|
I: IntoIterator<Item=DocumentId>,
|
|
|
|
{
|
|
|
|
DocumentIter {
|
|
|
|
database_view: self,
|
|
|
|
document_ids: ids.into_iter(),
|
|
|
|
_phantom: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<D> fmt::Debug for DatabaseView<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
2018-12-03 22:26:24 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut options = ReadOptions::new();
|
2018-12-22 12:00:24 +01:00
|
|
|
let lower = DocumentKey::new(DocumentId(0));
|
2018-12-03 22:26:24 +01:00
|
|
|
options.set_iterate_lower_bound(lower.as_ref());
|
|
|
|
|
|
|
|
let mut iter = self.snapshot.iter_opt(options);
|
|
|
|
iter.seek(SeekKey::Start);
|
|
|
|
let iter = iter.map(|(key, _)| DocumentKeyAttr::from_bytes(&key));
|
|
|
|
|
|
|
|
if f.alternate() {
|
|
|
|
writeln!(f, "DatabaseView(")?;
|
|
|
|
} else {
|
|
|
|
write!(f, "DatabaseView(")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.schema.fmt(f)?;
|
|
|
|
|
|
|
|
if f.alternate() {
|
|
|
|
writeln!(f, ",")?;
|
|
|
|
} else {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
f.debug_list().entries(iter).finish()?;
|
|
|
|
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:18:01 +01:00
|
|
|
// TODO this is just an iter::Map !!!
|
2018-12-07 17:59:03 +01:00
|
|
|
pub struct DocumentIter<'a, D, T, I>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
database_view: &'a DatabaseView<D>,
|
2018-12-03 15:23:49 +01:00
|
|
|
document_ids: I,
|
2018-12-07 17:59:03 +01:00
|
|
|
_phantom: marker::PhantomData<T>,
|
2018-12-03 15:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<'a, D, T, I> Iterator for DocumentIter<'a, D, T, I>
|
|
|
|
where D: Deref<Target=DB>,
|
|
|
|
T: DeserializeOwned,
|
2018-12-03 15:23:49 +01:00
|
|
|
I: Iterator<Item=DocumentId>,
|
|
|
|
{
|
2018-12-07 17:59:03 +01:00
|
|
|
type Item = Result<T, Box<Error>>;
|
2018-12-03 15:23:49 +01:00
|
|
|
|
2018-12-03 16:18:01 +01:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.document_ids.size_hint()
|
|
|
|
}
|
|
|
|
|
2018-12-03 15:23:49 +01:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.document_ids.next() {
|
2018-12-25 12:26:38 +01:00
|
|
|
Some(id) => Some(self.database_view.document_by_id(id)),
|
2018-12-03 15:23:49 +01:00
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 16:18:01 +01:00
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<'a, D, T, I> ExactSizeIterator for DocumentIter<'a, D, T, I>
|
|
|
|
where D: Deref<Target=DB>,
|
|
|
|
T: DeserializeOwned,
|
2018-12-03 16:18:01 +01:00
|
|
|
I: ExactSizeIterator + Iterator<Item=DocumentId>,
|
|
|
|
{ }
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<'a, D, T, I> DoubleEndedIterator for DocumentIter<'a, D, T, I>
|
|
|
|
where D: Deref<Target=DB>,
|
|
|
|
T: DeserializeOwned,
|
2018-12-03 16:18:01 +01:00
|
|
|
I: DoubleEndedIterator + Iterator<Item=DocumentId>,
|
|
|
|
{
|
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.document_ids.next_back() {
|
2018-12-25 12:26:38 +01:00
|
|
|
Some(id) => Some(self.database_view.document_by_id(id)),
|
2018-12-03 16:18:01 +01:00
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|