Files
continuwuity/src/database/map/contains.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.6 KiB
Rust
Raw Normal View History

2024-11-27 06:28:32 +00:00
use std::{convert::AsRef, fmt::Debug, future::Future, io::Write, sync::Arc};
2024-10-24 06:03:45 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit::{
arrayvec::ArrayVec,
2024-11-27 06:28:32 +00:00
err, implement,
utils::{future::TryExtExt, result::FlatOk},
Result,
};
use futures::FutureExt;
2024-10-24 06:03:45 +00:00
use serde::Serialize;
2024-11-28 05:54:34 +00:00
use crate::{keyval::KeyBuf, ser};
2024-10-24 06:03:45 +00:00
/// Returns true if the map contains the key.
/// - key is serialized into allocated buffer
/// - harder errors may not be reported
2024-11-28 05:54:34 +00:00
#[inline]
2024-10-24 06:03:45 +00:00
#[implement(super::Map)]
2024-11-27 06:28:32 +00:00
pub fn contains<K>(self: &Arc<Self>, key: &K) -> impl Future<Output = bool> + Send + '_
2024-10-24 06:03:45 +00:00
where
K: Serialize + ?Sized + Debug,
{
2024-11-28 05:54:34 +00:00
let mut buf = KeyBuf::new();
2024-10-24 06:03:45 +00:00
self.bcontains(key, &mut buf)
}
/// Returns true if the map contains the key.
/// - key is serialized into stack-buffer
/// - harder errors will panic
2024-11-28 05:54:34 +00:00
#[inline]
2024-10-24 06:03:45 +00:00
#[implement(super::Map)]
pub fn acontains<const MAX: usize, K>(
self: &Arc<Self>,
key: &K,
) -> impl Future<Output = bool> + Send + '_
2024-10-24 06:03:45 +00:00
where
K: Serialize + ?Sized + Debug,
{
let mut buf = ArrayVec::<u8, MAX>::new();
self.bcontains(key, &mut buf)
}
/// Returns true if the map contains the key.
/// - key is serialized into provided buffer
/// - harder errors will panic
#[implement(super::Map)]
#[tracing::instrument(skip(self, buf), fields(%self), level = "trace")]
pub fn bcontains<K, B>(
self: &Arc<Self>,
key: &K,
buf: &mut B,
) -> impl Future<Output = bool> + Send + '_
2024-10-24 06:03:45 +00:00
where
K: Serialize + ?Sized + Debug,
B: Write + AsRef<[u8]>,
{
let key = ser::serialize(buf, key).expect("failed to serialize query key");
self.exists(key).is_ok()
}
/// Returns Ok if the map contains the key.
/// - key is raw
2024-11-28 05:54:34 +00:00
#[inline]
2024-10-24 06:03:45 +00:00
#[implement(super::Map)]
2024-11-27 06:28:32 +00:00
pub fn exists<'a, K>(self: &'a Arc<Self>, key: &K) -> impl Future<Output = Result> + Send + 'a
2024-10-24 06:03:45 +00:00
where
2024-11-27 06:28:32 +00:00
K: AsRef<[u8]> + ?Sized + Debug + 'a,
2024-10-24 06:03:45 +00:00
{
2024-11-27 06:28:32 +00:00
self.get(key).map(|res| res.map(|_| ()))
2024-10-24 06:03:45 +00:00
}
/// Returns Ok if the map contains the key; NotFound otherwise. Harder errors
/// may not always be reported properly.
#[implement(super::Map)]
#[tracing::instrument(skip(self, key), fields(%self), level = "trace")]
2024-11-27 06:28:32 +00:00
pub fn exists_blocking<K>(&self, key: &K) -> Result
2024-10-24 06:03:45 +00:00
where
K: AsRef<[u8]> + ?Sized + Debug,
{
2024-11-27 06:28:32 +00:00
self.maybe_exists(key)
.then(|| self.get_blocking(key))
.flat_ok()
.map(|_| ())
.ok_or_else(|| err!(Request(NotFound("Not found in database"))))
2024-10-24 06:03:45 +00:00
}
2024-11-27 06:28:32 +00:00
/// Rocksdb limits this to kBlockCacheTier internally so this is not actually a
/// blocking call; in case that changes we set this as well in our read_options.
2024-10-24 06:03:45 +00:00
#[implement(super::Map)]
2024-11-27 06:28:32 +00:00
pub(crate) fn maybe_exists<K>(&self, key: &K) -> bool
2024-10-24 06:03:45 +00:00
where
K: AsRef<[u8]> + ?Sized,
{
self.db
.db
2024-11-27 06:28:32 +00:00
.key_may_exist_cf_opt(&self.cf(), key, &self.cache_read_options)
2024-10-24 06:03:45 +00:00
}