Files
continuwuity/src/database/map.rs
T

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

272 lines
7.5 KiB
Rust
Raw Normal View History

2024-08-08 17:18:30 +00:00
mod count;
mod keys;
mod keys_from;
mod keys_prefix;
mod rev_keys;
mod rev_keys_from;
mod rev_keys_prefix;
mod rev_stream;
mod rev_stream_from;
mod rev_stream_prefix;
mod stream;
mod stream_from;
mod stream_prefix;
use std::{
convert::AsRef,
ffi::CStr,
fmt,
fmt::{Debug, Display},
future::Future,
io::Write,
pin::Pin,
sync::Arc,
2024-06-30 23:06:01 +00:00
};
2024-05-28 06:59:50 +00:00
2024-08-08 17:18:30 +00:00
use conduit::{err, Result};
use futures::future;
use rocksdb::{AsColumnFamilyRef, ColumnFamily, ReadOptions, WriteBatchWithTransaction, WriteOptions};
use serde::Serialize;
use crate::{
2024-08-08 17:18:30 +00:00
keyval::{OwnedKey, OwnedVal},
ser,
util::{map_err, or_else},
watchers::Watchers,
2024-08-08 17:18:30 +00:00
Engine, Handle,
};
2024-05-28 06:59:50 +00:00
pub struct Map {
name: String,
2024-06-30 23:06:01 +00:00
db: Arc<Engine>,
cf: Arc<ColumnFamily>,
2024-05-28 06:59:50 +00:00
watchers: Watchers,
2024-06-29 03:25:13 +00:00
write_options: WriteOptions,
read_options: ReadOptions,
2024-05-28 06:59:50 +00:00
}
impl Map {
pub(crate) fn open(db: &Arc<Engine>, name: &str) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
name: name.to_owned(),
2024-06-30 23:06:01 +00:00
db: db.clone(),
cf: open(db, name)?,
2024-05-28 06:59:50 +00:00
watchers: Watchers::default(),
2024-06-29 03:25:13 +00:00
write_options: write_options_default(),
read_options: read_options_default(),
2024-05-28 06:59:50 +00:00
}))
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn del<K>(&self, key: &K)
where
K: Serialize + ?Sized + Debug,
{
let mut buf = Vec::<u8>::with_capacity(64);
self.bdel(key, &mut buf);
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self, buf), fields(%self), level = "trace")]
pub fn bdel<K, B>(&self, key: &K, buf: &mut B)
where
K: Serialize + ?Sized + Debug,
B: Write + AsRef<[u8]>,
{
let key = ser::serialize(buf, key).expect("failed to serialize deletion key");
self.remove(&key);
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(level = "trace")]
pub fn remove<K>(&self, key: &K)
where
K: AsRef<[u8]> + ?Sized + Debug,
{
2024-06-29 03:25:13 +00:00
let write_options = &self.write_options;
2024-05-28 06:59:50 +00:00
self.db
.db
2024-08-08 17:18:30 +00:00
.delete_cf_opt(&self.cf(), key, write_options)
.or_else(or_else)
.expect("database remove error");
2024-05-28 06:59:50 +00:00
if !self.db.corked() {
2024-08-08 17:18:30 +00:00
self.db.flush().expect("database flush error");
2024-05-28 06:59:50 +00:00
}
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self, value), fields(%self), level = "trace")]
pub fn insert<K, V>(&self, key: &K, value: &V)
where
2024-08-08 17:18:30 +00:00
K: AsRef<[u8]> + ?Sized + Debug,
V: AsRef<[u8]> + ?Sized,
{
2024-06-29 03:25:13 +00:00
let write_options = &self.write_options;
2024-08-08 17:18:30 +00:00
self.db
.db
.put_cf_opt(&self.cf(), key, value, write_options)
.or_else(or_else)
.expect("database insert error");
2024-05-28 06:59:50 +00:00
if !self.db.corked() {
2024-08-08 17:18:30 +00:00
self.db.flush().expect("database flush error");
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
self.watchers.wake(key.as_ref());
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn insert_batch<'a, I, K, V>(&'a self, iter: I)
where
2024-08-08 17:18:30 +00:00
I: Iterator<Item = &'a (K, V)> + Send + Debug,
K: AsRef<[u8]> + Sized + Debug + 'a,
V: AsRef<[u8]> + Sized + 'a,
{
2024-05-28 06:59:50 +00:00
let mut batch = WriteBatchWithTransaction::<false>::default();
2024-08-08 17:18:30 +00:00
for (key, val) in iter {
batch.put_cf(&self.cf(), key.as_ref(), val.as_ref());
2024-05-28 06:59:50 +00:00
}
2024-06-29 03:25:13 +00:00
let write_options = &self.write_options;
2024-08-08 17:18:30 +00:00
self.db
.db
.write_opt(batch, write_options)
.or_else(or_else)
.expect("database insert batch error");
2024-05-28 06:59:50 +00:00
if !self.db.corked() {
2024-08-08 17:18:30 +00:00
self.db.flush().expect("database flush error");
2024-05-28 06:59:50 +00:00
}
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn qry<K>(&self, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
where
K: Serialize + ?Sized + Debug,
{
let mut buf = Vec::<u8>::with_capacity(64);
self.bqry(key, &mut buf)
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self, buf), fields(%self), level = "trace")]
pub fn bqry<K, B>(&self, key: &K, buf: &mut B) -> impl Future<Output = Result<Handle<'_>>> + Send
where
K: Serialize + ?Sized + Debug,
B: Write + AsRef<[u8]>,
{
let key = ser::serialize(buf, key).expect("failed to serialize query key");
let val = self.get(key);
future::ready(val)
2024-06-29 05:41:20 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn get<K>(&self, key: &K) -> Result<Handle<'_>>
where
K: AsRef<[u8]> + ?Sized + Debug,
{
self.db
.db
.get_pinned_cf_opt(&self.cf(), key, &self.read_options)
.map_err(map_err)?
.map(Handle::from)
.ok_or(err!(Request(NotFound("Not found in database"))))
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn multi_get<'a, I, K>(&self, keys: I) -> Vec<Option<OwnedVal>>
where
2024-08-08 17:18:30 +00:00
I: Iterator<Item = &'a K> + ExactSizeIterator + Send + Debug,
K: AsRef<[u8]> + Sized + Debug + 'a,
{
2024-08-08 17:18:30 +00:00
// Optimization can be `true` if key vector is pre-sorted **by the column
// comparator**.
const SORTED: bool = false;
2024-05-28 06:59:50 +00:00
2024-08-08 17:18:30 +00:00
let mut ret: Vec<Option<OwnedKey>> = Vec::with_capacity(keys.len());
let read_options = &self.read_options;
for res in self
.db
.db
.batched_multi_get_cf_opt(&self.cf(), keys, SORTED, read_options)
{
match res {
Ok(Some(res)) => ret.push(Some((*res).to_vec())),
Ok(None) => ret.push(None),
Err(e) => or_else(e).expect("database multiget error"),
}
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
ret
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[inline]
pub fn watch_prefix<'a, K>(&'a self, prefix: &K) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
where
K: AsRef<[u8]> + ?Sized + Debug,
{
self.watchers.watch(prefix.as_ref())
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
#[inline]
pub fn property_integer(&self, name: &CStr) -> Result<u64> { self.db.property_integer(&self.cf(), name) }
2024-08-08 17:18:30 +00:00
#[inline]
pub fn property(&self, name: &str) -> Result<String> { self.db.property(&self.cf(), name) }
2024-06-30 23:06:01 +00:00
#[inline]
pub fn name(&self) -> &str { &self.name }
fn cf(&self) -> impl AsColumnFamilyRef + '_ { &*self.cf }
2024-05-28 06:59:50 +00:00
}
2024-08-08 17:18:30 +00:00
impl Debug for Map {
fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result { write!(out, "Map {{name: {0}}}", self.name) }
}
2024-05-28 06:59:50 +00:00
2024-08-08 17:18:30 +00:00
impl Display for Map {
fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result { write!(out, "{0}", self.name) }
2024-05-28 06:59:50 +00:00
}
2024-06-29 03:25:13 +00:00
2024-06-30 23:06:01 +00:00
fn open(db: &Arc<Engine>, name: &str) -> Result<Arc<ColumnFamily>> {
let bounded_arc = db.open_cf(name)?;
let bounded_ptr = Arc::into_raw(bounded_arc);
let cf_ptr = bounded_ptr.cast::<ColumnFamily>();
// SAFETY: After thorough contemplation this appears to be the best solution,
// even by a significant margin.
//
// BACKGROUND: Column family handles out of RocksDB are basic pointers and can
// be invalidated: 1. when the database closes. 2. when the column is dropped or
// closed. rust_rocksdb wraps this for us by storing handles in their own
// `RwLock<BTreeMap>` map and returning an Arc<BoundColumnFamily<'_>>` to
// provide expected safety. Similarly in "single-threaded mode" we would
// receive `&'_ ColumnFamily`.
//
// PROBLEM: We need to hold these handles in a field, otherwise we have to take
// a lock and get them by name from this map for every query, which is what
// conduit was doing, but we're not going to make a query for every query so we
// need to be holding it right. The lifetime parameter on these references makes
// that complicated. If this can be done without polluting the userspace
// with lifetimes on every instance of `Map` then this `unsafe` might not be
// necessary.
//
// SOLUTION: After investigating the underlying types it appears valid to
// Arc-swap `BoundColumnFamily<'_>` for `ColumnFamily`. They have the
// same inner data, the same Drop behavior, Deref, etc. We're just losing the
// lifetime parameter. We should not hold this handle, even in its Arc, after
// closing the database (dropping `Engine`). Since `Arc<Engine>` is a sibling
// member along with this handle in `Map`, that is prevented.
Ok(unsafe {
Arc::increment_strong_count(cf_ptr);
2024-06-30 23:06:01 +00:00
Arc::from_raw(cf_ptr)
})
}
2024-06-29 03:25:13 +00:00
#[inline]
fn read_options_default() -> ReadOptions {
let mut read_options = ReadOptions::default();
read_options.set_total_order_seek(true);
read_options
}
#[inline]
fn write_options_default() -> WriteOptions { WriteOptions::default() }