#[cfg(test)]
mod test;
use crate::common::{RawKey, ende::ValueEnDe};
use crate::define_map_wrapper;
use ruc::*;
use std::{
borrow::Cow,
marker::PhantomData,
ops::{Deref, DerefMut, RangeBounds},
};
use vsdb_core::basic::mapx_raw::{self, MapxRaw, MapxRawIter};
define_map_wrapper! {
#[doc = "A disk-based, `BTreeMap`-like data structure with raw keys and typed values."]
#[doc = ""]
#[doc = "`MapxOrdRawKey` stores keys as raw bytes and values as encoded data."]
pub struct MapxOrdRawKey<V> {
pub(crate) inner: MapxRaw,
_p: PhantomData<V>,
}
where V: ValueEnDe
}
impl<V> MapxOrdRawKey<V>
where
V: ValueEnDe,
{
#[inline(always)]
pub fn get(&self, key: impl AsRef<[u8]>) -> Option<V> {
self.inner
.get(key.as_ref())
.map(|v| <V as ValueEnDe>::decode(&v).unwrap())
}
#[inline(always)]
pub fn get_mut(&mut self, key: impl AsRef<[u8]>) -> Option<ValueMut<'_, V>> {
self.inner.get_mut(key.as_ref()).map(|inner| ValueMut {
value: <V as ValueEnDe>::decode(&inner).unwrap(),
inner,
})
}
#[inline(always)]
pub(crate) fn mock_value_mut(&mut self, key: RawKey, value: V) -> ValueMut<'_, V> {
let v = value.encode();
ValueMut {
value,
inner: self.inner.mock_value_mut(key, v),
}
}
#[inline(always)]
pub fn contains_key(&self, key: impl AsRef<[u8]>) -> bool {
self.inner.contains_key(key.as_ref())
}
#[inline(always)]
pub fn get_le(&self, key: impl AsRef<[u8]>) -> Option<(RawKey, V)> {
self.inner
.get_le(key.as_ref())
.map(|(k, v)| (k, <V as ValueEnDe>::decode(&v).unwrap()))
}
#[inline(always)]
pub fn get_ge(&self, key: impl AsRef<[u8]>) -> Option<(RawKey, V)> {
self.inner
.get_ge(key.as_ref())
.map(|(k, v)| (k, <V as ValueEnDe>::decode(&v).unwrap()))
}
#[inline(always)]
pub fn insert(&mut self, key: impl AsRef<[u8]>, value: &V) {
self.inner.insert(key.as_ref(), value.encode())
}
#[inline(always)]
pub unsafe fn insert_encoded_value(
&mut self,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) {
self.inner.insert(key.as_ref(), value.as_ref())
}
#[inline(always)]
pub fn entry<'a>(&'a mut self, key: &'a [u8]) -> Entry<'a, V> {
Entry { key, hdr: self }
}
#[inline(always)]
pub fn iter(&self) -> MapxOrdRawKeyIter<'_, V> {
MapxOrdRawKeyIter {
inner: self.inner.iter(),
_p: PhantomData,
}
}
#[inline(always)]
pub fn iter_mut(&mut self) -> MapxOrdRawKeyIterMut<'_, V> {
MapxOrdRawKeyIterMut {
inner: self.inner.iter_mut(),
_p: PhantomData,
}
}
#[inline(always)]
pub fn range<'a, R: RangeBounds<Cow<'a, [u8]>>>(
&'a self,
bounds: R,
) -> MapxOrdRawKeyIter<'a, V> {
MapxOrdRawKeyIter {
inner: self.inner.range(bounds),
_p: PhantomData,
}
}
#[inline(always)]
pub fn range_mut<'a, R: RangeBounds<Cow<'a, [u8]>>>(
&'a mut self,
bounds: R,
) -> MapxOrdRawKeyIterMut<'a, V> {
MapxOrdRawKeyIterMut {
inner: self.inner.range_mut(bounds),
_p: PhantomData,
}
}
#[inline(always)]
pub fn first(&self) -> Option<(RawKey, V)> {
self.iter().next()
}
#[inline(always)]
pub fn last(&self) -> Option<(RawKey, V)> {
self.iter().next_back()
}
#[inline(always)]
pub fn remove(&mut self, key: impl AsRef<[u8]>) {
self.inner.remove(key.as_ref())
}
#[inline(always)]
pub fn batch_entry(&mut self) -> MapxOrdRawKeyBatchEntry<'_, V> {
MapxOrdRawKeyBatchEntry {
inner: self.inner.batch_entry(),
_marker: PhantomData,
}
}
}
pub struct MapxOrdRawKeyBatch<'a, V>
where
V: ValueEnDe,
{
inner: &'a mut dyn vsdb_core::common::BatchTrait,
_marker: PhantomData<V>,
}
impl<'a, V> MapxOrdRawKeyBatch<'a, V>
where
V: ValueEnDe,
{
pub fn insert(&mut self, key: impl AsRef<[u8]>, value: &V) {
self.inner.insert(key.as_ref(), &value.encode());
}
pub fn remove(&mut self, key: impl AsRef<[u8]>) {
self.inner.remove(key.as_ref());
}
}
pub struct MapxOrdRawKeyBatchEntry<'a, V>
where
V: ValueEnDe,
{
inner: Box<dyn vsdb_core::common::BatchTrait + 'a>,
_marker: PhantomData<V>,
}
impl<'a, V> MapxOrdRawKeyBatchEntry<'a, V>
where
V: ValueEnDe,
{
pub fn insert(&mut self, key: impl AsRef<[u8]>, value: &V) {
self.inner.insert(key.as_ref(), &value.encode());
}
pub fn remove(&mut self, key: impl AsRef<[u8]>) {
self.inner.remove(key.as_ref());
}
pub fn commit(mut self) -> Result<()> {
self.inner.commit()
}
}
#[derive(Debug)]
pub struct ValueMut<'a, V>
where
V: ValueEnDe,
{
value: V,
inner: mapx_raw::ValueMut<'a>,
}
impl<V> Drop for ValueMut<'_, V>
where
V: ValueEnDe,
{
fn drop(&mut self) {
*self.inner = self.value.encode();
}
}
impl<V> Deref for ValueMut<'_, V>
where
V: ValueEnDe,
{
type Target = V;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<V> DerefMut for ValueMut<'_, V>
where
V: ValueEnDe,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
pub struct Entry<'a, V>
where
V: ValueEnDe,
{
key: &'a [u8],
hdr: &'a mut MapxOrdRawKey<V>,
}
impl<'a, V> Entry<'a, V>
where
V: ValueEnDe,
{
pub fn or_insert(self, default: V) -> ValueMut<'a, V> {
crate::entry_or_insert_via_mock!(
self,
MapxOrdRawKey<V>,
get_mut(self.key),
mock_value_mut(self.key.to_vec(), default)
)
}
}
pub struct MapxOrdRawKeyIter<'a, V> {
inner: MapxRawIter<'a>,
_p: PhantomData<V>,
}
impl<V> Iterator for MapxOrdRawKeyIter<'_, V>
where
V: ValueEnDe,
{
type Item = (RawKey, V);
fn next(&mut self) -> Option<Self::Item> {
self.inner
.next()
.map(|(k, v)| (k, <V as ValueEnDe>::decode(&v).unwrap()))
}
}
impl<V> DoubleEndedIterator for MapxOrdRawKeyIter<'_, V>
where
V: ValueEnDe,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.inner
.next_back()
.map(|(k, v)| (k, <V as ValueEnDe>::decode(&v).unwrap()))
}
}
pub struct MapxOrdRawKeyIterMut<'a, V> {
inner: mapx_raw::MapxRawIterMut<'a>,
_p: PhantomData<V>,
}
impl<'a, V> Iterator for MapxOrdRawKeyIterMut<'a, V>
where
V: ValueEnDe,
{
type Item = (RawKey, ValueIterMut<'a, V>);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, v)| {
(
k,
ValueIterMut {
value: <V as ValueEnDe>::decode(&v).unwrap(),
inner: v,
},
)
})
}
}
impl<V> DoubleEndedIterator for MapxOrdRawKeyIterMut<'_, V>
where
V: ValueEnDe,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(|(k, v)| {
(
k,
ValueIterMut {
value: <V as ValueEnDe>::decode(&v).unwrap(),
inner: v,
},
)
})
}
}
#[derive(Debug)]
pub struct ValueIterMut<'a, V>
where
V: ValueEnDe,
{
pub(crate) value: V,
pub(crate) inner: mapx_raw::ValueIterMut<'a>,
}
impl<V> Drop for ValueIterMut<'_, V>
where
V: ValueEnDe,
{
fn drop(&mut self) {
*self.inner = self.value.encode();
}
}
impl<V> Deref for ValueIterMut<'_, V>
where
V: ValueEnDe,
{
type Target = V;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<V> DerefMut for ValueIterMut<'_, V>
where
V: ValueEnDe,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}