Merge pull request #27 from mattn/fix-sort
Some checks failed
build cli / make-release (push) Has been cancelled
test every commit / test (push) Has been cancelled
build cli / build-linux (push) Has been cancelled

fix sort order, sort events by id after created_at
This commit is contained in:
mattn
2024-07-26 18:02:03 +09:00
committed by GitHub
7 changed files with 13 additions and 13 deletions

View File

@@ -155,7 +155,7 @@ func (ess *ElasticsearchStorage) QueryEvents(ctx context.Context, filter nostr.F
es.Search.WithBody(bytes.NewReader(dsl)), es.Search.WithBody(bytes.NewReader(dsl)),
es.Search.WithSize(limit), es.Search.WithSize(limit),
es.Search.WithSort("event.created_at:desc"), es.Search.WithSort("event.created_at:desc", "event.id"),
) )
if err != nil { if err != nil {
log.Fatalf("Error getting response: %s", err) log.Fatalf("Error getting response: %s", err)

View File

@@ -164,7 +164,7 @@ func (b MySQLBackend) queryEventsSql(filter nostr.Filter, doCount bool) (string,
id, pubkey, created_at, kind, tags, content, sig id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+ FROM event WHERE `+
strings.Join(conditions, " AND ")+ strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?") " ORDER BY created_at DESC, id LIMIT ?")
} }
return query, params, nil return query, params, nil

View File

@@ -134,7 +134,7 @@ func (oss *OpensearchStorage) QueryEvents(ctx context.Context, filter nostr.Filt
Body: bytes.NewReader(dsl), Body: bytes.NewReader(dsl),
Params: opensearchapi.SearchParams{ Params: opensearchapi.SearchParams{
Size: opensearchapi.ToPointer(limit), Size: opensearchapi.ToPointer(limit),
Sort: []string{"event.created_at:desc"}, Sort: []string{"event.created_at:desc", "event.id"},
}, },
}, },
) )

View File

@@ -171,7 +171,7 @@ func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (stri
id, pubkey, created_at, kind, tags, content, sig id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+ FROM event WHERE `+
strings.Join(conditions, " AND ")+ strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?") " ORDER BY created_at DESC, id LIMIT ?")
} }
return query, params, nil return query, params, nil

View File

@@ -30,7 +30,7 @@ func TestQueryEventsSql(t *testing.T) {
name: "empty filter", name: "empty filter",
backend: defaultBackend, backend: defaultBackend,
filter: nostr.Filter{}, 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}, params: []any{100},
err: nil, err: nil,
}, },
@@ -40,7 +40,7 @@ func TestQueryEventsSql(t *testing.T) {
filter: nostr.Filter{ filter: nostr.Filter{
Limit: 50, 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}, params: []any{50},
err: nil, err: nil,
}, },
@@ -50,7 +50,7 @@ func TestQueryEventsSql(t *testing.T) {
filter: nostr.Filter{ filter: nostr.Filter{
Limit: 2000, 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}, params: []any{100},
err: nil, err: nil,
}, },
@@ -63,7 +63,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event FROM event
WHERE id IN ($1) WHERE id IN ($1)
ORDER BY created_at DESC LIMIT $2`, ORDER BY created_at DESC, id LIMIT $2`,
params: []any{"083ec57f36a7b39ab98a57bedab4f85355b2ee89e4b205bed58d7c3ef9edd294", 100}, params: []any{"083ec57f36a7b39ab98a57bedab4f85355b2ee89e4b205bed58d7c3ef9edd294", 100},
err: nil, err: nil,
}, },
@@ -76,7 +76,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event FROM event
WHERE kind IN($1,$2,$3) 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}, params: []any{1, 2, 3, 100},
err: nil, err: nil,
}, },
@@ -89,7 +89,7 @@ func TestQueryEventsSql(t *testing.T) {
query: `SELECT id, pubkey, created_at, kind, tags, content, sig query: `SELECT id, pubkey, created_at, kind, tags, content, sig
FROM event FROM event
WHERE pubkey IN ($1) WHERE pubkey IN ($1)
ORDER BY created_at DESC LIMIT $2`, ORDER BY created_at DESC, id LIMIT $2`,
params: []any{"7bdef7bdebb8721f77927d0e77c66059360fa62371fdf15f3add93923a613229", 100}, params: []any{"7bdef7bdebb8721f77927d0e77c66059360fa62371fdf15f3add93923a613229", 100},
err: nil, err: nil,
}, },

View File

@@ -38,7 +38,7 @@ func (b *PostgresBackend) AfterSave(evt *nostr.Event) {
// delete all but the 100 most recent ones for each key // 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 < ( b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND created_at < (
SELECT created_at FROM event WHERE pubkey = $1 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) )`, evt.PubKey, evt.Kind)
} }

View File

@@ -165,7 +165,7 @@ func (b SQLite3Backend) queryEventsSql(filter nostr.Filter, doCount bool) (strin
id, pubkey, created_at, kind, tags, content, sig id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE `+ FROM event WHERE `+
strings.Join(conditions, " AND ")+ strings.Join(conditions, " AND ")+
" ORDER BY created_at DESC LIMIT ?") " ORDER BY created_at DESC, id LIMIT ?")
} }
return query, params, nil return query, params, nil