2020-01-03 08:07:05 -06:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 17:40:52 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-03 08:07:05 -06:00
|
|
|
package postgres
|
2017-02-15 08:43:19 -06:00
|
|
|
|
|
|
|
import (
|
2017-09-13 10:30:19 -05:00
|
|
|
"context"
|
2017-02-15 08:43:19 -06:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-04-26 07:25:57 -05:00
|
|
|
"sort"
|
2017-08-07 05:51:46 -05:00
|
|
|
|
2017-02-15 08:43:19 -06:00
|
|
|
"github.com/lib/pq"
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-05-24 05:14:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/postgres/deltas"
|
2020-05-26 10:45:28 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-02-15 08:43:19 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const eventsSchema = `
|
|
|
|
-- The events table holds metadata for each event, the actual JSON is stored
|
|
|
|
-- separately to keep the size of the rows small.
|
2017-08-07 05:51:46 -05:00
|
|
|
CREATE SEQUENCE IF NOT EXISTS roomserver_event_nid_seq;
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_events (
|
2017-02-15 08:43:19 -06:00
|
|
|
-- Local numeric ID for the event.
|
2017-08-07 05:51:46 -05:00
|
|
|
event_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_event_nid_seq'),
|
2017-02-15 08:43:19 -06:00
|
|
|
-- Local numeric ID for the room the event is in.
|
|
|
|
-- This is never 0.
|
|
|
|
room_nid BIGINT NOT NULL,
|
|
|
|
-- Local numeric ID for the type of the event.
|
|
|
|
-- This is never 0.
|
|
|
|
event_type_nid BIGINT NOT NULL,
|
|
|
|
-- Local numeric ID for the state_key of the event
|
|
|
|
-- This is 0 if the event is not a state event.
|
|
|
|
event_state_key_nid BIGINT NOT NULL,
|
2017-02-27 05:25:35 -06:00
|
|
|
-- Whether the event has been written to the output log.
|
|
|
|
sent_to_output BOOLEAN NOT NULL DEFAULT FALSE,
|
2017-02-15 08:43:19 -06:00
|
|
|
-- Local numeric ID for the state at the event.
|
|
|
|
-- This is 0 if we don't know the state at the event.
|
|
|
|
-- If the state is not 0 then this event is part of the contiguous
|
|
|
|
-- part of the event graph
|
|
|
|
-- Since many different events can have the same state we store the
|
|
|
|
-- state into a separate state table and refer to it by numeric ID.
|
2017-06-27 09:28:44 -05:00
|
|
|
state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
|
|
|
|
-- Depth of the event in the event graph.
|
|
|
|
depth BIGINT NOT NULL,
|
2017-02-15 08:43:19 -06:00
|
|
|
-- The textual event id.
|
|
|
|
-- Used to lookup the numeric ID when processing requests.
|
|
|
|
-- Needed for state resolution.
|
|
|
|
-- An event may only appear in this table once.
|
2017-08-07 05:51:46 -05:00
|
|
|
event_id TEXT NOT NULL CONSTRAINT roomserver_event_id_unique UNIQUE,
|
2017-02-15 08:43:19 -06:00
|
|
|
-- A list of numeric IDs for events that can authenticate this event.
|
2020-09-16 07:00:52 -05:00
|
|
|
auth_event_nids BIGINT[] NOT NULL,
|
|
|
|
is_rejected BOOLEAN NOT NULL DEFAULT FALSE
|
2017-02-15 08:43:19 -06:00
|
|
|
);
|
2023-02-07 07:31:23 -06:00
|
|
|
|
|
|
|
-- Create an index which helps in resolving membership events (event_type_nid = 5) - (used for history visibility)
|
|
|
|
CREATE INDEX IF NOT EXISTS roomserver_events_memberships_idx ON roomserver_events (room_nid, event_state_key_nid) WHERE (event_type_nid = 5);
|
2017-02-15 08:43:19 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertEventSQL = "" +
|
2023-05-24 05:14:42 -05:00
|
|
|
"INSERT INTO roomserver_events AS e (room_nid, event_type_nid, event_state_key_nid, event_id, auth_event_nids, depth, is_rejected)" +
|
|
|
|
" VALUES ($1, $2, $3, $4, $5, $6, $7)" +
|
2022-02-08 07:45:48 -06:00
|
|
|
" ON CONFLICT ON CONSTRAINT roomserver_event_id_unique DO UPDATE" +
|
2023-05-24 05:14:42 -05:00
|
|
|
" SET is_rejected = $7 WHERE e.event_id = $4 AND e.is_rejected = TRUE" +
|
2017-02-15 08:43:19 -06:00
|
|
|
" RETURNING event_nid, state_snapshot_nid"
|
|
|
|
|
2017-02-22 10:51:10 -06:00
|
|
|
const selectEventSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT event_nid, state_snapshot_nid FROM roomserver_events WHERE event_id = $1"
|
2017-02-22 10:51:10 -06:00
|
|
|
|
2022-11-01 10:07:17 -05:00
|
|
|
const bulkSelectSnapshotsForEventIDsSQL = "" +
|
|
|
|
"SELECT event_id, state_snapshot_nid FROM roomserver_events WHERE event_id = ANY($1)"
|
|
|
|
|
2017-02-15 08:43:19 -06:00
|
|
|
// Bulk lookup of events by string ID.
|
|
|
|
// Sort by the numeric IDs for event type and state key.
|
|
|
|
// This means we can use binary search to lookup entries by type and state key.
|
|
|
|
const bulkSelectStateEventByIDSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
|
2017-02-15 08:43:19 -06:00
|
|
|
" WHERE event_id = ANY($1)" +
|
|
|
|
" ORDER BY event_type_nid, event_state_key_nid ASC"
|
|
|
|
|
2022-08-18 11:06:13 -05:00
|
|
|
// Bulk lookup of events by string ID that aren't excluded.
|
|
|
|
// Sort by the numeric IDs for event type and state key.
|
|
|
|
// This means we can use binary search to lookup entries by type and state key.
|
|
|
|
const bulkSelectStateEventByIDExcludingRejectedSQL = "" +
|
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
|
|
|
|
" WHERE event_id = ANY($1) AND is_rejected = FALSE" +
|
|
|
|
" ORDER BY event_type_nid, event_state_key_nid ASC"
|
|
|
|
|
2021-04-26 07:25:57 -05:00
|
|
|
// Bulk look up of events by event NID, optionally filtering by the event type
|
|
|
|
// or event state key NIDs if provided. (The CARDINALITY check will return true
|
|
|
|
// if the provided arrays are empty, ergo no filtering).
|
|
|
|
const bulkSelectStateEventByNIDSQL = "" +
|
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
|
|
|
|
" WHERE event_nid = ANY($1)" +
|
|
|
|
" AND (CARDINALITY($2::bigint[]) = 0 OR event_type_nid = ANY($2))" +
|
|
|
|
" AND (CARDINALITY($3::bigint[]) = 0 OR event_state_key_nid = ANY($3))" +
|
|
|
|
" ORDER BY event_type_nid, event_state_key_nid ASC"
|
|
|
|
|
2017-02-15 08:43:19 -06:00
|
|
|
const bulkSelectStateAtEventByIDSQL = "" +
|
2020-09-16 07:00:52 -05:00
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid, is_rejected FROM roomserver_events" +
|
2017-02-15 08:43:19 -06:00
|
|
|
" WHERE event_id = ANY($1)"
|
|
|
|
|
|
|
|
const updateEventStateSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"UPDATE roomserver_events SET state_snapshot_nid = $2 WHERE event_nid = $1"
|
2017-02-15 08:43:19 -06:00
|
|
|
|
2017-02-27 05:25:35 -06:00
|
|
|
const selectEventSentToOutputSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT sent_to_output FROM roomserver_events WHERE event_nid = $1"
|
2017-02-27 05:25:35 -06:00
|
|
|
|
|
|
|
const updateEventSentToOutputSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"UPDATE roomserver_events SET sent_to_output = TRUE WHERE event_nid = $1"
|
2017-02-27 05:25:35 -06:00
|
|
|
|
|
|
|
const selectEventIDSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT event_id FROM roomserver_events WHERE event_nid = $1"
|
2017-02-27 05:25:35 -06:00
|
|
|
|
2017-02-21 08:50:30 -06:00
|
|
|
const bulkSelectStateAtEventAndReferenceSQL = "" +
|
2023-05-24 05:14:42 -05:00
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid, event_id" +
|
2017-08-07 05:51:46 -05:00
|
|
|
" FROM roomserver_events WHERE event_nid = ANY($1)"
|
2017-02-21 08:50:30 -06:00
|
|
|
|
2017-03-07 04:25:01 -06:00
|
|
|
const bulkSelectEventIDSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT event_nid, event_id FROM roomserver_events WHERE event_nid = ANY($1)"
|
2017-03-07 04:25:01 -06:00
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
const bulkSelectEventNIDSQL = "" +
|
2023-02-24 02:40:20 -06:00
|
|
|
"SELECT event_id, event_nid, room_nid FROM roomserver_events WHERE event_id = ANY($1)"
|
2017-06-02 08:32:36 -05:00
|
|
|
|
2022-02-17 07:53:48 -06:00
|
|
|
const bulkSelectUnsentEventNIDSQL = "" +
|
2023-02-24 02:40:20 -06:00
|
|
|
"SELECT event_id, event_nid, room_nid FROM roomserver_events WHERE event_id = ANY($1) AND sent_to_output = FALSE"
|
2022-02-17 07:53:48 -06:00
|
|
|
|
2017-06-27 09:28:44 -05:00
|
|
|
const selectMaxEventDepthSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"SELECT COALESCE(MAX(depth) + 1, 0) FROM roomserver_events WHERE event_nid = ANY($1)"
|
2017-06-27 09:28:44 -05:00
|
|
|
|
2020-12-16 04:33:28 -06:00
|
|
|
const selectRoomNIDsForEventNIDsSQL = "" +
|
|
|
|
"SELECT event_nid, room_nid FROM roomserver_events WHERE event_nid = ANY($1)"
|
2020-03-27 11:28:22 -05:00
|
|
|
|
2022-08-18 04:37:47 -05:00
|
|
|
const selectEventRejectedSQL = "" +
|
|
|
|
"SELECT is_rejected FROM roomserver_events WHERE room_nid = $1 AND event_id = $2"
|
|
|
|
|
2017-02-15 08:43:19 -06:00
|
|
|
type eventStatements struct {
|
2022-08-18 11:06:13 -05:00
|
|
|
insertEventStmt *sql.Stmt
|
|
|
|
selectEventStmt *sql.Stmt
|
2022-11-01 10:07:17 -05:00
|
|
|
bulkSelectSnapshotsForEventIDsStmt *sql.Stmt
|
2022-08-18 11:06:13 -05:00
|
|
|
bulkSelectStateEventByIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateEventByIDExcludingRejectedStmt *sql.Stmt
|
|
|
|
bulkSelectStateEventByNIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateAtEventByIDStmt *sql.Stmt
|
|
|
|
updateEventStateStmt *sql.Stmt
|
|
|
|
selectEventSentToOutputStmt *sql.Stmt
|
|
|
|
updateEventSentToOutputStmt *sql.Stmt
|
|
|
|
selectEventIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateAtEventAndReferenceStmt *sql.Stmt
|
|
|
|
bulkSelectEventIDStmt *sql.Stmt
|
|
|
|
bulkSelectEventNIDStmt *sql.Stmt
|
|
|
|
bulkSelectUnsentEventNIDStmt *sql.Stmt
|
|
|
|
selectMaxEventDepthStmt *sql.Stmt
|
|
|
|
selectRoomNIDsForEventNIDsStmt *sql.Stmt
|
|
|
|
selectEventRejectedStmt *sql.Stmt
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
|
|
|
|
2022-05-09 08:30:32 -05:00
|
|
|
func CreateEventsTable(db *sql.DB) error {
|
2020-05-26 10:45:28 -05:00
|
|
|
_, err := db.Exec(eventsSchema)
|
2023-05-24 05:14:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations([]sqlutil.Migration{
|
|
|
|
{
|
|
|
|
Version: "roomserver: drop column reference_sha from roomserver_events",
|
|
|
|
Up: deltas.UpDropEventReferenceSHAEvents,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
return m.Up(context.Background())
|
2021-04-26 07:25:57 -05:00
|
|
|
}
|
|
|
|
|
2022-05-09 08:30:32 -05:00
|
|
|
func PrepareEventsTable(db *sql.DB) (tables.Events, error) {
|
2021-04-26 07:25:57 -05:00
|
|
|
s := &eventStatements{}
|
2017-03-07 04:25:01 -06:00
|
|
|
|
2021-07-28 12:30:04 -05:00
|
|
|
return s, sqlutil.StatementList{
|
2017-03-07 04:25:01 -06:00
|
|
|
{&s.insertEventStmt, insertEventSQL},
|
|
|
|
{&s.selectEventStmt, selectEventSQL},
|
2022-11-01 10:07:17 -05:00
|
|
|
{&s.bulkSelectSnapshotsForEventIDsStmt, bulkSelectSnapshotsForEventIDsSQL},
|
2017-03-07 04:25:01 -06:00
|
|
|
{&s.bulkSelectStateEventByIDStmt, bulkSelectStateEventByIDSQL},
|
2022-08-18 11:06:13 -05:00
|
|
|
{&s.bulkSelectStateEventByIDExcludingRejectedStmt, bulkSelectStateEventByIDExcludingRejectedSQL},
|
2021-04-26 07:25:57 -05:00
|
|
|
{&s.bulkSelectStateEventByNIDStmt, bulkSelectStateEventByNIDSQL},
|
2017-03-07 04:25:01 -06:00
|
|
|
{&s.bulkSelectStateAtEventByIDStmt, bulkSelectStateAtEventByIDSQL},
|
|
|
|
{&s.updateEventStateStmt, updateEventStateSQL},
|
|
|
|
{&s.updateEventSentToOutputStmt, updateEventSentToOutputSQL},
|
|
|
|
{&s.selectEventSentToOutputStmt, selectEventSentToOutputSQL},
|
|
|
|
{&s.selectEventIDStmt, selectEventIDSQL},
|
|
|
|
{&s.bulkSelectStateAtEventAndReferenceStmt, bulkSelectStateAtEventAndReferenceSQL},
|
|
|
|
{&s.bulkSelectEventIDStmt, bulkSelectEventIDSQL},
|
2017-06-02 08:32:36 -05:00
|
|
|
{&s.bulkSelectEventNIDStmt, bulkSelectEventNIDSQL},
|
2022-02-17 07:53:48 -06:00
|
|
|
{&s.bulkSelectUnsentEventNIDStmt, bulkSelectUnsentEventNIDSQL},
|
2017-06-27 09:28:44 -05:00
|
|
|
{&s.selectMaxEventDepthStmt, selectMaxEventDepthSQL},
|
2020-12-16 04:33:28 -06:00
|
|
|
{&s.selectRoomNIDsForEventNIDsStmt, selectRoomNIDsForEventNIDsSQL},
|
2022-08-18 04:37:47 -05:00
|
|
|
{&s.selectEventRejectedStmt, selectEventRejectedSQL},
|
2020-05-27 05:03:47 -05:00
|
|
|
}.Prepare(db)
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) InsertEvent(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context,
|
2020-05-26 10:45:28 -05:00
|
|
|
txn *sql.Tx,
|
2017-09-13 10:30:19 -05:00
|
|
|
roomNID types.RoomNID,
|
|
|
|
eventTypeNID types.EventTypeNID,
|
|
|
|
eventStateKeyNID types.EventStateKeyNID,
|
2017-02-15 08:43:19 -06:00
|
|
|
eventID string,
|
|
|
|
authEventNIDs []types.EventNID,
|
2017-06-27 09:28:44 -05:00
|
|
|
depth int64,
|
2020-09-16 07:00:52 -05:00
|
|
|
isRejected bool,
|
2017-02-15 08:43:19 -06:00
|
|
|
) (types.EventNID, types.StateSnapshotNID, error) {
|
|
|
|
var eventNID int64
|
|
|
|
var stateNID int64
|
2022-02-10 03:30:16 -06:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertEventStmt)
|
|
|
|
err := stmt.QueryRowContext(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx, int64(roomNID), int64(eventTypeNID), int64(eventStateKeyNID),
|
2023-05-24 05:14:42 -05:00
|
|
|
eventID, eventNIDsAsArray(authEventNIDs), depth,
|
2020-09-16 07:00:52 -05:00
|
|
|
isRejected,
|
2017-02-15 08:43:19 -06:00
|
|
|
).Scan(&eventNID, &stateNID)
|
|
|
|
return types.EventNID(eventNID), types.StateSnapshotNID(stateNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) SelectEvent(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventID string,
|
2017-09-13 10:30:19 -05:00
|
|
|
) (types.EventNID, types.StateSnapshotNID, error) {
|
2017-02-22 10:51:10 -06:00
|
|
|
var eventNID int64
|
|
|
|
var stateNID int64
|
2017-09-13 10:30:19 -05:00
|
|
|
err := s.selectEventStmt.QueryRowContext(ctx, eventID).Scan(&eventNID, &stateNID)
|
2017-02-22 10:51:10 -06:00
|
|
|
return types.EventNID(eventNID), types.StateSnapshotNID(stateNID), err
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:07:17 -05:00
|
|
|
func (s *eventStatements) BulkSelectSnapshotsFromEventIDs(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) (map[types.StateSnapshotNID][]string, error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectSnapshotsForEventIDsStmt)
|
|
|
|
|
|
|
|
rows, err := stmt.QueryContext(ctx, pq.Array(eventIDs))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-09 01:42:33 -06:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "BulkSelectSnapshotsFromEventIDs: rows.close() failed")
|
2022-11-01 10:07:17 -05:00
|
|
|
|
|
|
|
var eventID string
|
|
|
|
var stateNID types.StateSnapshotNID
|
|
|
|
result := make(map[types.StateSnapshotNID][]string)
|
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Scan(&eventID, &stateNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[stateNID] = append(result[stateNID], eventID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:32:53 -05:00
|
|
|
// bulkSelectStateEventByID lookups a list of state events by event ID.
|
2022-08-18 11:06:13 -05:00
|
|
|
// If not excluding rejected events, and any of the requested events are missing from
|
|
|
|
// the database it returns a types.MissingEventError. If excluding rejected events,
|
|
|
|
// the events will be silently omitted without error.
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) BulkSelectStateEventByID(
|
2022-08-18 11:06:13 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string, excludeRejected bool,
|
2017-09-13 10:30:19 -05:00
|
|
|
) ([]types.StateEntry, error) {
|
2022-08-18 11:06:13 -05:00
|
|
|
var stmt *sql.Stmt
|
|
|
|
if excludeRejected {
|
|
|
|
stmt = sqlutil.TxStmt(txn, s.bulkSelectStateEventByIDExcludingRejectedStmt)
|
|
|
|
} else {
|
|
|
|
stmt = sqlutil.TxStmt(txn, s.bulkSelectStateEventByIDStmt)
|
|
|
|
}
|
2022-02-04 04:39:34 -06:00
|
|
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs))
|
2017-02-15 08:43:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateEventByID: rows.close() failed")
|
2017-02-15 08:43:19 -06:00
|
|
|
// We know that we will only get as many results as event IDs
|
|
|
|
// because of the unique constraint on event IDs.
|
|
|
|
// So we can allocate an array of the correct size now.
|
|
|
|
// We might get fewer results than IDs so we adjust the length of the slice before returning it.
|
2022-08-18 11:06:13 -05:00
|
|
|
results := make([]types.StateEntry, 0, len(eventIDs))
|
2017-02-15 08:43:19 -06:00
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
2022-08-18 11:06:13 -05:00
|
|
|
var result types.StateEntry
|
2017-02-15 08:43:19 -06:00
|
|
|
if err = rows.Scan(
|
|
|
|
&result.EventTypeNID,
|
|
|
|
&result.EventStateKeyNID,
|
2017-02-22 10:51:10 -06:00
|
|
|
&result.EventNID,
|
2017-02-15 08:43:19 -06:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-18 11:06:13 -05:00
|
|
|
results = append(results, result)
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-18 11:06:13 -05:00
|
|
|
if !excludeRejected && i != len(eventIDs) {
|
2017-02-15 08:43:19 -06:00
|
|
|
// If there are fewer rows returned than IDs then we were asked to lookup event IDs we don't have.
|
|
|
|
// We don't know which ones were missing because we don't return the string IDs in the query.
|
|
|
|
// However it should be possible debug this by replaying queries or entries from the input kafka logs.
|
|
|
|
// If this turns out to be impossible and we do need the debug information here, it would be better
|
2020-05-21 08:40:13 -05:00
|
|
|
// to do it as a separate query rather than slowing down/complicating the internal case.
|
2017-06-07 08:32:53 -05:00
|
|
|
return nil, types.MissingEventError(
|
|
|
|
fmt.Sprintf("storage: state event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
|
|
|
)
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
return results, nil
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
|
|
|
|
2021-04-26 07:25:57 -05:00
|
|
|
// bulkSelectStateEventByNID lookups a list of state events by event NID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError
|
|
|
|
func (s *eventStatements) BulkSelectStateEventByNID(
|
2022-02-04 04:39:34 -06:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
2021-04-26 07:25:57 -05:00
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
|
|
|
) ([]types.StateEntry, error) {
|
2022-05-16 12:33:16 -05:00
|
|
|
tuples := types.StateKeyTupleSorter(stateKeyTuples)
|
2021-04-26 07:25:57 -05:00
|
|
|
sort.Sort(tuples)
|
2022-05-16 12:33:16 -05:00
|
|
|
eventTypeNIDArray, eventStateKeyNIDArray := tuples.TypesAndStateKeysAsArrays()
|
2022-02-04 04:39:34 -06:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectStateEventByNIDStmt)
|
2022-05-16 12:33:16 -05:00
|
|
|
rows, err := stmt.QueryContext(ctx, eventNIDsAsArray(eventNIDs), pq.Int64Array(eventTypeNIDArray), pq.Int64Array(eventStateKeyNIDArray))
|
2021-04-26 07:25:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateEventByID: rows.close() failed")
|
|
|
|
// We know that we will only get as many results as event IDs
|
|
|
|
// because of the unique constraint on event IDs.
|
|
|
|
// So we can allocate an array of the correct size now.
|
|
|
|
// We might get fewer results than IDs so we adjust the length of the slice before returning it.
|
|
|
|
results := make([]types.StateEntry, len(eventNIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
|
|
|
if err = rows.Scan(
|
|
|
|
&result.EventTypeNID,
|
|
|
|
&result.EventStateKeyNID,
|
|
|
|
&result.EventNID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return results[:i], nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:32:53 -05:00
|
|
|
// bulkSelectStateAtEventByID lookups the state at a list of events by event ID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError.
|
|
|
|
// If we do not have the state for any of the requested events it returns a types.MissingEventError.
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) BulkSelectStateAtEventByID(
|
2022-02-04 04:39:34 -06:00
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
2017-09-13 10:30:19 -05:00
|
|
|
) ([]types.StateAtEvent, error) {
|
2022-02-04 04:39:34 -06:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectStateAtEventByIDStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs))
|
2017-02-15 08:43:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateAtEventByID: rows.close() failed")
|
2017-02-15 08:43:19 -06:00
|
|
|
results := make([]types.StateAtEvent, len(eventIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
|
|
|
if err = rows.Scan(
|
|
|
|
&result.EventTypeNID,
|
|
|
|
&result.EventStateKeyNID,
|
2017-02-22 10:51:10 -06:00
|
|
|
&result.EventNID,
|
2017-02-15 08:43:19 -06:00
|
|
|
&result.BeforeStateSnapshotNID,
|
2020-09-16 07:00:52 -05:00
|
|
|
&result.IsRejected,
|
2017-02-15 08:43:19 -06:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-27 08:29:14 -06:00
|
|
|
// Genuine create events are the only case where it's OK to have no previous state.
|
|
|
|
isCreate := result.EventTypeNID == types.MRoomCreateNID && result.EventStateKeyNID == 1
|
|
|
|
if result.BeforeStateSnapshotNID == 0 && !isCreate {
|
2022-08-23 07:57:11 -05:00
|
|
|
return nil, types.MissingStateError(
|
2017-06-07 08:32:53 -05:00
|
|
|
fmt.Sprintf("storage: missing state for event NID %d", result.EventNID),
|
|
|
|
)
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-15 08:43:19 -06:00
|
|
|
if i != len(eventIDs) {
|
2017-06-07 08:32:53 -05:00
|
|
|
return nil, types.MissingEventError(
|
|
|
|
fmt.Sprintf("storage: event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
|
|
|
)
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
return results, nil
|
2017-02-15 08:43:19 -06:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) UpdateEventState(
|
2020-08-20 10:24:33 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
2017-09-13 10:30:19 -05:00
|
|
|
) error {
|
2020-08-20 10:24:33 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.updateEventStateStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, int64(eventNID), int64(stateNID))
|
2017-02-15 08:43:19 -06:00
|
|
|
return err
|
|
|
|
}
|
2017-02-21 08:50:30 -06:00
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) SelectEventSentToOutput(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
|
|
|
) (sentToOutput bool, err error) {
|
2020-06-12 08:55:57 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventSentToOutputStmt)
|
2017-09-20 04:59:19 -05:00
|
|
|
err = stmt.QueryRowContext(ctx, int64(eventNID)).Scan(&sentToOutput)
|
2017-02-27 05:25:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) UpdateEventSentToOutput(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) error {
|
2020-06-12 08:55:57 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.updateEventSentToOutputStmt)
|
2017-09-13 10:30:19 -05:00
|
|
|
_, err := stmt.ExecContext(ctx, int64(eventNID))
|
2017-02-27 05:25:35 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) SelectEventID(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
|
|
|
) (eventID string, err error) {
|
2020-06-12 08:55:57 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventIDStmt)
|
2017-09-13 10:30:19 -05:00
|
|
|
err = stmt.QueryRowContext(ctx, int64(eventNID)).Scan(&eventID)
|
2017-02-27 05:25:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) BulkSelectStateAtEventAndReference(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
|
|
|
) ([]types.StateAtEventAndReference, error) {
|
2020-06-12 08:55:57 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectStateAtEventAndReferenceStmt)
|
2017-09-13 10:30:19 -05:00
|
|
|
rows, err := stmt.QueryContext(ctx, eventNIDsAsArray(eventNIDs))
|
2017-02-21 08:50:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateAtEventAndReference: rows.close() failed")
|
2017-02-21 08:50:30 -06:00
|
|
|
results := make([]types.StateAtEventAndReference, len(eventNIDs))
|
|
|
|
i := 0
|
2022-05-09 08:30:32 -05:00
|
|
|
var (
|
|
|
|
eventTypeNID int64
|
|
|
|
eventStateKeyNID int64
|
|
|
|
eventNID int64
|
|
|
|
stateSnapshotNID int64
|
|
|
|
eventID string
|
|
|
|
)
|
2017-02-21 08:50:30 -06:00
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
if err = rows.Scan(
|
2023-05-24 05:14:42 -05:00
|
|
|
&eventTypeNID, &eventStateKeyNID, &eventNID, &stateSnapshotNID, &eventID,
|
2017-02-21 08:50:30 -06:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := &results[i]
|
|
|
|
result.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
|
|
|
result.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
|
|
|
result.EventNID = types.EventNID(eventNID)
|
|
|
|
result.BeforeStateSnapshotNID = types.StateSnapshotNID(stateSnapshotNID)
|
|
|
|
result.EventID = eventID
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-06 08:29:39 -06:00
|
|
|
if i != len(eventNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2017-03-07 04:25:01 -06:00
|
|
|
// bulkSelectEventID returns a map from numeric event ID to string event ID.
|
2022-02-04 04:39:34 -06:00
|
|
|
func (s *eventStatements) BulkSelectEventID(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (map[types.EventNID]string, error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectEventIDStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, eventNIDsAsArray(eventNIDs))
|
2017-03-07 04:25:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventID: rows.close() failed")
|
2017-03-07 04:25:01 -06:00
|
|
|
results := make(map[types.EventNID]string, len(eventNIDs))
|
|
|
|
i := 0
|
2022-05-09 08:30:32 -05:00
|
|
|
var eventNID int64
|
|
|
|
var eventID string
|
2017-03-07 04:25:01 -06:00
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
if err = rows.Scan(&eventNID, &eventID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results[types.EventNID(eventNID)] = eventID
|
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-07 04:25:01 -06:00
|
|
|
if i != len(eventNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:53:48 -06:00
|
|
|
// BulkSelectEventNIDs returns a map from string event ID to numeric event ID.
|
2017-06-02 08:32:36 -05:00
|
|
|
// If an event ID is not in the database then it is omitted from the map.
|
2023-02-24 02:40:20 -06:00
|
|
|
func (s *eventStatements) BulkSelectEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventMetadata, error) {
|
2022-02-17 07:53:48 -06:00
|
|
|
return s.bulkSelectEventNID(ctx, txn, eventIDs, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BulkSelectEventNIDs returns a map from string event ID to numeric event ID
|
|
|
|
// only for events that haven't already been sent to the roomserver output.
|
|
|
|
// If an event ID is not in the database then it is omitted from the map.
|
2023-02-24 02:40:20 -06:00
|
|
|
func (s *eventStatements) BulkSelectUnsentEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventMetadata, error) {
|
2022-02-17 07:53:48 -06:00
|
|
|
return s.bulkSelectEventNID(ctx, txn, eventIDs, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bulkSelectEventNIDs returns a map from string event ID to numeric event ID.
|
|
|
|
// If an event ID is not in the database then it is omitted from the map.
|
2023-02-24 02:40:20 -06:00
|
|
|
func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string, onlyUnsent bool) (map[string]types.EventMetadata, error) {
|
2022-02-17 07:53:48 -06:00
|
|
|
var stmt *sql.Stmt
|
|
|
|
if onlyUnsent {
|
|
|
|
stmt = sqlutil.TxStmt(txn, s.bulkSelectUnsentEventNIDStmt)
|
|
|
|
} else {
|
|
|
|
stmt = sqlutil.TxStmt(txn, s.bulkSelectEventNIDStmt)
|
|
|
|
}
|
2022-02-04 04:39:34 -06:00
|
|
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs))
|
2017-06-02 08:32:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventNID: rows.close() failed")
|
2023-02-24 02:40:20 -06:00
|
|
|
results := make(map[string]types.EventMetadata, len(eventIDs))
|
2022-05-09 08:30:32 -05:00
|
|
|
var eventID string
|
|
|
|
var eventNID int64
|
2023-02-24 02:40:20 -06:00
|
|
|
var roomNID int64
|
2017-06-02 08:32:36 -05:00
|
|
|
for rows.Next() {
|
2023-02-24 02:40:20 -06:00
|
|
|
if err = rows.Scan(&eventID, &eventNID, &roomNID); err != nil {
|
2017-06-02 08:32:36 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-24 02:40:20 -06:00
|
|
|
results[eventID] = types.EventMetadata{
|
|
|
|
EventNID: types.EventNID(eventNID),
|
|
|
|
RoomNID: types.RoomNID(roomNID),
|
|
|
|
}
|
2017-06-02 08:32:36 -05:00
|
|
|
}
|
2020-02-11 08:12:21 -06:00
|
|
|
return results, rows.Err()
|
2017-06-02 08:32:36 -05:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:45:28 -05:00
|
|
|
func (s *eventStatements) SelectMaxEventDepth(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (int64, error) {
|
2017-06-27 09:28:44 -05:00
|
|
|
var result int64
|
2017-09-13 10:30:19 -05:00
|
|
|
stmt := s.selectMaxEventDepthStmt
|
|
|
|
err := stmt.QueryRowContext(ctx, eventNIDsAsArray(eventNIDs)).Scan(&result)
|
2017-06-27 09:28:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 04:33:28 -06:00
|
|
|
func (s *eventStatements) SelectRoomNIDsForEventNIDs(
|
2022-02-04 04:39:34 -06:00
|
|
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
2020-12-16 04:33:28 -06:00
|
|
|
) (map[types.EventNID]types.RoomNID, error) {
|
2022-02-04 04:39:34 -06:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectRoomNIDsForEventNIDsStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, eventNIDsAsArray(eventNIDs))
|
2020-12-16 04:33:28 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectRoomNIDsForEventNIDsStmt: rows.close() failed")
|
|
|
|
result := make(map[types.EventNID]types.RoomNID)
|
2022-05-09 08:30:32 -05:00
|
|
|
var eventNID types.EventNID
|
|
|
|
var roomNID types.RoomNID
|
2020-12-16 04:33:28 -06:00
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Scan(&eventNID, &roomNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[eventNID] = roomNID
|
|
|
|
}
|
2023-11-09 01:42:33 -06:00
|
|
|
return result, rows.Err()
|
2020-03-27 11:28:22 -05:00
|
|
|
}
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
func eventNIDsAsArray(eventNIDs []types.EventNID) pq.Int64Array {
|
|
|
|
nids := make([]int64, len(eventNIDs))
|
|
|
|
for i := range eventNIDs {
|
|
|
|
nids[i] = int64(eventNIDs[i])
|
|
|
|
}
|
|
|
|
return nids
|
|
|
|
}
|
2022-08-18 04:37:47 -05:00
|
|
|
|
|
|
|
func (s *eventStatements) SelectEventRejected(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventID string,
|
|
|
|
) (rejected bool, err error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventRejectedStmt)
|
|
|
|
err = stmt.QueryRowContext(ctx, roomNID, eventID).Scan(&rejected)
|
|
|
|
return
|
|
|
|
}
|