Skip to content

Commit

Permalink
Add function for batch inserting snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Jul 12, 2023
1 parent 0abc8e3 commit caf6ad8
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 caf6ad8

Please sign in to comment.