[][src]Struct heed::PolyDatabase

pub struct PolyDatabase { /* fields omitted */ }

A polymorphic database that accepts types on call methods and not at creation.

Example: Iterate over ranges of 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::PolyDatabase;
use heed::types::*;
use heed::{zerocopy::I64, byteorder::BigEndian};

type BEI64 = I64<BigEndian>;

let db: PolyDatabase = env.create_poly_database(Some("big-endian-iter"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(0), &())?;
db.put::<_, OwnedType<BEI64>, Str>(&mut wtxn, &BEI64::new(35), "thirty five")?;
db.put::<_, OwnedType<BEI64>, Str>(&mut wtxn, &BEI64::new(42), "forty two")?;
db.put::<_, OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(68), &())?;

// you can iterate over database entries in order
let range = BEI64::new(35)..=BEI64::new(42);
let mut range = db.range::<_, OwnedType<BEI64>, Str, _>(&wtxn, &range)?;
assert_eq!(range.next().transpose()?, Some((BEI64::new(35), "thirty five")));
assert_eq!(range.next().transpose()?, Some((BEI64::new(42), "forty two")));
assert_eq!(range.next().transpose()?, None);

drop(range);
wtxn.commit()?;

Example: Select ranges of entries

Heed also support 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::PolyDatabase;
use heed::types::*;
use heed::{zerocopy::I64, byteorder::BigEndian};

type BEI64 = I64<BigEndian>;

let db: PolyDatabase = env.create_poly_database(Some("big-endian-iter"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(0), &())?;
db.put::<_, OwnedType<BEI64>, Str>(&mut wtxn, &BEI64::new(35), "thirty five")?;
db.put::<_, OwnedType<BEI64>, Str>(&mut wtxn, &BEI64::new(42), "forty two")?;
db.put::<_, OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(68), &())?;

// even delete a range of keys
let range = BEI64::new(35)..=BEI64::new(42);
let deleted = db.delete_range::<_, OwnedType<BEI64>, _>(&mut wtxn, &range)?;
assert_eq!(deleted, 2);

let rets: Result<_, _> = db.iter::<_, OwnedType<BEI64>, Unit>(&wtxn)?.collect();
let rets: Vec<(BEI64, _)> = rets?;

let expected = vec![
    (BEI64::new(0), ()),
    (BEI64::new(68), ()),
];

assert_eq!(deleted, 2);
assert_eq!(rets, expected);

wtxn.commit()?;

Implementations

impl PolyDatabase[src]

pub fn get<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    key: &'a KC::EItem
) -> Result<Option<DC::DItem>> where
    KC: BytesEncode<'a>,
    DC: BytesDecode<'txn>, 
[src]

Retrieves the value associated with a key.

If the key does not exist, then None is returned.

use heed::Database;
use heed::types::*;

let db = env.create_poly_database(Some("get-poly-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, Str, OwnedType<i32>>(&mut wtxn, "i-am-forty-two", &42)?;
db.put::<_, Str, OwnedType<i32>>(&mut wtxn, "i-am-twenty-seven", &27)?;

let ret = db.get::<_, Str, OwnedType<i32>>(&wtxn, "i-am-forty-two")?;
assert_eq!(ret, Some(42));

let ret = db.get::<_, Str, OwnedType<i32>>(&wtxn, "i-am-twenty-one")?;
assert_eq!(ret, None);

wtxn.commit()?;

pub fn get_lower_than<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    key: &'a KC::EItem
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesEncode<'a> + BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

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::{zerocopy::U32, byteorder::BigEndian};

type BEU32 = U32<BigEndian>;

let db = env.create_poly_database(Some("get-lt-u32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(27), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(42), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(43), &())?;

let ret = db.get_lower_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(4404))?;
assert_eq!(ret, Some((BEU32::new(43), ())));

let ret = db.get_lower_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(43))?;
assert_eq!(ret, Some((BEU32::new(42), ())));

let ret = db.get_lower_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(27))?;
assert_eq!(ret, None);

wtxn.commit()?;

pub fn get_lower_than_or_equal_to<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    key: &'a KC::EItem
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesEncode<'a> + BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

Retrieves the key/value pair lower than or equal 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::{zerocopy::U32, byteorder::BigEndian};

type BEU32 = U32<BigEndian>;

let db = env.create_poly_database(Some("get-lte-u32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(27), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(42), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(43), &())?;

let ret = db.get_lower_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(4404))?;
assert_eq!(ret, Some((BEU32::new(43), ())));

let ret = db.get_lower_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(43))?;
assert_eq!(ret, Some((BEU32::new(43), ())));

let ret = db.get_lower_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(26))?;
assert_eq!(ret, None);

