simplify cursor struct hierarchy

without.crypto
Dan Burkert 10 years ago
parent 11836f84a4
commit b1c1bb5b0b
  1. 74
      src/cursor.rs
  2. 7
      src/lib.rs

@ -16,7 +16,7 @@ pub trait Cursor<'txn> {
fn cursor(&self) -> *mut MDB_cursor; fn cursor(&self) -> *mut MDB_cursor;
} }
pub trait ReadCursor<'txn> : Cursor<'txn> { pub trait CursorExt<'txn> : Cursor<'txn> {
/// Retrieves a key/data pair from the cursor. Depending on the cursor op, the current key is /// Retrieves a key/data pair from the cursor. Depending on the cursor op, the current key is
/// returned. /// returned.
@ -45,41 +45,7 @@ pub trait ReadCursor<'txn> : Cursor<'txn> {
} }
} }
pub trait WriteCursor<'txn> : ReadCursor<'txn> { impl<'txn, T> CursorExt<'txn> for T where T: Cursor<'txn> {}
/// Puts a key/data pair into the database. The cursor will be positioned at the new data item,
/// or on failure usually near it.
fn put(&self,
key: &[u8],
data: &[u8],
flags: WriteFlags)
-> LmdbResult<()> {
let mut key_val: ffi::MDB_val = ffi::MDB_val { mv_size: key.len() as size_t,
mv_data: key.as_ptr() as *mut c_void };
let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: data.len() as size_t,
mv_data: data.as_ptr() as *mut c_void };
unsafe {
lmdb_result(ffi::mdb_cursor_put(self.cursor(),
&mut key_val,
&mut data_val,
flags.bits()))
}
}
/// Deletes the current key/data pair.
///
/// ### Flags
///
/// `MDB_NODUPDATA` may be used to delete all data items for the current key, if the database
/// was opened with `MDB_DUPSORT`.
fn del(&self, flags: WriteFlags) -> LmdbResult<()> {
unsafe {
lmdb_result(ffi::mdb_cursor_del(self.cursor(), flags.bits()))
}
}
}
/// A read-only cursor for navigating items within a database. /// A read-only cursor for navigating items within a database.
pub struct RoCursor<'txn> { pub struct RoCursor<'txn> {
@ -95,8 +61,6 @@ impl <'txn> Cursor<'txn> for RoCursor<'txn> {
} }
} }
impl <'txn> ReadCursor<'txn> for RoCursor<'txn> { }
#[unsafe_destructor] #[unsafe_destructor]
impl <'txn> Drop for RoCursor<'txn> { impl <'txn> Drop for RoCursor<'txn> {
fn drop(&mut self) { fn drop(&mut self) {
@ -135,9 +99,6 @@ impl <'txn> Cursor<'txn> for RwCursor<'txn> {
} }
} }
impl <'txn> ReadCursor<'txn> for RwCursor<'txn> { }
impl <'txn> WriteCursor<'txn> for RwCursor<'txn> { }
#[unsafe_destructor] #[unsafe_destructor]
impl <'txn> Drop for RwCursor<'txn> { impl <'txn> Drop for RwCursor<'txn> {
fn drop(&mut self) { fn drop(&mut self) {
@ -160,6 +121,37 @@ impl <'txn> RwCursor<'txn> {
_contravariant: marker::ContravariantLifetime::<'txn>, _contravariant: marker::ContravariantLifetime::<'txn>,
}) })
} }
/// Puts a key/data pair into the database. The cursor will be positioned at the new data item,
/// or on failure usually near it.
pub fn put(&self,
key: &[u8],
data: &[u8],
flags: WriteFlags)
-> LmdbResult<()> {
let mut key_val: ffi::MDB_val = ffi::MDB_val { mv_size: key.len() as size_t,
mv_data: key.as_ptr() as *mut c_void };
let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: data.len() as size_t,
mv_data: data.as_ptr() as *mut c_void };
unsafe {
lmdb_result(ffi::mdb_cursor_put(self.cursor(),
&mut key_val,
&mut data_val,
flags.bits()))
}
}
/// Deletes the current key/data pair.
///
/// ### Flags
///
/// `MDB_NODUPDATA` may be used to delete all data items for the current key, if the database
/// was opened with `MDB_DUPSORT`.
pub fn del(&self, flags: WriteFlags) -> LmdbResult<()> {
unsafe {
lmdb_result(ffi::mdb_cursor_del(self.cursor(), flags.bits()))
}
}
} }
unsafe fn slice_to_val(slice: Option<&[u8]>) -> MDB_val { unsafe fn slice_to_val(slice: Option<&[u8]>) -> MDB_val {

@ -13,7 +13,12 @@ extern crate "lmdb-sys" as ffi;
extern crate test; extern crate test;
extern crate collections; extern crate collections;
pub use cursor::Cursor; pub use cursor::{
Cursor,
CursorExt,
RoCursor,
RwCursor
};
pub use database::Database; pub use database::Database;
pub use environment::{Environment, EnvironmentBuilder}; pub use environment::{Environment, EnvironmentBuilder};
pub use error::{LmdbResult, LmdbError}; pub use error::{LmdbResult, LmdbError};

Loading…
Cancel
Save