Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce better support to acquire global database handles #154

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add lifetimes to the PolyDatabase and Database types
  • Loading branch information
Kerollmops committed Apr 8, 2023
commit 1af05e7e4994ef0bd20cdee38bc4aae0a8e762ea
18 changes: 9 additions & 9 deletions heed/src/db/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ use crate::*;
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub struct Database<KC, DC> {
pub(crate) dyndb: PolyDatabase,
marker: marker::PhantomData<(KC, DC)>,
pub struct Database<'t, KC, DC> {
pub(crate) dyndb: PolyDatabase<'t>,
pub(crate) marker: marker::PhantomData<(KC, DC)>,
}

impl<KC, DC> Database<KC, DC> {
pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> Database<KC, DC> {
impl<KC, DC> Database<'_, KC, DC> {
pub(crate) fn new<'t>(env_ident: usize, dbi: ffi::MDB_dbi) -> Database<'t, KC, DC> {
Database { dyndb: PolyDatabase::new(env_ident, dbi), marker: std::marker::PhantomData }
}

Expand Down Expand Up @@ -1604,15 +1604,15 @@ impl<KC, DC> Database<KC, DC> {
}
}

impl<KC, DC> Clone for Database<KC, DC> {
fn clone(&self) -> Database<KC, DC> {
impl<'t, KC, DC> Clone for Database<'t, KC, DC> {
fn clone(&self) -> Database<'t, KC, DC> {
Database { dyndb: self.dyndb, marker: marker::PhantomData }
}
}

impl<KC, DC> Copy for Database<KC, DC> {}
impl<KC, DC> Copy for Database<'_, KC, DC> {}

impl<KC, DC> fmt::Debug for Database<KC, DC> {
impl<KC, DC> fmt::Debug for Database<'_, KC, DC> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Database")
.field("key_codec", &any::type_name::<KC>())
Expand Down
13 changes: 7 additions & 6 deletions heed/src/db/poly_database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::ops::{Bound, RangeBounds};
use std::{fmt, mem, ptr};
use std::{fmt, marker, mem, ptr};

use crate::mdb::error::mdb_result;
use crate::mdb::ffi;
Expand Down Expand Up @@ -103,14 +103,15 @@ use crate::*;
/// # Ok(()) }
/// ```
#[derive(Copy, Clone)]
pub struct PolyDatabase {
pub struct PolyDatabase<'t> {
pub(crate) env_ident: usize,
pub(crate) txn: marker::PhantomData<&'t ()>,
pub(crate) dbi: ffi::MDB_dbi,
}

impl PolyDatabase {
pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> PolyDatabase {
PolyDatabase { env_ident, dbi }
impl PolyDatabase<'_> {
pub(crate) fn new<'t>(env_ident: usize, dbi: ffi::MDB_dbi) -> PolyDatabase<'t> {
PolyDatabase { env_ident, txn: marker::PhantomData, dbi }
}

/// Retrieves the value associated with a key.
Expand Down Expand Up @@ -1856,7 +1857,7 @@ impl PolyDatabase {
}
}

impl fmt::Debug for PolyDatabase {
impl fmt::Debug for PolyDatabase<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PolyDatabase").finish()
}
Expand Down
24 changes: 12 additions & 12 deletions heed/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ impl Env {
/// LMDB have an important restriction on the unnamed database when named ones are opened,
/// the names of the named databases are stored as keys in the unnamed one and are immutable,
/// these keys can only be read and not written.
pub fn open_database<KC, DC>(
pub fn open_database<'t, KC, DC>(
&self,
rtxn: &RoTxn,
rtxn: &'t RoTxn,
name: Option<&str>,
) -> Result<Option<Database<KC, DC>>>
) -> Result<Option<Database<'t, KC, DC>>>
where
KC: 'static,
DC: 'static,
Expand All @@ -477,11 +477,11 @@ impl Env {
/// LMDB have an important restriction on the unnamed database when named ones are opened,
/// the names of the named databases are stored as keys in the unnamed one and are immutable,
/// these keys can only be read and not written.
pub fn open_poly_database(
pub fn open_poly_database<'t>(
&self,
rtxn: &RoTxn,
rtxn: &'t RoTxn,
name: Option<&str>,
) -> Result<Option<PolyDatabase>> {
) -> Result<Option<PolyDatabase<'t>>> {
assert_eq_env_txn!(self, rtxn);

match self.raw_init_database(rtxn.txn, name, None, false) {
Expand All @@ -500,11 +500,11 @@ impl Env {
/// LMDB have an important restriction on the unnamed database when named ones are opened,
/// the names of the named databases are stored as keys in the unnamed one and are immutable,
/// these keys can only be read and not written.
pub fn create_database<KC, DC>(
pub fn create_database<'t, KC, DC>(
&self,
wtxn: &mut RwTxn,
wtxn: &'t mut RwTxn,
name: Option<&str>,
) -> Result<Database<KC, DC>>
) -> Result<Database<'t, KC, DC>>
where
KC: 'static,
DC: 'static,
Expand All @@ -527,11 +527,11 @@ impl Env {
/// LMDB have an important restriction on the unnamed database when named ones are opened,
/// the names of the named databases are stored as keys in the unnamed one and are immutable,
/// these keys can only be read and not written.
pub fn create_poly_database(
pub fn create_poly_database<'t>(
&self,
wtxn: &mut RwTxn,
wtxn: &'t mut RwTxn,
name: Option<&str>,
) -> Result<PolyDatabase> {
) -> Result<PolyDatabase<'t>> {
assert_eq_env_txn!(self, wtxn);

match self.raw_init_database(wtxn.txn.txn, name, None, true) {
Expand Down