Skip to content

Commit

Permalink
[db] Add db index #967 (#968)
Browse files Browse the repository at this point in the history
add db_index
  • Loading branch information
michaelvlach authored Jan 2, 2024
1 parent 8d5ac5f commit 5a6f241
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 9 deletions.
16 changes: 12 additions & 4 deletions agdb/src/collections/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ where
fn commit(&mut self, storage: &mut Storage<D>, id: u64) -> Result<(), DbError>;
fn len(&self) -> u64;
fn key(&self, storage: &Storage<D>, index: u64) -> Result<K, DbError>;
fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), DbError>;
fn resize(&mut self, storage: &mut Storage<D>, capacity: u64) -> Result<(), DbError>;
fn set_len(&mut self, storage: &mut Storage<D>, len: u64) -> Result<(), DbError>;
fn set_state(
Expand Down Expand Up @@ -231,6 +232,15 @@ where
self.keys.value(storage, index)
}

fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), DbError> {
let id = storage.transaction();
self.states.remove_from_storage(storage)?;
self.values.remove_from_storage(storage)?;
self.keys.remove_from_storage(storage)?;
storage.remove(self.storage_index)?;
storage.commit(id)
}

fn resize(&mut self, storage: &mut Storage<D>, capacity: u64) -> Result<(), DbError> {
self.states
.resize(storage, capacity, &MapValueState::Empty)?;
Expand Down Expand Up @@ -442,10 +452,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{
storage::file_storage_memory_mapped::FileStorageMemoryMapped,
test_utilities::test_file::TestFile,
};
use crate::storage::file_storage_memory_mapped::FileStorageMemoryMapped;
use crate::test_utilities::test_file::TestFile;

#[test]
fn derived_from_clone() {
Expand Down
39 changes: 35 additions & 4 deletions agdb/src/collections/multi_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ where
self.data.commit(storage, id)
}

pub fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), DbError> {
self.data.remove_from_storage(storage)
}

pub fn reserve(&mut self, storage: &mut Storage<D>, capacity: u64) -> Result<(), DbError> {
if self.capacity() < capacity {
self.rehash(storage, capacity)?;
Expand Down Expand Up @@ -567,10 +571,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{
storage::file_storage_memory_mapped::FileStorageMemoryMapped,
test_utilities::test_file::TestFile,
};
use crate::storage::file_storage_memory_mapped::FileStorageMemoryMapped;
use crate::test_utilities::test_file::TestFile;
use crate::MemoryStorage;

#[test]
fn new() {
Expand Down Expand Up @@ -783,4 +786,32 @@ mod tests {
]
);
}

#[test]
fn remove_from_storage() {
let mut storage: Storage<MemoryStorage> = Storage::new("test").unwrap();
let mut map = MultiMapStorage::<String, String, MemoryStorage>::new(&mut storage).unwrap();

map.insert(
&mut storage,
&"key".to_string(),
&"This is a long string that does not fit into small value".to_string(),
)
.unwrap();

map.insert(
&mut storage,
&"key".to_string(),
&"Some other value that is also longish".to_string(),
)
.unwrap();

map.remove_from_storage(&mut storage).unwrap();

assert_ne!(storage.len(), 0);

storage.shrink_to_fit().unwrap();

assert_eq!(storage.len(), 0)
}
}
41 changes: 40 additions & 1 deletion agdb/src/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ where
fn capacity(&self) -> u64;
fn len(&self) -> u64;
fn reallocate(&mut self, storage: &mut Storage<D>, capacity: u64) -> Result<(), E>;
fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), E>;
fn remove(&mut self, storage: &mut Storage<D>, index: u64) -> Result<T, E>;
fn replace(&mut self, storage: &mut Storage<D>, index: u64, value: &T) -> Result<T, E>;
fn resize(&mut self, storage: &mut Storage<D>, new_len: u64, value: &T) -> Result<(), E>;
Expand Down Expand Up @@ -152,6 +153,21 @@ where
Ok(value)
}

fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), E> {
let id = storage.transaction();

for index in 0..self.len() {
let bytes = storage
.value_as_bytes_at_size(self.storage_index, Self::offset(index), T::storage_len())?
.to_vec();
T::remove(storage, &bytes)?;
}

storage.remove(self.storage_index)?;
storage.commit(id)?;
Ok(())
}

fn replace(&mut self, storage: &mut Storage<D>, index: u64, value: &T) -> Result<T, E> {
let old_bytes = storage
.value_as_bytes_at_size(self.storage_index, Self::offset(index), T::storage_len())?
Expand Down Expand Up @@ -299,6 +315,10 @@ where
self.data.resize(storage, self.data.len() + 1, value)
}

pub fn remove_from_storage(self, storage: &mut Storage<D>) -> Result<(), E> {
self.data.remove_from_storage(storage)
}

#[allow(dead_code)]
pub fn remove(&mut self, storage: &mut Storage<D>, index: u64) -> Result<T, E> {
self.validate_index(index)?;
Expand Down Expand Up @@ -407,7 +427,7 @@ mod tests {
use super::*;
use crate::{
storage::file_storage_memory_mapped::FileStorageMemoryMapped,
test_utilities::test_file::TestFile,
test_utilities::test_file::TestFile, MemoryStorage,
};

#[test]
Expand Down Expand Up @@ -880,4 +900,23 @@ mod tests {
Err(DbError::from("Index (0) out of bounds (0)"))
);
}

#[test]
fn remove_from_storage() {
let mut storage: Storage<MemoryStorage> = Storage::new("test").unwrap();
let mut vec = DbVec::<String, MemoryStorage>::new(&mut storage).unwrap();
vec.push(
&mut storage,
&"This is a long string that does not fit into small value".to_string(),
)
.unwrap();

vec.remove_from_storage(&mut storage).unwrap();

assert_ne!(storage.len(), 0);

storage.shrink_to_fit().unwrap();

assert_eq!(storage.len(), 0)
}
}
1 change: 1 addition & 0 deletions agdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod db_element;
pub mod db_error;
pub mod db_f64;
pub mod db_id;
pub mod db_index;
pub mod db_key;
pub mod db_key_value;
pub mod db_user_value;
Expand Down
Loading

0 comments on commit 5a6f241

Please sign in to comment.