Skip to content

Commit ffa5e12

Browse files
committed
database: downcasting WIP
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 510f578 commit ffa5e12

File tree

9 files changed

+107
-68
lines changed

9 files changed

+107
-68
lines changed

crates/nostr-database/src/events/mod.rs

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,6 @@ impl SaveEventStatus {
6363
}
6464
}
6565

66-
#[doc(hidden)]
67-
pub trait IntoNostrEventsDatabase {
68-
fn into_database(self) -> Arc<dyn NostrEventsDatabase>;
69-
}
70-
71-
impl IntoNostrEventsDatabase for Arc<dyn NostrEventsDatabase> {
72-
fn into_database(self) -> Arc<dyn NostrEventsDatabase> {
73-
self
74-
}
75-
}
76-
77-
impl<T> IntoNostrEventsDatabase for T
78-
where
79-
T: NostrEventsDatabase + Sized + 'static,
80-
{
81-
fn into_database(self) -> Arc<dyn NostrEventsDatabase> {
82-
Arc::new(self)
83-
}
84-
}
85-
86-
impl<T> IntoNostrEventsDatabase for Arc<T>
87-
where
88-
T: NostrEventsDatabase + 'static,
89-
{
90-
fn into_database(self) -> Arc<dyn NostrEventsDatabase> {
91-
self
92-
}
93-
}
94-
9566
/// Nostr Events Database
9667
///
9768
/// Store for the nostr events.
@@ -149,6 +120,67 @@ pub trait NostrEventsDatabase: Any + Debug + Send + Sync {
149120
fn delete(&self, filter: Filter) -> BoxedFuture<Result<(), DatabaseError>>;
150121
}
151122

123+
impl<T> NostrEventsDatabase for Arc<T>
124+
where
125+
T: NostrEventsDatabase
126+
{
127+
#[inline]
128+
fn save_event<'a>(
129+
&'a self,
130+
event: &'a Event,
131+
) -> BoxedFuture<'a, Result<SaveEventStatus, DatabaseError>> {
132+
self.as_ref().save_event(event)
133+
}
134+
135+
#[inline]
136+
fn check_id<'a>(
137+
&'a self,
138+
event_id: &'a EventId,
139+
) -> BoxedFuture<'a, Result<DatabaseEventStatus, DatabaseError>> {
140+
self.as_ref().check_id(event_id)
141+
}
142+
143+
#[inline]
144+
fn has_coordinate_been_deleted<'a>(
145+
&'a self,
146+
coordinate: &'a CoordinateBorrow<'a>,
147+
timestamp: &'a Timestamp,
148+
) -> BoxedFuture<'a, Result<bool, DatabaseError>> {
149+
self.as_ref().has_coordinate_been_deleted(coordinate, timestamp)
150+
}
151+
152+
#[inline]
153+
fn event_by_id<'a>(
154+
&'a self,
155+
event_id: &'a EventId,
156+
) -> BoxedFuture<'a, Result<Option<Event>, DatabaseError>> {
157+
self.as_ref().event_by_id(event_id)
158+
}
159+
160+
#[inline]
161+
fn count(&self, filter: Filter) -> BoxedFuture<Result<usize, DatabaseError>> {
162+
self.as_ref().count(filter)
163+
}
164+
165+
#[inline]
166+
fn query(&self, filter: Filter) -> BoxedFuture<Result<Events, DatabaseError>> {
167+
self.as_ref().query(filter)
168+
}
169+
170+
#[inline]
171+
fn negentropy_items(
172+
&self,
173+
filter: Filter,
174+
) -> BoxedFuture<Result<Vec<(EventId, Timestamp)>, DatabaseError>> {
175+
self.as_ref().negentropy_items(filter)
176+
}
177+
178+
#[inline]
179+
fn delete(&self, filter: Filter) -> BoxedFuture<Result<(), DatabaseError>> {
180+
self.as_ref().delete(filter)
181+
}
182+
}
183+
152184
/// Nostr Event Store Extension
153185
pub trait NostrEventsDatabaseExt: NostrEventsDatabase {
154186
/// Get public key metadata

crates/nostr-database/src/lib.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use self::collections::events::Events;
2727
pub use self::error::DatabaseError;
2828
pub use self::events::helper::{DatabaseEventResult, DatabaseHelper};
2929
pub use self::events::{
30-
DatabaseEventStatus, IntoNostrEventsDatabase, NostrEventsDatabase, NostrEventsDatabaseExt,
30+
DatabaseEventStatus, NostrEventsDatabase, NostrEventsDatabaseExt,
3131
RejectedReason, SaveEventStatus,
3232
};
3333
#[cfg(feature = "flatbuf")]
@@ -62,41 +62,21 @@ impl Backend {
6262
}
6363
}
6464

65-
#[doc(hidden)]
66-
pub trait IntoNostrDatabase {
67-
fn into_nostr_database(self) -> Arc<dyn NostrDatabase>;
68-
}
69-
70-
impl IntoNostrDatabase for Arc<dyn NostrDatabase> {
71-
fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
72-
self
73-
}
74-
}
75-
76-
impl<T> IntoNostrDatabase for T
77-
where
78-
T: NostrDatabase + Sized + 'static,
79-
{
80-
fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
81-
Arc::new(self)
82-
}
83-
}
84-
85-
impl<T> IntoNostrDatabase for Arc<T>
86-
where
87-
T: NostrDatabase + 'static,
88-
{
89-
fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
90-
self
91-
}
92-
}
93-
9465
/// Nostr Database
9566
pub trait NostrDatabase: NostrEventsDatabase + NostrDatabaseWipe {
9667
/// Name of the backend database used
9768
fn backend(&self) -> Backend;
9869
}
9970

