From 37ff4bc53ab2b8eb11098abd09672239bda7b07e Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 11 Jul 2023 19:35:30 +0100 Subject: [PATCH] Batch snapshot inserts --- state/snapshot_table.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/state/snapshot_table.go b/state/snapshot_table.go index f5096082..0a0925c3 100644 --- a/state/snapshot_table.go +++ b/state/snapshot_table.go @@ -2,6 +2,7 @@ package state import ( "fmt" + "github.com/matrix-org/sliding-sync/sqlutil" "github.com/jmoiron/sqlx" "github.com/lib/pq" @@ -83,6 +84,21 @@ func (s *SnapshotTable) Insert(txn *sqlx.Tx, row *SnapshotRow) error { return err } +// BulkInsert multiple rows. The caller MUST provide SnapshotIDs for these rows that +// have been reserved from the DB using ReserveSnapshotIDs. +func (s *SnapshotTable) BulkInsert(txn *sqlx.Tx, rows []SnapshotRow) error { + chunks := sqlutil.Chunkify2[SnapshotRow](4, MaxPostgresParameters, rows) + for _, chunk := range chunks { + _, err := txn.NamedExec(` + INSERT INTO syncv3_snapshots (snapshot_id, room_id, events, membership_events) + VALUES (:snapshot_id, :room_id, :events, :membership_events)`, chunk) + if err != nil { + return err + } + } + return nil +} + // Delete the snapshot IDs given func (s *SnapshotTable) Delete(txn *sqlx.Tx, snapshotIDs []int64) error { query, args, err := sqlx.In(`DELETE FROM syncv3_snapshots WHERE snapshot_id = ANY(?)`, pq.Int64Array(snapshotIDs))