From 6f3921e329445c3a356cbcdf65daefa37c15e45b Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 27 Sep 2021 16:52:08 -0600 Subject: [PATCH] use btreeset instead of indexset --- patch-db/Cargo.toml | 1 - patch-db/src/handle.rs | 8 ++++---- patch-db/src/model.rs | 21 +++------------------ patch-db/src/patch.rs | 4 ++-- patch-db/src/store.rs | 8 ++++---- patch-db/src/transaction.rs | 4 ++-- 6 files changed, 15 insertions(+), 31 deletions(-) diff --git a/patch-db/Cargo.toml b/patch-db/Cargo.toml index 3c3e8e3..70285c0 100644 --- a/patch-db/Cargo.toml +++ b/patch-db/Cargo.toml @@ -16,7 +16,6 @@ version = "0.1.0" async-trait = "0.1.42" fd-lock-rs = "0.1.3" futures = "0.3.8" -indexmap = { version = "1.6.2", features = ["serde"] } json-patch = { path = "../json-patch" } json-ptr = { path = "../json-ptr" } lazy_static = "1.4.0" diff --git a/patch-db/src/handle.rs b/patch-db/src/handle.rs index 07688fe..d5f7ce5 100644 --- a/patch-db/src/handle.rs +++ b/patch-db/src/handle.rs @@ -1,10 +1,10 @@ use std::sync::Arc; use async_trait::async_trait; -use indexmap::IndexSet; use json_ptr::{JsonPointer, SegList}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::collections::BTreeSet; use tokio::sync::{broadcast::Receiver, RwLock, RwLockReadGuard}; use crate::{locker::Guard, Locker, PatchDb, Revision, Store, Transaction}; @@ -27,7 +27,7 @@ pub trait DbHandle: Send + Sync { &mut self, ptr: &JsonPointer, store_read_lock: Option>, - ) -> Result, Error>; + ) -> Result, Error>; async fn get_value + Send + Sync, V: SegList + Send + Sync>( &mut self, ptr: &JsonPointer, @@ -101,7 +101,7 @@ impl DbHandle for &mut Handle { &mut self, ptr: &JsonPointer, store_read_lock: Option>, - ) -> Result, Error> { + ) -> Result, Error> { (*self).keys(ptr, store_read_lock).await } async fn get_value + Send + Sync, V: SegList + Send + Sync>( @@ -201,7 +201,7 @@ impl DbHandle for PatchDbHandle { &mut self, ptr: &JsonPointer, store_read_lock: Option>, - ) -> Result, Error> { + ) -> Result, Error> { if let Some(lock) = store_read_lock { lock.keys(ptr) } else { diff --git a/patch-db/src/model.rs b/patch-db/src/model.rs index ebb493f..98aa4f7 100644 --- a/patch-db/src/model.rs +++ b/patch-db/src/model.rs @@ -1,10 +1,9 @@ -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::hash::Hash; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::sync::Arc; -use indexmap::{IndexMap, IndexSet}; use json_patch::{Patch, PatchOperation, RemoveOperation}; use json_ptr::JsonPointer; use serde::{Deserialize, Serialize}; @@ -444,13 +443,6 @@ impl + Ord, V> Map for BTreeMap { self.get(key) } } -impl + Eq + Hash, V> Map for IndexMap { - type Key = K; - type Value = V; - fn get(&self, key: &Self::Key) -> Option<&Self::Value> { - IndexMap::get(self, key) - } -} #[derive(Debug)] pub struct MapModel(Model) @@ -497,14 +489,14 @@ where impl MapModel where T: Serialize + for<'de> Deserialize<'de> + Map, - T::Key: Hash + Eq + for<'de> Deserialize<'de>, + T::Key: Ord + Eq + for<'de> Deserialize<'de>, T::Value: Serialize + for<'de> Deserialize<'de>, { pub async fn keys( &self, db: &mut Db, lock: bool, - ) -> Result, Error> { + ) -> Result, Error> { if lock { db.lock(self.as_ref().clone(), false).await; } @@ -584,10 +576,3 @@ where { type Model = MapModel>; } -impl HasModel for IndexMap -where - K: Serialize + for<'de> Deserialize<'de> + Hash + Eq + AsRef, - V: Serialize + for<'de> Deserialize<'de>, -{ - type Model = MapModel>; -} diff --git a/patch-db/src/patch.rs b/patch-db/src/patch.rs index f5561d5..796118e 100644 --- a/patch-db/src/patch.rs +++ b/patch-db/src/patch.rs @@ -1,10 +1,10 @@ use std::ops::Deref; -use indexmap::IndexSet; use json_patch::{AddOperation, Patch, PatchOperation, RemoveOperation, ReplaceOperation}; use json_ptr::{JsonPointer, SegList}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::collections::BTreeSet; #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] @@ -185,7 +185,7 @@ impl DiffPatch { res } - pub fn keys(&self, mut keys: IndexSet) -> IndexSet { + pub fn keys(&self, mut keys: BTreeSet) -> BTreeSet { for op in &(self.0).0 { match op { PatchOperation::Add(a) => { diff --git a/patch-db/src/store.rs b/patch-db/src/store.rs index b96dede..26b5f70 100644 --- a/patch-db/src/store.rs +++ b/patch-db/src/store.rs @@ -6,11 +6,11 @@ use std::sync::atomic::AtomicU64; use std::sync::Arc; use fd_lock_rs::FdLock; -use indexmap::IndexSet; use json_ptr::{JsonPointer, SegList}; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::collections::BTreeSet; use tokio::fs::File; use tokio::sync::broadcast::{Receiver, Sender}; use tokio::sync::{Mutex, OwnedMutexGuard, RwLock, RwLockWriteGuard}; @@ -132,10 +132,10 @@ impl Store { pub(crate) fn keys, V: SegList>( &self, ptr: &JsonPointer, - ) -> Result, Error> { + ) -> Result, Error> { Ok(match ptr.get(self.get_data()?).unwrap_or(&Value::Null) { Value::Object(o) => o.keys().cloned().collect(), - _ => IndexSet::new(), + _ => BTreeSet::new(), }) } pub(crate) fn get Deserialize<'de>, S: AsRef, V: SegList>( @@ -240,7 +240,7 @@ impl PatchDb { pub async fn keys, V: SegList>( &self, ptr: &JsonPointer, - ) -> Result, Error> { + ) -> Result, Error> { self.store.read().await.keys(ptr) } pub async fn get Deserialize<'de>, S: AsRef, V: SegList>( diff --git a/patch-db/src/transaction.rs b/patch-db/src/transaction.rs index 5035c8d..cc6d766 100644 --- a/patch-db/src/transaction.rs +++ b/patch-db/src/transaction.rs @@ -1,10 +1,10 @@ use std::sync::Arc; use async_trait::async_trait; -use indexmap::IndexSet; use json_ptr::{JsonPointer, SegList}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::collections::BTreeSet; use tokio::sync::broadcast::error::TryRecvError; use tokio::sync::broadcast::Receiver; use tokio::sync::{RwLock, RwLockReadGuard}; @@ -123,7 +123,7 @@ impl DbHandle for Transaction { &mut self, ptr: &JsonPointer, store_read_lock: Option>, - ) -> Result, Error> { + ) -> Result, Error> { let keys = { let store_lock = self.parent.store(); let store = if let Some(store_read_lock) = store_read_lock {