wtxn.commit()?;

pub fn get_greater_than<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    key: &'a KC::EItem
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesEncode<'a> + BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

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::{zerocopy::U32, byteorder::BigEndian};

type BEU32 = U32<BigEndian>;

let db = env.create_poly_database(Some("get-lt-u32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(27), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(42), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(43), &())?;

let ret = db.get_greater_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(0))?;
assert_eq!(ret, Some((BEU32::new(27), ())));

let ret = db.get_greater_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(42))?;
assert_eq!(ret, Some((BEU32::new(43), ())));

let ret = db.get_greater_than::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(43))?;
assert_eq!(ret, None);

wtxn.commit()?;

pub fn get_greater_than_or_equal_to<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    key: &'a KC::EItem
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesEncode<'a> + BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

Retrieves the key/value pair greater than or equal 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::{zerocopy::U32, byteorder::BigEndian};

type BEU32 = U32<BigEndian>;

let db = env.create_poly_database(Some("get-lt-u32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(27), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(42), &())?;
db.put::<_, OwnedType<BEU32>, Unit>(&mut wtxn, &BEU32::new(43), &())?;

let ret = db.get_greater_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(0))?;
assert_eq!(ret, Some((BEU32::new(27), ())));

let ret = db.get_greater_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(42))?;
assert_eq!(ret, Some((BEU32::new(42), ())));

let ret = db.get_greater_than_or_equal_to::<_, OwnedType<BEU32>, Unit>(&wtxn, &BEU32::new(44))?;
assert_eq!(ret, None);

wtxn.commit()?;

pub fn first<'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("first-poly-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;

let ret = db.first::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(ret, Some((BEI32::new(27), "i-am-twenty-seven")));

wtxn.commit()?;

pub fn last<'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>
) -> Result<Option<(KC::DItem, DC::DItem)>> where
    KC: BytesDecode<'txn>,
    DC: BytesDecode<'txn>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("last-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;

let ret = db.last::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(ret, Some((BEI32::new(42), "i-am-forty-two")));

wtxn.commit()?;

pub fn len<'txn, T>(&self, txn: &'txn RoTxn<'_, T>) -> Result<usize>[src]

Returns the number of elements in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let ret = db.len(&wtxn)?;
assert_eq!(ret, 4);

db.delete::<_, OwnedType<BEI32>>(&mut wtxn, &BEI32::new(27))?;

let ret = db.len(&wtxn)?;
assert_eq!(ret, 3);

wtxn.commit()?;

pub fn is_empty<'txn, T>(&self, txn: &'txn RoTxn<'_, T>) -> Result<bool>[src]

Returns true if and only if this database is empty.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(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()?;

pub fn iter<'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>
) -> Result<RoIter<'txn, KC, DC>>
[src]

Return a lexicographically ordered iterator of all key-value pairs in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;

let mut iter = db.iter::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn iter_mut<'txn, T, KC, DC>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>
) -> Result<RwIter<'txn, KC, DC>>
[src]

Return a mutable lexicographically ordered iterator of all key-value pairs in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;

let mut iter = db.iter_mut::<_, OwnedType<BEI32>, Str>(&mut wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
let ret = iter.del_current()?;
assert!(ret);

assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
let ret = iter.put_current(&BEI32::new(42), "i-am-the-new-forty-two")?;
assert!(ret);

assert_eq!(iter.next().transpose()?, None);

drop(iter);

let ret = db.get::<_, OwnedType<BEI32>, Str>(&wtxn, &BEI32::new(13))?;
assert_eq!(ret, None);

let ret = db.get::<_, OwnedType<BEI32>, Str>(&wtxn, &BEI32::new(42))?;
assert_eq!(ret, Some("i-am-the-new-forty-two"));

wtxn.commit()?;

pub fn rev_iter<'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>
) -> Result<RoRevIter<'txn, KC, DC>>
[src]

Returns a reversed lexicographically ordered iterator of all key-value pairs in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;

let mut iter = db.rev_iter::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn rev_iter_mut<'txn, T, KC, DC>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>
) -> Result<RwRevIter<'txn, KC, DC>>
[src]

Return a mutable reversed lexicographically ordered iterator of all key-value pairs in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;

