parent
25f240282e
commit
9facaae124
@ -1,53 +1,73 @@ |
|||||||
use std::kinds::marker; |
|
||||||
use std::ptr; |
use std::ptr; |
||||||
|
|
||||||
use ffi::*; |
use ffi; |
||||||
|
|
||||||
use error::{LmdbResult, lmdb_result}; |
use error::{LmdbResult, lmdb_result}; |
||||||
use flags::DatabaseFlags; |
use flags::DatabaseFlags; |
||||||
use transaction::{RoTransaction, RwTransaction, Transaction}; |
use transaction::{RwTransaction, Transaction}; |
||||||
|
|
||||||
/// A handle to an individual database in an environment.
|
/// A handle to an individual database in an environment.
|
||||||
///
|
///
|
||||||
/// A database handle denotes the name and parameters of a database in an environment. The database
|
/// A database handle denotes the name and parameters of a database in an environment.
|
||||||
/// may not exist in the environment (for instance, if the database is opened during a transaction
|
|
||||||
/// that has not yet committed).
|
|
||||||
#[deriving(Clone, Copy)] |
#[deriving(Clone, Copy)] |
||||||
pub struct Database<'env> { |
pub struct Database { |
||||||
dbi: MDB_dbi, |
dbi: ffi::MDB_dbi, |
||||||
_marker: marker::ContravariantLifetime<'env>, |
|
||||||
} |
} |
||||||
|
|
||||||
impl <'env> Database<'env> { |
impl Database { |
||||||
|
|
||||||
pub unsafe fn open<'e>(txn: &RoTransaction<'e>, |
/// Opens a database in the provided transaction.
|
||||||
|
///
|
||||||
|
/// If `name` is `None`, then the default database will be opened, otherwise a named database
|
||||||
|
/// will be opened. The database handle will be private to the transaction until the transaction
|
||||||
|
/// is successfully committed. If the transaction is aborted the returned database handle
|
||||||
|
/// should no longer be used.
|
||||||
|
///
|
||||||
|
/// ## Unsafety
|
||||||
|
///
|
||||||
|
/// * This function (as well as `Environment::open_db`, `Environment::create_db`, and
|
||||||
|
/// `Database::create`) **must not** be called from multiple concurrent transactions in the same
|
||||||
|
/// environment. A transaction which uses this function must finish (either commit or abort)
|
||||||
|
/// before any other transaction may use this function.
|
||||||
|
pub unsafe fn open(txn: &Transaction, |
||||||
name: Option<&str>) |
name: Option<&str>) |
||||||
-> LmdbResult<Database<'e>> { |
-> LmdbResult<Database> { |
||||||
let c_name = name.map(|n| n.to_c_str()); |
let c_name = name.map(|n| n.to_c_str()); |
||||||
let name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() }; |
let name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() }; |
||||||
let mut dbi: MDB_dbi = 0; |
let mut dbi: ffi::MDB_dbi = 0; |
||||||
try!(lmdb_result(mdb_dbi_open(txn.txn(), name_ptr, 0, &mut dbi))); |
try!(lmdb_result(ffi::mdb_dbi_open(txn.txn(), name_ptr, 0, &mut dbi))); |
||||||
Ok(Database { dbi: dbi, |
Ok(Database { dbi: dbi }) |
||||||
_marker: marker::ContravariantLifetime::<'e> }) |
|
||||||
} |
} |
||||||
|
|
||||||
pub unsafe fn create<'e>(txn: &RwTransaction<'e>, |
/// Opens a handle in the provided transaction, creating the database if necessary.
|
||||||
|
///
|
||||||
|
/// If `name` is `None`, then the default database will be opened, otherwise a named database
|
||||||
|
/// will be opened. The database handle will be private to the transaction until the transaction
|
||||||
|
/// is successfully committed. If the transaction is aborted the returned database handle
|
||||||
|
/// should no longer be used.
|
||||||
|
///
|
||||||
|
/// ## Unsafety
|
||||||
|
///
|
||||||
|
/// * This function (as well as `Environment::open_db`, `Environment::create_db`, and
|
||||||
|
/// `Database::open`) **must not** be called from multiple concurrent transactions in the same
|
||||||
|
/// environment. A transaction which uses this function must finish (either commit or abort)
|
||||||
|
/// before any other transaction may use this function.
|
||||||
|
pub unsafe fn create(txn: &RwTransaction, |
||||||
name: Option<&str>, |
name: Option<&str>, |
||||||
flags: DatabaseFlags) |
flags: DatabaseFlags) |
||||||
-> LmdbResult<Database<'e>> { |
-> LmdbResult<Database> { |
||||||
let c_name = name.map(|n| n.to_c_str()); |
let c_name = name.map(|n| n.to_c_str()); |
||||||
let name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() }; |
let name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() }; |
||||||
let mut dbi: MDB_dbi = 0; |
let mut dbi: ffi::MDB_dbi = 0; |
||||||
try!(lmdb_result(mdb_dbi_open(txn.txn(), name_ptr, flags.bits() | MDB_CREATE, &mut dbi))); |
try!(lmdb_result(ffi::mdb_dbi_open(txn.txn(), name_ptr, flags.bits() | ffi::MDB_CREATE, &mut dbi))); |
||||||
Ok(Database { dbi: dbi, |
Ok(Database { dbi: dbi }) |
||||||
_marker: marker::ContravariantLifetime::<'e> }) |
|
||||||
} |
} |
||||||
|
|
||||||
/// Returns the underlying LMDB database handle.
|
/// Returns the underlying LMDB database handle.
|
||||||
///
|
///
|
||||||
/// The caller **must** ensure that the handle is not used after the lifetime of the
|
/// The caller **must** ensure that the handle is not used after the lifetime of the
|
||||||
/// environment, or after the database handle has been closed.
|
/// environment, or after the database handle has been closed.
|
||||||
pub fn dbi(&self) -> MDB_dbi { |
pub fn dbi(&self) -> ffi::MDB_dbi { |
||||||
self.dbi |
self.dbi |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue