Commit 337a6078 authored by Benjamin Woodruff's avatar Benjamin Woodruff

more lifetime cleanup, return guard object instead of using with_* method

parent 00555abd
......@@ -2,8 +2,7 @@ use std::{fs::create_dir_all, marker::PhantomData, path::Path, thread::available
use anyhow::{Context, Result};
use lmdb::{
Database, DatabaseFlags, Environment, EnvironmentFlags, RoTransaction, RwTransaction,
Transaction, WriteFlags,
Database, DatabaseFlags, Environment, EnvironmentFlags, RwTransaction, Transaction, WriteFlags,
};
use crate::database::{
......@@ -92,11 +91,8 @@ impl LmbdKeyValueDatabase {
}
}
fn with_read_tx<'db, R>(
&'db self,
f: impl FnOnce(&'db RoTransaction<'db>, read_tx_cache::ReadTxGuard<'db>) -> Result<R>,
) -> Result<R> {
self.read_tx_cache.with_read_tx(&self.env, f)
fn get_read_tx(&self) -> read_tx_cache::ReadTxGuard<'_> {
self.read_tx_cache.get_read_tx(&self.env)
}
fn get_from_tx<'tx>(
......@@ -115,21 +111,16 @@ impl LmbdKeyValueDatabase {
impl KeyValueDatabase for LmbdKeyValueDatabase {
type ValueBuffer<'l> = LmdbValueBuffer<'l>;
fn get<'l>(
&'l self,
key_space: super::key_value_database::KeySpace,
key: &[u8],
) -> Result<Option<Self::ValueBuffer<'l>>> {
self.with_read_tx(|tx, guard| {
let Some(value) = Self::get_from_tx(tx, self.db(key_space), key)? else {
return Ok(None);
};
Ok(Some(LmdbValueBuffer::from_raw_parts(
value.as_ptr(),
value.len(),
guard,
)))
})
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>> {
let tx = self.get_read_tx();
let Some(value) = Self::get_from_tx(&**tx, self.db(key_space), key)? else {
return Ok(None);
};
Ok(Some(LmdbValueBuffer::from_raw_parts(
value.as_ptr(),
value.len(),
tx,
)))
}
fn batch_get<'l>(
......@@ -137,17 +128,16 @@ impl KeyValueDatabase for LmbdKeyValueDatabase {
key_space: KeySpace,
keys: &[&[u8]],
) -> Result<Vec<Option<Self::ValueBuffer<'l>>>> {
self.with_read_tx(|tx, guard| {
let db = self.db(key_space);
keys.iter()
.map(|key| {
let value = Self::get_from_tx(tx, db, key)?;
Ok(value.map(|value| {
LmdbValueBuffer::from_raw_parts(value.as_ptr(), value.len(), guard.clone())
}))
})
.collect()
})
let tx = self.get_read_tx();
let db = self.db(key_space);
keys.iter()
.map(|key| {
let value = Self::get_from_tx(&**tx, db, key)?;
Ok(value.map(|value| {
LmdbValueBuffer::from_raw_parts(value.as_ptr(), value.len(), tx.clone())
}))
})
.collect()
}
type SerialWriteBatch<'l>
......
use std::{marker::PhantomData, mem::transmute, ops::Deref, sync::Arc};
use std::{mem::transmute, ops::Deref, sync::Arc};
use arc_swap::ArcSwap;
use lmdb::{Environment, RoTransaction};
use thread_local::ThreadLocal;
type ReadTransactionsCache = ThreadLocal<SendRoTransaction>;
type ReadTransactionsCache = ThreadLocal<SendRoTransaction<'static>>;
struct SendRoTransaction(RoTransaction<'static>);
pub struct SendRoTransaction<'tx>(RoTransaction<'tx>);
impl Deref for SendRoTransaction {
type Target = RoTransaction<'static>;
impl<'tx> Deref for SendRoTransaction<'tx> {
type Target = RoTransaction<'tx>;
fn deref(&self) -> &Self::Target {
&self.0
......@@ -23,14 +23,22 @@ impl Deref for SendRoTransaction {
// We still do not use a transaction concurrently from multiple threads. This `Send` wrapper only
// allows moving ownership between threads when `ThreadLocal` internals move stored values at drop
// time.
unsafe impl Send for SendRoTransaction {}
unsafe impl<'tx> Send for SendRoTransaction<'tx> {}
#[derive(Clone)]
pub struct ReadTxGuard<'db> {
tx: &'db SendRoTransaction<'db>,
// Keep this generation alive so the borrowed read transaction (and value pointers from it)
// remain valid until all value buffers/guards from that generation are dropped.
_thread_locals: Arc<ReadTransactionsCache>,
_marker: PhantomData<&'db Environment>,
}
impl<'db> Deref for ReadTxGuard<'db> {
type Target = SendRoTransaction<'db>;
fn deref(&self) -> &Self::Target {
self.tx
}
}
pub struct ReadTxCache {
......@@ -46,11 +54,7 @@ impl ReadTxCache {
}
}
pub fn with_read_tx<'db, R>(
&'db self,
env: &'db Environment,
f: impl FnOnce(&'db RoTransaction<'db>, ReadTxGuard<'db>) -> R,
) -> R {
pub fn get_read_tx<'db>(&'db self, env: &'db Environment) -> ReadTxGuard<'db> {
let thread_locals = self.read_transactions_cache.load().clone();
let guard_thread_locals = thread_locals.clone();
let tx_static = thread_locals.get_or(|| {
......@@ -63,14 +67,14 @@ impl ReadTxCache {
// Safety: `tx_static` always originates from `env` and `ReadTxCache` is dropped before
// the environment in the parent database type. We narrow the externally-visible lifetime
// to `'db` so callers cannot observe or depend on `'static`.
let tx: &'db RoTransaction<'db> =
unsafe { transmute::<&RoTransaction<'static>, &'db RoTransaction<'db>>(tx_static) };
let tx: &'db SendRoTransaction<'db> = unsafe {
transmute::<&SendRoTransaction<'static>, &'db SendRoTransaction<'db>>(tx_static)
};
let guard = ReadTxGuard {
ReadTxGuard {
tx,
_thread_locals: guard_thread_locals,
_marker: PhantomData,
};
f(tx, guard)
}
}
pub fn swap_generation(&self) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment