use crate::error::{Error, Result};
use crate::serial::*;
use ofilter::SyncStream;
use rocksdb::DBIteratorWithThreadMode;
use rocksdb::DB;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt;
use std::marker::PhantomData;
pub struct Iter<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for Iter<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = Result<(K, V)>;
fn next(&mut self) -> Option<Result<(K, V)>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let (kbuf, vbuf) = item;
let kvec: &Vec<u8> = &kbuf.to_vec();
if !self.filter.check(kvec) {
continue;
}
return match deserialize_key(kbuf) {
Ok(k) => match deserialize_value(vbuf) {
Ok(v) => Some(Ok((k, v))),
Err(e) => Some(Err(e)),
},
Err(e) => Some(Err(e)),
};
}
Err(e) => return Some(Err(Error::from(e))),
}
} else {
break;
}
}
None
}
}
pub struct Keys<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for Keys<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> Iterator for Keys<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = Result<K>;
fn next(&mut self) -> Option<Result<K>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let kvec: &Vec<u8> = &item.0.to_vec();
if !self.filter.check(kvec) {
continue;
}
return Some(deserialize_key(item.0));
}
Err(e) => return Some(Err(Error::from(e))),
}
} else {
break;
}
}
None
}
}
pub struct Values<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for Values<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> Iterator for Values<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = Result<V>;
fn next(&mut self) -> Option<Result<V>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let (kbuf, vbuf) = item;
let kvec: &Vec<u8> = &kbuf.to_vec();
if !self.filter.check(kvec) {
continue;
}
return Some(deserialize_value(vbuf));
}
Err(e) => return Some(Err(Error::from(e))),
}
} else {
break;
}
}
None
}
}
pub struct Export<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for Export<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> Export<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub fn try_next(&mut self) -> Result<Option<(K, V)>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let (kbuf, vbuf) = item;
let kvec: &Vec<u8> = &kbuf.to_vec();
if !self.filter.check(kvec) {
continue;
}
return match deserialize_key(kbuf) {
Ok(k) => match deserialize_value(vbuf) {
Ok(v) => Ok(Some((k, v))),
Err(e) => Err(e),
},
Err(e) => Err(e),
};
}
Err(e) => return Err(Error::from(e)),
}
} else {
break;
}
}
Ok(None)
}
}
impl<'a, K, V> Iterator for Export<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
match self.try_next() {
Ok(item) => item,
Err(e) => panic!("export error: {}", e),
}
}
}
pub struct ExportKeys<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for ExportKeys<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> ExportKeys<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub fn try_next(&mut self) -> Result<Option<K>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let kvec: &Vec<u8> = &item.0.to_vec();
if !self.filter.check(kvec) {
continue;
}
return match deserialize_key(item.0) {
Ok(k) => Ok(Some(k)),
Err(e) => Err(e),
};
}
Err(e) => return Err(Error::from(e)),
}
} else {
break;
}
}
Ok(None)
}
}
impl<'a, K, V> Iterator for ExportKeys<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = K;
fn next(&mut self) -> Option<K> {
match self.try_next() {
Ok(item) => item,
Err(e) => panic!("export error: {}", e),
}
}
}
pub struct ExportValues<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub(crate) inner: DBIteratorWithThreadMode<'a, DB>,
pub(crate) filter: SyncStream<Vec<u8>>,
pub(crate) done: usize,
pub(crate) phantom_data: PhantomData<(K, V)>,
}
impl<K, V> fmt::Display for ExportValues<'_, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/?", self.done)
}
}
impl<'a, K, V> ExportValues<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
pub fn try_next(&mut self) -> Result<Option<V>> {
loop {
if let Some(res) = self.inner.next() {
match res {
Ok(item) => {
let (kbuf, vbuf) = item;
let kvec: &Vec<u8> = &kbuf.to_vec();
if !self.filter.check(kvec) {
continue;
};
return match deserialize_value(vbuf) {
Ok(v) => Ok(Some(v)),
Err(e) => Err(e),
};
}
Err(e) => return Err(Error::from(e)),
}
} else {
break;
}
}
Ok(None)
}
}
impl<'a, K, V> Iterator for ExportValues<'a, K, V>
where
K: Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
{
type Item = V;
fn next(&mut self) -> Option<V> {
match self.try_next() {
Ok(item) => item,
Err(e) => panic!("export error: {}", e),
}
}
}
pub struct IterCfNames<'a> {
pub(crate) idx: isize,
pub(crate) other_cfs: &'a Vec<String>,
}
impl fmt::Display for IterCfNames<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{}", self.idx + 1, self.other_cfs.len() + 1)
}
}
impl<'a> Iterator for IterCfNames<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if self.idx < 0 {
self.idx = 0;
return Some("");
}
if self.idx as usize >= self.other_cfs.len() {
return None;
}
let cf_name = self.other_cfs[self.idx as usize].as_str();
self.idx += 1;
Some(cf_name)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.other_cfs.len() + 1 - ((self.idx + 1) as usize);
(remaining, Some(remaining))
}
}
impl<'a> ExactSizeIterator for IterCfNames<'a> {}