2020-02-13 17:27:33 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-27 10:03:47 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
2020-05-27 08:36:09 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2020-02-13 17:27:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const previousEventSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_previous_events (
|
|
|
|
previous_event_id TEXT NOT NULL,
|
|
|
|
previous_reference_sha256 BLOB NOT NULL,
|
|
|
|
event_nids TEXT NOT NULL,
|
|
|
|
UNIQUE (previous_event_id, previous_reference_sha256)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
// 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 = `
|
|
|
|
INSERT OR REPLACE INTO roomserver_previous_events
|
|
|
|
(previous_event_id, previous_reference_sha256, event_nids)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
`
|
|
|
|
|
|
|
|
// 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 = `
|
|
|
|
SELECT 1 FROM roomserver_previous_events
|
|
|
|
WHERE previous_event_id = $1 AND previous_reference_sha256 = $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type previousEventStatements struct {
|
2020-07-21 09:48:49 +00:00
|
|
|
db *sql.DB
|
2020-02-13 17:27:33 +00:00
|
|
|
insertPreviousEventStmt *sql.Stmt
|
|
|
|
selectPreviousEventExistsStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:38:27 +00:00
|
|
|
func NewSqlitePrevEventsTable(db *sql.DB) (tables.PreviousEvents, error) {
|
2020-07-21 09:48:49 +00:00
|
|
|
s := &previousEventStatements{
|
2020-08-19 14:38:27 +00:00
|
|
|
db: db,
|
2020-07-21 09:48:49 +00:00
|
|
|
}
|
2020-05-27 08:36:09 +00:00
|
|
|
_, err := db.Exec(previousEventSchema)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
2020-05-27 08:36:09 +00:00
|
|
|
return nil, err
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
return s, shared.StatementList{
|
2020-02-13 17:27:33 +00:00
|
|
|
{&s.insertPreviousEventStmt, insertPreviousEventSQL},
|
|
|
|
{&s.selectPreviousEventExistsStmt, selectPreviousEventExistsSQL},
|
2020-05-27 10:03:47 +00:00
|
|
|
}.Prepare(db)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *previousEventStatements) InsertPreviousEvent(
|
2020-02-13 17:27:33 +00:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
previousEventID string,
|
|
|
|
previousEventReferenceSHA256 []byte,
|
|
|
|
eventNID types.EventNID,
|
|
|
|
) error {
|
2020-08-19 14:38:27 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
|
|
|
|
_, err := stmt.ExecContext(
|
|
|
|
ctx, previousEventID, previousEventReferenceSHA256, int64(eventNID),
|
|
|
|
)
|
|
|
|
return err
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event reference exists
|
|
|
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *previousEventStatements) SelectPreviousEventExists(
|
2020-02-13 17:27:33 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
|
|
|
|
) error {
|
|
|
|
var ok int64
|
2020-06-12 13:55:57 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
|
|
|
|
}
|