1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

//! SQLite Storage backend for Nostr SDK

#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]

use std::collections::{BTreeSet, HashSet};
use std::path::Path;
use std::sync::Arc;

pub extern crate nostr;
pub extern crate nostr_database as database;

use async_trait::async_trait;
use deadpool_sqlite::{Config, Object, Pool, Runtime};
use nostr::nips::nip01::Coordinate;
use nostr::{Event, EventId, Filter, Timestamp, Url};
use nostr_database::{
    Backend, DatabaseIndexes, DatabaseOptions, EventIndexResult, FlatBufferBuilder,
    FlatBufferDecode, FlatBufferEncode, NostrDatabase, RawEvent,
};
use rusqlite::config::DbConfig;
use tokio::sync::RwLock;

mod error;
mod migration;

pub use self::error::Error;
use self::migration::STARTUP_SQL;

/// SQLite Nostr Database
#[derive(Debug, Clone)]
pub struct SQLiteDatabase {
    db: Pool,
    indexes: DatabaseIndexes,
    fbb: Arc<RwLock<FlatBufferBuilder<'static>>>,
}

impl SQLiteDatabase {
    /// Open SQLite store
    pub async fn open<P>(path: P) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        let cfg = Config::new(path.as_ref());
        let pool = cfg.create_pool(Runtime::Tokio1)?;

        // Execute migrations
        let conn = pool.get().await?;
        migration::run(&conn).await?;

        let this = Self {
            db: pool,
            indexes: DatabaseIndexes::new(),
            fbb: Arc::new(RwLock::new(FlatBufferBuilder::with_capacity(70_000))),
        };

        // Build indexes
        this.build_indexes(&conn).await?;

        Ok(this)
    }

    async fn acquire(&self) -> Result<Object, Error> {
        Ok(self.db.get().await?)
    }

    #[tracing::instrument(skip_all)]
    async fn build_indexes(&self, conn: &Object) -> Result<(), Error> {
        let events = conn
            .interact(move |conn| {
                let mut stmt = conn.prepare_cached("SELECT event FROM events;")?;
                let mut rows = stmt.query([])?;
                let mut events = BTreeSet::new();
                while let Ok(Some(row)) = rows.next() {
                    let buf: Vec<u8> = row.get(0)?;
                    let raw = RawEvent::decode(&buf)?;
                    events.insert(raw);
                }
                Ok::<BTreeSet<RawEvent>, Error>(events)
            })
            .await??;

        // Build indexes
        let to_discard = self.indexes.bulk_index(events).await;

        // Discard events
        if !to_discard.is_empty() {
            let conn = self.acquire().await?;
            conn.interact(move |conn| {
                let delete_query = format!(
                    "DELETE FROM events WHERE {};",
                    to_discard
                        .iter()
                        .map(|id| format!("event_id = '{id}'"))
                        .collect::<Vec<_>>()
                        .join(" AND ")
                );
                conn.execute(&delete_query, [])
            })
            .await??;
        }
        Ok(())
    }
}

#[async_trait]
impl NostrDatabase for SQLiteDatabase {
    type Err = Error;

    fn backend(&self) -> Backend {
        Backend::SQLite
    }

    fn opts(&self) -> DatabaseOptions {
        DatabaseOptions::default()
    }

