Skip to content

Commit 4ad2466

Browse files
committed
sqlite: use ValueRef instead of owned one
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 77e038f commit 4ad2466

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* pool: bump `async-wsocket` to `v0.6` ([Yuki Kishimoto])
4040
* database: not match event if `Filter::search` field is set ([Yuki Kishimoto])
4141
* sdk: rename `Proxy` and `ProxyTarget` to `Connection` and `ConnectionTarget` ([Yuki Kishimoto])
42+
* sqlite: use `ValueRef` instead of owned one ([Yuki Kishimoto])
4243
* cli: improve `sync` command ([Yuki Kishimoto])
4344
* cli: allow to specify relays in `open` command ([Yuki Kishimoto])
4445

crates/nostr-sqlite/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the MIT software license
44

55
use nostr_database::{flatbuffers, DatabaseError};
6+
use rusqlite::types::FromSqlError;
67
use thiserror::Error;
78
use tokio::task::JoinError;
89

@@ -20,6 +21,9 @@ pub enum Error {
2021
/// Migration error
2122
#[error(transparent)]
2223
Migration(#[from] MigrationError),
24+
/// From SQL error
25+
#[error(transparent)]
26+
FromSql(#[from] FromSqlError),
2327
/// Database error
2428
#[error(transparent)]
2529
Database(#[from] DatabaseError),

crates/nostr-sqlite/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl SQLiteDatabase {
7676
let mut rows = stmt.query([])?;
7777
let mut events = BTreeSet::new();
7878
while let Ok(Some(row)) = rows.next() {
79-
let buf: Vec<u8> = row.get(0)?;
80-
let raw = TempEvent::decode(&buf)?;
79+
let buf: &[u8] = row.get_ref(0)?.as_bytes()?;
80+
let raw = TempEvent::decode(buf)?;
8181
events.insert(raw);
8282
}
8383
Ok::<BTreeSet<TempEvent>, Error>(events)
@@ -269,8 +269,8 @@ impl NostrDatabase for SQLiteDatabase {
269269
let mut rows = stmt.query([event_id.to_hex()])?;
270270
let mut relays = HashSet::new();
271271
while let Ok(Some(row)) = rows.next() {
272-
let url: String = row.get(0)?;
273-
relays.insert(Url::parse(&url)?);
272+
let url: &str = row.get_ref(0)?.as_str()?;
273+
relays.insert(Url::parse(url)?);
274274
}
275275
Ok(Some(relays))
276276
})
@@ -287,12 +287,13 @@ impl NostrDatabase for SQLiteDatabase {
287287
let row = rows
288288
.next()?
289289
.ok_or_else(|| Error::NotFound("event".into()))?;
290-
let buf: Vec<u8> = row.get(0)?;
291-
Ok(Event::decode(&buf)?)
290+
let buf: &[u8] = row.get_ref(0)?.as_bytes()?;
291+
Ok(Event::decode(buf)?)
292292
})
293293
.await?
294294
}
295295

296+
#[inline]
296297
#[tracing::instrument(skip_all, level = "trace")]
297298
async fn count(&self, filters: Vec<Filter>) -> Result<usize, Self::Err> {
298299
Ok(self.indexes.count(filters).await)
@@ -303,21 +304,22 @@ impl NostrDatabase for SQLiteDatabase {
303304
let ids: Vec<EventId> = self.indexes.query(filters, order).await;
304305
self.pool
305306
.interact(move |conn| {
306-
let mut events = Vec::with_capacity(ids.len());
307+
let mut events: Vec<Event> = Vec::with_capacity(ids.len());
307308
let mut stmt =
308309
conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?;
309310
for id in ids.into_iter() {
310311
let mut rows = stmt.query([id.to_hex()])?;
311312
while let Ok(Some(row)) = rows.next() {
312-
let buf: Vec<u8> = row.get(0)?;
313-
events.push(Event::decode(&buf)?);
313+
let buf: &[u8] = row.get_ref(0)?.as_bytes()?;
314+
events.push(Event::decode(buf)?);
314315
}
315316
}
316317
Ok(events)
317318
})
318319
.await?
319320
}
320321

322+
#[inline]
321323
async fn event_ids_by_filters(
322324
&self,
323325
filters: Vec<Filter>,
@@ -326,6 +328,7 @@ impl NostrDatabase for SQLiteDatabase {
326328
Ok(self.indexes.query(filters, order).await)
327329
}
328330

331+
#[inline]
329332
async fn negentropy_items(
330333
&self,
331334
filter: Filter,

0 commit comments

Comments
 (0)