Skip to content

Commit

Permalink
[server] Use index for token and username #1027 (#1028)
Browse files Browse the repository at this point in the history
* rename user field to username

* use index

* minor tweak

* fix multi_map

* remove dead code
  • Loading branch information
michaelvlach authored Jan 29, 2024
1 parent e7dbfc4 commit d060c43
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 194 deletions.
48 changes: 27 additions & 21 deletions agdb/src/collections/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl BitSet {
self.data.clear();
}

pub fn insert(&mut self, value: u64) {
pub fn set(&mut self, value: u64) {
let byte_index = value as usize / 8;
let bit_index = value as usize % 8;

Expand All @@ -24,8 +24,14 @@ impl BitSet {
Self { data: vec![] }
}

pub fn with_capacity(capacity: u64) -> Self {
Self {
data: Vec::with_capacity(capacity as usize / 8),
}
}

#[allow(dead_code)]
pub fn remove(&mut self, value: u64) {
pub fn unset(&mut self, value: u64) {
let byte_index = value as usize / 8;

if byte_index < self.data.len() {
Expand Down Expand Up @@ -55,7 +61,7 @@ mod tests {
fn clear() {
let mut bitset = BitSet::new();

bitset.insert(10_u64);
bitset.set(10_u64);
bitset.clear();

assert!(!bitset.value(10_u64));
Expand All @@ -72,7 +78,7 @@ mod tests {

assert!(!bitset.value(10_u64));

bitset.insert(10_u64);
bitset.set(10_u64);

assert!(bitset.value(10_u64));
}
Expand All @@ -85,24 +91,24 @@ mod tests {
assert!(!bitset.value(11_u64));
assert!(!bitset.value(2_u64));

bitset.insert(10_u64);
bitset.insert(11_u64);
bitset.insert(2_u64);
bitset.set(10_u64);
bitset.set(11_u64);
bitset.set(2_u64);

assert!(bitset.value(10_u64));
assert!(bitset.value(11_u64));
assert!(bitset.value(2_u64));
}

#[test]
fn remove() {
fn unset() {
let mut bitset = BitSet::new();

bitset.insert(10_u64);
bitset.insert(11_u64);
bitset.insert(2_u64);
bitset.set(10_u64);
bitset.set(11_u64);
bitset.set(2_u64);

bitset.remove(11_u64);
bitset.unset(11_u64);

assert!(bitset.value(10_u64));
assert!(!bitset.value(11_u64));
Expand All @@ -113,11 +119,11 @@ mod tests {
fn remove_unset() {
let mut bitset = BitSet::new();

bitset.insert(10_u64);
bitset.insert(11_u64);
bitset.insert(2_u64);
bitset.set(10_u64);
bitset.set(11_u64);
bitset.set(2_u64);

bitset.remove(9_u64);
bitset.unset(9_u64);

assert!(bitset.value(10_u64));
assert!(bitset.value(11_u64));
Expand All @@ -128,11 +134,11 @@ mod tests {
fn remove_beyond_length() {
let mut bitset = BitSet::new();

bitset.insert(10_u64);
bitset.insert(11_u64);
bitset.insert(2_u64);
bitset.set(10_u64);
bitset.set(11_u64);
bitset.set(2_u64);

bitset.remove(150_u64);
bitset.unset(150_u64);

assert!(bitset.value(10_u64));
assert!(bitset.value(11_u64));
Expand All @@ -143,7 +149,7 @@ mod tests {
fn value_missing() {
let mut bitset = BitSet::new();

bitset.insert(5_u64);
bitset.set(5_u64);

assert!(!bitset.value(2_u64));
}
Expand Down
50 changes: 42 additions & 8 deletions agdb/src/collections/multi_map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::collections::bit_set::BitSet;
use crate::collections::map::DbMapData;
use crate::collections::map::MapData;
use crate::collections::map::MapIterator;
Expand Down Expand Up @@ -460,14 +461,20 @@ where
storage: &mut Storage<D>,
i: &mut u64,
new_capacity: u64,
empty_list: &mut [bool],
occupancy: &mut BitSet,
) -> Result<(), DbError> {
if *i < new_capacity && occupancy.value(*i) {
*i += 1;
return Ok(());
}

let key = self.data.key(storage, *i)?;
let mut pos = key.stable_hash() % new_capacity;
let hash = key.stable_hash();
let mut pos = hash % new_capacity;

loop {
if empty_list[pos as usize] {
empty_list[pos as usize] = false;
if !occupancy.value(pos) {
occupancy.set(pos);
self.data.swap(storage, *i, pos)?;

if *i == pos {
Expand All @@ -493,12 +500,12 @@ where
state: MapValueState,
i: &mut u64,
new_capacity: u64,
empty_list: &mut [bool],
occupancy: &mut BitSet,
) -> Result<(), DbError> {
match state {
MapValueState::Empty => self.rehash_empty(i),
MapValueState::Deleted => self.rehash_deleted(storage, i, new_capacity),
MapValueState::Valid => self.rehash_valid(storage, i, new_capacity, empty_list),
MapValueState::Valid => self.rehash_valid(storage, i, new_capacity, occupancy),
}
}

Expand All @@ -510,10 +517,15 @@ where
new_capacity: u64,
) -> Result<(), DbError> {
let mut i = 0_u64;
let mut empty_list = vec![true; new_capacity as usize];
let mut occupancy = BitSet::with_capacity(new_capacity);

while i != current_capacity {
self.rehash_value(storage, self.data.state(storage, i)?, &mut i, new_capacity, &mut empty_list)?;
self.rehash_value(
storage,
self.data.state(storage, i)?,
&mut i,
new_capacity,
&mut occupancy)?;
}

Ok(())
Expand Down Expand Up @@ -814,4 +826,26 @@ mod tests {

assert_eq!(storage.len(), 0)
}

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

let users: Vec<(String, String)> = range
.clone()
.rev()
.map(|i| (format!("db_user{i}"), i.to_string()))
.collect();

for (user, value) in users {
map.insert(&mut storage, &user, &value).unwrap();
}

for i in range {
let value = map.value(&storage, &format!("db_user{i}")).unwrap();
assert_eq!(value, Some(i.to_string()));
}
}
}
3 changes: 0 additions & 3 deletions agdb/src/db/db_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub struct DbIndexStorageIndex {
ids_index: StorageIndex,
}

#[allow(dead_code)]
pub struct DbIndex<D>
where
D: StorageData,
Expand All @@ -27,7 +26,6 @@ where
ids: MultiMapStorage<DbValue, DbId, D>,
}

#[allow(dead_code)]
pub struct DbIndexes<D>
where
D: StorageData,
Expand All @@ -36,7 +34,6 @@ where
storage_indexes: DbVec<DbIndexStorageIndex, D>,
}

#[allow(dead_code)]
impl<D> DbIndex<D>
where
D: StorageData,
Expand Down
2 changes: 1 addition & 1 deletion agdb/src/graph_search/path_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where
if index.0 == self.destination.0 {
std::mem::swap(&mut self.result, &mut self.current_path.elements);
} else {
self.visited.insert(index.as_u64());
self.visited.set(index.as_u64());
self.expand(index)?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion agdb/src/graph_search/search_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where

fn visit_index(&mut self, index: &SearchIndex) -> bool {
let visited = self.visited.value(index.index.as_u64());
self.visited.insert(index.index.as_u64());
self.visited.set(index.index.as_u64());

visited
}
Expand Down
1 change: 0 additions & 1 deletion agdb/src/test_utilities.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod collision_value;
pub mod test_file;
124 changes: 0 additions & 124 deletions agdb/src/test_utilities/collision_value.rs

This file was deleted.

2 changes: 1 addition & 1 deletion agdb_server/openapi/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": {
"name": "Apache-2.0"
},
"version": "0.1.0"
"version": "0.6.4"
},
"servers": [
{
Expand Down
Loading

0 comments on commit d060c43

Please sign in to comment.