2020-01-03 14:07:05 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 23:40:52 +01: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 14:07:05 +00:00
|
|
|
package postgres
|
2017-02-21 14:50:30 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 16:30:19 +01:00
|
|
|
"context"
|
2017-02-21 14:50:30 +00:00
|
|
|
"database/sql"
|
2017-08-07 11:51:46 +01:00
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-05-27 09:36:09 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-02-21 14:50:30 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const previousEventSchema = `
|
|
|
|
-- The previous events table stores the event_ids referenced by the events
|
|
|
|
-- stored in the events table.
|
|
|
|
-- This is used to tell if a new event is already referenced by an event in
|
|
|
|
-- the database.
|
2017-08-07 11:51:46 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_previous_events (
|
2017-02-21 14:50:30 +00:00
|
|
|
-- The string event ID taken from the prev_events key of an event.
|
|
|
|
previous_event_id TEXT NOT NULL,
|
|
|
|
-- The SHA256 reference hash taken from the prev_events key of an event.
|
|
|
|
previous_reference_sha256 BYTEA NOT NULL,
|
|
|
|
-- A list of numeric event IDs of events that reference this prev_event.
|
|
|
|
event_nids BIGINT[] NOT NULL,
|
2017-08-07 11:51:46 +01:00
|
|
|
CONSTRAINT roomserver_previous_event_id_unique UNIQUE (previous_event_id, previous_reference_sha256)
|
2017-02-21 14:50:30 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
// Insert an entry into the previous_events table.
|
|
|
|
// If there is already an entry indicating that an event references that previous event then
|
|
|
|
// add the event NID to the list to indicate that this event references that previous event as well.
|
|
|
|
// This should only be modified while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
|
|
|
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
|
|
|
|
const insertPreviousEventSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"INSERT INTO roomserver_previous_events" +
|
2017-02-21 14:50:30 +00:00
|
|
|
" (previous_event_id, previous_reference_sha256, event_nids)" +
|
|
|
|
" VALUES ($1, $2, array_append('{}'::bigint[], $3))" +
|
2017-08-07 11:51:46 +01:00
|
|
|
" ON CONFLICT ON CONSTRAINT roomserver_previous_event_id_unique" +
|
|
|
|
" DO UPDATE SET event_nids = array_append(roomserver_previous_events.event_nids, $3)" +
|
|
|
|
" WHERE $3 != ALL(roomserver_previous_events.event_nids)"
|
2017-02-21 14:50:30 +00:00
|
|
|
|
|
|
|
// Check if the event is referenced by another event in the table.
|
|
|
|
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
|
|
|
const selectPreviousEventExistsSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"SELECT 1 FROM roomserver_previous_events" +
|
2017-02-21 14:50:30 +00:00
|
|
|
" WHERE previous_event_id = $1 AND previous_reference_sha256 = $2"
|
|
|
|
|
|
|
|
type previousEventStatements struct {
|
|
|
|
insertPreviousEventStmt *sql.Stmt
|
|
|
|
selectPreviousEventExistsStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:36:09 +01:00
|
|
|
func NewPostgresPreviousEventsTable(db *sql.DB) (tables.PreviousEvents, error) {
|
|
|
|
s := &previousEventStatements{}
|
|
|
|
_, err := db.Exec(previousEventSchema)
|
2017-02-21 14:50:30 +00:00
|
|
|
if err != nil {
|
2020-05-27 09:36:09 +01:00
|
|
|
return nil, err
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-03-07 10:37:41 +00:00
|
|
|
|
2020-05-27 09:36:09 +01:00
|
|
|
return s, statementList{
|
2017-03-07 10:37:41 +00:00
|
|
|
{&s.insertPreviousEventStmt, insertPreviousEventSQL},
|
|
|
|
{&s.selectPreviousEventExistsStmt, selectPreviousEventExistsSQL},
|
|
|
|
}.prepare(db)
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:36:09 +01:00
|
|
|
func (s *previousEventStatements) InsertPreviousEvent(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
previousEventID string,
|
|
|
|
previousEventReferenceSHA256 []byte,
|
|
|
|
eventNID types.EventNID,
|
|
|
|
) error {
|
2020-05-21 14:40:13 +01:00
|
|
|
stmt := internal.TxStmt(txn, s.insertPreviousEventStmt)
|
2017-09-13 16:30:19 +01:00
|
|
|
_, err := stmt.ExecContext(
|
|
|
|
ctx, previousEventID, previousEventReferenceSHA256, int64(eventNID),
|
|
|
|
)
|
2017-02-21 14:50:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event reference exists
|
|
|
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
2020-05-27 09:36:09 +01:00
|
|
|
func (s *previousEventStatements) SelectPreviousEventExists(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
|
|
|
|
) error {
|
2017-02-21 14:50:30 +00:00
|
|
|
var ok int64
|
2020-05-21 14:40:13 +01:00
|
|
|
stmt := internal.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
2017-09-13 16:30:19 +01:00
|
|
|
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|