From 7338e522bdff893286b81ef37d3e274b46b57faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Fri, 12 Apr 2019 18:46:36 +0200 Subject: [PATCH] squash-me: Add set/get/del_document_attribute to Index methods --- meilidb-data/src/database.rs | 47 ++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/meilidb-data/src/database.rs b/meilidb-data/src/database.rs index 7cfc68f22..41d343f05 100644 --- a/meilidb-data/src/database.rs +++ b/meilidb-data/src/database.rs @@ -1,12 +1,12 @@ use std::sync::Arc; use std::path::Path; -use meilidb_core::Index as WordIndex; +use meilidb_core::{DocumentId, Index as WordIndex}; use meilidb_core::shared_data_cursor::{FromSharedDataCursor, SharedDataCursor}; use meilidb_core::write_to_bytes::WriteToBytes; use sled::IVec; -use crate::Schema; +use crate::{Schema, SchemaAttr}; #[derive(Debug)] pub enum Error { @@ -32,6 +32,17 @@ fn index_name(name: &str) -> Vec { format!("index-{}", name).into_bytes() } +fn document_key(id: DocumentId, attr: SchemaAttr) -> Vec { + let DocumentId(document_id) = id; + let SchemaAttr(schema_attr) = attr; + + let mut bytes = Vec::new(); + bytes.extend_from_slice(b"document-"); + bytes.extend_from_slice(&document_id.to_be_bytes()[..]); + bytes.extend_from_slice(&schema_attr.to_be_bytes()[..]); + bytes +} + fn ivec_into_arc(ivec: IVec) -> Arc<[u8]> { match ivec { IVec::Inline(len, bytes) => Arc::from(&bytes[..len as usize]), @@ -123,4 +134,36 @@ impl Index { pub fn word_index(&self) -> &WordIndex { &self.word_index } + + pub fn set_document_attribute( + &self, + id: DocumentId, + attr: SchemaAttr, + value: V, + ) -> Result, Error> + where IVec: From, + { + let key = document_key(id, attr); + Ok(self.inner.set(key, value)?) + } + + pub fn get_document_attribute( + &self, + id: DocumentId, + attr: SchemaAttr + ) -> Result, Error> + { + let key = document_key(id, attr); + Ok(self.inner.get(key)?) + } + + pub fn del_document_attribute( + &self, + id: DocumentId, + attr: SchemaAttr + ) -> Result, Error> + { + let key = document_key(id, attr); + Ok(self.inner.del(key)?) + } }