let mut iter = db.rev_iter_mut::<_, OwnedType<BEI32>, Str>(&mut wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
let ret = iter.del_current()?;
assert!(ret);

assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
let ret = iter.put_current(&BEI32::new(13), "i-am-the-new-thirteen")?;
assert!(ret);

assert_eq!(iter.next().transpose()?, None);

drop(iter);

let ret = db.get::<_, OwnedType<BEI32>, Str>(&wtxn, &BEI32::new(42))?;
assert_eq!(ret, None);

let ret = db.get::<_, OwnedType<BEI32>, Str>(&wtxn, &BEI32::new(13))?;
assert_eq!(ret, Some("i-am-the-new-thirteen"));

wtxn.commit()?;

pub fn range<'a, 'txn, T, KC, DC, R>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    range: &'a R
) -> Result<RoRange<'txn, KC, DC>> where
    KC: BytesEncode<'a>,
    R: RangeBounds<KC::EItem>, 
[src]

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.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let range = BEI32::new(27)..=BEI32::new(42);
let mut iter = db.range::<_, OwnedType<BEI32>, Str, _>(&wtxn, &range)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn range_mut<'a, 'txn, T, KC, DC, R>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>,
    range: &'a R
) -> Result<RwRange<'txn, KC, DC>> where
    KC: BytesEncode<'a>,
    R: RangeBounds<KC::EItem>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let range = BEI32::new(27)..=BEI32::new(42);
let mut range = db.range_mut::<_, OwnedType<BEI32>, Str, _>(&mut wtxn, &range)?;
assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
let ret = range.del_current()?;
assert!(ret);
assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
let ret = range.put_current(&BEI32::new(42), "i-am-the-new-forty-two")?;
assert!(ret);

assert_eq!(range.next().transpose()?, None);
drop(range);


let mut iter = db.iter::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-the-new-forty-two")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn rev_range<'a, 'txn, T, KC, DC, R>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    range: &'a R
) -> Result<RoRevRange<'txn, KC, DC>> where
    KC: BytesEncode<'a>,
    R: RangeBounds<KC::EItem>, 
[src]

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.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let range = BEI32::new(27)..=BEI32::new(43);
let mut iter = db.rev_range::<_, OwnedType<BEI32>, Str, _>(&wtxn, &range)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn rev_range_mut<'a, 'txn, T, KC, DC, R>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>,
    range: &'a R
) -> Result<RwRevRange<'txn, KC, DC>> where
    KC: BytesEncode<'a>,
    R: RangeBounds<KC::EItem>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let range = BEI32::new(27)..=BEI32::new(42);
let mut range = db.rev_range_mut::<_, OwnedType<BEI32>, Str, _>(&mut wtxn, &range)?;
assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two")));
let ret = range.del_current()?;
assert!(ret);
assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven")));
let ret = range.put_current(&BEI32::new(27), "i-am-the-new-twenty-seven")?;
assert!(ret);

assert_eq!(range.next().transpose()?, None);
drop(range);


let mut iter = db.iter::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-the-new-twenty-seven")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn prefix_iter<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    prefix: &'a KC::EItem
) -> Result<RoPrefix<'txn, KC, DC>> where
    KC: BytesEncode<'a>, 
[src]

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.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-nine",  &BEI32::new(29))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-one",    &BEI32::new(41))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-two",    &BEI32::new(42))?;

let mut iter = db.prefix_iter::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27))));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn prefix_iter_mut<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>,
    prefix: &'a KC::EItem
) -> Result<RwPrefix<'txn, KC, DC>> where
    KC: BytesEncode<'a>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-nine",  &BEI32::new(29))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-one",    &BEI32::new(41))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-two",    &BEI32::new(42))?;

let mut iter = db.prefix_iter_mut::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28))));
let ret = iter.del_current()?;
assert!(ret);

assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27))));
let ret = iter.put_current("i-am-twenty-seven", &BEI32::new(27000))?;
assert!(ret);

assert_eq!(iter.next().transpose()?, None);

drop(iter);

let ret = db.get::<_, Str, OwnedType<BEI32>>(&wtxn, "i-am-twenty-eight")?;
assert_eq!(ret, None);

let ret = db.get::<_, Str, OwnedType<BEI32>>(&wtxn, "i-am-twenty-seven")?;
assert_eq!(ret, Some(BEI32::new(27000)));

wtxn.commit()?;

pub fn rev_prefix_iter<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn RoTxn<'_, T>,
    prefix: &'a KC::EItem
) -> Result<RoRevPrefix<'txn, KC, DC>> where
    KC: BytesEncode<'a>, 
[src]

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.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-nine",  &BEI32::new(29))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-one",    &BEI32::new(41))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-two",    &BEI32::new(42))?;

let mut iter = db.rev_prefix_iter::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28))));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn rev_prefix_iter_mut<'a, 'txn, T, KC, DC>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>,
    prefix: &'a KC::EItem
) -> Result<RwRevPrefix<'txn, KC, DC>> where
    KC: BytesEncode<'a>, 
