|
@@ -1,7 +1,10 @@
|
|
//! Rocks DB implementation of the storage layer
|
|
//! Rocks DB implementation of the storage layer
|
|
-use crate::{event_filter::EventFilter, RocksDb};
|
|
|
|
-use futures::{Future, FutureExt, Stream};
|
|
|
|
-use nostr_rs_storage_base::{Error, Storage};
|
|
|
|
|
|
+use crate::RocksDb;
|
|
|
|
+use futures::{Future, Stream};
|
|
|
|
+use nostr_rs_storage_base::{
|
|
|
|
+ cursor::{check_future_call, FutureValue},
|
|
|
|
+ Error, EventFilter, Storage,
|
|
|
|
+};
|
|
use nostr_rs_types::types::Event;
|
|
use nostr_rs_types::types::Event;
|
|
use rocksdb::{BoundColumnFamily, DBIteratorWithThreadMode, DB};
|
|
use rocksdb::{BoundColumnFamily, DBIteratorWithThreadMode, DB};
|
|
use std::{
|
|
use std::{
|
|
@@ -63,37 +66,6 @@ impl<'a> Cursor<'a> {
|
|
self.current_prefix = prefix;
|
|
self.current_prefix = prefix;
|
|
Some(())
|
|
Some(())
|
|
}
|
|
}
|
|
-
|
|
|
|
- fn handle_future_call(&mut self, cx: &mut Context<'_>) -> FutureStatus {
|
|
|
|
- if let Some(mut future_event) = self.future_event.take() {
|
|
|
|
- match future_event.poll_unpin(cx) {
|
|
|
|
- Poll::Ready(Ok(None)) => FutureStatus::FoundNotMatch,
|
|
|
|
- Poll::Ready(Ok(Some(event))) => {
|
|
|
|
- // event is ready, apply the neccesary filters
|
|
|
|
- if let Some(filter) = &self.filter {
|
|
|
|
- if filter.check_event(&event) {
|
|
|
|
- FutureStatus::Found(Ok(event))
|
|
|
|
- } else {
|
|
|
|
- FutureStatus::FoundNotMatch
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- FutureStatus::Found(Ok(event))
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- Poll::Ready(Err(error)) => return FutureStatus::Found(Err(error)),
|
|
|
|
- Poll::Pending => FutureStatus::Pending,
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- FutureStatus::Ended
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-enum FutureStatus {
|
|
|
|
- Found(Result<Event, Error>),
|
|
|
|
- Pending,
|
|
|
|
- Ended,
|
|
|
|
- FoundNotMatch,
|
|
|
|
}
|
|
}
|
|
|
|
|
|
impl<'a> Stream for Cursor<'a> {
|
|
impl<'a> Stream for Cursor<'a> {
|
|
@@ -111,10 +83,10 @@ impl<'a> Stream for Cursor<'a> {
|
|
let this = Pin::into_inner(self);
|
|
let this = Pin::into_inner(self);
|
|
let db = this.db;
|
|
let db = this.db;
|
|
|
|
|
|
- match this.handle_future_call(cx) {
|
|
|
|
- FutureStatus::Found(event) => return Poll::Ready(Some(event)),
|
|
|
|
- FutureStatus::Pending => return Poll::Pending,
|
|
|
|
- FutureStatus::FoundNotMatch | FutureStatus::Ended => {}
|
|
|
|
|
|
+ match check_future_call(&mut this.future_event, &this.filter, cx) {
|
|
|
|
+ FutureValue::Found(event) => return Poll::Ready(Some(event)),
|
|
|
|
+ FutureValue::Pending => return Poll::Pending,
|
|
|
|
+ FutureValue::FoundNotMatch | FutureValue::Ended => {}
|
|
}
|
|
}
|
|
|
|
|
|
loop {
|
|
loop {
|
|
@@ -134,10 +106,10 @@ impl<'a> Stream for Cursor<'a> {
|
|
// primary index to prefetch events that may satisfy the query
|
|
// primary index to prefetch events that may satisfy the query
|
|
return if let Some(prefix) = this.prefixes.pop_front() {
|
|
return if let Some(prefix) = this.prefixes.pop_front() {
|
|
this.future_event = Some(db.get_event(prefix));
|
|
this.future_event = Some(db.get_event(prefix));
|
|
- match this.handle_future_call(cx) {
|
|
|
|
- FutureStatus::Found(event) => Poll::Ready(Some(event)),
|
|
|
|
- FutureStatus::Pending => Poll::Pending,
|
|
|
|
- FutureStatus::FoundNotMatch | FutureStatus::Ended => continue,
|
|
|
|
|
|
+ match check_future_call(&mut this.future_event, &this.filter, cx) {
|
|
|
|
+ FutureValue::Found(event) => Poll::Ready(Some(event)),
|
|
|
|
+ FutureValue::Pending => Poll::Pending,
|
|
|
|
+ FutureValue::FoundNotMatch | FutureValue::Ended => continue,
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
Poll::Ready(None)
|
|
Poll::Ready(None)
|
|
@@ -153,10 +125,10 @@ impl<'a> Stream for Cursor<'a> {
|
|
}
|
|
}
|
|
|
|
|
|
this.future_event = Some(db.get_event(value));
|
|
this.future_event = Some(db.get_event(value));
|
|
- match this.handle_future_call(cx) {
|
|
|
|
- FutureStatus::Found(event) => Poll::Ready(Some(event)),
|
|
|
|
- FutureStatus::Pending => Poll::Pending,
|
|
|
|
- FutureStatus::FoundNotMatch | FutureStatus::Ended => continue,
|
|
|
|
|
|
+ match check_future_call(&mut this.future_event, &this.filter, cx) {
|
|
|
|
+ FutureValue::Found(event) => Poll::Ready(Some(event)),
|
|
|
|
+ FutureValue::Pending => Poll::Pending,
|
|
|
|
+ FutureValue::FoundNotMatch | FutureValue::Ended => continue,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Some(Err(err)) => Poll::Ready(Some(Err(Error::Internal(err.to_string())))),
|
|
Some(Err(err)) => Poll::Ready(Some(Err(Error::Internal(err.to_string())))),
|