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] Move HashMap to crate #299 #300

Merged
merged 5 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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ edition = "2021"

[dependencies]
agdb_db_error = { path = "crates/db_error", version = "<=0.1.0" }
agdb_map_common = { path = "crates/map_common", version = "<=0.1.0" }
agdb_multi_map = { path = "crates/multi_map", version = "<=0.1.0" }
agdb_serialize = { path = "crates/serialize", version = "<=0.1.0" }
agdb_storage = { path = "crates/storage", version = "<=0.1.0" }
agdb_storage_map = { path = "crates/storage_map", version = "<=0.1.0" }
agdb_storage_vec = { path = "crates/storage_vec", version = "<=0.1.0" }
agdb_utilities = { path = "crates/utilities", version = "<=0.1.0" }

[dev-dependencies]
agdb_test_file = { path = "crates/test_file", version = "<=0.1.0" }
11 changes: 11 additions & 0 deletions crates/map_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "agdb_map_common"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
agdb_db_error = { path = "../db_error", version = "<=0.1.0" }
agdb_serialize = { path = "../serialize", version = "<=0.1.0" }
agdb_utilities = { path = "../utilities", version = "<=0.1.0" }
14 changes: 14 additions & 0 deletions crates/map_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod map_common;
mod map_common_from;
mod map_data;
mod map_iterator;
mod map_value;
mod map_value_serialize;
mod map_value_state;
mod map_value_state_serialize;

pub use map_common::MapCommon;
pub use map_data::MapData;
pub use map_iterator::MapIterator;
pub use map_value::MapValue;
pub use map_value_state::MapValueState;
148 changes: 148 additions & 0 deletions crates/map_common/src/map_common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use super::map_data::MapData;
use super::map_iterator::MapIterator;
use super::map_value::MapValue;
use super::map_value_state::MapValueState;
use agdb_db_error::DbError;
use agdb_serialize::Serialize;
use agdb_utilities::StableHash;
use std::cmp::max;
use std::hash::Hash;
use std::marker::PhantomData;

pub struct MapCommon<K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: MapData<K, T>,
{
pub data: Data,
pub(crate) phantom_data: PhantomData<(K, T)>,
}

impl<K, T, Data> MapCommon<K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: MapData<K, T>,
{
pub fn capacity(&self) -> u64 {
self.data.capacity()
}

pub fn count(&self) -> u64 {
self.data.count()
}

pub fn iter(&self) -> MapIterator<K, T, Data> {
MapIterator::<K, T, Data> {
pos: 0,
data: &self.data,
phantom_data: PhantomData,
}
}

pub fn reserve(&mut self, new_capacity: u64) -> Result<(), DbError> {
if self.capacity() < new_capacity {
return self.rehash(new_capacity);
}

Ok(())
}

pub fn value(&self, key: &K) -> Result<Option<T>, DbError> {
let hash = key.stable_hash();
let mut pos = hash % self.capacity();

loop {
let record = self.data.record(pos)?;

match record.state {
MapValueState::Empty => return Ok(None),
MapValueState::Valid if record.key == *key => return Ok(Some(record.value)),
MapValueState::Valid | MapValueState::Deleted => {
pos = Self::next_pos(pos, self.capacity())
}
}
}
}

pub fn insert_value(&mut self, pos: u64, key: K, value: T) -> Result<(), DbError> {
self.data.set_value(
pos,
MapValue {
state: MapValueState::Valid,
key,
value,
},
)
}

pub fn max_size(&self) -> u64 {
self.capacity() * 15 / 16
}

pub fn next_pos(pos: u64, capacity: u64) -> u64 {
if pos == capacity - 1 {
0
} else {
pos + 1
}
}

pub fn rehash(&mut self, mut new_capacity: u64) -> Result<(), DbError> {
new_capacity = max(new_capacity, 64);

if new_capacity != self.capacity() {
let old_data = self.data.take_values()?;
self.data.transaction();
self.data
.set_values(self.rehash_old_data(old_data, new_capacity))?;
self.data.commit()?;
}

Ok(())
}

pub fn remove_record(&mut self, pos: u64) -> Result<(), DbError> {
self.data.set_meta_value(pos, MapValueState::Deleted)?;
self.data.set_count(self.data.count() - 1)?;

if 0 != self.data.count() && (self.data.count() - 1) < self.min_size() {
self.rehash(self.capacity() / 2)?;
}

Ok(())
}

fn min_size(&self) -> u64 {
self.capacity() * 7 / 16
}

fn place_new_record(&self, new_data: &mut Vec<MapValue<K, T>>, record: MapValue<K, T>) {
let hash = record.key.stable_hash();
let mut pos = hash % new_data.len() as u64;

while new_data[pos as usize].state != MapValueState::Empty {
pos = Self::next_pos(pos, new_data.len() as u64);
}

new_data[pos as usize] = record;
}

fn rehash_old_data(
&self,
old_data: Vec<MapValue<K, T>>,
new_capacity: u64,
) -> Vec<MapValue<K, T>> {
let mut new_data: Vec<MapValue<K, T>> =
vec![MapValue::<K, T>::default(); new_capacity as usize];

for record in old_data {
if record.state == MapValueState::Valid {
self.place_new_record(&mut new_data, record);
}
}

new_data
}
}
20 changes: 20 additions & 0 deletions crates/map_common/src/map_common_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::map_common::MapCommon;
use crate::map_data::MapData;
use agdb_serialize::Serialize;
use agdb_utilities::StableHash;
use std::hash::Hash;
use std::marker::PhantomData;

