Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage] Add DictionaryIndex #303 #304

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions crates/dictionary/src/dictionary_data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::dictionary_index::DictionaryIndex;

use super::dictionary_value::DictionaryValue;
use agdb_db_error::DbError;
use agdb_serialize::Serialize;
Expand All @@ -9,14 +11,18 @@ where
{
fn capacity(&self) -> u64;
fn commit(&mut self) -> Result<(), DbError>;
fn indexes(&self, hash: u64) -> Result<Vec<i64>, DbError>;
fn insert(&mut self, hash: u64, index: i64) -> Result<(), DbError>;
fn hash(&self, index: i64) -> Result<u64, DbError>;
fn meta(&self, index: i64) -> Result<i64, DbError>;
fn remove(&mut self, hash: u64, index: i64) -> Result<(), DbError>;
fn set_hash(&mut self, index: i64, hash: u64) -> Result<(), DbError>;
fn set_meta(&mut self, index: i64, meta: i64) -> Result<(), DbError>;
fn set_value(&mut self, index: i64, value: DictionaryValue<T>) -> Result<(), DbError>;
fn indexes(&self, hash: u64) -> Result<Vec<DictionaryIndex>, DbError>;
fn insert(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError>;
fn hash(&self, index: &DictionaryIndex) -> Result<u64, DbError>;
fn meta(&self, index: &DictionaryIndex) -> Result<i64, DbError>;
fn remove(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError>;
fn set_hash(&mut self, index: &DictionaryIndex, hash: u64) -> Result<(), DbError>;
fn set_meta(&mut self, index: &DictionaryIndex, meta: i64) -> Result<(), DbError>;
fn set_value(
&mut self,
index: &DictionaryIndex,
value: DictionaryValue<T>,
) -> Result<(), DbError>;
fn transaction(&mut self);
fn value(&self, index: i64) -> Result<DictionaryValue<T>, DbError>;
fn value(&self, index: &DictionaryIndex) -> Result<DictionaryValue<T>, DbError>;
}
47 changes: 26 additions & 21 deletions crates/dictionary/src/dictionary_data_memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::dictionary_data::DictionaryData;
use super::dictionary_value::DictionaryValue;
use crate::dictionary_data::DictionaryData;
use crate::dictionary_index::DictionaryIndex;
use crate::dictionary_value::DictionaryValue;
use agdb_db_error::DbError;
use agdb_multi_map::MultiMap;
use agdb_serialize::Serialize;
Expand All @@ -9,7 +10,7 @@ pub struct DictionaryDataMemory<T>
where
T: Clone + Default + Eq + PartialEq + StableHash + Serialize,
{
pub(crate) index: MultiMap<u64, i64>,
pub(crate) index: MultiMap<u64, DictionaryIndex>,
pub(crate) values: Vec<DictionaryValue<T>>,
}

Expand All @@ -25,53 +26,57 @@ where
Ok(())
}

fn indexes(&self, hash: u64) -> Result<Vec<i64>, DbError> {
fn indexes(&self, hash: u64) -> Result<Vec<DictionaryIndex>, DbError> {
self.index.values(&hash)
}

fn insert(&mut self, hash: u64, index: i64) -> Result<(), DbError> {
self.index.insert(hash, index)
fn insert(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError> {
self.index.insert(hash, index.clone())
}

fn hash(&self, index: i64) -> Result<u64, DbError> {
Ok(self.values[index as usize].hash)
fn hash(&self, index: &DictionaryIndex) -> Result<u64, DbError> {
Ok(self.values[index.as_usize()].hash)
}

fn meta(&self, index: i64) -> Result<i64, DbError> {
Ok(self.values[index as usize].meta)
fn meta(&self, index: &DictionaryIndex) -> Result<i64, DbError> {
Ok(self.values[index.as_usize()].meta)
}

fn remove(&mut self, hash: u64, index: i64) -> Result<(), DbError> {
self.index.remove_value(&hash, &index)?;
fn remove(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError> {
self.index.remove_value(&hash, index)?;

Ok(())
}

fn set_hash(&mut self, index: i64, hash: u64) -> Result<(), DbError> {
self.values[index as usize].hash = hash;
fn set_hash(&mut self, index: &DictionaryIndex, hash: u64) -> Result<(), DbError> {
self.values[index.as_usize()].hash = hash;

Ok(())
}

fn set_meta(&mut self, index: i64, meta: i64) -> Result<(), DbError> {
self.values[index as usize].meta = meta;
fn set_meta(&mut self, index: &DictionaryIndex, meta: i64) -> Result<(), DbError> {
self.values[index.as_usize()].meta = meta;

Ok(())
}

fn set_value(&mut self, index: i64, value: DictionaryValue<T>) -> Result<(), DbError> {
if self.capacity() == index as u64 {
fn set_value(
&mut self,
index: &DictionaryIndex,
value: DictionaryValue<T>,
) -> Result<(), DbError> {
if self.capacity() == index.as_u64() {
self.values.push(value);
} else {
self.values[index as usize] = value;
self.values[index.as_usize()] = value;
}

Ok(())
}

fn transaction(&mut self) {}

fn value(&self, index: i64) -> Result<DictionaryValue<T>, DbError> {
Ok(self.values[index as usize].clone())
fn value(&self, index: &DictionaryIndex) -> Result<DictionaryValue<T>, DbError> {
Ok(self.values[index.as_usize()].clone())
}
}
3 changes: 2 additions & 1 deletion crates/dictionary/src/dictionary_data_memory_default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::dictionary_data_memory::DictionaryDataMemory;
use crate::dictionary_index::DictionaryIndex;
use crate::dictionary_value::DictionaryValue;
use agdb_multi_map::MultiMap;
use agdb_serialize::Serialize;
Expand All @@ -10,7 +11,7 @@ where
{
fn default() -> Self {
Self {
index: MultiMap::<u64, i64>::new(),
index: MultiMap::<u64, DictionaryIndex>::new(),
values: vec![DictionaryValue::<T>::default()],
}
}
Expand Down
51 changes: 28 additions & 23 deletions crates/dictionary/src/dictionary_data_storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::dictionary_data::DictionaryData;
use super::dictionary_value::DictionaryValue;
use crate::dictionary_data::DictionaryData;
use crate::dictionary_index::DictionaryIndex;
use crate::dictionary_value::DictionaryValue;
use agdb_db_error::DbError;
use agdb_multi_map::StorageMultiMap;
use agdb_serialize::Serialize;
Expand All @@ -16,10 +17,10 @@ where
T: Clone + Default + Eq + PartialEq + StableHash + Serialize,
Data: Storage,
{
pub(super) storage: Rc<RefCell<Data>>,
pub(super) storage_index: StorageIndex,
pub(super) index: StorageMultiMap<u64, i64, Data>,
pub(super) values: StorageVec<DictionaryValue<T>, Data>,
pub(crate) storage: Rc<RefCell<Data>>,
pub(crate) storage_index: StorageIndex,
pub(crate) index: StorageMultiMap<u64, DictionaryIndex, Data>,
pub(crate) values: StorageVec<DictionaryValue<T>, Data>,
}

impl<T, Data> DictionaryData<T> for DictionaryDataStorage<T, Data>
Expand All @@ -35,65 +36,69 @@ where
self.storage.borrow_mut().commit()
}

fn indexes(&self, hash: u64) -> Result<Vec<i64>, DbError> {
fn indexes(&self, hash: u64) -> Result<Vec<DictionaryIndex>, DbError> {
self.index.values(&hash)
}

fn insert(&mut self, hash: u64, index: i64) -> Result<(), DbError> {
self.index.insert(hash, index)
fn insert(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError> {
self.index.insert(hash, index.clone())
}

fn hash(&self, index: i64) -> Result<u64, DbError> {
fn hash(&self, index: &DictionaryIndex) -> Result<u64, DbError> {
let values_index = self.values.storage_index();
self.storage.borrow_mut().value_at::<u64>(
&values_index,
StorageVec::<DictionaryValue<T>>::value_offset(index as u64) + i64::serialized_size(),
StorageVec::<DictionaryValue<T>>::value_offset(index.as_u64()) + i64::serialized_size(),
)
}

fn meta(&self, index: i64) -> Result<i64, DbError> {
fn meta(&self, index: &DictionaryIndex) -> Result<i64, DbError> {
let values_index = self.values.storage_index();
self.storage.borrow_mut().value_at::<i64>(
&values_index,
StorageVec::<DictionaryValue<T>>::value_offset(index as u64),
StorageVec::<DictionaryValue<T>>::value_offset(index.as_u64()),
)
}

fn remove(&mut self, hash: u64, index: i64) -> Result<(), DbError> {
fn remove(&mut self, hash: u64, index: &DictionaryIndex) -> Result<(), DbError> {
self.index.remove_value(&hash, &index)
}

fn set_hash(&mut self, index: i64, hash: u64) -> Result<(), DbError> {
fn set_hash(&mut self, index: &DictionaryIndex, hash: u64) -> Result<(), DbError> {
let values_index = self.values.storage_index();
self.storage.borrow_mut().insert_at(
&values_index,
StorageVec::<DictionaryValue<T>>::value_offset(index as u64) + u64::serialized_size(),
StorageVec::<DictionaryValue<T>>::value_offset(index.as_u64()) + u64::serialized_size(),
&hash,
)
}

fn set_meta(&mut self, index: i64, meta: i64) -> Result<(), DbError> {
fn set_meta(&mut self, index: &DictionaryIndex, meta: i64) -> Result<(), DbError> {
let values_index = self.values.storage_index();
self.storage.borrow_mut().insert_at(
&values_index,
StorageVec::<DictionaryValue<T>>::value_offset(index as u64),
StorageVec::<DictionaryValue<T>>::value_offset(index.as_u64()),
&meta,
)
}

fn set_value(&mut self, index: i64, value: DictionaryValue<T>) -> Result<(), DbError> {
if self.capacity() == index as u64 {
fn set_value(
&mut self,
index: &DictionaryIndex,
value: DictionaryValue<T>,
) -> Result<(), DbError> {
if self.capacity() == index.as_u64() {
self.values.push(&value)
} else {
self.values.set_value(index as u64, &value)
self.values.set_value(index.as_u64(), &value)
}
}

fn transaction(&mut self) {
self.storage.borrow_mut().transaction()
}

fn value(&self, index: i64) -> Result<DictionaryValue<T>, DbError> {
self.values.value(index as u64)
fn value(&self, index: &DictionaryIndex) -> Result<DictionaryValue<T>, DbError> {
self.values.value(index.as_u64())
}
}
6 changes: 3 additions & 3 deletions crates/dictionary/src/dictionary_data_storage_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use agdb_serialize::Serialize;
use agdb_storage::StorageIndex;
use std::mem::size_of;

pub(super) struct DictionaryDataStorageIndexes {
pub(super) index: StorageIndex,
pub(super) values: StorageIndex,
pub(crate) struct DictionaryDataStorageIndexes {
pub(crate) index: StorageIndex,
pub(crate) values: StorageIndex,
}

impl Serialize for DictionaryDataStorageIndexes {
Expand Down
Loading