2017-08-02 16:21:35 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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 storage
|
|
|
|
|
|
|
|
import (
|
2017-09-18 16:52:22 +01:00
|
|
|
"context"
|
2017-08-02 16:21:35 +01:00
|
|
|
"database/sql"
|
|
|
|
|
2019-08-08 06:10:42 +01:00
|
|
|
"github.com/lib/pq"
|
2017-09-19 17:15:46 +01:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2019-08-08 06:10:42 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-08-02 16:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const accountDataSchema = `
|
2017-09-19 17:15:46 +01:00
|
|
|
-- This sequence is shared between all the tables generated from kafka logs.
|
|
|
|
CREATE SEQUENCE IF NOT EXISTS syncapi_stream_id;
|
|
|
|
|
|
|
|
-- Stores the types of account data that a user set has globally and in each room
|
|
|
|
-- and the stream ID when that type was last updated.
|
2017-08-07 11:51:46 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_account_data_type (
|
2017-09-19 17:15:46 +01:00
|
|
|
-- An incrementing ID which denotes the position in the log that this event resides at.
|
|
|
|
id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_stream_id'),
|
2017-08-02 16:21:35 +01:00
|
|
|
-- ID of the user the data belongs to
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
-- ID of the room the data is related to (empty string if not related to a specific room)
|
|
|
|
room_id TEXT NOT NULL,
|
|
|
|
-- Type of the data
|
|
|
|
type TEXT NOT NULL,
|
|
|
|
|
|
|
|
-- We don't want two entries of the same type for the same user
|
2017-08-07 11:51:46 +01:00
|
|
|
CONSTRAINT syncapi_account_data_unique UNIQUE (user_id, room_id, type)
|
2017-08-02 16:21:35 +01:00
|
|
|
);
|
|
|
|
|
2019-08-08 06:10:42 +01:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS syncapi_account_data_id_idx ON syncapi_account_data_type(id, type);
|
2017-08-02 16:21:35 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertAccountDataSQL = "" +
|
2017-09-19 17:15:46 +01:00
|
|
|
"INSERT INTO syncapi_account_data_type (user_id, room_id, type) VALUES ($1, $2, $3)" +
|
2017-08-07 11:51:46 +01:00
|
|
|
" ON CONFLICT ON CONSTRAINT syncapi_account_data_unique" +
|
2017-09-19 17:15:46 +01:00
|
|
|
" DO UPDATE SET id = EXCLUDED.id" +
|
|
|
|
" RETURNING id"
|
2017-08-02 16:21:35 +01:00
|
|
|
|
|
|
|
const selectAccountDataInRangeSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"SELECT room_id, type FROM syncapi_account_data_type" +
|
2017-08-02 16:21:35 +01:00
|
|
|
" WHERE user_id = $1 AND id > $2 AND id <= $3" +
|
2019-08-08 06:10:42 +01:00
|
|
|
" AND ( $4::text[] IS NULL OR type LIKE ANY($4) )" +
|
|
|
|
" AND ( $5::text[] IS NULL OR NOT(type LIKE ANY($5)) )" +
|
|
|
|
" ORDER BY id ASC LIMIT $6"
|
2017-08-02 16:21:35 +01:00
|
|
|
|
2017-09-19 17:15:46 +01:00
|
|
|
const selectMaxAccountDataIDSQL = "" +
|
|
|
|
"SELECT MAX(id) FROM syncapi_account_data_type"
|
|
|
|
|
2017-08-02 16:21:35 +01:00
|
|
|
type accountDataStatements struct {
|
|
|
|
insertAccountDataStmt *sql.Stmt
|
|
|
|
selectAccountDataInRangeStmt *sql.Stmt
|
2017-09-19 17:15:46 +01:00
|
|
|
selectMaxAccountDataIDStmt *sql.Stmt
|
2017-08-02 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
|
|
|
|
_, err = db.Exec(accountDataSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.selectAccountDataInRangeStmt, err = db.Prepare(selectAccountDataInRangeSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-19 17:15:46 +01:00
|
|
|
if s.selectMaxAccountDataIDStmt, err = db.Prepare(selectMaxAccountDataIDSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-08-02 16:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *accountDataStatements) insertAccountData(
|
2017-09-18 16:52:22 +01:00
|
|
|
ctx context.Context,
|
|
|
|
userID, roomID, dataType string,
|
2017-09-19 17:15:46 +01:00
|
|
|
) (pos int64, err error) {
|
2017-09-20 10:59:19 +01:00
|
|
|
err = s.insertAccountDataStmt.QueryRowContext(ctx, userID, roomID, dataType).Scan(&pos)
|
2017-08-02 16:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *accountDataStatements) selectAccountDataInRange(
|
2017-09-18 16:52:22 +01:00
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
2019-07-12 15:59:53 +01:00
|
|
|
oldPos, newPos int64,
|
2019-08-08 06:10:42 +01:00
|
|
|
accountDataFilterPart *gomatrixserverlib.FilterPart,
|
2017-08-02 16:21:35 +01:00
|
|
|
) (data map[string][]string, err error) {
|
|
|
|
data = make(map[string][]string)
|
|
|
|
|
|
|
|
// If both positions are the same, it means that the data was saved after the
|
|
|
|
// latest room event. In that case, we need to decrement the old position as
|
|
|
|
// it would prevent the SQL request from returning anything.
|
|
|
|
if oldPos == newPos {
|
|
|
|
oldPos--
|
|
|
|
}
|
|
|
|
|
2019-08-08 06:10:42 +01:00
|
|
|
rows, err := s.selectAccountDataInRangeStmt.QueryContext(ctx, userID, oldPos, newPos,
|
|
|
|
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.Types)),
|
|
|
|
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.NotTypes)),
|
|
|
|
accountDataFilterPart.Limit,
|
|
|
|
)
|
2017-08-02 16:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var dataType string
|
|
|
|
var roomID string
|
|
|
|
|
|
|
|
if err = rows.Scan(&roomID, &dataType); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data[roomID]) > 0 {
|
|
|
|
data[roomID] = append(data[roomID], dataType)
|
|
|
|
} else {
|
|
|
|
data[roomID] = []string{dataType}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-09-19 17:15:46 +01:00
|
|
|
|
|
|
|
func (s *accountDataStatements) selectMaxAccountDataID(
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
) (id int64, err error) {
|
|
|
|
var nullableID sql.NullInt64
|
|
|
|
stmt := common.TxStmt(txn, s.selectMaxAccountDataIDStmt)
|
|
|
|
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
|
|
|
|
if nullableID.Valid {
|
|
|
|
id = nullableID.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|