    #[tracing::instrument(skip_all, level = "trace")]
    async fn save_event(&self, event: &Event) -> Result<bool, Self::Err> {
        // Index event
        let EventIndexResult {
            to_store,
            to_discard,
        } = self.indexes.index_event(event).await;

        if !to_discard.is_empty() {
            let conn = self.acquire().await?;
            conn.interact(move |conn| {
                let delete_query = format!(
                    "DELETE FROM events WHERE {};",
                    to_discard
                        .iter()
                        .map(|id| format!("event_id = '{id}'"))
                        .collect::<Vec<_>>()
                        .join(" AND ")
                );
                conn.execute(&delete_query, [])
            })
            .await??;
        }

        if to_store {
            // Acquire FlatBuffers Builder
            let mut fbb = self.fbb.write().await;

            // Encode
            let event_id: EventId = event.id;
            let value: Vec<u8> = event.encode(&mut fbb).to_vec();

            // Save event
            let conn = self.acquire().await?;
            conn.interact(move |conn| {
                conn.execute(
                    "INSERT OR IGNORE INTO events (event_id, event) VALUES (?, ?);",
                    (event_id.to_hex(), value),
                )
            })
            .await??;

            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn has_event_already_been_saved(&self, event_id: &EventId) -> Result<bool, Self::Err> {
        if self.indexes.has_event_id_been_deleted(event_id).await {
            Ok(true)
        } else {
            let conn = self.acquire().await?;
            let event_id: String = event_id.to_hex();
            conn.interact(move |conn| {
                let mut stmt = conn.prepare_cached(
                    "SELECT EXISTS(SELECT 1 FROM events WHERE event_id = ? LIMIT 1);",
                )?;
                let mut rows = stmt.query([event_id])?;
                let exists: u8 = match rows.next()? {
                    Some(row) => row.get(0)?,
                    None => 0,
                };
                Ok(exists == 1)
            })
            .await?
        }
    }

    async fn has_event_already_been_seen(&self, event_id: &EventId) -> Result<bool, Self::Err> {
        let conn = self.acquire().await?;
        let event_id: String = event_id.to_hex();
        conn.interact(move |conn| {
            let mut stmt = conn.prepare_cached(
                "SELECT EXISTS(SELECT 1 FROM event_seen_by_relays WHERE event_id = ? LIMIT 1);",
            )?;
            let mut rows = stmt.query([event_id])?;
            let exists: u8 = match rows.next()? {
                Some(row) => row.get(0)?,
                None => 0,
            };
            Ok(exists == 1)
        })
        .await?
    }

    async fn has_event_id_been_deleted(&self, event_id: &EventId) -> Result<bool, Self::Err> {
        Ok(self.indexes.has_event_id_been_deleted(event_id).await)
    }

    async fn has_coordinate_been_deleted(
        &self,
        coordinate: &Coordinate,
        timestamp: Timestamp,
    ) -> Result<bool, Self::Err> {
        Ok(self
            .indexes
            .has_coordinate_been_deleted(coordinate, timestamp)
            .await)
    }

    async fn event_id_seen(&self, event_id: EventId, relay_url: Url) -> Result<(), Self::Err> {
        let conn = self.acquire().await?;
        conn.interact(move |conn| {
            conn.execute(
                "INSERT OR IGNORE INTO event_seen_by_relays (event_id, relay_url) VALUES (?, ?);",
                (event_id.to_hex(), relay_url.to_string()),
            )
        })
        .await??;
        Ok(())
    }

    async fn event_seen_on_relays(
        &self,
        event_id: EventId,
    ) -> Result<Option<HashSet<Url>>, Self::Err> {
        let conn = self.acquire().await?;
        conn.interact(move |conn| {
            let mut stmt = conn
                .prepare_cached("SELECT relay_url FROM event_seen_by_relays WHERE event_id = ?;")?;
            let mut rows = stmt.query([event_id.to_hex()])?;
            let mut relays = HashSet::new();
            while let Ok(Some(row)) = rows.next() {
                let url: String = row.get(0)?;
                relays.insert(Url::parse(&url)?);
            }
            Ok(Some(relays))
        })
        .await?
    }

    #[tracing::instrument(skip_all, level = "trace")]
    async fn event_by_id(&self, event_id: EventId) -> Result<Event, Self::Err> {
        let conn = self.acquire().await?;
        conn.interact(move |conn| {
            let mut stmt = conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?;
            let mut rows = stmt.query([event_id.to_hex()])?;
            let row = rows
                .next()?
                .ok_or_else(|| Error::NotFound("event".into()))?;
            let buf: Vec<u8> = row.get(0)?;
            Ok(Event::decode(&buf)?)
        })
        .await?
    }

    #[tracing::instrument(skip_all, level = "trace")]
    async fn count(&self, filters: Vec<Filter>) -> Result<usize, Self::Err> {
        Ok(self.indexes.count(filters).await)
    }

    #[tracing::instrument(skip_all, level = "trace")]
    async fn query(&self, filters: Vec<Filter>) -> Result<Vec<Event>, Self::Err> {
        let ids: Vec<EventId> = self.indexes.query(filters).await;
        let conn = self.acquire().await?;
        conn.interact(move |conn| {
            let mut stmt = conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?;
            let mut events = Vec::with_capacity(ids.len());
            for id in ids.into_iter() {
                let mut rows = stmt.query([id.to_hex()])?;
                while let Ok(Some(row)) = rows.next() {
                    let buf: Vec<u8> = row.get(0)?;
                    events.push(Event::decode(&buf)?);
                }
            }
            Ok(events)
        })
        .await?
    }

    async fn event_ids_by_filters(&self, filters: Vec<Filter>) -> Result<Vec<EventId>, Self::Err> {
        Ok(self.indexes.query(filters).await)
    }

    async fn negentropy_items(
        &self,
        filter: Filter,
    ) -> Result<Vec<(EventId, Timestamp)>, Self::Err> {
        let ids: Vec<EventId> = self.indexes.query(vec![filter]).await;
        let conn = self.acquire().await?;
        conn.interact(move |conn| {
            let mut stmt = conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?;
            let mut events = Vec::with_capacity(ids.len());
            for id in ids.into_iter() {
                let mut rows = stmt.query([id.to_hex()])?;
                while let Ok(Some(row)) = rows.next() {
                    let buf: Vec<u8> = row.get(0)?;
                    let event = Event::decode(&buf)?;
                    events.push((event.id, event.created_at));
                }
            }
            Ok(events)
        })
        .await?
    }

    async fn wipe(&self) -> Result<(), Self::Err> {
        let conn = self.acquire().await?;

        conn.interact(|conn| {
            // Reset DB
            conn.set_db_config(DbConfig::SQLITE_DBCONFIG_RESET_DATABASE, true)?;
            conn.execute("VACUUM;", [])?;
            conn.set_db_config(DbConfig::SQLITE_DBCONFIG_RESET_DATABASE, false)?;

            // Execute migrations
            conn.execute_batch(STARTUP_SQL)?;

            Ok::<(), Error>(())
        })
        .await??;

        migration::run(&conn).await?;

        Ok(())
    }
}