Skip to content

Commit

Permalink
sqlite: fix wrong event order when querying
Browse files Browse the repository at this point in the history
Ref #454 (comment)

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jun 5, 2024
1 parent 5d2e654 commit 6c41bd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* nostr: fix re-serialization of events that contains unknown keys during deserialization ([Yuki Kishimoto])
* pool: fix relay doesn't auto reconnect in certain cases ([Yuki Kishimoto])
* sqlite: fix SQLite database panics when used outside the client context in bindings ([Yuki Kishimoto])
* sqlite: fix wrong event order when querying ([Yuki Kishimoto])

### Removed

Expand Down
16 changes: 5 additions & 11 deletions crates/nostr-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,16 @@ impl NostrDatabase for SQLiteDatabase {
Ok(self.indexes.count(filters).await)
}

#[tracing::instrument(skip_all, level = "trace")]
#[tracing::instrument(skip_all)]
async fn query(&self, filters: Vec<Filter>, order: Order) -> Result<Vec<Event>, Self::Err> {
let ids: Vec<EventId> = self.indexes.query(filters, order).await;
self.pool
.interact(move |conn| {
let mut events = Vec::with_capacity(ids.len());
for chunk in ids.chunks(BATCH_SIZE) {
let mut stmt = conn.prepare_cached(&format!(
"SELECT event FROM events WHERE {};",
chunk
.iter()
.map(|id| format!("event_id = '{id}'"))
.collect::<Vec<_>>()
.join(" OR ")
))?;
let mut rows = stmt.query([])?;
let mut stmt =
conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?;
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)?);
Expand Down

0 comments on commit 6c41bd6

Please sign in to comment.