Simplify cht impls

next
Sayan Nandan 1 year ago
parent 05d93d2102
commit 60157e110b
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -90,7 +90,7 @@ pub enum DatabaseError {
NeedLock, NeedLock,
/// expected a full entity, but found a single implicit entity /// expected a full entity, but found a single implicit entity
ExpectedEntity, ExpectedEntity,
// ddl: create space // ddl
/// unknown property or bad type for property /// unknown property or bad type for property
DdlSpaceBadProperty, DdlSpaceBadProperty,
/// the space already exists /// the space already exists

@ -43,6 +43,7 @@ use {
pub type IndexSTSeqCns<K, V> = stord::IndexSTSeqDll<K, V, stord::config::ConservativeConfig<K, V>>; pub type IndexSTSeqCns<K, V> = stord::IndexSTSeqDll<K, V, stord::config::ConservativeConfig<K, V>>;
pub type IndexSTSeqLib<K, V> = stord::IndexSTSeqDll<K, V, stord::config::LiberalConfig<K, V>>; pub type IndexSTSeqLib<K, V> = stord::IndexSTSeqDll<K, V, stord::config::LiberalConfig<K, V>>;
pub type IndexMTRC<K, V> = mtchm::imp::ChmArc<K, V, mtchm::meta::DefConfig>; pub type IndexMTRC<K, V> = mtchm::imp::ChmArc<K, V, mtchm::meta::DefConfig>;
pub type IndexMTRaw<E> = mtchm::RawTree<E, mtchm::meta::DefConfig>;
pub type IndexMTCp<K, V> = mtchm::imp::ChmCopy<K, V, mtchm::meta::DefConfig>; pub type IndexMTCp<K, V> = mtchm::imp::ChmCopy<K, V, mtchm::meta::DefConfig>;
pub type IndexST<K, V, S = std::collections::hash_map::RandomState> = pub type IndexST<K, V, S = std::collections::hash_map::RandomState> =
std::collections::hash_map::HashMap<K, V, S>; std::collections::hash_map::HashMap<K, V, S>;
@ -100,7 +101,7 @@ pub struct DummyMetrics;
/// The base spec for any index. Iterators have meaningless order, and that is intentional and oftentimes /// The base spec for any index. Iterators have meaningless order, and that is intentional and oftentimes
/// consequential. For more specialized impls, use the [`STIndex`], [`MTIndex`] or [`STIndexSeq`] traits /// consequential. For more specialized impls, use the [`STIndex`], [`MTIndex`] or [`STIndexSeq`] traits
pub trait IndexBaseSpec<K: ?Sized, V: ?Sized>: Sized { pub trait IndexBaseSpec: Sized {
/// Index supports prealloc? /// Index supports prealloc?
const PREALLOC: bool; const PREALLOC: bool;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -126,7 +127,7 @@ pub trait IndexBaseSpec<K: ?Sized, V: ?Sized>: Sized {
} }
/// An unordered MTIndex /// An unordered MTIndex
pub trait MTIndex<K, V>: IndexBaseSpec<K, V> { pub trait MTIndex<K, V>: IndexBaseSpec {
type IterKV<'t, 'g, 'v>: Iterator<Item = (&'v K, &'v V)> type IterKV<'t, 'g, 'v>: Iterator<Item = (&'v K, &'v V)>
where where
'g: 't + 'v, 'g: 't + 'v,
@ -202,7 +203,7 @@ pub trait MTIndex<K, V>: IndexBaseSpec<K, V> {
} }
/// An unordered STIndex /// An unordered STIndex
pub trait STIndex<K: ?Sized, V>: IndexBaseSpec<K, V> { pub trait STIndex<K: ?Sized, V>: IndexBaseSpec {
/// An iterator over the keys and values /// An iterator over the keys and values
type IterKV<'a>: Iterator<Item = (&'a K, &'a V)> type IterKV<'a>: Iterator<Item = (&'a K, &'a V)>
where where

@ -34,261 +34,135 @@ use {
RawTree, RawTree,
}, },
crate::engine::{ crate::engine::{
idx::{meta::Comparable, AsKey, AsKeyClone, AsValue, AsValueClone, IndexBaseSpec, MTIndex}, idx::{meta::Comparable, AsKeyClone, AsValue, AsValueClone, IndexBaseSpec, MTIndex},
sync::atm::{upin, Guard}, sync::atm::Guard,
}, },
std::sync::Arc, std::sync::Arc,
}; };
#[inline(always)] pub type Raw<E, C> = RawTree<E, C>;
fn arc<K, V>(k: K, v: V) -> Arc<(K, V)> { pub type ChmArc<K, V, C> = Raw<Arc<(K, V)>, C>;
Arc::new((k, v)) pub type ChmCopy<K, V, C> = Raw<(K, V), C>;
}
pub type ChmArc<K, V, C> = RawTree<Arc<(K, V)>, C>;
impl<K, V, C> IndexBaseSpec<K, V> for ChmArc<K, V, C> impl<E, C: Config> IndexBaseSpec for Raw<E, C> {
where
C: Config,
{
const PREALLOC: bool = false; const PREALLOC: bool = false;
#[cfg(debug_assertions)]
type Metrics = CHTRuntimeLog; type Metrics = CHTRuntimeLog;
fn idx_init() -> Self { fn idx_init() -> Self {
ChmArc::new() Self::new()
} }
fn idx_init_with(s: Self) -> Self { fn idx_init_with(s: Self) -> Self {
s s
} }
#[cfg(debug_assertions)]
fn idx_metrics(&self) -> &Self::Metrics { fn idx_metrics(&self) -> &Self::Metrics {
&self.m &self.m
} }
} }
impl<K, V, C> MTIndex<K, V> for ChmArc<K, V, C> impl<E: TreeElement, C: Config> MTIndex<E::Key, E::Value> for Raw<E, C> {
where type IterKV<'t, 'g, 'v> = IterKV<'t, 'g, 'v, E, C>
C: Config,
K: AsKey,
V: AsValue,
{
type IterKV<'t, 'g, 'v> = IterKV<'t, 'g, 'v, Arc<(K, V)>, C>
where where
'g: 't + 'v, 'g: 't + 'v,
't: 'v, 't: 'v,
K: 'v, E::Key: 'v,
V: 'v, E::Value: 'v,
Self: 't; Self: 't;
type IterKey<'t, 'g, 'v> = IterKey<'t, 'g, 'v, Arc<(K, V)>, C> type IterKey<'t, 'g, 'v> = IterKey<'t, 'g, 'v, E, C>
where where
'g: 't + 'v, 'g: 't + 'v,
't: 'v, 't: 'v,
K: 'v, E::Key: 'v,
Self: 't; Self: 't;
type IterVal<'t, 'g, 'v> = IterVal<'t, 'g, 'v, Arc<(K, V)>, C> type IterVal<'t, 'g, 'v> = IterVal<'t, 'g, 'v, E, C>
where where
'g: 't + 'v, 'g: 't + 'v,
't: 'v, 't: 'v,
V: 'v, E::Value: 'v,
Self: 't; Self: 't;
fn mt_clear(&self, g: &Guard) { fn mt_clear(&self, g: &Guard) {
self.nontransactional_clear(g) self.nontransactional_clear(g)
} }
fn mt_insert(&self, key: K, val: V, g: &Guard) -> bool { fn mt_insert(&self, key: E::Key, val: E::Value, g: &Guard) -> bool
self.patch(VanillaInsert(arc(key, val)), g)
}
fn mt_upsert(&self, key: K, val: V, g: &Guard) {
self.patch(VanillaUpsert(arc(key, val)), g)
}
fn mt_contains<Q>(&self, key: &Q, g: &Guard) -> bool
where
Q: ?Sized + Comparable<K>,
{
self.contains_key(key, g)
}
fn mt_get<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
Q: ?Sized + Comparable<K>,
't: 'v,
'g: 't + 'v,
{
self.get(key, g)
}
fn mt_get_cloned<Q>(&self, key: &Q, g: &Guard) -> Option<V>
where
Q: ?Sized + Comparable<K>,
V: AsValueClone,
{
self.get(key, g).cloned()
}
fn mt_update(&self, key: K, val: V, g: &Guard) -> bool {
self.patch(VanillaUpdate(arc(key, val)), g)
}
fn mt_update_return<'t, 'g, 'v>(&'t self, key: K, val: V, g: &'g Guard) -> Option<&'v V>
where where
't: 'v, E::Value: AsValue,
'g: 't + 'v,
{ {
self.patch(VanillaUpdateRet(arc(key, val)), g) self.patch(VanillaInsert(E::new(key, val)), g)
} }
fn mt_delete<Q>(&self, key: &Q, g: &Guard) -> bool fn mt_upsert(&self, key: E::Key, val: E::Value, g: &Guard)
where where
Q: ?Sized + Comparable<K>, E::Value: AsValue,
{ {
self.remove(key, g) self.patch(VanillaUpsert(E::new(key, val)), g)
}
fn mt_delete_return<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
Q: ?Sized + Comparable<K>,
't: 'v,
'g: 't + 'v,
{
self.remove_return(key, g)
}
}
pub type ChmCopy<K, V, C> = RawTree<(K, V), C>;
impl<K, V, C> IndexBaseSpec<K, V> for ChmCopy<K, V, C>
where
C: Config,
{
const PREALLOC: bool = false;
#[cfg(debug_assertions)]
type Metrics = CHTRuntimeLog;
fn idx_init() -> Self {
ChmCopy::new()
}
fn idx_init_with(s: Self) -> Self {
s
}
#[cfg(debug_assertions)]
fn idx_metrics(&self) -> &Self::Metrics {
&self.m
}
}
impl<K, V, C> MTIndex<K, V> for ChmCopy<K, V, C>
where
C: Config,
K: AsKeyClone,
V: AsValueClone,
{
type IterKV<'t, 'g, 'v> = IterKV<'t, 'g, 'v, (K, V), C>
where
'g: 't + 'v,
't: 'v,
K: 'v,
V: 'v,
Self: 't;
type IterKey<'t, 'g, 'v> = IterKey<'t, 'g, 'v, (K, V), C>
where
'g: 't + 'v,
't: 'v,
K: 'v,
Self: 't;
type IterVal<'t, 'g, 'v> = IterVal<'t, 'g, 'v, (K, V), C>
where
'g: 't + 'v,
't: 'v,
V: 'v,
Self: 't;
fn mt_clear(&self, g: &Guard) {
self.nontransactional_clear(g)
}
fn mt_insert(&self, key: K, val: V, g: &Guard) -> bool {
self.patch(VanillaInsert((key, val)), g)
}
fn mt_upsert(&self, key: K, val: V, g: &Guard) {
self.patch(VanillaUpsert((key, val)), g)
} }
fn mt_contains<Q>(&self, key: &Q, g: &Guard) -> bool fn mt_contains<Q>(&self, key: &Q, g: &Guard) -> bool
where where
Q: ?Sized + Comparable<K>, Q: ?Sized + Comparable<E::Key>,
{ {
self.contains_key(key, g) self.contains_key(key, g)
} }
fn mt_get<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V> fn mt_get<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v E::Value>
where where
Q: ?Sized + Comparable<K>, Q: ?Sized + Comparable<E::Key>,
't: 'v, 't: 'v,
'g: 't + 'v, 'g: 't + 'v,
{ {
self.get(key, g) self.get(key, g)
} }
fn mt_get_cloned<Q>(&self, key: &Q, g: &Guard) -> Option<V> fn mt_get_cloned<Q>(&self, key: &Q, g: &Guard) -> Option<E::Value>
where where
Q: ?Sized + Comparable<K>, Q: ?Sized + Comparable<E::Key>,
E::Value: AsValueClone,
{ {
self.get(key, g).cloned() self.get(key, g).cloned()
} }
fn mt_update(&self, key: K, val: V, g: &Guard) -> bool { fn mt_update(&self, key: E::Key, val: E::Value, g: &Guard) -> bool
self.patch(VanillaUpdate((key, val)), g) where
E::Key: AsKeyClone,
E::Value: AsValue,
{
self.patch(VanillaUpdate(E::new(key, val)), g)
} }
fn mt_update_return<'t, 'g, 'v>(&'t self, key: K, val: V, g: &'g Guard) -> Option<&'v V> fn mt_update_return<'t, 'g, 'v>(
&'t self,
key: E::Key,
val: E::Value,
g: &'g Guard,
) -> Option<&'v E::Value>
where where
E::Key: AsKeyClone,
E::Value: AsValue,
't: 'v, 't: 'v,
'g: 't + 'v, 'g: 't + 'v,
{ {
self.patch(VanillaUpdateRet((key, val)), g) self.patch(VanillaUpdateRet(E::new(key, val)), g)
} }
fn mt_delete<Q>(&self, key: &Q, g: &Guard) -> bool fn mt_delete<Q>(&self, key: &Q, g: &Guard) -> bool
where where
Q: ?Sized + Comparable<K>, Q: ?Sized + Comparable<E::Key>,
{ {
self.remove(key, g) self.remove(key, g)
} }
fn mt_delete_return<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V> fn mt_delete_return<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v E::Value>
where where
Q: ?Sized + Comparable<K>, Q: ?Sized + Comparable<E::Key>,
't: 'v, 't: 'v,
'g: 't + 'v, 'g: 't + 'v,
{ {
self.remove_return(key, g) self.remove_return(key, g)
} }
} }
impl<T: TreeElement, C: Config> FromIterator<T> for RawTree<T, C> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let g = unsafe {
// UNSAFE(@ohsayan): it's me, hi, I'm the problem, it's me. yeah, Taylor knows it too. it's just us
upin()
};
let t = RawTree::new();
iter.into_iter()
.for_each(|te| assert!(t.patch(VanillaInsert(te), g)));
t
}
}

