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 22:40:52 +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-02-15 14:43:19 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 15:30:19 +00:00
|
|
|
"context"
|
2017-02-15 14:43:19 +00:00
|
|
|
"database/sql"
|
2017-08-07 10:51:46 +00:00
|
|
|
|
2017-02-15 14:43:19 +00:00
|
|
|
"github.com/lib/pq"
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-26 14:42:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-02-15 14:43:19 +00: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 10:51:46 +00: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 14:43:19 +00:00
|
|
|
-- Local numeric ID for the state key.
|
2017-08-07 10:51:46 +00: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 14:43:19 +00:00
|
|
|
);
|
2017-08-07 10:51:46 +00:00
|
|
|
INSERT INTO roomserver_event_state_keys (event_state_key_nid, event_state_key) VALUES
|
2017-02-15 14:43:19 +00:00
|
|
|
(1, '') ON CONFLICT DO NOTHING;
|
|
|
|
`
|
|
|
|
|
|
|
|
// Same as insertEventTypeNIDSQL
|
|
|
|
const insertEventStateKeyNIDSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"INSERT INTO roomserver_event_state_keys (event_state_key) VALUES ($1)" +
|
|
|
|
" ON CONFLICT ON CONSTRAINT roomserver_event_state_key_unique" +
|
2017-02-22 16:51:10 +00:00
|
|
|
" DO NOTHING RETURNING (event_state_key_nid)"
|
2017-02-15 14:43:19 +00:00
|
|
|
|
|
|
|
const selectEventStateKeyNIDSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"SELECT event_state_key_nid FROM roomserver_event_state_keys" +
|
|
|
|
" WHERE event_state_key = $1"
|
2017-02-15 14:43:19 +00: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 10:51:46 +00:00
|
|
|
"SELECT event_state_key, event_state_key_nid FROM roomserver_event_state_keys" +
|
2017-02-15 14:43:19 +00:00
|
|
|
" WHERE event_state_key = ANY($1)"
|
|
|
|
|
2017-08-21 15:34:26 +00: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 14:43:19 +00:00
|
|
|
type eventStateKeyStatements struct {
|
|
|
|
insertEventStateKeyNIDStmt *sql.Stmt
|
|
|
|
selectEventStateKeyNIDStmt *sql.Stmt
|
|
|
|
bulkSelectEventStateKeyNIDStmt *sql.Stmt
|
2017-08-21 15:34:26 +00:00
|
|
|
bulkSelectEventStateKeyStmt *sql.Stmt
|
2017-02-15 14:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 12:25:57 +00:00
|
|
|
func createEventStateKeysTable(db *sql.DB) error {
|
2020-05-26 14:42:42 +00:00
|
|
|
_, err := db.Exec(eventStateKeysSchema)
|
2021-04-26 12:25:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareEventStateKeysTable(db *sql.DB) (tables.EventStateKeys, error) {
|
|
|
|
s := &eventStateKeyStatements{}
|
|
|
|
|
2021-07-28 17:30:04 +00:00
|
|
|
return s, sqlutil.StatementList{
|
2017-03-07 10:37:41 +00:00
|
|
|
{&s.insertEventStateKeyNIDStmt, insertEventStateKeyNIDSQL},
|
|
|
|
{&s.selectEventStateKeyNIDStmt, selectEventStateKeyNIDSQL},
|
|
|
|
{&s.bulkSelectEventStateKeyNIDStmt, bulkSelectEventStateKeyNIDSQL},
|
2017-08-21 15:34:26 +00:00
|
|
|
{&s.bulkSelectEventStateKeyStmt, bulkSelectEventStateKeySQL},
|
2020-05-27 10:03:47 +00:00
|
|
|
}.Prepare(db)
|
2017-02-15 14:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 14:42:42 +00:00
|
|
|
func (s *eventStateKeyStatements) InsertEventStateKeyNID(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2017-02-15 14:43:19 +00:00
|
|
|
var eventStateKeyNID int64
|
2020-06-12 13:55:57 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertEventStateKeyNIDStmt)
|
2017-09-13 15:30:19 +00:00
|
|
|
err := stmt.QueryRowContext(ctx, eventStateKey).Scan(&eventStateKeyNID)
|
2017-02-15 14:43:19 +00:00
|
|
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:42:42 +00:00
|
|
|
func (s *eventStateKeyStatements) SelectEventStateKeyNID(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2017-02-15 14:43:19 +00:00
|
|
|
var eventStateKeyNID int64
|
2020-06-12 13:55:57 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventStateKeyNIDStmt)
|
2017-09-13 15:30:19 +00:00
|
|
|
err := stmt.QueryRowContext(ctx, eventStateKey).Scan(&eventStateKeyNID)
|
2017-02-15 14:43:19 +00:00
|
|
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:42:42 +00:00
|
|
|
func (s *eventStateKeyStatements) BulkSelectEventStateKeyNID(
|
2022-02-04 10:39:34 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKeys []string,
|
2017-09-13 15:30:19 +00:00
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
2022-02-04 10:39:34 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectEventStateKeyNIDStmt)
|
|
|
|
rows, err := stmt.QueryContext(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx, pq.StringArray(eventStateKeys),
|
|
|
|
)
|
2017-02-15 14:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKeyNID: rows.close() failed")
|
2017-02-15 14:43:19 +00:00
|
|
|
|
|
|
|
result := make(map[string]types.EventStateKeyNID, len(eventStateKeys))
|
|
|
|
for rows.Next() {
|
|
|
|
var stateKey string
|
|
|
|
var stateKeyNID int64
|
|
|
|
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[stateKey] = types.EventStateKeyNID(stateKeyNID)
|
|
|
|
}
|
2020-02-11 14:12:21 +00:00
|
|
|
return result, rows.Err()
|
2017-02-15 14:43:19 +00:00
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
2020-05-26 14:42:42 +00:00
|
|
|
func (s *eventStateKeyStatements) BulkSelectEventStateKey(
|
2022-02-04 10:39:34 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKeyNIDs []types.EventStateKeyNID,
|
2017-09-13 15:30:19 +00:00
|
|
|
) (map[types.EventStateKeyNID]string, error) {
|
2018-06-18 11:03:08 +00:00
|
|
|
nIDs := make(pq.Int64Array, len(eventStateKeyNIDs))
|
2017-08-21 15:34:26 +00:00
|
|
|
for i := range eventStateKeyNIDs {
|
|
|
|
nIDs[i] = int64(eventStateKeyNIDs[i])
|
|
|
|
}
|
2022-02-04 10:39:34 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectEventStateKeyStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, nIDs)
|
2017-08-21 15:34:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKey: rows.close() failed")
|
2017-08-21 15:34:26 +00:00
|
|
|
|
|
|
|
result := make(map[types.EventStateKeyNID]string, len(eventStateKeyNIDs))
|
|
|
|
for rows.Next() {
|
|
|
|
var stateKey string
|
|
|
|
var stateKeyNID int64
|
|
|
|
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[types.EventStateKeyNID(stateKeyNID)] = stateKey
|
|
|
|
}
|
2020-02-11 14:12:21 +00:00
|
|
|
return result, rows.Err()
|
2017-08-21 15:34:26 +00:00
|
|
|
}
|