71+
impl<T> NostrDatabase for Arc<T>
72+
where
73+
T: NostrDatabase + 'static
74+
{
75+
fn backend(&self) -> Backend {
76+
self.as_ref().backend()
77+
}
78+
}
79+
10080
#[cfg(test)]
10181
mod tests {
10282
use super::*;

crates/nostr-database/src/wipe.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
//! Wipe trait
66
7+
use std::sync::Arc;
8+
79
use nostr::util::BoxedFuture;
810

911
use crate::error::DatabaseError;
@@ -13,3 +15,12 @@ pub trait NostrDatabaseWipe {
1315
/// Wipe all data
1416
fn wipe(&self) -> BoxedFuture<Result<(), DatabaseError>>;
1517
}
18+
19+
impl<T> NostrDatabaseWipe for Arc<T>
20+
where
21+
T: NostrDatabaseWipe,
22+
{
23+
fn wipe(&self) -> BoxedFuture<Result<(), DatabaseError>> {
24+
self.as_ref().wipe()
25+
}
26+
}

crates/nostr-lmdb/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![warn(rustdoc::bare_urls)]
1111
#![allow(clippy::mutable_key_type)]
1212

13+
use std::any::Any;
1314
use std::path::Path;
1415

1516
use nostr_database::prelude::*;
@@ -19,7 +20,7 @@ mod store;
1920
use self::store::Store;
2021

2122
/// LMDB Nostr Database
22-
#[derive(Debug)]
23+
#[derive(Debug, Clone)]
2324
pub struct NostrLMDB {
2425
db: Store,
2526
}
@@ -35,6 +36,15 @@ impl NostrLMDB {
3536
db: Store::open(path).map_err(DatabaseError::backend)?,
3637
})
3738
}
39+
40+
/// Downcast [`NostrDatabase`] to [`NostrLMDB`]
41+
pub fn downcast<T>(database: &T) -> Option<&Self>
42+
where
43+
T: NostrDatabase,
44+
{
45+
let any: &dyn Any = database as &dyn Any;
46+
any.downcast_ref()
47+
}
3848
}
3949

4050
impl NostrDatabase for NostrLMDB {

crates/nostr-lmdb/src/store/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use self::error::Error;
2020
use self::ingester::{Ingester, IngesterItem};
2121
use self::lmdb::Lmdb;
2222

23-
#[derive(Debug)]
23+
#[derive(Debug, Clone)]
2424
pub struct Store {
2525
db: Lmdb,
2626
ingester: Sender<IngesterItem>,

crates/nostr-relay-builder/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ impl RelayBuilder {
236236
#[inline]
237237
pub fn database<D>(mut self, database: D) -> Self
238238
where
239-
D: IntoNostrEventsDatabase,
239+
D: NostrEventsDatabase,
240240
{
241-
self.database = database.into_database();
241+
self.database = Arc::new(database);
242242
self
243243
}
244244

crates/nostr-relay-pool/src/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::{Arc, Mutex};
1212
use lru::LruCache;
1313
use nostr::prelude::IntoNostrSigner;
1414
use nostr::{EventId, NostrSigner};
15-
use nostr_database::{IntoNostrDatabase, MemoryDatabase, NostrDatabase};
15+
use nostr_database::{MemoryDatabase, NostrDatabase};
1616
use tokio::sync::RwLock;
1717

1818
use crate::policy::AdmitPolicy;
@@ -52,7 +52,7 @@ pub struct SharedState {
5252
impl Default for SharedState {
5353
fn default() -> Self {
5454
Self::new(
55-
MemoryDatabase::new().into_nostr_database(),
55+
Arc::new(MemoryDatabase::new()),
5656
Arc::new(DefaultWebsocketTransport),
5757
None,
5858
None,

crates/nostr-sdk/examples/lmdb.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Copyright (c) 2023-2025 Rust Nostr Developers
33
// Distributed under the MIT software license
44

5+
use std::any::Any;
6+
use std::sync::Arc;
57
use nostr_sdk::prelude::*;
68

79
#[tokio::main]
@@ -21,6 +23,10 @@ async fn main() -> Result<()> {
2123
client.add_relay("wss://nostr.oxtr.dev").await?;
2224

2325
client.connect().await;
26+
27+
let database = client.database();
28+
let lmdb = NostrLMDB::downcast(database).unwrap();
29+
2430

2531
// Publish a text note
2632
let builder = EventBuilder::text_note("Hello world");

crates/nostr-sdk/src/client/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::Arc;
88

99
use nostr::signer::{IntoNostrSigner, NostrSigner};
1010
use nostr_database::memory::MemoryDatabase;
11-
use nostr_database::{IntoNostrDatabase, NostrDatabase};
11+
use nostr_database::NostrDatabase;
1212
use nostr_relay_pool::policy::AdmitPolicy;
1313
use nostr_relay_pool::transport::websocket::{
1414
DefaultWebsocketTransport, IntoWebSocketTransport, WebSocketTransport,
@@ -95,9 +95,9 @@ impl ClientBuilder {
9595
#[inline]
9696
pub fn database<D>(mut self, database: D) -> Self
9797
where
98-
D: IntoNostrDatabase,
98+
D: NostrDatabase,
9999
{
100-
self.database = database.into_nostr_database();
100+
self.database = Arc::new(database);
101101
self
102102
}
103103

0 commit comments

Comments
 (0)