[src]

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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty-nine",  &BEI32::new(29))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-one",    &BEI32::new(41))?;
db.put::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-forty-two",    &BEI32::new(42))?;

let mut iter = db.rev_prefix_iter_mut::<_, Str, OwnedType<BEI32>>(&mut wtxn, "i-am-twenty")?;
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27))));
let ret = iter.del_current()?;
assert!(ret);

assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29))));
assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28))));
let ret = iter.put_current("i-am-twenty-eight", &BEI32::new(28000))?;
assert!(ret);

assert_eq!(iter.next().transpose()?, None);

drop(iter);

let ret = db.get::<_, Str, OwnedType<BEI32>>(&wtxn, "i-am-twenty-seven")?;
assert_eq!(ret, None);

let ret = db.get::<_, Str, OwnedType<BEI32>>(&wtxn, "i-am-twenty-eight")?;
assert_eq!(ret, Some(BEI32::new(28000)));

wtxn.commit()?;

pub fn put<'a, T, KC, DC>(
    &self,
    txn: &mut RwTxn<'_, '_, T>,
    key: &'a KC::EItem,
    data: &'a DC::EItem
) -> Result<()> where
    KC: BytesEncode<'a>,
    DC: BytesEncode<'a>, 
[src]

Insert a key-value pairs in this database.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let ret = db.get::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27))?;
assert_eq!(ret, Some("i-am-twenty-seven"));

wtxn.commit()?;

pub fn append<'a, T, KC, DC>(
    &self,
    txn: &mut RwTxn<'_, '_, T>,
    key: &'a KC::EItem,
    data: &'a DC::EItem
) -> Result<()> where
    KC: BytesEncode<'a>,
    DC: BytesEncode<'a>, 
[src]

Append the given key/data pair to the end of the database.

This option allows fast bulk loading when keys are already known to be in the correct order. Loading unsorted keys will cause a MDB_KEYEXIST error.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("append-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let ret = db.get::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27))?;
assert_eq!(ret, Some("i-am-twenty-seven"));

wtxn.commit()?;

pub fn delete<'a, T, KC>(
    &self,
    txn: &mut RwTxn<'_, '_, T>,
    key: &'a KC::EItem
) -> Result<bool> where
    KC: BytesEncode<'a>, 
[src]

Deletes a key-value pairs in this database.

If the key does not exist, then false is returned.

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let ret = db.delete::<_, OwnedType<BEI32>>(&mut wtxn, &BEI32::new(27))?;
assert_eq!(ret, true);

let ret = db.get::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27))?;
assert_eq!(ret, None);

let ret = db.delete::<_, OwnedType<BEI32>>(&mut wtxn, &BEI32::new(467))?;
assert_eq!(ret, false);

wtxn.commit()?;

pub fn delete_range<'a, 'txn, T, KC, R>(
    &self,
    txn: &'txn mut RwTxn<'_, '_, T>,
    range: &'a R
) -> Result<usize> where
    KC: BytesEncode<'a> + BytesDecode<'txn>,
    R: RangeBounds<KC::EItem>, 
[src]

Deletes a range of key-value pairs in this database.

Perfer 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::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

let range = BEI32::new(27)..=BEI32::new(42);
let ret = db.delete_range::<_, OwnedType<BEI32>, _>(&mut wtxn, &range)?;
assert_eq!(ret, 2);


let mut iter = db.iter::<_, OwnedType<BEI32>, Str>(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen")));
assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one")));
assert_eq!(iter.next().transpose()?, None);

drop(iter);
wtxn.commit()?;

pub fn clear<T>(&self, txn: &mut RwTxn<'_, '_, T>) -> Result<()>[src]

Deletes all key/value pairs in this database.

Perfer using this method instead of a call to delete_range with a full range (..).

use heed::Database;
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put::<_, OwnedType<BEI32>, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

db.clear(&mut wtxn)?;

let ret = db.is_empty(&wtxn)?;
assert!(ret);

wtxn.commit()?;

pub fn as_uniform<KC, DC>(&self) -> Database<KC, DC>[src]

Read this polymorphic database like a typed one, 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, PolyDatabase};
use heed::types::*;
use heed::{zerocopy::I32, byteorder::BigEndian};

type BEI32 = I32<BigEndian>;

let db = env.create_poly_database(Some("iter-i32"))?;

let mut wtxn = env.write_txn()?;
// We remap the types for ease of use.
let db = db.as_uniform::<OwnedType<BEI32>, Str>();
db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?;
db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?;
db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?;
db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?;

wtxn.commit()?;

Trait Implementations

impl Clone for PolyDatabase[src]

impl Copy for PolyDatabase[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.