impl<K, T, Data> From<Data> for MapCommon<K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: MapData<K, T>,
{
fn from(data: Data) -> Self {
Self {
data,
phantom_data: PhantomData,
}
}
}
24 changes: 24 additions & 0 deletions crates/map_common/src/map_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::map_value::MapValue;
use super::map_value_state::MapValueState;
use agdb_db_error::DbError;
use agdb_serialize::Serialize;
use agdb_utilities::StableHash;
use std::hash::Hash;

pub trait MapData<K, T>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
{
fn capacity(&self) -> u64;
fn commit(&mut self) -> Result<(), DbError>;
fn count(&self) -> u64;
fn meta_value(&self, pos: u64) -> Result<MapValueState, DbError>;
fn record(&self, pos: u64) -> Result<MapValue<K, T>, DbError>;
fn set_count(&mut self, new_count: u64) -> Result<(), DbError>;
fn set_meta_value(&mut self, pos: u64, meta_value: MapValueState) -> Result<(), DbError>;
fn set_value(&mut self, pos: u64, value: MapValue<K, T>) -> Result<(), DbError>;
fn set_values(&mut self, values: Vec<MapValue<K, T>>) -> Result<(), DbError>;
fn take_values(&mut self) -> Result<Vec<MapValue<K, T>>, DbError>;
fn transaction(&mut self);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
use super::hash_map_data::HashMapData;
use super::hash_map_meta_value::HashMapMetaValue;
use super::StableHash;
use super::map_data::MapData;
use super::map_value_state::MapValueState;
use agdb_serialize::Serialize;
use agdb_utilities::StableHash;
use std::hash::Hash;
use std::marker::PhantomData;

pub(crate) struct HashMapIterator<'a, K, T, Data>
pub struct MapIterator<'a, K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: HashMapData<K, T>,
Data: MapData<K, T>,
{
pub(super) pos: u64,
pub(super) data: &'a Data,
pub(super) phantom_data: PhantomData<(K, T)>,
}

impl<'a, K, T, Data> HashMapIterator<'a, K, T, Data>
impl<'a, K, T, Data> Iterator for MapIterator<'a, K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: HashMapData<K, T>,
{
}

impl<'a, K, T, Data> Iterator for HashMapIterator<'a, K, T, Data>
where
K: Clone + Default + Eq + Hash + PartialEq + StableHash + Serialize,
T: Clone + Default + Serialize,
Data: HashMapData<K, T>,
Data: MapData<K, T>,
{
type Item = (K, T);

Expand All @@ -38,7 +30,7 @@ where

self.pos += 1;

if value.meta_value == HashMapMetaValue::Valid {
if value.state == MapValueState::Valid {
return Some((value.key, value.value));
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/map_common/src/map_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::map_value_state::MapValueState;
use agdb_serialize::Serialize;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MapValue<K, T>
where
K: Clone + Default + Serialize,
T: Clone + Default + Serialize,
{
pub state: MapValueState,
pub key: K,
pub value: T,
}
34 changes: 34 additions & 0 deletions crates/map_common/src/map_value_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::map_value::MapValue;
use crate::map_value_state::MapValueState;
use agdb_db_error::DbError;
use agdb_serialize::Serialize;

impl<K, T> Serialize for MapValue<K, T>
where
K: Clone + Default + Serialize,
T: Clone + Default + Serialize,
{
fn deserialize(bytes: &[u8]) -> Result<Self, DbError> {
Ok(Self {
state: MapValueState::deserialize(bytes)?,
key: K::deserialize(&bytes[(MapValueState::serialized_size() as usize)..])?,
value: T::deserialize(
&bytes[((MapValueState::serialized_size() + K::serialized_size()) as usize)..],
)?,
})
}

fn serialize(&self) -> Vec<u8> {
let mut data = Vec::<u8>::new();
data.reserve(Self::serialized_size() as usize);
data.extend(self.state.serialize());
data.extend(self.key.serialize());
data.extend(self.value.serialize());

data
}

fn serialized_size() -> u64 {
MapValueState::serialized_size() + K::serialized_size() + T::serialized_size()
}
}
7 changes: 7 additions & 0 deletions crates/map_common/src/map_value_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Clone, Default, Debug, Eq, PartialEq)]
pub enum MapValueState {
#[default]
Empty,
Deleted,
Valid,
}
27 changes: 27 additions & 0 deletions crates/map_common/src/map_value_state_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::map_value_state::MapValueState;
use agdb_db_error::DbError;
use agdb_serialize::Serialize;
use std::mem::size_of;

impl Serialize for MapValueState {
fn deserialize(bytes: &[u8]) -> Result<Self, DbError> {
match bytes.first() {
Some(0) => Ok(MapValueState::Empty),
Some(1) => Ok(MapValueState::Valid),
Some(2) => Ok(MapValueState::Deleted),
_ => Err(DbError::from("value out of bounds")),
}
}

fn serialize(&self) -> Vec<u8> {
match self {
MapValueState::Empty => vec![0_u8],
MapValueState::Deleted => vec![2_u8],
MapValueState::Valid => vec![1_u8],
}
}

fn serialized_size() -> u64 {
size_of::<u8>() as u64
}
}
Loading