use std::marker;
use std::ops::RangeBounds;
use crate::*;
use crate::mdb::ffi;
pub struct Database<KC, DC> {
pub(crate) dyndb: PolyDatabase,
marker: marker::PhantomData<(KC, DC)>,
}
impl<KC, DC> Database<KC, DC> {
pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> Database<KC, DC> {
Database {
dyndb: PolyDatabase::new(env_ident, dbi),
marker: std::marker::PhantomData,
}
}
#[cfg(all(feature = "mdbx", not(feature = "lmdb")))]
pub fn sequence<T>(&self, txn: &RoTxn<T>) -> Result<u64> {
self.dyndb.sequence(txn)
}
#[cfg(all(feature = "mdbx", not(feature = "lmdb")))]
pub fn increase_sequence<T>(&self, txn: &mut RwTxn<T>, increment: u64) -> Result<Option<u64>> {
self.dyndb.increase_sequence(txn, increment)
}
pub fn get<'a, 'txn, T>(&self, txn: &'txn RoTxn<T>, key: &'a KC::EItem) -> Result<Option<DC::DItem>>
where
KC: BytesEncode<'a>,
DC: BytesDecode<'txn>,
{
self.dyndb.get::<T, KC, DC>(txn, key)
}
pub fn get_lower_than<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesEncode<'a> + BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.get_lower_than::<T, KC, DC>(txn, key)
}
pub fn get_lower_than_or_equal_to<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesEncode<'a> + BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.get_lower_than_or_equal_to::<T, KC, DC>(txn, key)
}
pub fn get_greater_than<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesEncode<'a> + BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.get_greater_than::<T, KC, DC>(txn, key)
}
pub fn get_greater_than_or_equal_to<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesEncode<'a> + BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.get_greater_than_or_equal_to::<T, KC, DC>(txn, key)
}
pub fn first<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.first::<T, KC, DC>(txn)
}
pub fn last<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
{
self.dyndb.last::<T, KC, DC>(txn)
}
pub fn len<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<usize> {
self.dyndb.len(txn)
}
pub fn is_empty<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<bool> {
self.dyndb.is_empty(txn)
}
pub fn iter<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<RoIter<'txn, KC, DC>> {
self.dyndb.iter::<T, KC, DC>(txn)
}
pub fn iter_mut<'txn, T>(&self, txn: &'txn mut RwTxn<T>) -> Result<RwIter<'txn, KC, DC>> {
self.dyndb.iter_mut::<T, KC, DC>(txn)
}
pub fn rev_iter<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<RoRevIter<'txn, KC, DC>> {
self.dyndb.rev_iter::<T, KC, DC>(txn)
}
pub fn rev_iter_mut<'txn, T>(&self, txn: &'txn mut RwTxn<T>) -> Result<RwRevIter<'txn, KC, DC>> {
self.dyndb.rev_iter_mut::<T, KC, DC>(txn)
}
pub fn range<'a, 'txn, T, R>(
&self,
txn: &'txn RoTxn<T>,
range: &'a R,
) -> Result<RoRange<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
{
self.dyndb.range::<T, KC, DC, R>(txn, range)
}
pub fn range_mut<'a, 'txn, T, R>(
&self,
txn: &'txn mut RwTxn<T>,
range: &'a R,
) -> Result<RwRange<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
{
self.dyndb.range_mut::<T, KC, DC, R>(txn, range)
}
pub fn rev_range<'a, 'txn, T, R>(
&self,
txn: &'txn RoTxn<T>,
range: &'a R,
) -> Result<RoRevRange<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
{
self.dyndb.rev_range::<T, KC, DC, R>(txn, range)
}
pub fn rev_range_mut<'a, 'txn, T, R>(
&self,
txn: &'txn mut RwTxn<T>,
range: &'a R,
) -> Result<RwRevRange<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
{
self.dyndb.rev_range_mut::<T, KC, DC, R>(txn, range)
}
pub fn prefix_iter<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
prefix: &'a KC::EItem,
) -> Result<RoPrefix<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
{
self.dyndb.prefix_iter::<T, KC, DC>(txn, prefix)
}
pub fn prefix_iter_mut<'a, 'txn, T>(
&self,
txn: &'txn mut RwTxn<T>,
prefix: &'a KC::EItem,
) -> Result<RwPrefix<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
{
self.dyndb.prefix_iter_mut::<T, KC, DC>(txn, prefix)
}
pub fn rev_prefix_iter<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
prefix: &'a KC::EItem,
) -> Result<RoRevPrefix<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
{
self.dyndb.rev_prefix_iter::<T, KC, DC>(txn, prefix)
}
pub fn rev_prefix_iter_mut<'a, 'txn, T>(
&self,
txn: &'txn mut RwTxn<T>,
prefix: &'a KC::EItem,
) -> Result<RwRevPrefix<'txn, KC, DC>>
where
KC: BytesEncode<'a>,
{
self.dyndb.rev_prefix_iter_mut::<T, KC, DC>(txn, prefix)
}
pub fn put<'a, T>(&self, txn: &mut RwTxn<T>, key: &'a KC::EItem, data: &'a DC::EItem) -> Result<()>
where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
{
self.dyndb.put::<T, KC, DC>(txn, key, data)
}
pub fn append<'a, T>(&self, txn: &mut RwTxn<T>, key: &'a KC::EItem, data: &'a DC::EItem) -> Result<()>
where
KC: BytesEncode<'a>,
DC: BytesEncode<'a>,
{
self.dyndb.append::<T, KC, DC>(txn, key, data)
}
pub fn delete<'a, T>(&self, txn: &mut RwTxn<T>, key: &'a KC::EItem) -> Result<bool>
where
KC: BytesEncode<'a>,
{
self.dyndb.delete::<T, KC>(txn, key)
}
pub fn delete_range<'a, 'txn, T, R>(&self, txn: &'txn mut RwTxn<T>, range: &'a R) -> Result<usize>
where
KC: BytesEncode<'a> + BytesDecode<'txn>,
R: RangeBounds<KC::EItem>,
{
self.dyndb.delete_range::<T, KC, R>(txn, range)
}
pub fn clear<T>(&self, txn: &mut RwTxn<T>) -> Result<()> {
self.dyndb.clear(txn)
}
pub fn remap_types<KC2, DC2>(&self) -> Database<KC2, DC2> {
Database::new(self.dyndb.env_ident, self.dyndb.dbi)
}
pub fn remap_key_type<KC2>(&self) -> Database<KC2, DC> {
self.remap_types::<KC2, DC>()
}
pub fn remap_data_type<DC2>(&self) -> Database<KC, DC2> {
self.remap_types::<KC, DC2>()
}
pub fn lazily_decode_data(&self) -> Database<KC, LazyDecode<DC>> {
self.remap_types::<KC, LazyDecode<DC>>()
}
pub fn as_polymorph(&self) -> &PolyDatabase {
&self.dyndb
}
}
impl<KC, DC> Clone for Database<KC, DC> {
fn clone(&self) -> Database<KC, DC> {
Database {
dyndb: self.dyndb,
marker: marker::PhantomData,
}
}
}
impl<KC, DC> Copy for Database<KC, DC> {}