Skip to content

Commit

Permalink
Batch snapshot inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Jul 11, 2023
1 parent b60352a commit 37ff4bc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions state/snapshot_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"fmt"
"github.com/matrix-org/sliding-sync/sqlutil"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 37ff4bc

Please sign in to comment.