mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Fix pagination
This commit is contained in:
parent
566bfae939
commit
ba9822899a
|
|
@ -48,25 +48,37 @@ const insertRelationSQL = "" +
|
||||||
const deleteRelationSQL = "" +
|
const deleteRelationSQL = "" +
|
||||||
"DELETE FROM syncapi_relations WHERE room_id = $1 AND child_event_id = $2"
|
"DELETE FROM syncapi_relations WHERE room_id = $1 AND child_event_id = $2"
|
||||||
|
|
||||||
const selectRelationsInRangeSQL = "" +
|
const selectRelationsInRangeAscSQL = "" +
|
||||||
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" +
|
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" +
|
||||||
|
" ORDER BY id ASC LIMIT $5"
|
||||||
|
|
||||||
|
const selectRelationsInRangeDescSQL = "" +
|
||||||
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
|
" WHERE room_id = $1 AND event_id = $2 AND id >= $3 AND id < $4" +
|
||||||
" ORDER BY id DESC LIMIT $5"
|
" ORDER BY id DESC LIMIT $5"
|
||||||
|
|
||||||
const selectRelationsByTypeInRangeSQL = "" +
|
const selectRelationsByTypeInRangeAscSQL = "" +
|
||||||
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
|
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
|
||||||
|
" ORDER BY id ASC LIMIT $6"
|
||||||
|
|
||||||
|
const selectRelationsByTypeInRangeDescSQL = "" +
|
||||||
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
|
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id >= $4 AND id < $5" +
|
||||||
" ORDER BY id DESC LIMIT $6"
|
" ORDER BY id DESC LIMIT $6"
|
||||||
|
|
||||||
const selectMaxRelationIDSQL = "" +
|
const selectMaxRelationIDSQL = "" +
|
||||||
"SELECT MAX(id) FROM syncapi_relations"
|
"SELECT MAX(id) FROM syncapi_relations"
|
||||||
|
|
||||||
type relationsStatements struct {
|
type relationsStatements struct {
|
||||||
insertRelationStmt *sql.Stmt
|
insertRelationStmt *sql.Stmt
|
||||||
selectRelationsInRangeStmt *sql.Stmt
|
selectRelationsInRangeAscStmt *sql.Stmt
|
||||||
selectRelationsByTypeInRangeStmt *sql.Stmt
|
selectRelationsInRangeDescStmt *sql.Stmt
|
||||||
deleteRelationStmt *sql.Stmt
|
selectRelationsByTypeInRangeAscStmt *sql.Stmt
|
||||||
selectMaxRelationIDStmt *sql.Stmt
|
selectRelationsByTypeInRangeDescStmt *sql.Stmt
|
||||||
|
deleteRelationStmt *sql.Stmt
|
||||||
|
selectMaxRelationIDStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPostgresRelationsTable(db *sql.DB) (tables.Relations, error) {
|
func NewPostgresRelationsTable(db *sql.DB) (tables.Relations, error) {
|
||||||
|
|
@ -78,10 +90,16 @@ func NewPostgresRelationsTable(db *sql.DB) (tables.Relations, error) {
|
||||||
if s.insertRelationStmt, err = db.Prepare(insertRelationSQL); err != nil {
|
if s.insertRelationStmt, err = db.Prepare(insertRelationSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.selectRelationsInRangeStmt, err = db.Prepare(selectRelationsInRangeSQL); err != nil {
|
if s.selectRelationsInRangeAscStmt, err = db.Prepare(selectRelationsInRangeAscSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.selectRelationsByTypeInRangeStmt, err = db.Prepare(selectRelationsByTypeInRangeSQL); err != nil {
|
if s.selectRelationsInRangeDescStmt, err = db.Prepare(selectRelationsInRangeDescSQL); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.selectRelationsByTypeInRangeAscStmt, err = db.Prepare(selectRelationsByTypeInRangeAscSQL); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.selectRelationsByTypeInRangeDescStmt, err = db.Prepare(selectRelationsByTypeInRangeDescSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.deleteRelationStmt, err = db.Prepare(deleteRelationSQL); err != nil {
|
if s.deleteRelationStmt, err = db.Prepare(deleteRelationSQL); err != nil {
|
||||||
|
|
@ -121,13 +139,22 @@ func (s *relationsStatements) SelectRelationsInRange(
|
||||||
r types.Range, limit int,
|
r types.Range, limit int,
|
||||||
) (map[string][]types.RelationEntry, types.StreamPosition, error) {
|
) (map[string][]types.RelationEntry, types.StreamPosition, error) {
|
||||||
var lastPos types.StreamPosition
|
var lastPos types.StreamPosition
|
||||||
|
var stmt *sql.Stmt
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
var err error
|
var err error
|
||||||
if relType != "" {
|
if relType != "" {
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeStmt)
|
if r.Backwards {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeDescStmt)
|
||||||
|
} else {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeAscStmt)
|
||||||
|
}
|
||||||
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
|
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
|
||||||
} else {
|
} else {
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectRelationsInRangeStmt)
|
if r.Backwards {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt)
|
||||||
|
} else {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt)
|
||||||
|
}
|
||||||
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit)
|
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -605,13 +605,18 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
|
||||||
To: to,
|
To: to,
|
||||||
Backwards: backwards,
|
Backwards: backwards,
|
||||||
}
|
}
|
||||||
if r.To == 0 && !backwards {
|
if r.Backwards {
|
||||||
if r.To, err = d.MaxStreamPositionForRelations(ctx); err != nil {
|
if r.From == 0 {
|
||||||
return nil, "", "", fmt.Errorf("d.MaxStreamPositionForRelations: %w", err)
|
if r.From, err = d.MaxStreamPositionForRelations(ctx); err != nil {
|
||||||
|
return nil, "", "", fmt.Errorf("d.MaxStreamPositionForRelations: %w", err)
|
||||||
|
}
|
||||||
|
r.From++
|
||||||
}
|
}
|
||||||
} else if r.From == 0 && backwards {
|
} else {
|
||||||
if r.From, err = d.MaxStreamPositionForRelations(ctx); err != nil {
|
if r.To == 0 {
|
||||||
return nil, "", "", fmt.Errorf("d.MaxStreamPositionForRelations: %w", err)
|
if r.To, err = d.MaxStreamPositionForRelations(ctx); err != nil {
|
||||||
|
return nil, "", "", fmt.Errorf("d.MaxStreamPositionForRelations: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -641,13 +646,11 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
|
||||||
|
|
||||||
// Otherwise, let's try and work out what sensible prev_batch and next_batch values
|
// Otherwise, let's try and work out what sensible prev_batch and next_batch values
|
||||||
// could be.
|
// could be.
|
||||||
if from > 0 {
|
|
||||||
prevBatch = fmt.Sprintf("%d", r.Low()-1)
|
|
||||||
}
|
|
||||||
if len(entries) > limit {
|
if len(entries) > limit {
|
||||||
nextBatch = fmt.Sprintf("%d", entries[len(entries)-1].Position)
|
|
||||||
entries = entries[:len(entries)-1]
|
entries = entries[:len(entries)-1]
|
||||||
|
nextBatch = fmt.Sprintf("%d", entries[len(entries)-1].Position)
|
||||||
}
|
}
|
||||||
|
// TODO: set prevBatch? doesn't seem to affect the tests...
|
||||||
|
|
||||||
// Extract all of the event IDs from the relation entries so that we can pull the
|
// Extract all of the event IDs from the relation entries so that we can pull the
|
||||||
// events out of the database.
|
// events out of the database.
|
||||||
|
|
|
||||||
|
|
@ -46,26 +46,38 @@ const insertRelationSQL = "" +
|
||||||
const deleteRelationSQL = "" +
|
const deleteRelationSQL = "" +
|
||||||
"DELETE FROM syncapi_relations WHERE room_id = $1 AND child_event_id = $2"
|
"DELETE FROM syncapi_relations WHERE room_id = $1 AND child_event_id = $2"
|
||||||
|
|
||||||
const selectRelationsInRangeSQL = "" +
|
const selectRelationsInRangeAscSQL = "" +
|
||||||
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" +
|
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" +
|
||||||
|
" ORDER BY id ASC LIMIT $5"
|
||||||
|
|
||||||
|
const selectRelationsInRangeDescSQL = "" +
|
||||||
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
|
" WHERE room_id = $1 AND event_id = $2 AND id >= $3 AND id < $4" +
|
||||||
" ORDER BY id DESC LIMIT $5"
|
" ORDER BY id DESC LIMIT $5"
|
||||||
|
|
||||||
const selectRelationsByTypeInRangeSQL = "" +
|
const selectRelationsByTypeInRangeAscSQL = "" +
|
||||||
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
|
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
|
||||||
|
" ORDER BY id ASC LIMIT $6"
|
||||||
|
|
||||||
|
const selectRelationsByTypeInRangeDescSQL = "" +
|
||||||
|
"SELECT id, room_id, child_event_id, rel_type FROM syncapi_relations" +
|
||||||
|
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id >= $4 AND id < $5" +
|
||||||
" ORDER BY id DESC LIMIT $6"
|
" ORDER BY id DESC LIMIT $6"
|
||||||
|
|
||||||
const selectMaxRelationIDSQL = "" +
|
const selectMaxRelationIDSQL = "" +
|
||||||
"SELECT MAX(id) FROM syncapi_relations"
|
"SELECT MAX(id) FROM syncapi_relations"
|
||||||
|
|
||||||
type relationsStatements struct {
|
type relationsStatements struct {
|
||||||
streamIDStatements *StreamIDStatements
|
streamIDStatements *StreamIDStatements
|
||||||
insertRelationStmt *sql.Stmt
|
insertRelationStmt *sql.Stmt
|
||||||
selectRelationsInRangeStmt *sql.Stmt
|
selectRelationsInRangeAscStmt *sql.Stmt
|
||||||
selectRelationsByTypeInRangeStmt *sql.Stmt
|
selectRelationsInRangeDescStmt *sql.Stmt
|
||||||
deleteRelationStmt *sql.Stmt
|
selectRelationsByTypeInRangeAscStmt *sql.Stmt
|
||||||
selectMaxRelationIDStmt *sql.Stmt
|
selectRelationsByTypeInRangeDescStmt *sql.Stmt
|
||||||
|
deleteRelationStmt *sql.Stmt
|
||||||
|
selectMaxRelationIDStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSqliteRelationsTable(db *sql.DB, streamID *StreamIDStatements) (tables.Relations, error) {
|
func NewSqliteRelationsTable(db *sql.DB, streamID *StreamIDStatements) (tables.Relations, error) {
|
||||||
|
|
@ -79,10 +91,16 @@ func NewSqliteRelationsTable(db *sql.DB, streamID *StreamIDStatements) (tables.R
|
||||||
if s.insertRelationStmt, err = db.Prepare(insertRelationSQL); err != nil {
|
if s.insertRelationStmt, err = db.Prepare(insertRelationSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.selectRelationsInRangeStmt, err = db.Prepare(selectRelationsInRangeSQL); err != nil {
|
if s.selectRelationsInRangeAscStmt, err = db.Prepare(selectRelationsInRangeAscSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.selectRelationsByTypeInRangeStmt, err = db.Prepare(selectRelationsByTypeInRangeSQL); err != nil {
|
if s.selectRelationsInRangeDescStmt, err = db.Prepare(selectRelationsInRangeDescSQL); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.selectRelationsByTypeInRangeAscStmt, err = db.Prepare(selectRelationsByTypeInRangeAscSQL); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.selectRelationsByTypeInRangeDescStmt, err = db.Prepare(selectRelationsByTypeInRangeDescSQL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if s.deleteRelationStmt, err = db.Prepare(deleteRelationSQL); err != nil {
|
if s.deleteRelationStmt, err = db.Prepare(deleteRelationSQL); err != nil {
|
||||||
|
|
@ -125,13 +143,22 @@ func (s *relationsStatements) SelectRelationsInRange(
|
||||||
r types.Range, limit int,
|
r types.Range, limit int,
|
||||||
) (map[string][]types.RelationEntry, types.StreamPosition, error) {
|
) (map[string][]types.RelationEntry, types.StreamPosition, error) {
|
||||||
var lastPos types.StreamPosition
|
var lastPos types.StreamPosition
|
||||||
|
var stmt *sql.Stmt
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
var err error
|
var err error
|
||||||
if relType != "" {
|
if relType != "" {
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeStmt)
|
if r.Backwards {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeDescStmt)
|
||||||
|
} else {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeAscStmt)
|
||||||
|
}
|
||||||
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
|
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
|
||||||
} else {
|
} else {
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectRelationsInRangeStmt)
|
if r.Backwards {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt)
|
||||||
|
} else {
|
||||||
|
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt)
|
||||||
|
}
|
||||||
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit)
|
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue