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-06-28 15:10:17 +00: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-06-28 15:10:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-18 13:15:17 +00:00
|
|
|
"context"
|
2017-06-28 15:10:17 +00:00
|
|
|
"database/sql"
|
2017-08-21 16:20:23 +00:00
|
|
|
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2017-06-28 15:10:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const roomSchema = `
|
2017-08-07 10:51:46 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_rooms (
|
2017-06-28 15:10:17 +00:00
|
|
|
-- The string ID of the room
|
|
|
|
room_id TEXT PRIMARY KEY,
|
|
|
|
-- The most recent event state by the room server.
|
|
|
|
-- We can use this to tell if our view of the room state has become
|
|
|
|
-- desynchronised.
|
|
|
|
last_event_id TEXT NOT NULL
|
|
|
|
);`
|
|
|
|
|
|
|
|
const insertRoomSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"INSERT INTO federationsender_rooms (room_id, last_event_id) VALUES ($1, '')" +
|
2017-06-28 15:10:17 +00:00
|
|
|
" ON CONFLICT DO NOTHING"
|
|
|
|
|
|
|
|
const selectRoomForUpdateSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"SELECT last_event_id FROM federationsender_rooms WHERE room_id = $1 FOR UPDATE"
|
2017-06-28 15:10:17 +00:00
|
|
|
|
|
|
|
const updateRoomSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"UPDATE federationsender_rooms SET last_event_id = $2 WHERE room_id = $1"
|
2017-06-28 15:10:17 +00:00
|
|
|
|
|
|
|
type roomStatements struct {
|
2020-07-20 15:55:20 +00:00
|
|
|
db *sql.DB
|
2017-06-28 15:10:17 +00:00
|
|
|
insertRoomStmt *sql.Stmt
|
|
|
|
selectRoomForUpdateStmt *sql.Stmt
|
|
|
|
updateRoomStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:55:20 +00:00
|
|
|
func NewPostgresRoomsTable(db *sql.DB) (s *roomStatements, err error) {
|
|
|
|
s = &roomStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err = s.db.Exec(roomSchema)
|
2017-06-28 15:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-07-20 15:55:20 +00:00
|
|
|
if s.insertRoomStmt, err = s.db.Prepare(insertRoomSQL); err != nil {
|
2017-06-28 15:10:17 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-20 15:55:20 +00:00
|
|
|
if s.selectRoomForUpdateStmt, err = s.db.Prepare(selectRoomForUpdateSQL); err != nil {
|
2017-06-28 15:10:17 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-20 15:55:20 +00:00
|
|
|
if s.updateRoomStmt, err = s.db.Prepare(updateRoomSQL); err != nil {
|
2017-06-28 15:10:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// insertRoom inserts the room if it didn't already exist.
|
|
|
|
// If the room didn't exist then last_event_id is set to the empty string.
|
2020-07-20 15:55:20 +00:00
|
|
|
func (s *roomStatements) InsertRoom(
|
2017-09-18 13:15:17 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) error {
|
2020-06-12 13:55:57 +00:00
|
|
|
_, err := sqlutil.TxStmt(txn, s.insertRoomStmt).ExecContext(ctx, roomID)
|
2017-06-28 15:10:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectRoomForUpdate locks the row for the room and returns the last_event_id.
|
|
|
|
// The row must already exist in the table. Callers can ensure that the row
|
|
|
|
// exists by calling insertRoom first.
|
2020-07-20 15:55:20 +00:00
|
|
|
func (s *roomStatements) SelectRoomForUpdate(
|
2017-09-18 13:15:17 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) (string, error) {
|
2017-06-28 15:10:17 +00:00
|
|
|
var lastEventID string
|
2020-06-12 13:55:57 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectRoomForUpdateStmt)
|
2017-09-18 13:15:17 +00:00
|
|
|
err := stmt.QueryRowContext(ctx, roomID).Scan(&lastEventID)
|
2017-06-28 15:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return lastEventID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateRoom updates the last_event_id for the room. selectRoomForUpdate should
|
|
|
|
// have already been called earlier within the transaction.
|
2020-07-20 15:55:20 +00:00
|
|
|
func (s *roomStatements) UpdateRoom(
|
2017-09-18 13:15:17 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID, lastEventID string,
|
|
|
|
) error {
|
2020-06-12 13:55:57 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.updateRoomStmt)
|
2017-09-18 13:15:17 +00:00
|
|
|
_, err := stmt.ExecContext(ctx, roomID, lastEventID)
|
2017-06-28 15:10:17 +00:00
|
|
|
return err
|
|
|
|
}
|