Merge pull request #27 from mattn/fix-sort
fix sort order, sort events by id after created_at
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"},
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -155,7 +155,7 @@ func (oss *OpensearchStorage) QueryEvents(ctx context.Context, filter nostr.Filt
|
||||
}
|
||||
if ch != nil {
|
||||
close(ch)
|
||||
ch = nil
|
||||
ch = nil
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
@@ -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,
|
||||
},
|
||||
@@ -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,
|
||||
},
|
||||
@@ -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,
|
||||
},
|
||||
@@ -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,
|
||||
},
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user