From caf6ad872c90f321538aeb5eca68318f8b8c4b72 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 11 Jul 2023 19:35:30 +0100 Subject: [PATCH] Add function for batch inserting snapshots --- 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))