//! Mock database use crate::{ common::{IterPairResult, PairResult, ValueOnlyResult}, cursor::{ DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, RangeWalker, ReverseWalker, Walker, }, database::Database, table::{DupSort, Table, TableImporter}, transaction::{DbTx, DbTxMut}, DatabaseError, }; use core::ops::Bound; use std::{collections::BTreeMap, ops::RangeBounds}; /// Mock database used for testing with inner `BTreeMap` structure // TODO #[derive(Clone, Debug, Default)] pub struct DatabaseMock { /// Main data. TODO (Make it table aware) pub data: BTreeMap, Vec>, } impl Database for DatabaseMock { type TX = TxMock; type TXMut = TxMock; fn tx(&self) -> Result { Ok(TxMock::default()) } fn tx_mut(&self) -> Result { Ok(TxMock::default()) } } /// Mock read only tx #[derive(Debug, Clone, Default)] pub struct TxMock { /// Table representation _table: BTreeMap, Vec>, } impl DbTx for TxMock { type Cursor = CursorMock; type DupCursor = CursorMock; fn get(&self, _key: T::Key) -> Result, DatabaseError> { Ok(None) } fn commit(self) -> Result { Ok(true) } fn abort(self) {} fn cursor_read(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } fn cursor_dup_read(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } fn entries(&self) -> Result { Ok(self._table.len()) } fn disable_long_read_transaction_safety(&mut self) {} } impl DbTxMut for TxMock { type CursorMut = CursorMock; type DupCursorMut = CursorMock; fn put(&self, _key: T::Key, _value: T::Value) -> Result<(), DatabaseError> { Ok(()) } fn delete( &self, _key: T::Key, _value: Option, ) -> Result { Ok(true) } fn clear(&self) -> Result<(), DatabaseError> { Ok(()) } fn cursor_write(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } fn cursor_dup_write(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } } impl TableImporter for TxMock {} /// Cursor that iterates over table #[derive(Debug)] pub struct CursorMock { _cursor: u32, } impl DbCursorRO for CursorMock { fn first(&mut self) -> PairResult { Ok(None) } fn seek_exact(&mut self, _key: T::Key) -> PairResult { Ok(None) } fn seek(&mut self, _key: T::Key) -> PairResult { Ok(None) } fn next(&mut self) -> PairResult { Ok(None) } fn prev(&mut self) -> PairResult { Ok(None) } fn last(&mut self) -> PairResult { Ok(None) } fn current(&mut self) -> PairResult { Ok(None) } fn walk(&mut self, start_key: Option) -> Result, DatabaseError> { let start: IterPairResult = match start_key { Some(key) => >::seek(self, key).transpose(), None => >::first(self).transpose(), }; Ok(Walker::new(self, start)) } fn walk_range( &mut self, range: impl RangeBounds, ) -> Result, DatabaseError> { let start_key = match range.start_bound() { Bound::Included(key) | Bound::Excluded(key) => Some((*key).clone()), Bound::Unbounded => None, }; let end_key = match range.end_bound() { Bound::Included(key) | Bound::Excluded(key) => Bound::Included((*key).clone()), Bound::Unbounded => Bound::Unbounded, }; let start: IterPairResult = match start_key { Some(key) => >::seek(self, key).transpose(), None => >::first(self).transpose(), }; Ok(RangeWalker::new(self, start, end_key)) } fn walk_back( &mut self, start_key: Option, ) -> Result, DatabaseError> { let start: IterPairResult = match start_key { Some(key) => >::seek(self, key).transpose(), None => >::last(self).transpose(), }; Ok(ReverseWalker::new(self, start)) } } impl DbDupCursorRO for CursorMock { fn next_dup(&mut self) -> PairResult { Ok(None) } fn next_no_dup(&mut self) -> PairResult { Ok(None) } fn next_dup_val(&mut self) -> ValueOnlyResult { Ok(None) } fn seek_by_key_subkey( &mut self, _key: ::Key, _subkey: ::SubKey, ) -> ValueOnlyResult { Ok(None) } fn walk_dup( &mut self, _key: Option<::Key>, _subkey: Option<::SubKey>, ) -> Result, DatabaseError> { Ok(DupWalker { cursor: self, start: None }) } } impl DbCursorRW for CursorMock { fn upsert( &mut self, _key: ::Key, _value: ::Value, ) -> Result<(), DatabaseError> { Ok(()) } fn insert( &mut self, _key: ::Key, _value: ::Value, ) -> Result<(), DatabaseError> { Ok(()) } fn append( &mut self, _key: ::Key, _value: ::Value, ) -> Result<(), DatabaseError> { Ok(()) } fn delete_current(&mut self) -> Result<(), DatabaseError> { Ok(()) } } impl DbDupCursorRW for CursorMock { fn delete_current_duplicates(&mut self) -> Result<(), DatabaseError> { Ok(()) } fn append_dup(&mut self, _key: ::Key, _value: ::Value) -> Result<(), DatabaseError> { Ok(()) } }