Skip to content

Commit

Permalink
Merge pull request #27 from mattn/fix-sort
Browse files Browse the repository at this point in the history
fix sort order, sort events by id after created_at
  • Loading branch information
mattn authored Jul 26, 2024
2 parents 7115e66 + 72bf1d9 commit 86c0f01
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion elasticsearch/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (ess *ElasticsearchStorage) QueryEvents(ctx context.Context, filter nostr.F

es.Search.WithBody(bytes.NewReader(dsl)),
es.Search.WithSize(limit),
es.Search.WithSort("event.created_at:desc"),
es.Search.WithSort("event.created_at:desc", "event.id"),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion mysql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (b MySQLBackend) queryEventsSql(filter nostr.Filter, doCount bool) (string,
id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+
strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?")
" ORDER BY created_at DESC, id LIMIT ?")
}

return query, params, nil
Expand Down
4 changes: 2 additions & 2 deletions opensearch/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (oss *OpensearchStorage) QueryEvents(ctx context.Context, filter nostr.Filt
Body: bytes.NewReader(dsl),
Params: opensearchapi.SearchParams{
Size: opensearchapi.ToPointer(limit),
Sort: []string{"event.created_at:desc"},
Sort: []string{"event.created_at:desc", "event.id"},
},
},
)
Expand All @@ -155,7 +155,7 @@ func (oss *OpensearchStorage) QueryEvents(ctx context.Context, filter nostr.Filt
}
if ch != nil {
close(ch)
ch = nil
ch = nil
}
}()

Expand Down
2 changes: 1 addition & 1 deletion postgresql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (stri
id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+
strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?")
" ORDER BY created_at DESC, id LIMIT ?")
}

return query, params, nil
Expand Down
12 changes: 6 additions & 6 deletions postgresql/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestQueryEventsSql(t *testing.T) {
name: "empty filter",
backend: defaultBackend,
filter: nostr.Filter{},
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC LIMIT $1",
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC, id LIMIT $1",
params: []any{100},
err: nil,
},
Expand All @@ -40,7 +40,7 @@ func TestQueryEventsSql(t *testing.T) {
filter: nostr.Filter{
Limit: 50,
},
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC LIMIT $1",
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC, id LIMIT $1",
params: []any{50},
err: nil,
},
Expand All @@ -50,7 +50,7 @@ func TestQueryEventsSql(t *testing.T) {
filter: nostr.Filter{
Limit: 2000,
},
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC LIMIT $1",
query: "SELECT id, pubkey, created_at, kind, tags, content, sig FROM event WHERE true ORDER BY created_at DESC, id LIMIT $1",
params: []any{100},
err: nil,
},
Expand All @@ -63,7 +63,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event
WHERE id IN ($1)
ORDER BY created_at DESC LIMIT $2`,
ORDER BY created_at DESC, id LIMIT $2`,
params: []any{"083ec57f36a7b39ab98a57bedab4f85355b2ee89e4b205bed58d7c3ef9edd294", 100},
err: nil,
},
Expand All @@ -76,7 +76,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event
WHERE kind IN($1,$2,$3)
ORDER BY created_at DESC LIMIT $4`,
ORDER BY created_at DESC, id LIMIT $4`,
params: []any{1, 2, 3, 100},
err: nil,
},
Expand All @@ -89,7 +89,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event
WHERE pubkey IN ($1)
ORDER BY created_at DESC LIMIT $2`,
ORDER BY created_at DESC, id LIMIT $2`,
params: []any{"7bdef7bdebb8721f77927d0e77c66059360fa62371fdf15f3add93923a613229", 100},
err: nil,
},
Expand Down
2 changes: 1 addition & 1 deletion postgresql/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (b *PostgresBackend) AfterSave(evt *nostr.Event) {
// delete all but the 100 most recent ones for each key
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND created_at < (
SELECT created_at FROM event WHERE pubkey = $1
ORDER BY created_at DESC OFFSET 100 LIMIT 1
ORDER BY created_at DESC, id OFFSET 100 LIMIT 1
)`, evt.PubKey, evt.Kind)
}

Expand Down
2 changes: 1 addition & 1 deletion sqlite3/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (b SQLite3Backend) queryEventsSql(filter nostr.Filter, doCount bool) (strin
id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+
strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?")
" ORDER BY created_at DESC, id LIMIT ?")
}

return query, params, nil
Expand Down

0 comments on commit 86c0f01

Please sign in to comment.