without.crypto
Dan Burkert 7 years ago
parent c15f22c884
commit d0fda113ca
  1. 8
      src/cursor.rs
  2. 2
      src/database.rs
  3. 16
      src/environment.rs
  4. 17
      src/transaction.rs

@ -28,7 +28,7 @@ pub trait Cursor<'txn> {
let mut key_val = slice_to_val(key); let mut key_val = slice_to_val(key);
let mut data_val = slice_to_val(data); let mut data_val = slice_to_val(data);
let key_ptr = key_val.mv_data; let key_ptr = key_val.mv_data;
try!(lmdb_result(ffi::mdb_cursor_get(self.cursor(), &mut key_val, &mut data_val, op))); lmdb_result(ffi::mdb_cursor_get(self.cursor(), &mut key_val, &mut data_val, op))?;
let key_out = if key_ptr != key_val.mv_data { Some(val_to_slice(key_val)) } else { None }; let key_out = if key_ptr != key_val.mv_data { Some(val_to_slice(key_val)) } else { None };
let data_out = val_to_slice(data_val); let data_out = val_to_slice(data_val);
Ok((key_out, data_out)) Ok((key_out, data_out))
@ -91,7 +91,7 @@ pub trait Cursor<'txn> {
/// key. /// key.
fn iter_dup_of<K>(&mut self, key: &K) -> Result<Iter<'txn>> where K: fn iter_dup_of<K>(&mut self, key: &K) -> Result<Iter<'txn>> where K:
AsRef<[u8]> { AsRef<[u8]> {
try!(self.get(Some(key.as_ref()), None, ffi::MDB_SET)); self.get(Some(key.as_ref()), None, ffi::MDB_SET)?;
Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT_DUP)) Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT_DUP))
} }
} }
@ -127,7 +127,7 @@ impl <'txn> RoCursor<'txn> {
#[doc(hidden)] #[doc(hidden)]
pub fn new<T>(txn: &'txn T, db: Database) -> Result<RoCursor<'txn>> where T: Transaction { pub fn new<T>(txn: &'txn T, db: Database) -> Result<RoCursor<'txn>> where T: Transaction {
let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut(); let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut();
unsafe { try!(lmdb_result(ffi::mdb_cursor_open(txn.txn(), db.dbi(), &mut cursor))); } unsafe { lmdb_result(ffi::mdb_cursor_open(txn.txn(), db.dbi(), &mut cursor))?; }
Ok(RoCursor { Ok(RoCursor {
cursor: cursor, cursor: cursor,
_marker: PhantomData, _marker: PhantomData,
@ -166,7 +166,7 @@ impl <'txn> RwCursor<'txn> {
#[doc(hidden)] #[doc(hidden)]
pub fn new<T>(txn: &'txn T, db: Database) -> Result<RwCursor<'txn>> where T: Transaction { pub fn new<T>(txn: &'txn T, db: Database) -> Result<RwCursor<'txn>> where T: Transaction {
let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut(); let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut();
unsafe { try!(lmdb_result(ffi::mdb_cursor_open(txn.txn(), db.dbi(), &mut cursor))); } unsafe { lmdb_result(ffi::mdb_cursor_open(txn.txn(), db.dbi(), &mut cursor))?; }
Ok(RwCursor { cursor: cursor, _marker: PhantomData }) Ok(RwCursor { cursor: cursor, _marker: PhantomData })
} }

@ -28,7 +28,7 @@ impl Database {
let c_name = name.map(|n| CString::new(n).unwrap()); let c_name = name.map(|n| CString::new(n).unwrap());
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: ffi::MDB_dbi = 0; let mut dbi: ffi::MDB_dbi = 0;
try!(lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))); lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))?;
Ok(Database { dbi: dbi }) Ok(Database { dbi: dbi })
} }

@ -76,9 +76,9 @@ impl Environment {
/// The database name may not contain the null character. /// The database name may not contain the null character.
pub fn open_db<'env>(&'env self, name: Option<&str>) -> Result<Database> { pub fn open_db<'env>(&'env self, name: Option<&str>) -> Result<Database> {
let mutex = self.dbi_open_mutex.lock(); let mutex = self.dbi_open_mutex.lock();
let txn = try!(self.begin_ro_txn()); let txn = self.begin_ro_txn()?;
let db = unsafe { try!(txn.open_db(name)) }; let db = unsafe { txn.open_db(name)? };
try!(txn.commit()); txn.commit()?;
drop(mutex); drop(mutex);
Ok(db) Ok(db)
} }
@ -102,18 +102,18 @@ impl Environment {
flags: DatabaseFlags) flags: DatabaseFlags)
-> Result<Database> { -> Result<Database> {
let mutex = self.dbi_open_mutex.lock(); let mutex = self.dbi_open_mutex.lock();
let txn = try!(self.begin_rw_txn()); let txn = self.begin_rw_txn()?;
let db = unsafe { try!(txn.create_db(name, flags)) }; let db = unsafe { txn.create_db(name, flags)? };
try!(txn.commit()); txn.commit()?;
drop(mutex); drop(mutex);
Ok(db) Ok(db)
} }
pub fn get_db_flags<'env>(&'env self, db: Database) -> Result<DatabaseFlags> { pub fn get_db_flags<'env>(&'env self, db: Database) -> Result<DatabaseFlags> {
let txn = try!(self.begin_ro_txn()); let txn = self.begin_ro_txn()?;
let mut flags: c_uint = 0; let mut flags: c_uint = 0;
unsafe { unsafe {
try!(lmdb_result(ffi::mdb_dbi_flags(txn.txn(), db.dbi(), &mut flags))); lmdb_result(ffi::mdb_dbi_flags(txn.txn(), db.dbi(), &mut flags))?;
} }
Ok(DatabaseFlags::from_bits(flags).unwrap()) Ok(DatabaseFlags::from_bits(flags).unwrap())
} }

@ -98,7 +98,7 @@ pub trait Transaction : Sized {
fn db_flags(&self, db: Database) -> Result<DatabaseFlags> { fn db_flags(&self, db: Database) -> Result<DatabaseFlags> {
let mut flags: c_uint = 0; let mut flags: c_uint = 0;
unsafe { unsafe {
try!(lmdb_result(ffi::mdb_dbi_flags(self.txn(), db.dbi(), &mut flags))); lmdb_result(ffi::mdb_dbi_flags(self.txn(), db.dbi(), &mut flags))?;
} }
Ok(DatabaseFlags::from_bits_truncate(flags)) Ok(DatabaseFlags::from_bits_truncate(flags))
} }
@ -130,10 +130,7 @@ impl <'env> RoTransaction<'env> {
pub fn new(env: &'env Environment) -> Result<RoTransaction<'env>> { pub fn new(env: &'env Environment) -> Result<RoTransaction<'env>> {
let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); let mut txn: *mut ffi::MDB_txn = ptr::null_mut();
unsafe { unsafe {
try!(lmdb_result(ffi::mdb_txn_begin(env.env(), lmdb_result(ffi::mdb_txn_begin(env.env(), ptr::null_mut(), ffi::MDB_RDONLY, &mut txn))?;
ptr::null_mut(),
ffi::MDB_RDONLY,
&mut txn)));
Ok(RoTransaction { txn: txn, _marker: PhantomData }) Ok(RoTransaction { txn: txn, _marker: PhantomData })
} }
} }
@ -195,7 +192,7 @@ impl <'env> InactiveTransaction<'env> {
let txn = self.txn; let txn = self.txn;
unsafe { unsafe {
mem::forget(self); mem::forget(self);
try!(lmdb_result(ffi::mdb_txn_renew(txn))) lmdb_result(ffi::mdb_txn_renew(txn))?
}; };
Ok(RoTransaction { txn: txn, _marker: PhantomData }) Ok(RoTransaction { txn: txn, _marker: PhantomData })
} }
@ -227,10 +224,10 @@ impl <'env> RwTransaction<'env> {
pub fn new(env: &'env Environment) -> Result<RwTransaction<'env>> { pub fn new(env: &'env Environment) -> Result<RwTransaction<'env>> {
let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); let mut txn: *mut ffi::MDB_txn = ptr::null_mut();
unsafe { unsafe {
try!(lmdb_result(ffi::mdb_txn_begin(env.env(), lmdb_result(ffi::mdb_txn_begin(env.env(),
ptr::null_mut(), ptr::null_mut(),
EnvironmentFlags::empty().bits(), EnvironmentFlags::empty().bits(),
&mut txn))); &mut txn))?;
Ok(RwTransaction { txn: txn, _marker: PhantomData }) Ok(RwTransaction { txn: txn, _marker: PhantomData })
} }
} }
@ -305,11 +302,11 @@ impl <'env> RwTransaction<'env> {
let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: len, let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: len,
mv_data: ptr::null_mut::<c_void>() }; mv_data: ptr::null_mut::<c_void>() };
unsafe { unsafe {
try!(lmdb_result(ffi::mdb_put(self.txn(), lmdb_result(ffi::mdb_put(self.txn(),
database.dbi(), database.dbi(),
&mut key_val, &mut key_val,
&mut data_val, &mut data_val,
flags.bits() | ffi::MDB_RESERVE))); flags.bits() | ffi::MDB_RESERVE))?;
Ok(slice::from_raw_parts_mut(data_val.mv_data as *mut u8, Ok(slice::from_raw_parts_mut(data_val.mv_data as *mut u8,
data_val.mv_size as usize)) data_val.mv_size as usize))
} }

Loading…
Cancel
Save