Add mt idx impls

next
Sayan Nandan 2 years ago
parent 48ada558ad
commit 57c7a6447b
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -30,6 +30,7 @@ mod stord;
#[cfg(test)]
mod tests;
use super::sync::atm::Guard;
use core::{borrow::Borrow, hash::Hash};
// re-exports
@ -139,59 +140,65 @@ pub trait MTIndex<K, V>: IndexBaseSpec<K, V> {
/// Attempts to compact the backing storage
fn mt_compact(&self) {}
/// Clears all the entries in the MTIndex
fn mt_clear(&self);
fn mt_clear(&self, g: &Guard);
// write
/// Returns true if the entry was inserted successfully; returns false if the uniqueness constraint is
/// violated
fn mt_insert(&self, key: K, val: V) -> bool
fn mt_insert(&self, key: K, val: V, g: &Guard) -> bool
where
K: AsKeyClone,
V: AsValue;
/// Updates or inserts the given value
fn mt_upsert(&self, key: K, val: V)
fn mt_upsert(&self, key: K, val: V, g: &Guard)
where
K: AsKeyClone,
V: AsValue;
// read
fn mt_contains<Q>(&self, key: &Q) -> bool
fn mt_contains<Q>(&self, key: &Q, g: &Guard) -> bool
where
K: Borrow<Q> + AsKeyClone,
Q: ?Sized + AsKey;
/// Returns a reference to the value corresponding to the key, if it exists
fn mt_get<Q>(&self, key: &Q) -> Option<&V>
fn mt_get<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
K: AsKeyClone + Borrow<Q>,
Q: ?Sized + AsKey;
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v;
/// Returns a clone of the value corresponding to the key, if it exists
fn mt_get_cloned<Q>(&self, key: &Q) -> Option<V>
fn mt_get_cloned<Q>(&self, key: &Q, g: &Guard) -> Option<V>
where
K: AsKeyClone + Borrow<Q>,
Q: ?Sized + AsKey,
V: AsValueClone;
// update
/// Returns true if the entry is updated
fn mt_update<Q>(&self, key: &Q, val: V) -> bool
fn mt_update<Q>(&self, key: K, val: V, g: &Guard) -> bool
where
K: AsKeyClone + Borrow<Q>,
K: AsKeyClone,
V: AsValue,
Q: ?Sized + AsKey;
/// Updates the entry and returns the old value, if it exists
fn mt_update_return<Q>(&self, key: &Q, val: V) -> Option<V>
fn mt_update_return<'t, 'g, 'v, Q>(&'t self, key: K, val: V, g: &'g Guard) -> Option<&'v V>
where
K: AsKeyClone + Borrow<Q>,
K: AsKeyClone,
V: AsValue,
Q: ?Sized + AsKey;
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v;
// delete
/// Returns true if the entry was deleted
fn mt_delete<Q>(&self, key: &Q) -> bool
fn mt_delete<Q>(&self, key: &Q, g: &Guard) -> bool
where
K: AsKeyClone + Borrow<Q>,
Q: ?Sized + AsKey;
/// Removes the entry and returns it, if it exists
fn mt_delete_return<Q>(&self, key: &Q) -> Option<V>
fn mt_delete_return<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
K: AsKeyClone + Borrow<Q>,
Q: ?Sized + AsKey;
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v;
}
/// An unordered STIndex

@ -25,11 +25,17 @@
*/
use super::{
super::{DummyMetrics, IndexBaseSpec},
meta::{AsHasher, Config},
super::{super::sync::atm::Guard, AsKey, DummyMetrics, IndexBaseSpec, MTIndex},
iter::{IterKV, IterKey, IterVal},
meta::{AsHasher, Config, Key, Value},
Tree,
};
use std::sync::Arc;
use std::{borrow::Borrow, sync::Arc};
#[inline(always)]
fn arc<K, V>(k: K, v: V) -> Arc<(K, V)> {
Arc::new((k, v))
}
pub type MTArc<K, V, S, C> = Tree<Arc<(K, V)>, S, C>;
@ -54,3 +60,105 @@ where
&DummyMetrics
}
}
impl<K, V, S, C> MTIndex<K, V> for MTArc<K, V, S, C>
where
C: Config,
S: AsHasher,
K: Key,
V: Value,
{
type IterKV<'t, 'g, 'v> = IterKV<'t, 'g, 'v, (K, V), S, C>
where
'g: 't + 'v,
't: 'v,
K: 'v,
V: 'v,
Self: 't;
type IterKey<'t, 'g, 'v> = IterKey<'t, 'g, 'v, (K, V), S, C>
where
'g: 't + 'v,
't: 'v,
K: 'v,
Self: 't;
type IterVal<'t, 'g, 'v> = IterVal<'t, 'g, 'v, (K, V), S, 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.insert(arc(key, val), g)
}
fn mt_upsert(&self, key: K, val: V, g: &Guard) {
self.upsert(arc(key, val), g)
}
fn mt_contains<Q>(&self, key: &Q, g: &Guard) -> bool
where
K: Borrow<Q>,
Q: ?Sized + AsKey,
{
self.contains_key(key, g)
}
fn mt_get<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
K: Borrow<Q>,
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v,
{
self.get(key, g)
}
fn mt_get_cloned<Q>(&self, key: &Q, g: &Guard) -> Option<V>
where
K: Borrow<Q>,
Q: ?Sized + AsKey,
{
self.get(key, g).cloned()
}
fn mt_update<Q>(&self, key: K, val: V, g: &Guard) -> bool
where
Q: ?Sized + AsKey,
{
self.update(arc(key, val), g)
}
fn mt_update_return<'t, 'g, 'v, Q>(&'t self, key: K, val: V, g: &'g Guard) -> Option<&'v V>
where
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v,
{
self.update_return(arc(key, val), g)
}
fn mt_delete<Q>(&self, key: &Q, g: &Guard) -> bool
where
K: Borrow<Q>,
Q: ?Sized + AsKey,
{
self.remove(key, g)
}
fn mt_delete_return<'t, 'g, 'v, Q>(&'t self, key: &Q, g: &'g Guard) -> Option<&'v V>
where
K: Borrow<Q>,
Q: ?Sized + AsKey,
't: 'v,
'g: 't + 'v,
{
self.remove_return(key, g)
}
}

