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

[collections] Use new collections in Dictionary #383 #384

Merged
merged 10 commits into from
Dec 3, 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
2 changes: 1 addition & 1 deletion src/agdb/collections.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod bit_set;
pub mod dictionary;
pub mod dictionary_storage;
pub mod map_storage;
pub mod multi_map;
pub mod multi_map_storage;
pub mod old_multi_map;
pub mod old_storage_vec;
pub mod storage_dictionary;
pub mod storage_map;
pub mod storage_multi_map;
pub mod vec;
Expand Down
120 changes: 56 additions & 64 deletions src/agdb/collections/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,47 @@
pub mod dictionary_data_storage;
pub mod dictionary_data_storage_indexes;
pub mod dictionary_impl;
pub mod dictionary_index;
pub mod dictionary_value;

mod dictionary_data;
mod dictionary_data_memory;

use self::dictionary_data_memory::DictionaryDataMemory;
use self::dictionary_impl::DictionaryImpl;
use crate::utilities::old_serialize::OldSerialize;
use crate::storage::storage_value::StorageValue;
use crate::utilities::stable_hash::StableHash;
use std::marker::PhantomData;

pub type Dictionary<T> = DictionaryImpl<T, DictionaryDataMemory<T>>;

#[allow(dead_code)]
impl<T> Dictionary<T>
where
T: Clone + Default + Eq + PartialEq + StableHash + OldSerialize,
T: Clone + Default + Eq + PartialEq + StableHash + StorageValue,
{
pub fn new() -> Dictionary<T> {
Dictionary {
data: DictionaryDataMemory::<T>::default(),
data: DictionaryDataMemory::<T>::new(),
phantom_data: PhantomData,
}
}
}

