2018-12-03 15:23:49 +01:00
|
|
|
use std::error::Error;
|
2018-12-03 22:26:24 +01:00
|
|
|
use std::{fmt, marker};
|
2018-12-03 15:23:49 +01:00
|
|
|
|
2018-12-03 22:26:24 +01:00
|
|
|
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey};
|
|
|
|
use rocksdb::rocksdb_options::ReadOptions;
|
2018-12-03 15:29:05 +01:00
|
|
|
use serde::de::DeserializeOwned;
|
2018-12-03 15:23:49 +01:00
|
|
|
|
2018-12-07 11:32:27 +01:00
|
|
|
use crate::database::{retrieve_data_schema, DocumentKey, DocumentKeyAttr};
|
2018-12-07 12:22:51 +01:00
|
|
|
use crate::database::deserializer::Deserializer;
|
2018-12-07 14:41:06 +01:00
|
|
|
use crate::rank::criterion::Criterion;
|
2018-12-07 12:22:51 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
pub struct DatabaseView<'a> {
|
|
|
|
snapshot: Snapshot<&'a DB>,
|
|
|
|
schema: Schema,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DatabaseView<'a> {
|
|
|
|
pub fn new(snapshot: Snapshot<&'a DB>) -> Result<DatabaseView, Box<Error>> {
|
|
|
|
let schema = retrieve_data_schema(&snapshot)?;
|
|
|
|
Ok(DatabaseView { snapshot, schema })
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:41:06 +01:00
|
|
|
pub fn schema(&self) -> &Schema {
|
|
|
|
&self.schema
|
|
|
|
}
|
|
|
|
|
2018-12-03 15:23:49 +01:00
|
|
|
pub fn into_snapshot(self) -> Snapshot<&'a DB> {
|
|
|
|
self.snapshot
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:41:06 +01:00
|
|
|
pub fn snapshot(&self) -> &Snapshot<&'a DB> {
|
|
|
|
&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-07 14:41:06 +01:00
|
|
|
pub fn query_builder(&self) -> Result<QueryBuilder<Box<dyn Criterion>>, Box<Error>> {
|
|
|
|
QueryBuilder::new(self)
|
|
|
|
}
|
|
|
|
|
2018-12-03 15:23:49 +01:00
|
|
|
// TODO create an enum error type
|
|
|
|
pub fn retrieve_document<D>(&self, id: DocumentId) -> Result<D, Box<Error>>
|
|
|
|
where D: DeserializeOwned
|
|
|
|
{
|
|
|
|
let mut deserializer = Deserializer::new(&self.snapshot, &self.schema, id);
|
|
|
|
Ok(D::deserialize(&mut deserializer)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrieve_documents<D, I>(&self, ids: I) -> DocumentIter<D, I::IntoIter>
|
|
|
|
where D: DeserializeOwned,
|
|
|
|
I: IntoIterator<Item=DocumentId>,
|
|
|
|
{
|
|
|
|
DocumentIter {
|
|
|
|
database_view: self,
|
|
|
|
document_ids: ids.into_iter(),
|
|
|
|
_phantom: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:26:24 +01:00
|
|
|
impl<'a> fmt::Debug for DatabaseView<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut options = ReadOptions::new();
|
|
|
|
let lower = DocumentKey::new(0);
|
|
|
|
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-03 15:23:49 +01:00
|
|
|
pub struct DocumentIter<'a, D, I> {
|
|
|
|
database_view: &'a DatabaseView<'a>,
|
|
|
|
document_ids: I,
|
|
|
|
_phantom: marker::PhantomData<D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, D, I> Iterator for DocumentIter<'a, D, I>
|
|
|
|
where D: DeserializeOwned,
|
|
|
|
I: Iterator<Item=DocumentId>,
|
|
|
|
{
|
|
|
|
type Item = Result<D, Box<Error>>;
|
|
|
|
|
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() {
|
|
|
|
Some(id) => Some(self.database_view.retrieve_document(id)),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 16:18:01 +01:00
|
|
|
|
|
|
|
impl<'a, D, I> ExactSizeIterator for DocumentIter<'a, D, I>
|
|
|
|
where D: DeserializeOwned,
|
|
|
|
I: ExactSizeIterator + Iterator<Item=DocumentId>,
|
|
|
|
{ }
|
|
|
|
|
|
|
|
impl<'a, D, I> DoubleEndedIterator for DocumentIter<'a, D, I>
|
|
|
|
where D: DeserializeOwned,
|
|
|
|
I: DoubleEndedIterator + Iterator<Item=DocumentId>,
|
|
|
|
{
|
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.document_ids.next_back() {
|
|
|
|
Some(id) => Some(self.database_view.retrieve_document(id)),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|