|
9 | 9 | #![warn(clippy::large_futures)]
|
10 | 10 | #![allow(clippy::mutable_key_type)] // TODO: remove when possible. Needed to suppress false positive for `BTreeSet<Event>`
|
11 | 11 |
|
| 12 | +use std::collections::HashMap; |
| 13 | +use std::fmt::Debug; |
12 | 14 | use std::sync::Arc;
|
13 | 15 |
|
14 | 16 | pub use nostr;
|
| 17 | +use nostr::prelude::*; |
15 | 18 |
|
16 | 19 | mod collections;
|
17 | 20 | mod error;
|
18 |
| -mod events; |
| 21 | +pub mod ext; |
19 | 22 | #[cfg(feature = "flatbuf")]
|
20 | 23 | pub mod flatbuffers;
|
| 24 | +mod helper; |
21 | 25 | pub mod memory;
|
22 | 26 | pub mod prelude;
|
23 | 27 | pub mod profile;
|
24 |
| -mod wipe; |
25 | 28 |
|
26 | 29 | pub use self::collections::events::Events;
|
27 | 30 | pub use self::error::DatabaseError;
|
28 |
| -pub use self::events::helper::{DatabaseEventResult, DatabaseHelper}; |
29 |
| -pub use self::events::{ |
30 |
| - DatabaseEventStatus, IntoNostrEventsDatabase, NostrEventsDatabase, NostrEventsDatabaseExt, |
31 |
| - RejectedReason, SaveEventStatus, |
32 |
| -}; |
33 | 31 | #[cfg(feature = "flatbuf")]
|
34 | 32 | pub use self::flatbuffers::{FlatBufferBuilder, FlatBufferDecode, FlatBufferEncode};
|
| 33 | +pub use self::helper::{DatabaseEventResult, DatabaseHelper}; |
35 | 34 | pub use self::memory::{MemoryDatabase, MemoryDatabaseOptions};
|
36 | 35 | pub use self::profile::Profile;
|
37 |
| -pub use self::wipe::NostrDatabaseWipe; |
| 36 | + |
| 37 | +/// NIP65 relays map |
| 38 | +pub type RelaysMap = HashMap<RelayUrl, Option<RelayMetadata>>; |
38 | 39 |
|
39 | 40 | /// Backend
|
40 | 41 | #[derive(Debug, Clone, PartialEq, Eq)]
|
@@ -62,6 +63,53 @@ impl Backend {
|
62 | 63 | }
|
63 | 64 | }
|
64 | 65 |
|
| 66 | +/// Database event status |
| 67 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 68 | +pub enum DatabaseEventStatus { |
| 69 | + /// The event is saved into the database |
| 70 | + Saved, |
| 71 | + /// The event is marked as deleted |
| 72 | + Deleted, |
| 73 | + /// The event doesn't exist |
| 74 | + NotExistent, |
| 75 | +} |
| 76 | + |
| 77 | +/// Reason why event wasn't stored into the database |
| 78 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 79 | +pub enum RejectedReason { |
| 80 | + /// Ephemeral events aren't expected to be stored |
| 81 | + Ephemeral, |
| 82 | + /// The event already exists |
| 83 | + Duplicate, |
| 84 | + /// The event was deleted |
| 85 | + Deleted, |
| 86 | + /// The event is expired |
| 87 | + Expired, |
| 88 | + /// The event was replaced |
| 89 | + Replaced, |
| 90 | + /// Attempt to delete a non-owned event |
| 91 | + InvalidDelete, |
| 92 | + /// Other reason |
| 93 | + Other, |
| 94 | +} |
| 95 | + |
| 96 | +/// Save event status |
| 97 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 98 | +pub enum SaveEventStatus { |
| 99 | + /// The event has been successfully saved |
| 100 | + Success, |
| 101 | + /// The event has been rejected |
| 102 | + Rejected(RejectedReason), |
| 103 | +} |
| 104 | + |
| 105 | +impl SaveEventStatus { |
| 106 | + /// Check if event is successfully saved |
| 107 | + #[inline] |
| 108 | + pub fn is_success(&self) -> bool { |
| 109 | + matches!(self, Self::Success) |
| 110 | + } |
| 111 | +} |
| 112 | + |
65 | 113 | #[doc(hidden)]
|
66 | 114 | pub trait IntoNostrDatabase {
|
67 | 115 | fn into_nostr_database(self) -> Arc<dyn NostrDatabase>;
|
@@ -91,10 +139,65 @@ where
|
91 | 139 | }
|
92 | 140 | }
|
93 | 141 |
|
94 |
| -/// Nostr Database |
95 |
| -pub trait NostrDatabase: NostrEventsDatabase + NostrDatabaseWipe { |
| 142 | +/// Nostr (Events) Database |
| 143 | +pub trait NostrDatabase: Debug + Send + Sync { |
96 | 144 | /// Name of the backend database used
|
97 | 145 | fn backend(&self) -> Backend;
|
| 146 | + |
| 147 | + /// Save [`Event`] into store |
| 148 | + /// |
| 149 | + /// **This method assumes that [`Event`] was already verified** |
| 150 | + fn save_event<'a>( |
| 151 | + &'a self, |
| 152 | + event: &'a Event, |
| 153 | + ) -> BoxedFuture<'a, Result<SaveEventStatus, DatabaseError>>; |
| 154 | + |
| 155 | + /// Check event status by ID |
| 156 | + /// |
| 157 | + /// Check if the event is saved, deleted or not existent. |
| 158 | + fn check_id<'a>( |
| 159 | + &'a self, |
| 160 | + event_id: &'a EventId, |
| 161 | + ) -> BoxedFuture<'a, Result<DatabaseEventStatus, DatabaseError>>; |
| 162 | + |
| 163 | + // TODO: rename to `check_coordinate`? |
| 164 | + /// Check if [`Coordinate`] has been deleted before a certain [`Timestamp`] |
| 165 | + fn has_coordinate_been_deleted<'a>( |
| 166 | + &'a self, |
| 167 | + coordinate: &'a CoordinateBorrow<'a>, |
| 168 | + timestamp: &'a Timestamp, |
| 169 | + ) -> BoxedFuture<'a, Result<bool, DatabaseError>>; |
| 170 | + |
| 171 | + /// Get [`Event`] by [`EventId`] |
| 172 | + fn event_by_id<'a>( |
| 173 | + &'a self, |
| 174 | + event_id: &'a EventId, |
| 175 | + ) -> BoxedFuture<'a, Result<Option<Event>, DatabaseError>>; |
| 176 | + |
| 177 | + /// Count the number of events found with [`Filter`]. |
| 178 | + /// |
| 179 | + /// Use `Filter::new()` or `Filter::default()` to count all events. |
| 180 | + fn count(&self, filter: Filter) -> BoxedFuture<Result<usize, DatabaseError>>; |
| 181 | + |
| 182 | + /// Query stored events. |
| 183 | + fn query(&self, filter: Filter) -> BoxedFuture<Result<Events, DatabaseError>>; |
| 184 | + |
| 185 | + /// Get `negentropy` items |
| 186 | + fn negentropy_items( |
| 187 | + &self, |
| 188 | + filter: Filter, |
| 189 | + ) -> BoxedFuture<Result<Vec<(EventId, Timestamp)>, DatabaseError>> { |
| 190 | + Box::pin(async move { |
| 191 | + let events: Events = self.query(filter).await?; |
| 192 | + Ok(events.into_iter().map(|e| (e.id, e.created_at)).collect()) |
| 193 | + }) |
| 194 | + } |
| 195 | + |
| 196 | + /// Delete all events that match the [Filter] |
| 197 | + fn delete(&self, filter: Filter) -> BoxedFuture<Result<(), DatabaseError>>; |
| 198 | + |
| 199 | + /// Wipe all data |
| 200 | + fn wipe(&self) -> BoxedFuture<Result<(), DatabaseError>>; |
98 | 201 | }
|
99 | 202 |
|
100 | 203 | #[cfg(test)]
|
|
0 commit comments