@ -39,6 +39,7 @@ where
't: 'v,
'g: 'v + 't,
C: Config,
T: TreeElement,
{
i: RawIter<'t, 'g, 'v, T, S, C, CfgIterKV>,
}
@ -48,6 +49,7 @@ where
't: 'v,
'g: 'v + 't,
C: Config,
T: TreeElement,
{
pub fn new(t: &'t Tree<T, S, C>, g: &'g Guard) -> Self {
Self {
@ -63,7 +65,7 @@ where
C: Config,
T: TreeElement,
{
type Item = &'v T;
type Item = (&'v T::Key, &'v T::Value);
fn next(&mut self) -> Option<Self::Item> {
self.i.next()
@ -154,10 +156,10 @@ trait IterConfig<T> {
}
struct CfgIterKV;
impl<T> IterConfig<T> for CfgIterKV {
type Ret<'a> = &'a T where T: 'a;
impl<T: TreeElement> IterConfig<T> for CfgIterKV {
type Ret<'a> = (&'a T::Key, &'a T::Value) where T: 'a;
fn some<'a>(v: &'a T) -> Option<Self::Ret<'a>> {
Some(v)
Some((v.key(), v.val()))
}
}

@ -25,9 +25,9 @@
*/
mod access;
mod imp;
mod iter;
mod meta;
mod imp;
use self::{
access::{ReadMode, WriteMode},
@ -57,8 +57,12 @@ impl<C: Config> Node<C> {
const NULL: Atomic<Self> = Atomic::null();
const NULL_BRANCH: [Atomic<Self>; <DefConfig as Config>::BRANCH_MX] =
[Self::NULL; <DefConfig as Config>::BRANCH_MX];
const _SZ: usize = mem::size_of::<Self>() / mem::size_of::<Atomic<Self>>();
const _ALIGN: usize = C::BRANCH_MX / Self::_SZ;
const _EQ: () = assert!(Self::_ALIGN == 1);
#[inline(always)]
const fn null() -> Self {
let _ = Self::_EQ;
Self {
branch: Self::NULL_BRANCH,
}
@ -161,6 +165,11 @@ impl<T: TreeElement, S, C: Config> Tree<T, S, C> {
}
impl<T: TreeElement, S: AsHasher, C: Config> Tree<T, S, C> {
fn nontransactional_clear(&self, g: &Guard) {
self.iter_key(g).for_each(|k| {
let _ = self.remove(k, g);
});
}
fn insert(&self, elem: T, g: &Guard) -> bool {
self._insert::<access::WModeFresh>(elem, g)
}
@ -334,14 +343,14 @@ impl<T: TreeElement, S: AsHasher, C: Config> Tree<T, S, C> {
}
}
}
fn contains_key<'g, Q, R: ReadMode<T>>(&'g self, k: &Q, g: &'g Guard) -> bool
fn contains_key<'g, Q>(&'g self, k: &Q, g: &'g Guard) -> bool
where
T::Key: Borrow<Q>,
Q: AsKey + ?Sized,
{
self._lookup::<Q, access::RModeExists>(k, g)
}
fn get<'g, Q, R: ReadMode<T>>(&'g self, k: &Q, g: &'g Guard) -> Option<&'g T::Value>
fn get<'g, Q>(&'g self, k: &Q, g: &'g Guard) -> Option<&'g T::Value>
where
T::Key: Borrow<Q>,
Q: AsKey + ?Sized,
@ -551,9 +560,7 @@ impl<T, S, C: Config> Tree<T, S, C> {
debug_assert!(hf(ldfl(&leaf), NodeFlag::DATA));
drop(Owned::<LNode<T>>::from_raw(leaf.as_raw() as *mut _))
}
unsafe fn rdrop(n: &Atomic<Node<C>>) {
let g = upin();
let node = n.ld_acq(g);
unsafe fn _rdrop(node: Shared<Node<C>>) {
match ldfl(&node) {
_ if node.is_null() => {}
flag if hf(flag, NodeFlag::DATA) => Self::ldrop(node),
@ -567,6 +574,11 @@ impl<T, S, C: Config> Tree<T, S, C> {
}
}
}
unsafe fn rdrop(n: &Atomic<Node<C>>) {
let g = upin();
let node = n.ld_acq(g);
Self::_rdrop(node);
}
unsafe fn compress<'g>(
parent: &Atomic<Node<C>>,
child: Shared<'g, Node<C>>,

Loading…
Cancel
Save