#[cfg(test)]
mod test;
use crate::{
basic::{
mapx_ord::{Entry, MapxOrdValues, MapxOrdValuesMut},
mapx_ord_rawkey::{
self, MapxOrdRawKey, MapxOrdRawKeyBatchEntry, MapxOrdRawKeyIter,
MapxOrdRawKeyIterMut, ValueMut,
},
},
common::ende::{KeyEnDe, ValueEnDe},
define_map_wrapper,
};
use ruc::*;
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
define_map_wrapper! {
#[doc = "A disk-based, `HashMap`-like data structure with typed keys and values."]
#[doc = ""]
#[doc = "`Mapx` stores key-value pairs on disk, encoding both keys and values"]
#[doc = "for type safety and persistence."]
pub struct Mapx<K, V> {
inner: MapxOrdRawKey<V>,
_p: PhantomData<K>,
}
where K: KeyEnDe, V: ValueEnDe
}
impl<K, V> Mapx<K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
#[inline(always)]
pub fn get(&self, key: &K) -> Option<V> {
self.inner.get(key.encode())
}
#[inline(always)]
pub fn get_mut(&mut self, key: &K) -> Option<ValueMut<'_, V>> {
self.inner.get_mut(key.encode())
}
#[inline(always)]
pub fn contains_key(&self, key: &K) -> bool {
self.inner.contains_key(key.encode())
}
#[inline(always)]
pub fn insert(&mut self, key: &K, value: &V) {
self.inner.insert(key.encode(), value)
}
#[inline(always)]
pub fn entry(&mut self, key: &K) -> Entry<'_, V> {
Entry {
key: key.encode(),
hdr: &mut self.inner,
}
}
#[inline(always)]
pub fn iter(&self) -> MapxIter<'_, K, V> {
MapxIter {
iter: self.inner.iter(),
_p: PhantomData,
}
}
#[inline(always)]
pub fn iter_mut(&mut self) -> MapxIterMut<'_, K, V> {
MapxIterMut {
inner: self.inner.iter_mut(),
_p: PhantomData,
}
}
#[inline(always)]
pub fn values(&self) -> MapxValues<'_, V> {
MapxValues {
inner: self.inner.iter(),
}
}
#[inline(always)]
pub fn values_mut(&mut self) -> MapxValuesMut<'_, V> {
MapxValuesMut {
inner: self.inner.inner.iter_mut(),
_p: PhantomData,
}
}
#[inline(always)]
pub fn remove(&mut self, key: &K) {
self.inner.remove(key.encode())
}
#[inline(always)]
pub fn batch_entry(&mut self) -> MapxBatchEntry<'_, K, V> {
MapxBatchEntry {
inner: self.inner.batch_entry(),
_marker: PhantomData,
}
}
#[inline(always)]
pub fn keys(&self) -> impl Iterator<Item = K> + '_ {
self.iter().map(|(k, _)| k)
}
}
impl<'a, K, V> IntoIterator for &'a Mapx<K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
type Item = (K, V);
type IntoIter = MapxIter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut Mapx<K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
type Item = (K, ValueIterMut<'a, V>);
type IntoIter = MapxIterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
pub struct MapxBatchEntry<'a, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
inner: MapxOrdRawKeyBatchEntry<'a, V>,
_marker: PhantomData<K>,
}
impl<'a, K, V> MapxBatchEntry<'a, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
pub fn insert(&mut self, key: &K, value: &V) {
self.inner.insert(key.encode(), value);
}
pub fn remove(&mut self, key: &K) {
self.inner.remove(key.encode());
}
pub fn commit(self) -> Result<()> {
self.inner.commit()
}
}
pub struct MapxIter<'a, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
iter: MapxOrdRawKeyIter<'a, V>,
_p: PhantomData<K>,
}
impl<K, V> Iterator for MapxIter<'_, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|(k, v)| (<K as KeyEnDe>::decode(&k).unwrap(), v))
}
}
impl<K, V> DoubleEndedIterator for MapxIter<'_, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter
.next_back()
.map(|(k, v)| (<K as KeyEnDe>::decode(&k).unwrap(), v))
}
}
pub struct MapxIterMut<'a, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
inner: MapxOrdRawKeyIterMut<'a, V>,
_p: PhantomData<K>,
}
impl<'a, K, V> Iterator for MapxIterMut<'a, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
type Item = (K, ValueIterMut<'a, V>);
fn next(&mut self) -> Option<Self::Item> {
self.inner
.next()
.map(|(k, v)| (pnk!(<K as KeyEnDe>::decode(&k)), ValueIterMut { inner: v }))
}
}
impl<K, V> DoubleEndedIterator for MapxIterMut<'_, K, V>
where
K: KeyEnDe,
V: ValueEnDe,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.inner
.next_back()
.map(|(k, v)| (pnk!(<K as KeyEnDe>::decode(&k)), ValueIterMut { inner: v }))
}
}
type MapxValues<'a, V> = MapxOrdValues<'a, V>;
type MapxValuesMut<'a, V> = MapxOrdValuesMut<'a, V>;
#[derive(Debug)]
pub struct ValueIterMut<'a, V>
where
V: ValueEnDe,
{
pub(crate) inner: mapx_ord_rawkey::ValueIterMut<'a, V>,
}
impl<V> Deref for ValueIterMut<'_, V>
where
V: ValueEnDe,
{
type Target = V;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<V> DerefMut for ValueIterMut<'_, V>
where
V: ValueEnDe,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}