@ -42,7 +42,7 @@ use {
}, },
}; };
impl<K, V, S> IndexBaseSpec<K, V> for StdMap<K, V, S> impl<K, V, S> IndexBaseSpec for StdMap<K, V, S>
where where
S: BuildHasher + Default, S: BuildHasher + Default,
{ {

@ -536,7 +536,7 @@ impl<K: AsKey, V: AsValue, C: Config<K, V>> FromIterator<(K, V)> for IndexSTSeqD
} }
} }
impl<K, V, C: Config<K, V>> IndexBaseSpec<K, V> for IndexSTSeqDll<K, V, C> { impl<K, V, C: Config<K, V>> IndexBaseSpec for IndexSTSeqDll<K, V, C> {
const PREALLOC: bool = true; const PREALLOC: bool = true;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]

@ -51,7 +51,9 @@ pub fn parse_inspect<'a, Qd: QueryData<'a>>(
} }
match state.fw_read() { match state.fw_read() {
Token![model] => Entity::parse_from_state_rounded_result(state).map(Statement::InspectModel), Token![model] => {
Entity::parse_from_state_rounded_result(state).map(Statement::InspectModel)
}
Token![space] if state.cursor_has_ident_rounded() => { Token![space] if state.cursor_has_ident_rounded() => {
Ok(Statement::InspectSpace(unsafe { Ok(Statement::InspectSpace(unsafe {
// UNSAFE(@ohsayan): Safe because of the match predicate // UNSAFE(@ohsayan): Safe because of the match predicate

@ -436,16 +436,9 @@ mod stmt_insert {
let r = parse_ast_node_full::<InsertStatement>(&x[1..]).unwrap(); let r = parse_ast_node_full::<InsertStatement>(&x[1..]).unwrap();
let e = InsertStatement::new( let e = InsertStatement::new(
Entity::Full(Ident::from("twitter"), Ident::from("users")), Entity::Full(Ident::from("twitter"), Ident::from("users")),
into_array_nullable![ into_array_nullable!["sayan", "Sayan", "sayan@example.com", true, 12345, 67890]
"sayan", .to_vec()
"Sayan", .into(),
"sayan@example.com",
true,
12345,
67890
]
.to_vec()
.into(),
); );
assert_eq!(e, r); assert_eq!(e, r);
} }

Loading…
Cancel
Save