2020-01-03 15:07:05 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02: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 15:07:05 +01:00
|
|
|
package postgres
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 17:30:19 +02:00
|
|
|
"context"
|
2017-02-15 15:43:19 +01:00
|
|
|
"database/sql"
|
2017-08-07 12:51:46 +02:00
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
"github.com/lib/pq"
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-26 16:42:42 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-02-15 15:43:19 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const eventStateKeysSchema = `
|
|
|
|
-- Numeric versions of the event "state_key"s. State keys tend to be reused so
|
|
|
|
-- assigning each string a numeric ID should reduce the amount of data that
|
|
|
|
-- needs to be stored and fetched from the database.
|
|
|
|
-- It also means that many operations can work with int64 arrays rather than
|
|
|
|
-- string arrays which may help reduce GC pressure.
|
|
|
|
-- Well known state keys are pre-assigned numeric IDs:
|
|
|
|
-- 1 -> "" (the empty string)
|
|
|
|
-- Other state keys are automatically assigned numeric IDs starting from 2**16.
|
|
|
|
-- This leaves room to add more pre-assigned numeric IDs and clearly separates
|
|
|
|
-- the automatically assigned IDs from the pre-assigned IDs.
|
2017-08-07 12:51:46 +02:00
|
|
|
CREATE SEQUENCE IF NOT EXISTS roomserver_event_state_key_nid_seq START 65536;
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_event_state_keys (
|
2017-02-15 15:43:19 +01:00
|
|
|
-- Local numeric ID for the state key.
|
2017-08-07 12:51:46 +02:00
|
|
|
event_state_key_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_event_state_key_nid_seq'),
|
|
|
|
event_state_key TEXT NOT NULL CONSTRAINT roomserver_event_state_key_unique UNIQUE
|
2017-02-15 15:43:19 +01:00
|
|
|
);
|
2017-08-07 12:51:46 +02:00
|
|
|
INSERT INTO roomserver_event_state_keys (event_state_key_nid, event_state_key) VALUES
|
2017-02-15 15:43:19 +01:00
|
|
|
(1, '') ON CONFLICT DO NOTHING;
|
|
|
|
`
|
|
|
|
|
|
|
|
// Same as insertEventTypeNIDSQL
|
|
|
|
const insertEventStateKeyNIDSQL = "" +
|
2017-08-07 12:51:46 +02:00
|
|
|
"INSERT INTO roomserver_event_state_keys (event_state_key) VALUES ($1)" +
|
|
|
|
" ON CONFLICT ON CONSTRAINT roomserver_event_state_key_unique" +
|
2017-02-22 17:51:10 +01:00
|
|
|
" DO NOTHING RETURNING (event_state_key_nid)"
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
const selectEventStateKeyNIDSQL = "" +
|
2017-08-07 12:51:46 +02:00
|
|
|
"SELECT event_state_key_nid FROM roomserver_event_state_keys" +
|
|
|
|
" WHERE event_state_key = $1"
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
// Bulk lookup from string state key to numeric ID for that state key.
|
|
|
|
// Takes an array of strings as the query parameter.
|
|
|
|
const bulkSelectEventStateKeyNIDSQL = "" +
|
2017-08-07 12:51:46 +02:00
|
|
|
"SELECT event_state_key, event_state_key_nid FROM roomserver_event_state_keys" +
|
2017-02-15 15:43:19 +01:00
|
|
|
" WHERE event_state_key = ANY($1)"
|
|
|
|
|
2017-08-21 17:34:26 +02:00
|
|
|
// Bulk lookup from numeric ID to string state key for that state key.
|
|
|
|
// Takes an array of strings as the query parameter.
|
|
|
|
const bulkSelectEventStateKeySQL = "" +
|
|
|
|
"SELECT event_state_key, event_state_key_nid FROM roomserver_event_state_keys" +
|
|
|
|
" WHERE event_state_key_nid = ANY($1)"
|
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
type eventStateKeyStatements struct {
|
|
|
|
insertEventStateKeyNIDStmt *sql.Stmt
|
|
|
|
selectEventStateKeyNIDStmt *sql.Stmt
|
|
|
|
bulkSelectEventStateKeyNIDStmt *sql.Stmt
|
2017-08-21 17:34:26 +02:00
|
|
|
bulkSelectEventStateKeyStmt *sql.Stmt
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 15:30:32 +02:00
|
|
|
func CreateEventStateKeysTable(db *sql.DB) error {
|
2020-05-26 16:42:42 +02:00
|
|
|
_, err := db.Exec(eventStateKeysSchema)
|
2021-04-26 14:25:57 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-09 15:30:32 +02:00
|
|
|
func PrepareEventStateKeysTable(db *sql.DB) (tables.EventStateKeys, error) {
|
2021-04-26 14:25:57 +02:00
|
|
|
s := &eventStateKeyStatements{}
|
|
|
|
|
2021-07-28 19:30:04 +02:00
|
|
|
return s, sqlutil.StatementList{
|
2017-03-07 11:37:41 +01:00
|
|
|
{&s.insertEventStateKeyNIDStmt, insertEventStateKeyNIDSQL},
|
|
|
|
{&s.selectEventStateKeyNIDStmt, selectEventStateKeyNIDSQL},
|
|
|
|
{&s.bulkSelectEventStateKeyNIDStmt, bulkSelectEventStateKeyNIDSQL},
|
2017-08-21 17:34:26 +02:00
|
|
|
{&s.bulkSelectEventStateKeyStmt, bulkSelectEventStateKeySQL},
|
2020-05-27 12:03:47 +02:00
|
|
|
}.Prepare(db)
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventStateKeyStatements) InsertEventStateKeyNID(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2017-02-15 15:43:19 +01:00
|
|
|
var eventStateKeyNID int64
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertEventStateKeyNIDStmt)
|
2017-09-13 17:30:19 +02:00
|
|
|
err := stmt.QueryRowContext(ctx, eventStateKey).Scan(&eventStateKeyNID)
|
2017-02-15 15:43:19 +01:00
|
|
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventStateKeyStatements) SelectEventStateKeyNID(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2017-02-15 15:43:19 +01:00
|
|
|
var eventStateKeyNID int64
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventStateKeyNIDStmt)
|
2017-09-13 17:30:19 +02:00
|
|
|
err := stmt.QueryRowContext(ctx, eventStateKey).Scan(&eventStateKeyNID)
|
2017-02-15 15:43:19 +01:00
|
|
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventStateKeyStatements) BulkSelectEventStateKeyNID(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKeys []string,
|
2017-09-13 17:30:19 +02:00
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectEventStateKeyNIDStmt)
|
|
|
|
rows, err := stmt.QueryContext(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx, pq.StringArray(eventStateKeys),
|
|
|
|
)
|
2017-02-15 15:43:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKeyNID: rows.close() failed")
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
result := make(map[string]types.EventStateKeyNID, len(eventStateKeys))
|
2022-05-09 15:30:32 +02:00
|
|
|
var stateKey string
|
|
|
|
var stateKeyNID int64
|
2017-02-15 15:43:19 +01:00
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[stateKey] = types.EventStateKeyNID(stateKeyNID)
|
|
|
|
}
|
2020-02-11 15:12:21 +01:00
|
|
|
return result, rows.Err()
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
2017-08-21 17:34:26 +02:00
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventStateKeyStatements) BulkSelectEventStateKey(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKeyNIDs []types.EventStateKeyNID,
|
2017-09-13 17:30:19 +02:00
|
|
|
) (map[types.EventStateKeyNID]string, error) {
|
2018-06-18 13:03:08 +02:00
|
|
|
nIDs := make(pq.Int64Array, len(eventStateKeyNIDs))
|
2017-08-21 17:34:26 +02:00
|
|
|
for i := range eventStateKeyNIDs {
|
|
|
|
nIDs[i] = int64(eventStateKeyNIDs[i])
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectEventStateKeyStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, nIDs)
|
2017-08-21 17:34:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKey: rows.close() failed")
|
2017-08-21 17:34:26 +02:00
|
|
|
|
|
|
|
result := make(map[types.EventStateKeyNID]string, len(eventStateKeyNIDs))
|
2022-05-09 15:30:32 +02:00
|
|
|
var stateKey string
|
|
|
|
var stateKeyNID int64
|
2017-08-21 17:34:26 +02:00
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[types.EventStateKeyNID(stateKeyNID)] = stateKey
|
|
|
|
}
|
2020-02-11 15:12:21 +01:00
|
|
|
return result, rows.Err()
|
2017-08-21 17:34:26 +02:00
|
|
|
}
|