impl<T> Default for Dictionary<T>
where
T: Clone + Default + Eq + PartialEq + StableHash + OldSerialize,
{
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::super::dictionary::dictionary_index::DictionaryIndex;
use super::*;
use crate::test_utilities::collided_value::CollidedValue;
use crate::test_utilities::collision_value::CollisionValue;

#[test]
fn count_invalid_index() {
let dictionary = Dictionary::<i64>::new();

assert_eq!(dictionary.count(&DictionaryIndex::default()), Ok(None));
assert_eq!(dictionary.count(&DictionaryIndex::from(-1_i64)), Ok(None));
assert_eq!(dictionary.count(0), Ok(None));
assert_eq!(dictionary.count(u64::MAX), Ok(None));
}

#[test]
fn derived_from_default() {
let _dictionary = Dictionary::<i64>::default();
let _dictionary = Dictionary::<i64>::new();
}

#[test]
Expand All @@ -76,7 +65,7 @@ mod tests {
let mut dictionary = Dictionary::<i64>::new();

let index = dictionary.insert(&10).unwrap();
dictionary.remove(&index).unwrap();
dictionary.remove(index).unwrap();

assert_eq!(dictionary.index(&10), Ok(None));
}
Expand All @@ -89,34 +78,34 @@ mod tests {
let index2 = dictionary.insert(&10).unwrap();
let index3 = dictionary.insert(&7).unwrap();

dictionary.remove(&index2).unwrap();
dictionary.remove(&index1).unwrap();
dictionary.remove(&index3).unwrap();
dictionary.remove(index2).unwrap();
dictionary.remove(index1).unwrap();
dictionary.remove(index3).unwrap();

assert_eq!(dictionary.count(&index1), Ok(None));
assert_eq!(dictionary.count(&index2), Ok(None));
assert_eq!(dictionary.count(&index3), Ok(None));
assert_eq!(dictionary.count(index1), Ok(None));
assert_eq!(dictionary.count(index2), Ok(None));
assert_eq!(dictionary.count(index3), Ok(None));

assert_eq!(dictionary.insert(&3), Ok(index3.clone()));
assert_eq!(dictionary.insert(&2), Ok(index1.clone()));
assert_eq!(dictionary.insert(&1), Ok(index2.clone()));
assert_eq!(dictionary.insert(&3), Ok(index3));
assert_eq!(dictionary.insert(&2), Ok(index1));
assert_eq!(dictionary.insert(&1), Ok(index2));

assert_eq!(dictionary.value(&index1), Ok(Some(2)));
assert_eq!(dictionary.value(&index2), Ok(Some(1)));
assert_eq!(dictionary.value(&index3), Ok(Some(3)));
assert_eq!(dictionary.value(index1), Ok(Some(2)));
assert_eq!(dictionary.value(index2), Ok(Some(1)));
assert_eq!(dictionary.value(index3), Ok(Some(3)));
}

#[test]
fn index_with_collisions() {
let mut dictionary = Dictionary::<CollidedValue<i64>>::new();
let mut dictionary = Dictionary::<CollisionValue<i64>>::new();

let index1 = dictionary.insert(&CollidedValue::new(1)).unwrap();
let index2 = dictionary.insert(&CollidedValue::new(2)).unwrap();
let index3 = dictionary.insert(&CollidedValue::new(3)).unwrap();
let index1 = dictionary.insert(&CollisionValue::new(1)).unwrap();
let index2 = dictionary.insert(&CollisionValue::new(2)).unwrap();
let index3 = dictionary.insert(&CollisionValue::new(3)).unwrap();

assert_eq!(dictionary.index(&CollidedValue::new(1)), Ok(Some(index1)));
assert_eq!(dictionary.index(&CollidedValue::new(2)), Ok(Some(index2)));
assert_eq!(dictionary.index(&CollidedValue::new(3)), Ok(Some(index3)));
assert_eq!(dictionary.index(&CollisionValue::new(1)), Ok(Some(index1)));
assert_eq!(dictionary.index(&CollisionValue::new(2)), Ok(Some(index2)));
assert_eq!(dictionary.index(&CollisionValue::new(3)), Ok(Some(index3)));
}

#[test]
Expand All @@ -126,8 +115,8 @@ mod tests {
let index = dictionary.insert(&10).unwrap();

assert_eq!(dictionary.len(), Ok(1));
assert_eq!(dictionary.value(&index), Ok(Some(10_i64)));
assert_eq!(dictionary.count(&index), Ok(Some(1)));
assert_eq!(dictionary.value(index), Ok(Some(10_i64)));
assert_eq!(dictionary.count(index), Ok(Some(1)));
}

#[test]
Expand All @@ -140,14 +129,14 @@ mod tests {

assert_eq!(dictionary.len(), Ok(3));

assert_eq!(dictionary.value(&index1).unwrap(), Some(10_i64));
assert_eq!(dictionary.count(&index1), Ok(Some(1)));
assert_eq!(dictionary.value(index1).unwrap(), Some(10_i64));
assert_eq!(dictionary.count(index1), Ok(Some(1)));

assert_eq!(dictionary.value(&index2).unwrap(), Some(15_i64));
assert_eq!(dictionary.count(&index2), Ok(Some(1)));
assert_eq!(dictionary.value(index2).unwrap(), Some(15_i64));
assert_eq!(dictionary.count(index2), Ok(Some(1)));

assert_eq!(dictionary.value(&index3).unwrap(), Some(20_i64));
assert_eq!(dictionary.count(&index3), Ok(Some(1)));
assert_eq!(dictionary.value(index3).unwrap(), Some(20_i64));
assert_eq!(dictionary.count(index3), Ok(Some(1)));
}

#[test]
Expand All @@ -164,18 +153,23 @@ mod tests {
dictionary.insert(&20).unwrap();

assert_eq!(dictionary.len(), Ok(3));
assert_eq!(dictionary.count(&index2), Ok(Some(3)));
assert_eq!(dictionary.count(index2), Ok(Some(3)));
}

#[test]
fn remove() {
let mut dictionary = Dictionary::<i64>::new();

let index = dictionary.insert(&10).unwrap();
dictionary.remove(&index).unwrap();
dictionary.remove(index).unwrap();

assert_eq!(dictionary.value(index), Ok(None));
assert_eq!(dictionary.count(index), Ok(None));

dictionary.remove(index).unwrap();

assert_eq!(dictionary.value(&index), Ok(None));
assert_eq!(dictionary.count(&index), Ok(None));
assert_eq!(dictionary.value(index), Ok(None));
assert_eq!(dictionary.count(index), Ok(None));
}

#[test]
Expand All @@ -186,19 +180,19 @@ mod tests {
dictionary.insert(&10).unwrap();
dictionary.insert(&10).unwrap();

assert_eq!(dictionary.value(&index), Ok(Some(10)));
assert_eq!(dictionary.count(&index), Ok(Some(3)));
assert_eq!(dictionary.value(index), Ok(Some(10)));
assert_eq!(dictionary.count(index), Ok(Some(3)));

dictionary.remove(&index).unwrap();
dictionary.remove(index).unwrap();

assert_eq!(dictionary.value(&index), Ok(Some(10)));
assert_eq!(dictionary.count(&index), Ok(Some(2)));
assert_eq!(dictionary.value(index), Ok(Some(10)));
assert_eq!(dictionary.count(index), Ok(Some(2)));

dictionary.remove(&index).unwrap();
dictionary.remove(&index).unwrap();
dictionary.remove(index).unwrap();
dictionary.remove(index).unwrap();

assert_eq!(dictionary.value(&index), Ok(None));
assert_eq!(dictionary.count(&index), Ok(None));
assert_eq!(dictionary.value(index), Ok(None));
assert_eq!(dictionary.count(index), Ok(None));
}

#[test]
Expand All @@ -209,16 +203,14 @@ mod tests {

assert_eq!(dictionary.len(), Ok(1));

dictionary
.remove(&DictionaryIndex::from(index.value() + 1))
.unwrap();
dictionary.remove(index + 1).unwrap();

assert_eq!(dictionary.len(), Ok(1));
}

#[test]
fn value_missing_index() {
let dictionary = Dictionary::<i64>::new();
assert_eq!(dictionary.value(&DictionaryIndex::from(1_i64)), Ok(None));
assert_eq!(dictionary.value(1), Ok(None));
}
}
29 changes: 12 additions & 17 deletions src/agdb/collections/dictionary/dictionary_data.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
use super::dictionary_index::DictionaryIndex;
use super::dictionary_value::DictionaryValue;
use crate::db::db_error::DbError;
use crate::utilities::old_serialize::OldSerialize;
use crate::storage::storage_value::StorageValue;
use crate::utilities::stable_hash::StableHash;

pub trait DictionaryData<T>
where
T: Clone + Default + Eq + PartialEq + StableHash + OldSerialize,
T: Clone + Default + Eq + PartialEq + StableHash + StorageValue,
{
fn capacity(&self) -> u64;
fn commit(&mut self) -> 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 count(&self, index: u64) -> Result<u64, DbError>;
fn hash(&self, index: u64) -> Result<u64, DbError>;
fn indexes(&self, hash: u64) -> Result<Vec<u64>, DbError>;
fn insert(&mut self, hash: u64, index: u64) -> Result<(), DbError>;
fn remove(&mut self, hash: u64, index: u64) -> Result<(), DbError>;
fn set_capacity(&mut self, capacity: u64) -> Result<(), DbError>;
fn set_count(&mut self, index: u64, count: u64) -> Result<(), DbError>;
fn set_hash(&mut self, index: u64, hash: u64) -> Result<(), DbError>;
fn set_value(&mut self, index: u64, value: &T) -> Result<(), DbError>;
fn transaction(&mut self);
fn value(&self, index: &DictionaryIndex) -> Result<DictionaryValue<T>, DbError>;
fn value(&self, index: u64) -> Result<T, DbError>;
}
Loading