pub struct Database<KC, DC, C = DefaultComparator> { /* private fields */ }
Expand description
A typed database that accepts only the types it was created with.
§Example: Iterate over databases entries
In this example we store numbers in big endian this way those are ordered. Thanks to their bytes representation, heed is able to iterate over them from the lowest to the highest.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI64 = I64<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI64, Unit> = env.create_database(&mut wtxn, Some("big-endian-iter"))?;
db.put(&mut wtxn, &68, &())?;
db.put(&mut wtxn, &35, &())?;
db.put(&mut wtxn, &0, &())?;
db.put(&mut wtxn, &42, &())?;
// you can iterate over database entries in order
let rets: Result<_, _> = db.iter(&wtxn)?.collect();
let rets: Vec<(i64, _)> = rets?;
let expected = vec![
(0, ()),
(35, ()),
(42, ()),
(68, ()),
];
assert_eq!(rets, expected);
wtxn.commit()?;
§Example: Iterate over and delete ranges of entries
Discern also support ranges and ranges deletions. Same configuration as above, numbers are ordered, therefore it is safe to specify a range and be able to iterate over and/or delete it.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI64 = I64<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI64, Unit> = env.create_database(&mut wtxn, Some("big-endian-iter"))?;
db.put(&mut wtxn, &0, &())?;
db.put(&mut wtxn, &68, &())?;
db.put(&mut wtxn, &35, &())?;
db.put(&mut wtxn, &42, &())?;
// you can iterate over ranges too!!!
let range = 35..=42;
let rets: Result<_, _> = db.range(&wtxn, &range)?.collect();
let rets: Vec<(i64, _)> = rets?;
let expected = vec![
(35, ()),
(42, ()),
];
assert_eq!(rets, expected);
// even delete a range of keys
let range = 35..=42;
let deleted: usize = db.delete_range(&mut wtxn, &range)?;
let rets: Result<_, _> = db.iter(&wtxn)?.collect();
let rets: Vec<(i64, _)> = rets?;
let expected = vec![
(0, ()),
(68, ()),
];
assert_eq!(deleted, 2);
assert_eq!(rets, expected);
wtxn.commit()?;
Implementations§
Source§impl<KC, DC, C> Database<KC, DC, C>
impl<KC, DC, C> Database<KC, DC, C>
Sourcepub fn get<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<DC::DItem>>where
KC: BytesEncode<'a>,
DC: BytesDecode<'txn>,
pub fn get<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<DC::DItem>>where
KC: BytesEncode<'a>,
DC: BytesDecode<'txn>,
Retrieves the value associated with a key.
If the key does not exist, then None
is returned.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32= U32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Str, BEI32> = env.create_database(&mut wtxn, Some("get-i32"))?;
db.put(&mut wtxn, "i-am-forty-two", &42)?;
db.put(&mut wtxn, "i-am-twenty-seven", &27)?;
let ret = db.get(&wtxn, "i-am-forty-two")?;
assert_eq!(ret, Some(42));
let ret = db.get(&wtxn, "i-am-twenty-one")?;
assert_eq!(ret, None);
wtxn.commit()?;
Sourcepub fn get_duplicates<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<RoIter<'txn, KC, DC, MoveOnCurrentKeyDuplicates>>>where
KC: BytesEncode<'a>,
pub fn get_duplicates<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<RoIter<'txn, KC, DC, MoveOnCurrentKeyDuplicates>>>where
KC: BytesEncode<'a>,
Returns an iterator over all of the values of a single key.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI64 = I64<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.database_options()
.types::<BEI64, BEI64>()
.flags(DatabaseFlags::DUP_SORT)
.name("dup-sort")
.create(&mut wtxn)?;
db.put(&mut wtxn, &68, &120)?;
db.put(&mut wtxn, &68, &121)?;
db.put(&mut wtxn, &68, &122)?;
db.put(&mut wtxn, &68, &123)?;
db.put(&mut wtxn, &92, &32)?;
db.put(&mut wtxn, &35, &120)?;
db.put(&mut wtxn, &0, &120)?;
db.put(&mut wtxn, &42, &120)?;
let mut iter = db.get_duplicates(&wtxn, &68)?.expect("the key exists");
assert_eq!(iter.next().transpose()?, Some((68, 120)));
assert_eq!(iter.next().transpose()?, Some((68, 121)));
assert_eq!(iter.next().transpose()?, Some((68, 122)));
assert_eq!(iter.next().transpose()?, Some((68, 123)));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
let mut iter = db.get_duplicates(&wtxn, &68)?.expect("the key exists");
assert_eq!(iter.last().transpose()?, Some((68, 123)));
wtxn.commit()?;
Sourcepub fn get_lower_than<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn get_lower_than<'a, 'txn>( &self, txn: &'txn RoTxn<'_>, key: &'a KC::EItem, ) -> Result<Option<(KC::DItem, DC::DItem)>>
Retrieves the key/value pair lower than the given one in this database.
If the database if empty or there is no key lower than the given one,
then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEU32 = U32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEU32, Unit>(&mut wtxn, Some("get-lt-u32"))?;
db.put(&mut wtxn, &27, &())?;
db.put(&mut wtxn, &42, &())?;
db.put(&mut wtxn, &43, &())?;
let ret = db.get_lower_than(&wtxn, &4404)?;
assert_eq!(ret, Some((43, ())));
let ret = db.get_lower_than(&wtxn, &43)?;
assert_eq!(ret, Some((42, ())));
let ret = db.get_lower_than(&wtxn, &27)?;
assert_eq!(ret, None);
wtxn.commit()?;
Sourcepub fn get_lower_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn get_lower_than_or_equal_to<'a, 'txn>( &self, txn: &'txn RoTxn<'_>, key: &'a KC::EItem, ) -> Result<Option<(KC::DItem, DC::DItem)>>
Retrieves the key/value pair lower than or equal to the given one in this database.
If the database if empty or there is no key lower than or equal to the given one,
then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEU32 = U32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEU32, Unit>(&mut wtxn, Some("get-lt-u32"))?;
db.put(&mut wtxn, &27, &())?;
db.put(&mut wtxn, &42, &())?;
db.put(&mut wtxn, &43, &())?;
let ret = db.get_lower_than_or_equal_to(&wtxn, &4404)?;
assert_eq!(ret, Some((43, ())));
let ret = db.get_lower_than_or_equal_to(&wtxn, &43)?;
assert_eq!(ret, Some((43, ())));
let ret = db.get_lower_than_or_equal_to(&wtxn, &26)?;
assert_eq!(ret, None);
wtxn.commit()?;
Sourcepub fn get_greater_than<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn get_greater_than<'a, 'txn>( &self, txn: &'txn RoTxn<'_>, key: &'a KC::EItem, ) -> Result<Option<(KC::DItem, DC::DItem)>>
Retrieves the key/value pair greater than the given one in this database.
If the database if empty or there is no key greater than the given one,
then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEU32 = U32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEU32, Unit>(&mut wtxn, Some("get-lt-u32"))?;
db.put(&mut wtxn, &27, &())?;
db.put(&mut wtxn, &42, &())?;
db.put(&mut wtxn, &43, &())?;
let ret = db.get_greater_than(&wtxn, &0)?;
assert_eq!(ret, Some((27, ())));
let ret = db.get_greater_than(&wtxn, &42)?;
assert_eq!(ret, Some((43, ())));
let ret = db.get_greater_than(&wtxn, &43)?;
assert_eq!(ret, None);
wtxn.commit()?;
Sourcepub fn get_greater_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn get_greater_than_or_equal_to<'a, 'txn>( &self, txn: &'txn RoTxn<'_>, key: &'a KC::EItem, ) -> Result<Option<(KC::DItem, DC::DItem)>>
Retrieves the key/value pair greater than or equal to the given one in this database.
If the database if empty or there is no key greater than or equal to the given one,
then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEU32 = U32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEU32, Unit>(&mut wtxn, Some("get-lt-u32"))?;
db.put(&mut wtxn, &27, &())?;
db.put(&mut wtxn, &42, &())?;
db.put(&mut wtxn, &43, &())?;
let ret = db.get_greater_than_or_equal_to(&wtxn, &0)?;
assert_eq!(ret, Some((27, ())));
let ret = db.get_greater_than_or_equal_to(&wtxn, &42)?;
assert_eq!(ret, Some((42, ())));
let ret = db.get_greater_than_or_equal_to(&wtxn, &44)?;
assert_eq!(ret, None);
wtxn.commit()?;
Sourcepub fn first<'txn>(
&self,
txn: &'txn RoTxn<'_>,
) -> Result<Option<(KC::DItem, DC::DItem)>>where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
pub fn first<'txn>(
&self,
txn: &'txn RoTxn<'_>,
) -> Result<Option<(KC::DItem, DC::DItem)>>where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
Retrieves the first key/value pair of this database.
If the database if empty, then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("first-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
let ret = db.first(&wtxn)?;
assert_eq!(ret, Some((27, "i-am-twenty-seven")));
wtxn.commit()?;
Sourcepub fn last<'txn>(
&self,
txn: &'txn RoTxn<'_>,
) -> Result<Option<(KC::DItem, DC::DItem)>>where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
pub fn last<'txn>(
&self,
txn: &'txn RoTxn<'_>,
) -> Result<Option<(KC::DItem, DC::DItem)>>where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
Retrieves the last key/value pair of this database.
If the database if empty, then None
is returned.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("last-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
let ret = db.last(&wtxn)?;
assert_eq!(ret, Some((42, "i-am-forty-two")));
wtxn.commit()?;
Sourcepub fn len(&self, txn: &RoTxn<'_>) -> Result<u64>
pub fn len(&self, txn: &RoTxn<'_>) -> Result<u64>
Returns the number of elements in this database.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let ret = db.len(&wtxn)?;
assert_eq!(ret, 4);
db.delete(&mut wtxn, &27)?;
let ret = db.len(&wtxn)?;
assert_eq!(ret, 3);
wtxn.commit()?;
Sourcepub fn is_empty(&self, txn: &RoTxn<'_>) -> Result<bool>
pub fn is_empty(&self, txn: &RoTxn<'_>) -> Result<bool>
Returns true
if and only if this database is empty.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let ret = db.is_empty(&wtxn)?;
assert_eq!(ret, false);
db.clear(&mut wtxn)?;
let ret = db.is_empty(&wtxn)?;
assert_eq!(ret, true);
wtxn.commit()?;
Sourcepub fn stat(&self, txn: &RoTxn<'_>) -> Result<DatabaseStat>
pub fn stat(&self, txn: &RoTxn<'_>) -> Result<DatabaseStat>
Returns some statistics for this database.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let stat = db.stat(&wtxn)?;
assert_eq!(stat.depth, 1);
assert_eq!(stat.branch_pages, 0);
assert_eq!(stat.leaf_pages, 1);
assert_eq!(stat.overflow_pages, 0);
assert_eq!(stat.entries, 4);
wtxn.commit()?;
Sourcepub fn iter<'txn>(&self, txn: &'txn RoTxn<'_>) -> Result<RoIter<'txn, KC, DC>>
pub fn iter<'txn>(&self, txn: &'txn RoTxn<'_>) -> Result<RoIter<'txn, KC, DC>>
Return a lexicographically ordered iterator of all key-value pairs in this database.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn iter_mut<'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
) -> Result<RwIter<'txn, KC, DC>>
pub fn iter_mut<'txn>( &self, txn: &'txn mut RwTxn<'_>, ) -> Result<RwIter<'txn, KC, DC>>
Return a mutable lexicographically ordered iterator of all key-value pairs in this database.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
let mut iter = db.iter_mut(&mut wtxn)?;
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
let ret = unsafe { iter.del_current()? };
assert!(ret);
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
let ret = unsafe { iter.put_current(&42, "i-am-the-new-forty-two")? };
assert!(ret);
assert_eq!(iter.next().transpose()?, None);
drop(iter);
let ret = db.get(&wtxn, &13)?;
assert_eq!(ret, None);
let ret = db.get(&wtxn, &42)?;
assert_eq!(ret, Some("i-am-the-new-forty-two"));
wtxn.commit()?;
Sourcepub fn rev_iter<'txn>(
&self,
txn: &'txn RoTxn<'_>,
) -> Result<RoRevIter<'txn, KC, DC>>
pub fn rev_iter<'txn>( &self, txn: &'txn RoTxn<'_>, ) -> Result<RoRevIter<'txn, KC, DC>>
Return a reversed lexicographically ordered iterator of all key-value pairs in this database.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
let mut iter = db.rev_iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn rev_iter_mut<'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
) -> Result<RwRevIter<'txn, KC, DC>>
pub fn rev_iter_mut<'txn>( &self, txn: &'txn mut RwTxn<'_>, ) -> Result<RwRevIter<'txn, KC, DC>>
Return a mutable reversed lexicographically ordered iterator of all key-value
pairs in this database.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
let mut iter = db.rev_iter_mut(&mut wtxn)?;
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
let ret = unsafe { iter.del_current()? };
assert!(ret);
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
let ret = unsafe { iter.put_current(&13, "i-am-the-new-thirteen")? };
assert!(ret);
assert_eq!(iter.next().transpose()?, None);
drop(iter);
let ret = db.get(&wtxn, &42)?;
assert_eq!(ret, None);
let ret = db.get(&wtxn, &13)?;
assert_eq!(ret, Some("i-am-the-new-thirteen"));
wtxn.commit()?;
Sourcepub fn range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn<'_>,
range: &'a R,
) -> Result<RoRange<'txn, KC, DC>>
pub fn range<'a, 'txn, R>( &self, txn: &'txn RoTxn<'_>, range: &'a R, ) -> Result<RoRange<'txn, KC, DC>>
Return a lexicographically ordered iterator of a range of key-value pairs in this database.
Comparisons are made by using the bytes representation of the key.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let range = 27..=42;
let mut iter = db.range(&wtxn, &range)?;
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn range_mut<'a, 'txn, R>(
&self,
txn: &'txn mut RwTxn<'_>,
range: &'a R,
) -> Result<RwRange<'txn, KC, DC>>
pub fn range_mut<'a, 'txn, R>( &self, txn: &'txn mut RwTxn<'_>, range: &'a R, ) -> Result<RwRange<'txn, KC, DC>>
Return a mutable lexicographically ordered iterator of a range of key-value pairs in this database.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let range = 27..=42;
let mut range = db.range_mut(&mut wtxn, &range)?;
assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven")));
let ret = unsafe { range.del_current()? };
assert!(ret);
assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two")));
let ret = unsafe { range.put_current(&42, "i-am-the-new-forty-two")? };
assert!(ret);
assert_eq!(range.next().transpose()?, None);
drop(range);
let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-the-new-forty-two")));
assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn rev_range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn<'_>,
range: &'a R,
) -> Result<RoRevRange<'txn, KC, DC>>
pub fn rev_range<'a, 'txn, R>( &self, txn: &'txn RoTxn<'_>, range: &'a R, ) -> Result<RoRevRange<'txn, KC, DC>>
Return a reversed lexicographically ordered iterator of a range of key-value pairs in this database.
Comparisons are made by using the bytes representation of the key.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let range = 27..=43;
let mut iter = db.rev_range(&wtxn, &range)?;
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn rev_range_mut<'a, 'txn, R>(
&self,
txn: &'txn mut RwTxn<'_>,
range: &'a R,
) -> Result<RwRevRange<'txn, KC, DC>>
pub fn rev_range_mut<'a, 'txn, R>( &self, txn: &'txn mut RwTxn<'_>, range: &'a R, ) -> Result<RwRevRange<'txn, KC, DC>>
Return a mutable reversed lexicographically ordered iterator of a range of key-value pairs in this database.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let range = 27..=42;
let mut range = db.rev_range_mut(&mut wtxn, &range)?;
assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two")));
let ret = unsafe { range.del_current()? };
assert!(ret);
assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven")));
let ret = unsafe { range.put_current(&27, "i-am-the-new-twenty-seven")? };
assert!(ret);
assert_eq!(range.next().transpose()?, None);
drop(range);
let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((27, "i-am-the-new-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RoPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
pub fn prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RoPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Return a lexicographically ordered iterator of all key-value pairs in this database that starts with the given prefix.
Comparisons are made by using the bytes representation of the key.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Str, BEI32> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, "i-am-twenty-eight", &28)?;
db.put(&mut wtxn, "i-am-twenty-seven", &27)?;
db.put(&mut wtxn, "i-am-twenty-nine", &29)?;
db.put(&mut wtxn, "i-am-forty-one", &41)?;
db.put(&mut wtxn, "i-am-forty-two", &42)?;
let mut iter = db.prefix_iter(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27)));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn prefix_iter_mut<'a, 'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RwPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
pub fn prefix_iter_mut<'a, 'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RwPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Return a mutable lexicographically ordered iterator of all key-value pairs in this database that starts with the given prefix.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Str, BEI32> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, "i-am-twenty-eight", &28)?;
db.put(&mut wtxn, "i-am-twenty-seven", &27)?;
db.put(&mut wtxn, "i-am-twenty-nine", &29)?;
db.put(&mut wtxn, "i-am-forty-one", &41)?;
db.put(&mut wtxn, "i-am-forty-two", &42)?;
let mut iter = db.prefix_iter_mut(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28)));
let ret = unsafe { iter.del_current()? };
assert!(ret);
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27)));
let ret = unsafe { iter.put_current("i-am-twenty-seven", &27000)? };
assert!(ret);
assert_eq!(iter.next().transpose()?, None);
drop(iter);
let ret = db.get(&wtxn, "i-am-twenty-eight")?;
assert_eq!(ret, None);
let ret = db.get(&wtxn, "i-am-twenty-seven")?;
assert_eq!(ret, Some(27000));
wtxn.commit()?;
Sourcepub fn rev_prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RoRevPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
pub fn rev_prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RoRevPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Return a reversed lexicographically ordered iterator of all key-value pairs in this database that starts with the given prefix.
Comparisons are made by using the bytes representation of the key.
You can make this iterator Send
able between threads by
using the read-txn-no-tls
crate feature.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Str, BEI32> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, "i-am-twenty-eight", &28)?;
db.put(&mut wtxn, "i-am-twenty-seven", &27)?;
db.put(&mut wtxn, "i-am-twenty-nine", &29)?;
db.put(&mut wtxn, "i-am-forty-one", &41)?;
db.put(&mut wtxn, "i-am-forty-two", &42)?;
let mut iter = db.rev_prefix_iter(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28)));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn rev_prefix_iter_mut<'a, 'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RwRevPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
pub fn rev_prefix_iter_mut<'a, 'txn>(
&self,
txn: &'txn mut RwTxn<'_>,
prefix: &'a KC::EItem,
) -> Result<RwRevPrefix<'txn, KC, DC, C>>where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Return a mutable reversed lexicographically ordered iterator of all key-value pairs in this database that starts with the given prefix.
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Str, BEI32> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, "i-am-twenty-eight", &28)?;
db.put(&mut wtxn, "i-am-twenty-seven", &27)?;
db.put(&mut wtxn, "i-am-twenty-nine", &29)?;
db.put(&mut wtxn, "i-am-forty-one", &41)?;
db.put(&mut wtxn, "i-am-forty-two", &42)?;
let mut iter = db.rev_prefix_iter_mut(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27)));
let ret = unsafe { iter.del_current()? };
assert!(ret);
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29)));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28)));
let ret = unsafe { iter.put_current("i-am-twenty-eight", &28000)? };
assert!(ret);
assert_eq!(iter.next().transpose()?, None);
drop(iter);
let ret = db.get(&wtxn, "i-am-twenty-seven")?;
assert_eq!(ret, None);
let ret = db.get(&wtxn, "i-am-twenty-eight")?;
assert_eq!(ret, Some(28000));
wtxn.commit()?;
Sourcepub fn put<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<()>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
pub fn put<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<()>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
Insert a key-value pair in this database, replacing any previous value. The entry is written with no specific flag.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let ret = db.get(&mut wtxn, &27)?;
assert_eq!(ret, Some("i-am-twenty-seven"));
wtxn.commit()?;
Sourcepub fn put_reserved<'a, F>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data_size: usize,
write_func: F,
) -> Result<()>
pub fn put_reserved<'a, F>( &self, txn: &mut RwTxn<'_>, key: &'a KC::EItem, data_size: usize, write_func: F, ) -> Result<()>
Insert a key-value pair where the value can directly be written to disk, replacing any previous value.
use std::io::Write;
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEI32, Str>(&mut wtxn, Some("number-string"))?;
let value = "I am a long long long value";
db.put_reserved(&mut wtxn, &42, value.len(), |reserved| {
reserved.write_all(value.as_bytes())
})?;
let ret = db.get(&mut wtxn, &42)?;
assert_eq!(ret, Some(value));
wtxn.commit()?;
Sourcepub fn put_with_flags<'a>(
&self,
txn: &mut RwTxn<'_>,
flags: PutFlags,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<()>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
pub fn put_with_flags<'a>(
&self,
txn: &mut RwTxn<'_>,
flags: PutFlags,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<()>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
Insert a key-value pair in this database, replacing any previous value. The entry is written with the specified flags.
use heed::{Database, PutFlags, DatabaseFlags, Error, MdbError};
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.database_options()
.types::<BEI32, Str>()
.name("dup-i32")
.flags(DatabaseFlags::DUP_SORT)
.create(&mut wtxn)?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &42, "i-am-so-cool")?;
db.put(&mut wtxn, &42, "i-am-the-king")?;
db.put(&mut wtxn, &42, "i-am-fun")?;
db.put_with_flags(&mut wtxn, PutFlags::APPEND, &54, "i-am-older-than-you")?;
db.put_with_flags(&mut wtxn, PutFlags::APPEND_DUP, &54, "ok-but-i-am-better-than-you")?;
// You can compose flags by OR'ing them
db.put_with_flags(&mut wtxn, PutFlags::APPEND_DUP | PutFlags::NO_OVERWRITE, &55, "welcome")?;
// The NO_DUP_DATA flag will return KeyExist if we try to insert the exact same key/value pair.
let ret = db.put_with_flags(&mut wtxn, PutFlags::NO_DUP_DATA, &54, "ok-but-i-am-better-than-you");
assert!(matches!(ret, Err(Error::Mdb(MdbError::KeyExist))));
// The NO_OVERWRITE flag will return KeyExist if we try to insert something with an already existing key.
let ret = db.put_with_flags(&mut wtxn, PutFlags::NO_OVERWRITE, &54, "there-can-be-only-one-data");
assert!(matches!(ret, Err(Error::Mdb(MdbError::KeyExist))));
let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-fun")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-so-cool")));
assert_eq!(iter.next().transpose()?, Some((42, "i-am-the-king")));
assert_eq!(iter.next().transpose()?, Some((54, "i-am-older-than-you")));
assert_eq!(iter.next().transpose()?, Some((54, "ok-but-i-am-better-than-you")));
assert_eq!(iter.next().transpose()?, Some((55, "welcome")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn get_or_put<'a, 'txn>(
&'txn self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<Option<DC::DItem>>
pub fn get_or_put<'a, 'txn>( &'txn self, txn: &mut RwTxn<'_>, key: &'a KC::EItem, data: &'a DC::EItem, ) -> Result<Option<DC::DItem>>
Attempt to insert a key-value pair in this database, or if a value already exists for the key, returns the previous value.
The entry is always written with the NO_OVERWRITE
flag.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
assert_eq!(db.get_or_put(&mut wtxn, &42, "i-am-forty-two")?, None);
assert_eq!(db.get_or_put(&mut wtxn, &42, "the meaning of life")?, Some("i-am-forty-two"));
let ret = db.get(&mut wtxn, &42)?;
assert_eq!(ret, Some("i-am-forty-two"));
wtxn.commit()?;
Sourcepub fn get_or_put_with_flags<'a, 'txn>(
&'txn self,
txn: &mut RwTxn<'_>,
flags: PutFlags,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<Option<DC::DItem>>
pub fn get_or_put_with_flags<'a, 'txn>( &'txn self, txn: &mut RwTxn<'_>, flags: PutFlags, key: &'a KC::EItem, data: &'a DC::EItem, ) -> Result<Option<DC::DItem>>
Attempt to insert a key-value pair in this database, or if a value already exists for the key, returns the previous value.
The entry is written with the specified flags, in addition to
NO_OVERWRITE
which is always used.
use heed::{Database, PutFlags};
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
assert_eq!(db.get_or_put_with_flags(&mut wtxn, PutFlags::empty(), &42, "i-am-forty-two")?, None);
assert_eq!(db.get_or_put_with_flags(&mut wtxn, PutFlags::empty(), &42, "the meaning of life")?, Some("i-am-forty-two"));
let ret = db.get(&mut wtxn, &42)?;
assert_eq!(ret, Some("i-am-forty-two"));
wtxn.commit()?;
Sourcepub fn get_or_put_reserved<'a, 'txn, F>(
&'txn self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data_size: usize,
write_func: F,
) -> Result<Option<DC::DItem>>
pub fn get_or_put_reserved<'a, 'txn, F>( &'txn self, txn: &mut RwTxn<'_>, key: &'a KC::EItem, data_size: usize, write_func: F, ) -> Result<Option<DC::DItem>>
Attempt to insert a key-value pair in this database, where the value can be directly written to disk, or if a value already exists for the key, returns the previous value.
The entry is always written with the NO_OVERWRITE
and
MDB_RESERVE
flags.
use std::io::Write;
use heed::{Database, PutFlags};
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEI32, Str>(&mut wtxn, Some("number-string"))?;
let long = "I am a long long long value";
assert_eq!(
db.get_or_put_reserved(&mut wtxn, &42, long.len(), |reserved| {
reserved.write_all(long.as_bytes())
})?,
None
);
let longer = "I am an even longer long long long value";
assert_eq!(
db.get_or_put_reserved(&mut wtxn, &42, longer.len(), |reserved| {
unreachable!()
})?,
Some(long)
);
let ret = db.get(&mut wtxn, &42)?;
assert_eq!(ret, Some(long));
wtxn.commit()?;
Sourcepub fn get_or_put_reserved_with_flags<'a, 'txn, F>(
&'txn self,
txn: &mut RwTxn<'_>,
flags: PutFlags,
key: &'a KC::EItem,
data_size: usize,
write_func: F,
) -> Result<Option<DC::DItem>>
pub fn get_or_put_reserved_with_flags<'a, 'txn, F>( &'txn self, txn: &mut RwTxn<'_>, flags: PutFlags, key: &'a KC::EItem, data_size: usize, write_func: F, ) -> Result<Option<DC::DItem>>
Attempt to insert a key-value pair in this database, where the value can be directly written to disk, or if a value already exists for the key, returns the previous value.
The entry is written with the specified flags, in addition to
NO_OVERWRITE
and MDB_RESERVE
which are always used.
use std::io::Write;
use heed::{Database, PutFlags};
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.create_database::<BEI32, Str>(&mut wtxn, Some("number-string"))?;
let long = "I am a long long long value";
assert_eq!(
db.get_or_put_reserved_with_flags(&mut wtxn, PutFlags::empty(), &42, long.len(), |reserved| {
reserved.write_all(long.as_bytes())
})?,
None
);
let longer = "I am an even longer long long long value";
assert_eq!(
db.get_or_put_reserved_with_flags(&mut wtxn, PutFlags::empty(), &42, longer.len(), |reserved| {
unreachable!()
})?,
Some(long)
);
let ret = db.get(&mut wtxn, &42)?;
assert_eq!(ret, Some(long));
wtxn.commit()?;
Sourcepub fn delete<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
) -> Result<bool>where
KC: BytesEncode<'a>,
pub fn delete<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
) -> Result<bool>where
KC: BytesEncode<'a>,
Deletes an entry or every duplicate data items of a key if the database supports duplicate data items.
If the entry does not exist, then false
is returned.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let ret = db.delete(&mut wtxn, &27)?;
assert_eq!(ret, true);
let ret = db.get(&mut wtxn, &27)?;
assert_eq!(ret, None);
let ret = db.delete(&mut wtxn, &467)?;
assert_eq!(ret, false);
wtxn.commit()?;
Sourcepub fn delete_one_duplicate<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<bool>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
pub fn delete_one_duplicate<'a>(
&self,
txn: &mut RwTxn<'_>,
key: &'a KC::EItem,
data: &'a DC::EItem,
) -> Result<bool>where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
Deletes a single key-value pair in this database.
If the database doesn’t support duplicate data items the data is ignored.
If the key does not exist, then false
is returned.
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI64 = I64<BigEndian>;
let mut wtxn = env.write_txn()?;
let db = env.database_options()
.types::<BEI64, BEI64>()
.flags(DatabaseFlags::DUP_SORT)
.name("dup-sort")
.create(&mut wtxn)?;
db.put(&mut wtxn, &68, &120)?;
db.put(&mut wtxn, &68, &121)?;
db.put(&mut wtxn, &68, &122)?;
db.put(&mut wtxn, &68, &123)?;
db.put(&mut wtxn, &92, &32)?;
db.put(&mut wtxn, &35, &120)?;
db.put(&mut wtxn, &0, &120)?;
db.put(&mut wtxn, &42, &120)?;
let mut iter = db.get_duplicates(&wtxn, &68)?.expect("the key exists");
assert_eq!(iter.next().transpose()?, Some((68, 120)));
assert_eq!(iter.next().transpose()?, Some((68, 121)));
assert_eq!(iter.next().transpose()?, Some((68, 122)));
assert_eq!(iter.next().transpose()?, Some((68, 123)));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
assert!(db.delete_one_duplicate(&mut wtxn, &68, &121)?, "The entry must exist");
let mut iter = db.get_duplicates(&wtxn, &68)?.expect("the key exists");
assert_eq!(iter.next().transpose()?, Some((68, 120)));
// No more (68, 121) returned here!
assert_eq!(iter.next().transpose()?, Some((68, 122)));
assert_eq!(iter.next().transpose()?, Some((68, 123)));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn delete_range<'a, 'txn, R>(
&self,
txn: &'txn mut RwTxn<'_>,
range: &'a R,
) -> Result<usize>
pub fn delete_range<'a, 'txn, R>( &self, txn: &'txn mut RwTxn<'_>, range: &'a R, ) -> Result<usize>
Deletes a range of key-value pairs in this database.
Prefer using clear
instead of a call to this method with a full range (..
).
Comparisons are made by using the bytes representation of the key.
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
let range = 27..=42;
let ret = db.delete_range(&mut wtxn, &range)?;
assert_eq!(ret, 2);
let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);
drop(iter);
wtxn.commit()?;
Sourcepub fn clear(&self, txn: &mut RwTxn<'_>) -> Result<()>
pub fn clear(&self, txn: &mut RwTxn<'_>) -> Result<()>
Deletes all key/value pairs in this database.
Prefer using this method instead of a call to delete_range
with a full range (..
).
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<BEI32, Str> = env.create_database(&mut wtxn, Some("iter-i32"))?;
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
db.clear(&mut wtxn)?;
let ret = db.is_empty(&wtxn)?;
assert!(ret);
wtxn.commit()?;
Sourcepub fn remap_types<KC2, DC2>(&self) -> Database<KC2, DC2, C>
pub fn remap_types<KC2, DC2>(&self) -> Database<KC2, DC2, C>
Change the codec types of this database, specifying the codecs.
§Safety
It is up to you to ensure that the data read and written using the polymorphic handle correspond to the the typed, uniform one. If an invalid write is made, it can corrupt the database from the eyes of heed.
§Example
use heed::Database;
use heed::types::*;
use heed::byteorder::BigEndian;
type BEI32 = I32<BigEndian>;
let mut wtxn = env.write_txn()?;
let db: Database<Unit, Unit> = env.create_database(&mut wtxn, Some("iter-i32"))?;
// We remap the types for ease of use.
let db = db.remap_types::<BEI32, Str>();
db.put(&mut wtxn, &42, "i-am-forty-two")?;
db.put(&mut wtxn, &27, "i-am-twenty-seven")?;
db.put(&mut wtxn, &13, "i-am-thirteen")?;
db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?;
wtxn.commit()?;
Sourcepub fn remap_key_type<KC2>(&self) -> Database<KC2, DC, C>
pub fn remap_key_type<KC2>(&self) -> Database<KC2, DC, C>
Change the key codec type of this database, specifying the new codec.
Sourcepub fn remap_data_type<DC2>(&self) -> Database<KC, DC2, C>
pub fn remap_data_type<DC2>(&self) -> Database<KC, DC2, C>
Change the data codec type of this database, specifying the new codec.
Sourcepub fn lazily_decode_data(&self) -> Database<KC, LazyDecode<DC>, C>
pub fn lazily_decode_data(&self) -> Database<KC, LazyDecode<DC>, C>
Wrap the data bytes into a lazy decoder.
Trait Implementations§
impl<KC, DC, C> Copy for Database<KC, DC, C>
Auto Trait Implementations§
impl<KC, DC, C> Freeze for Database<KC, DC, C>
impl<KC, DC, C> RefUnwindSafe for Database<KC, DC, C>
impl<KC, DC, C> Send for Database<KC, DC, C>
impl<KC, DC, C> Sync for Database<KC, DC, C>
impl<KC, DC, C> Unpin for Database<KC, DC, C>
impl<KC, DC, C> UnwindSafe for Database<KC, DC, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 16 bytes