mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-02-07 03:24:27 -06:00
Move current work into single branch
This commit is contained in:
parent
054f5383c4
commit
f64e8feeec
|
@ -21,6 +21,7 @@ import (
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common/keydb/postgres"
|
"github.com/matrix-org/dendrite/common/keydb/postgres"
|
||||||
|
"github.com/matrix-org/dendrite/common/keydb/sqlite3"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -44,6 +45,8 @@ func NewDatabase(
|
||||||
switch uri.Scheme {
|
switch uri.Scheme {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return postgres.NewDatabase(dataSourceName, serverName, serverKey, serverKeyID)
|
return postgres.NewDatabase(dataSourceName, serverName, serverKey, serverKeyID)
|
||||||
|
case "file":
|
||||||
|
return sqlite3.NewDatabase(dataSourceName, serverName, serverKey, serverKeyID)
|
||||||
default:
|
default:
|
||||||
return postgres.NewDatabase(dataSourceName, serverName, serverKey, serverKeyID)
|
return postgres.NewDatabase(dataSourceName, serverName, serverKey, serverKeyID)
|
||||||
}
|
}
|
||||||
|
|
115
common/keydb/sqlite3/keydb.go
Normal file
115
common/keydb/sqlite3/keydb.go
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
// 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"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ed25519"
|
||||||
|
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Database implements gomatrixserverlib.KeyDatabase and is used to store
|
||||||
|
// the public keys for other matrix servers.
|
||||||
|
type Database struct {
|
||||||
|
statements serverKeyStatements
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDatabase prepares a new key database.
|
||||||
|
// It creates the necessary tables if they don't already exist.
|
||||||
|
// It prepares all the SQL statements that it will use.
|
||||||
|
// Returns an error if there was a problem talking to the database.
|
||||||
|
func NewDatabase(
|
||||||
|
dataSourceName string,
|
||||||
|
serverName gomatrixserverlib.ServerName,
|
||||||
|
serverKey ed25519.PublicKey,
|
||||||
|
serverKeyID gomatrixserverlib.KeyID,
|
||||||
|
) (*Database, error) {
|
||||||
|
db, err := sql.Open("sqlite3", dataSourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d := &Database{}
|
||||||
|
err = d.statements.prepare(db)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Store our own keys so that we don't end up making HTTP requests to find our
|
||||||
|
// own keys
|
||||||
|
index := gomatrixserverlib.PublicKeyLookupRequest{
|
||||||
|
ServerName: serverName,
|
||||||
|
KeyID: serverKeyID,
|
||||||
|
}
|
||||||
|
value := gomatrixserverlib.PublicKeyLookupResult{
|
||||||
|
VerifyKey: gomatrixserverlib.VerifyKey{
|
||||||
|
Key: gomatrixserverlib.Base64String(serverKey),
|
||||||
|
},
|
||||||
|
ValidUntilTS: math.MaxUint64 >> 1,
|
||||||
|
ExpiredTS: gomatrixserverlib.PublicKeyNotExpired,
|
||||||
|
}
|
||||||
|
err = d.StoreKeys(
|
||||||
|
context.Background(),
|
||||||
|
map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{
|
||||||
|
index: value,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetcherName implements KeyFetcher
|
||||||
|
func (d Database) FetcherName() string {
|
||||||
|
return "KeyDatabase"
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetchKeys implements gomatrixserverlib.KeyDatabase
|
||||||
|
func (d *Database) FetchKeys(
|
||||||
|
ctx context.Context,
|
||||||
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
||||||
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
||||||
|
return d.statements.bulkSelectServerKeys(ctx, requests)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StoreKeys implements gomatrixserverlib.KeyDatabase
|
||||||
|
func (d *Database) StoreKeys(
|
||||||
|
ctx context.Context,
|
||||||
|
keyMap map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
||||||
|
) error {
|
||||||
|
// TODO: Inserting all the keys within a single transaction may
|
||||||
|
// be more efficient since the transaction overhead can be quite
|
||||||
|
// high for a single insert statement.
|
||||||
|
var lastErr error
|
||||||
|
for request, keys := range keyMap {
|
||||||
|
if err := d.statements.upsertServerKeys(ctx, request, keys); err != nil {
|
||||||
|
// Rather than returning immediately on error we try to insert the
|
||||||
|
// remaining keys.
|
||||||
|
// Since we are inserting the keys outside of a transaction it is
|
||||||
|
// possible for some of the inserts to succeed even though some
|
||||||
|
// of the inserts have failed.
|
||||||
|
// Ensuring that we always insert all the keys we can means that
|
||||||
|
// this behaviour won't depend on the iteration order of the map.
|
||||||
|
lastErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
|
}
|
142
common/keydb/sqlite3/server_key_table.go
Normal file
142
common/keydb/sqlite3/server_key_table.go
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const serverKeysSchema = `
|
||||||
|
-- A cache of signing keys downloaded from remote servers.
|
||||||
|
CREATE TABLE IF NOT EXISTS keydb_server_keys (
|
||||||
|
-- The name of the matrix server the key is for.
|
||||||
|
server_name TEXT NOT NULL,
|
||||||
|
-- The ID of the server key.
|
||||||
|
server_key_id TEXT NOT NULL,
|
||||||
|
-- Combined server name and key ID separated by the ASCII unit separator
|
||||||
|
-- to make it easier to run bulk queries.
|
||||||
|
server_name_and_key_id TEXT NOT NULL,
|
||||||
|
-- When the key is valid until as a millisecond timestamp.
|
||||||
|
-- 0 if this is an expired key (in which case expired_ts will be non-zero)
|
||||||
|
valid_until_ts BIGINT NOT NULL,
|
||||||
|
-- When the key expired as a millisecond timestamp.
|
||||||
|
-- 0 if this is an active key (in which case valid_until_ts will be non-zero)
|
||||||
|
expired_ts BIGINT NOT NULL,
|
||||||
|
-- The base64-encoded public key.
|
||||||
|
server_key TEXT NOT NULL,
|
||||||
|
CONSTRAINT UNIQUE (server_name, server_key_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS keydb_server_name_and_key_id ON keydb_server_keys (server_name_and_key_id);
|
||||||
|
`
|
||||||
|
|
||||||
|
const bulkSelectServerKeysSQL = "" +
|
||||||
|
"SELECT server_name, server_key_id, valid_until_ts, expired_ts, " +
|
||||||
|
" server_key FROM keydb_server_keys" +
|
||||||
|
" WHERE server_name_and_key_id IN ($1)"
|
||||||
|
|
||||||
|
const upsertServerKeysSQL = "" +
|
||||||
|
"INSERT INTO keydb_server_keys (server_name, server_key_id," +
|
||||||
|
" server_name_and_key_id, valid_until_ts, expired_ts, server_key)" +
|
||||||
|
" VALUES ($1, $2, $3, $4, $5, $6)" +
|
||||||
|
" ON CONFLICT (server_name, server_key_id)" +
|
||||||
|
" DO UPDATE SET valid_until_ts = $4, expired_ts = $5, server_key = $6"
|
||||||
|
|
||||||
|
type serverKeyStatements struct {
|
||||||
|
bulkSelectServerKeysStmt *sql.Stmt
|
||||||
|
upsertServerKeysStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *serverKeyStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(serverKeysSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.bulkSelectServerKeysStmt, err = db.Prepare(bulkSelectServerKeysSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.upsertServerKeysStmt, err = db.Prepare(upsertServerKeysSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *serverKeyStatements) bulkSelectServerKeys(
|
||||||
|
ctx context.Context,
|
||||||
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
||||||
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
||||||
|
var nameAndKeyIDs []string
|
||||||
|
for request := range requests {
|
||||||
|
nameAndKeyIDs = append(nameAndKeyIDs, nameAndKeyID(request))
|
||||||
|
}
|
||||||
|
stmt := s.bulkSelectServerKeysStmt
|
||||||
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(nameAndKeyIDs))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
||||||
|
for rows.Next() {
|
||||||
|
var serverName string
|
||||||
|
var keyID string
|
||||||
|
var key string
|
||||||
|
var validUntilTS int64
|
||||||
|
var expiredTS int64
|
||||||
|
if err = rows.Scan(&serverName, &keyID, &validUntilTS, &expiredTS, &key); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r := gomatrixserverlib.PublicKeyLookupRequest{
|
||||||
|
ServerName: gomatrixserverlib.ServerName(serverName),
|
||||||
|
KeyID: gomatrixserverlib.KeyID(keyID),
|
||||||
|
}
|
||||||
|
vk := gomatrixserverlib.VerifyKey{}
|
||||||
|
err = vk.Key.Decode(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
results[r] = gomatrixserverlib.PublicKeyLookupResult{
|
||||||
|
VerifyKey: vk,
|
||||||
|
ValidUntilTS: gomatrixserverlib.Timestamp(validUntilTS),
|
||||||
|
ExpiredTS: gomatrixserverlib.Timestamp(expiredTS),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *serverKeyStatements) upsertServerKeys(
|
||||||
|
ctx context.Context,
|
||||||
|
request gomatrixserverlib.PublicKeyLookupRequest,
|
||||||
|
key gomatrixserverlib.PublicKeyLookupResult,
|
||||||
|
) error {
|
||||||
|
_, err := s.upsertServerKeysStmt.ExecContext(
|
||||||
|
ctx,
|
||||||
|
string(request.ServerName),
|
||||||
|
string(request.KeyID),
|
||||||
|
nameAndKeyID(request),
|
||||||
|
key.ValidUntilTS,
|
||||||
|
key.ExpiredTS,
|
||||||
|
key.Key.Encode(),
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func nameAndKeyID(request gomatrixserverlib.PublicKeyLookupRequest) string {
|
||||||
|
return string(request.ServerName) + "\x1F" + string(request.KeyID)
|
||||||
|
}
|
|
@ -29,7 +29,7 @@ CREATE TABLE IF NOT EXISTS ${prefix}_partition_offsets (
|
||||||
partition INTEGER NOT NULL,
|
partition INTEGER NOT NULL,
|
||||||
-- The 64-bit offset.
|
-- The 64-bit offset.
|
||||||
partition_offset BIGINT NOT NULL,
|
partition_offset BIGINT NOT NULL,
|
||||||
CONSTRAINT ${prefix}_topic_partition_unique UNIQUE (topic, partition)
|
CONSTRAINT UNIQUE (topic, partition)
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ const selectPartitionOffsetsSQL = "" +
|
||||||
|
|
||||||
const upsertPartitionOffsetsSQL = "" +
|
const upsertPartitionOffsetsSQL = "" +
|
||||||
"INSERT INTO ${prefix}_partition_offsets (topic, partition, partition_offset) VALUES ($1, $2, $3)" +
|
"INSERT INTO ${prefix}_partition_offsets (topic, partition, partition_offset) VALUES ($1, $2, $3)" +
|
||||||
" ON CONFLICT ON CONSTRAINT ${prefix}_topic_partition_unique" +
|
" ON CONFLICT (topic, partition)" +
|
||||||
" DO UPDATE SET partition_offset = $3"
|
" DO UPDATE SET partition_offset = $3"
|
||||||
|
|
||||||
// PartitionOffsetStatements represents a set of statements that can be run on a partition_offsets table.
|
// PartitionOffsetStatements represents a set of statements that can be run on a partition_offsets table.
|
||||||
|
|
102
roomserver/storage/sqlite3/event_json_table.go
Normal file
102
roomserver/storage/sqlite3/event_json_table.go
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const eventJSONSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_event_json (
|
||||||
|
event_nid INTEGER NOT NULL PRIMARY KEY,
|
||||||
|
event_json TEXT NOT NULL
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventJSONSQL = `
|
||||||
|
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES ($1, $2)
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
`
|
||||||
|
|
||||||
|
// Bulk event JSON lookup by numeric event ID.
|
||||||
|
// Sort by the numeric event ID.
|
||||||
|
// This means that we can use binary search to lookup by numeric event ID.
|
||||||
|
const bulkSelectEventJSONSQL = `
|
||||||
|
SELECT event_nid, event_json FROM roomserver_event_json
|
||||||
|
WHERE event_nid IN ($1)
|
||||||
|
ORDER BY event_nid ASC
|
||||||
|
`
|
||||||
|
|
||||||
|
type eventJSONStatements struct {
|
||||||
|
insertEventJSONStmt *sql.Stmt
|
||||||
|
bulkSelectEventJSONStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventJSONStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(eventJSONSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return statementList{
|
||||||
|
{&s.insertEventJSONStmt, insertEventJSONSQL},
|
||||||
|
{&s.bulkSelectEventJSONStmt, bulkSelectEventJSONSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventJSONStatements) insertEventJSON(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, eventJSON []byte,
|
||||||
|
) error {
|
||||||
|
_, err := common.TxStmt(txn, s.insertEventJSONStmt).ExecContext(ctx, int64(eventNID), eventJSON)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type eventJSONPair struct {
|
||||||
|
EventNID types.EventNID
|
||||||
|
EventJSON []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventJSONStatements) bulkSelectEventJSON(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
||||||
|
) ([]eventJSONPair, error) {
|
||||||
|
rows, err := common.TxStmt(txn, s.bulkSelectEventJSONStmt).QueryContext(ctx, eventNIDsAsArray(eventNIDs))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventJSON s.bulkSelectEventJSONStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
// We know that we will only get as many results as event NIDs
|
||||||
|
// because of the unique constraint on event NIDs.
|
||||||
|
// So we can allocate an array of the correct size now.
|
||||||
|
// We might get fewer results than NIDs so we adjust the length of the slice before returning it.
|
||||||
|
results := make([]eventJSONPair, len(eventNIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
result := &results[i]
|
||||||
|
var eventNID int64
|
||||||
|
if err := rows.Scan(&eventNID, &result.EventJSON); err != nil {
|
||||||
|
fmt.Println("bulkSelectEventJSON rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result.EventNID = types.EventNID(eventNID)
|
||||||
|
}
|
||||||
|
return results[:i], nil
|
||||||
|
}
|
168
roomserver/storage/sqlite3/event_state_keys_table.go
Normal file
168
roomserver/storage/sqlite3/event_state_keys_table.go
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const eventStateKeysSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_event_state_keys (
|
||||||
|
event_state_key_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
event_state_key TEXT NOT NULL UNIQUE
|
||||||
|
);
|
||||||
|
INSERT INTO roomserver_event_state_keys (event_state_key_nid, event_state_key)
|
||||||
|
VALUES (1, '')
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
// Same as insertEventTypeNIDSQL
|
||||||
|
const insertEventStateKeyNIDSQL = `
|
||||||
|
INSERT INTO roomserver_event_state_keys (event_state_key) VALUES ($1)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventStateKeyNIDResultSQL = `
|
||||||
|
SELECT event_state_key_nid FROM roomserver_event_state_keys
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectEventStateKeyNIDSQL = `
|
||||||
|
SELECT event_state_key_nid FROM roomserver_event_state_keys
|
||||||
|
WHERE event_state_key = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
// Bulk lookup from string state key to numeric ID for that state key.
|
||||||
|
// Takes an array of strings as the query parameter.
|
||||||
|
const bulkSelectEventStateKeyNIDSQL = `
|
||||||
|
SELECT event_state_key, event_state_key_nid FROM roomserver_event_state_keys
|
||||||
|
WHERE event_state_key IN ($1)
|
||||||
|
`
|
||||||
|
|
||||||
|
// 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 IN ($1)
|
||||||
|
`
|
||||||
|
|
||||||
|
type eventStateKeyStatements struct {
|
||||||
|
insertEventStateKeyNIDStmt *sql.Stmt
|
||||||
|
insertEventStateKeyNIDResultStmt *sql.Stmt
|
||||||
|
selectEventStateKeyNIDStmt *sql.Stmt
|
||||||
|
bulkSelectEventStateKeyNIDStmt *sql.Stmt
|
||||||
|
bulkSelectEventStateKeyStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStateKeyStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(eventStateKeysSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return statementList{
|
||||||
|
{&s.insertEventStateKeyNIDStmt, insertEventStateKeyNIDSQL},
|
||||||
|
{&s.insertEventStateKeyNIDResultStmt, insertEventStateKeyNIDResultSQL},
|
||||||
|
{&s.selectEventStateKeyNIDStmt, selectEventStateKeyNIDSQL},
|
||||||
|
{&s.bulkSelectEventStateKeyNIDStmt, bulkSelectEventStateKeyNIDSQL},
|
||||||
|
{&s.bulkSelectEventStateKeyStmt, bulkSelectEventStateKeySQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStateKeyStatements) insertEventStateKeyNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
||||||
|
) (types.EventStateKeyNID, error) {
|
||||||
|
var eventStateKeyNID int64
|
||||||
|
var err error
|
||||||
|
insertStmt := common.TxStmt(txn, s.insertEventStateKeyNIDStmt)
|
||||||
|
selectStmt := common.TxStmt(txn, s.insertEventStateKeyNIDResultStmt)
|
||||||
|
if _, err = insertStmt.ExecContext(ctx, eventStateKey); err == nil {
|
||||||
|
err = selectStmt.QueryRowContext(ctx).Scan(&eventStateKeyNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertEventStateKeyNID selectStmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("insertEventStateKeyNID insertStmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStateKeyStatements) selectEventStateKeyNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
||||||
|
) (types.EventStateKeyNID, error) {
|
||||||
|
var eventStateKeyNID int64
|
||||||
|
stmt := common.TxStmt(txn, s.selectEventStateKeyNIDStmt)
|
||||||
|
err := stmt.QueryRowContext(ctx, eventStateKey).Scan(&eventStateKeyNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectEventStateKeyNID stmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return types.EventStateKeyNID(eventStateKeyNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStateKeyStatements) bulkSelectEventStateKeyNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventStateKeys []string,
|
||||||
|
) (map[string]types.EventStateKeyNID, error) {
|
||||||
|
rows, err := common.TxStmt(txn, s.bulkSelectEventStateKeyNIDStmt).QueryContext(
|
||||||
|
ctx, sqliteInStr(pq.StringArray(eventStateKeys)),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventStateKeyNID s.bulkSelectEventStateKeyNIDStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
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 {
|
||||||
|
fmt.Println("bulkSelectEventStateKeyNID rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result[stateKey] = types.EventStateKeyNID(stateKeyNID)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStateKeyStatements) bulkSelectEventStateKey(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventStateKeyNIDs []types.EventStateKeyNID,
|
||||||
|
) (map[types.EventStateKeyNID]string, error) {
|
||||||
|
nIDs := make(pq.Int64Array, len(eventStateKeyNIDs))
|
||||||
|
for i := range eventStateKeyNIDs {
|
||||||
|
nIDs[i] = int64(eventStateKeyNIDs[i])
|
||||||
|
}
|
||||||
|
rows, err := common.TxStmt(txn, s.bulkSelectEventStateKeyStmt).QueryContext(ctx, nIDs)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventStateKey s.bulkSelectEventStateKeyStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
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 {
|
||||||
|
fmt.Println("bulkSelectEventStateKey rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result[types.EventStateKeyNID(stateKeyNID)] = stateKey
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
139
roomserver/storage/sqlite3/event_types_table.go
Normal file
139
roomserver/storage/sqlite3/event_types_table.go
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const eventTypesSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_event_types (
|
||||||
|
event_type_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
event_type TEXT NOT NULL UNIQUE
|
||||||
|
);
|
||||||
|
INSERT INTO roomserver_event_types (event_type_nid, event_type) VALUES
|
||||||
|
(1, 'm.room.create'),
|
||||||
|
(2, 'm.room.power_levels'),
|
||||||
|
(3, 'm.room.join_rules'),
|
||||||
|
(4, 'm.room.third_party_invite'),
|
||||||
|
(5, 'm.room.member'),
|
||||||
|
(6, 'm.room.redaction'),
|
||||||
|
(7, 'm.room.history_visibility') ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
// Assign a new numeric event type ID.
|
||||||
|
// The usual case is that the event type is not in the database.
|
||||||
|
// In that case the ID will be assigned using the next value from the sequence.
|
||||||
|
// We use `RETURNING` to tell postgres to return the assigned ID.
|
||||||
|
// But it's possible that the type was added in a query that raced with us.
|
||||||
|
// This will result in a conflict on the event_type_unique constraint, in this
|
||||||
|
// case we do nothing. Postgresql won't return a row in that case so we rely on
|
||||||
|
// the caller catching the sql.ErrNoRows error and running a select to get the row.
|
||||||
|
// We could get postgresql to return the row on a conflict by updating the row
|
||||||
|
// but it doesn't seem like a good idea to modify the rows just to make postgresql
|
||||||
|
// return it. Modifying the rows will cause postgres to assign a new tuple for the
|
||||||
|
// row even though the data doesn't change resulting in unncesssary modifications
|
||||||
|
// to the indexes.
|
||||||
|
const insertEventTypeNIDSQL = `
|
||||||
|
INSERT INTO roomserver_event_types (event_type) VALUES ($1)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventTypeNIDResultSQL = `
|
||||||
|
SELECT event_type_nid FROM roomserver_event_types
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectEventTypeNIDSQL = `
|
||||||
|
SELECT event_type_nid FROM roomserver_event_types WHERE event_type = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
// Bulk lookup from string event type to numeric ID for that event type.
|
||||||
|
// Takes an array of strings as the query parameter.
|
||||||
|
const bulkSelectEventTypeNIDSQL = `
|
||||||
|
SELECT event_type, event_type_nid FROM roomserver_event_types
|
||||||
|
WHERE event_type IN ($1)
|
||||||
|
`
|
||||||
|
|
||||||
|
type eventTypeStatements struct {
|
||||||
|
insertEventTypeNIDStmt *sql.Stmt
|
||||||
|
insertEventTypeNIDResultStmt *sql.Stmt
|
||||||
|
selectEventTypeNIDStmt *sql.Stmt
|
||||||
|
bulkSelectEventTypeNIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(eventTypesSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertEventTypeNIDStmt, insertEventTypeNIDSQL},
|
||||||
|
{&s.insertEventTypeNIDResultStmt, insertEventTypeNIDResultSQL},
|
||||||
|
{&s.selectEventTypeNIDStmt, selectEventTypeNIDSQL},
|
||||||
|
{&s.bulkSelectEventTypeNIDStmt, bulkSelectEventTypeNIDSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) insertEventTypeNID(
|
||||||
|
ctx context.Context, tx *sql.Tx, eventType string,
|
||||||
|
) (types.EventTypeNID, error) {
|
||||||
|
var eventTypeNID int64
|
||||||
|
var err error
|
||||||
|
insertStmt := common.TxStmt(tx, s.insertEventTypeNIDStmt)
|
||||||
|
resultStmt := common.TxStmt(tx, s.insertEventTypeNIDResultStmt)
|
||||||
|
if _, err = insertStmt.ExecContext(ctx, eventType); err == nil {
|
||||||
|
err = resultStmt.QueryRowContext(ctx).Scan(&eventTypeNID)
|
||||||
|
}
|
||||||
|
return types.EventTypeNID(eventTypeNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) selectEventTypeNID(
|
||||||
|
ctx context.Context, tx *sql.Tx, eventType string,
|
||||||
|
) (types.EventTypeNID, error) {
|
||||||
|
var eventTypeNID int64
|
||||||
|
selectStmt := common.TxStmt(tx, s.selectEventTypeNIDStmt)
|
||||||
|
err := selectStmt.QueryRowContext(ctx, eventType).Scan(&eventTypeNID)
|
||||||
|
return types.EventTypeNID(eventTypeNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) bulkSelectEventTypeNID(
|
||||||
|
ctx context.Context, tx *sql.Tx, eventTypes []string,
|
||||||
|
) (map[string]types.EventTypeNID, error) {
|
||||||
|
selectStmt := common.TxStmt(tx, s.bulkSelectEventTypeNIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteInStr(pq.StringArray(eventTypes)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
result := make(map[string]types.EventTypeNID, len(eventTypes))
|
||||||
|
for rows.Next() {
|
||||||
|
var eventType string
|
||||||
|
var eventTypeNID int64
|
||||||
|
if err := rows.Scan(&eventType, &eventTypeNID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result[eventType] = types.EventTypeNID(eventTypeNID)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
428
roomserver/storage/sqlite3/events_table.go
Normal file
428
roomserver/storage/sqlite3/events_table.go
Normal file
|
@ -0,0 +1,428 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const eventsSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_events (
|
||||||
|
event_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
room_nid INTEGER NOT NULL,
|
||||||
|
event_type_nid INTEGER NOT NULL,
|
||||||
|
event_state_key_nid INTEGER NOT NULL,
|
||||||
|
sent_to_output BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
state_snapshot_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
|
depth INTEGER NOT NULL,
|
||||||
|
event_id TEXT NOT NULL UNIQUE,
|
||||||
|
reference_sha256 BLOB NOT NULL,
|
||||||
|
auth_event_nids TEXT NOT NULL DEFAULT '{}'
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventSQL = `
|
||||||
|
INSERT INTO roomserver_events (room_nid, event_type_nid, event_state_key_nid, event_id, reference_sha256, auth_event_nids, depth)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventResultSQL = `
|
||||||
|
SELECT event_nid, state_snapshot_nid FROM roomserver_events
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectEventSQL = "" +
|
||||||
|
"SELECT event_nid, state_snapshot_nid FROM roomserver_events WHERE event_id = $1"
|
||||||
|
|
||||||
|
// Bulk lookup of events by string ID.
|
||||||
|
// Sort by the numeric IDs for event type and state key.
|
||||||
|
// This means we can use binary search to lookup entries by type and state key.
|
||||||
|
const bulkSelectStateEventByIDSQL = "" +
|
||||||
|
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
|
||||||
|
" WHERE event_id IN ($1)" +
|
||||||
|
" ORDER BY event_type_nid, event_state_key_nid ASC"
|
||||||
|
|
||||||
|
const bulkSelectStateAtEventByIDSQL = "" +
|
||||||
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid FROM roomserver_events" +
|
||||||
|
" WHERE event_id IN ($1)"
|
||||||
|
|
||||||
|
const updateEventStateSQL = "" +
|
||||||
|
"UPDATE roomserver_events SET state_snapshot_nid = $2 WHERE event_nid = $1"
|
||||||
|
|
||||||
|
const selectEventSentToOutputSQL = "" +
|
||||||
|
"SELECT sent_to_output FROM roomserver_events WHERE event_nid = $1"
|
||||||
|
|
||||||
|
const updateEventSentToOutputSQL = "" +
|
||||||
|
"UPDATE roomserver_events SET sent_to_output = TRUE WHERE event_nid = $1"
|
||||||
|
|
||||||
|
const selectEventIDSQL = "" +
|
||||||
|
"SELECT event_id FROM roomserver_events WHERE event_nid = $1"
|
||||||
|
|
||||||
|
const bulkSelectStateAtEventAndReferenceSQL = "" +
|
||||||
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid, event_id, reference_sha256" +
|
||||||
|
" FROM roomserver_events WHERE event_nid IN ($1)"
|
||||||
|
|
||||||
|
const bulkSelectEventReferenceSQL = "" +
|
||||||
|
"SELECT event_id, reference_sha256 FROM roomserver_events WHERE event_nid IN ($1)"
|
||||||
|
|
||||||
|
const bulkSelectEventIDSQL = "" +
|
||||||
|
"SELECT event_nid, event_id FROM roomserver_events WHERE event_nid IN ($1)"
|
||||||
|
|
||||||
|
const bulkSelectEventNIDSQL = "" +
|
||||||
|
"SELECT event_id, event_nid FROM roomserver_events WHERE event_id IN ($1)"
|
||||||
|
|
||||||
|
const selectMaxEventDepthSQL = "" +
|
||||||
|
"SELECT COALESCE(MAX(depth) + 1, 0) FROM roomserver_events WHERE event_nid IN ($1)"
|
||||||
|
|
||||||
|
type eventStatements struct {
|
||||||
|
insertEventStmt *sql.Stmt
|
||||||
|
insertEventResultStmt *sql.Stmt
|
||||||
|
selectEventStmt *sql.Stmt
|
||||||
|
bulkSelectStateEventByIDStmt *sql.Stmt
|
||||||
|
bulkSelectStateAtEventByIDStmt *sql.Stmt
|
||||||
|
updateEventStateStmt *sql.Stmt
|
||||||
|
selectEventSentToOutputStmt *sql.Stmt
|
||||||
|
updateEventSentToOutputStmt *sql.Stmt
|
||||||
|
selectEventIDStmt *sql.Stmt
|
||||||
|
bulkSelectStateAtEventAndReferenceStmt *sql.Stmt
|
||||||
|
bulkSelectEventReferenceStmt *sql.Stmt
|
||||||
|
bulkSelectEventIDStmt *sql.Stmt
|
||||||
|
bulkSelectEventNIDStmt *sql.Stmt
|
||||||
|
selectMaxEventDepthStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(eventsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertEventStmt, insertEventSQL},
|
||||||
|
{&s.insertEventResultStmt, insertEventResultSQL},
|
||||||
|
{&s.selectEventStmt, selectEventSQL},
|
||||||
|
{&s.bulkSelectStateEventByIDStmt, bulkSelectStateEventByIDSQL},
|
||||||
|
{&s.bulkSelectStateAtEventByIDStmt, bulkSelectStateAtEventByIDSQL},
|
||||||
|
{&s.updateEventStateStmt, updateEventStateSQL},
|
||||||
|
{&s.updateEventSentToOutputStmt, updateEventSentToOutputSQL},
|
||||||
|
{&s.selectEventSentToOutputStmt, selectEventSentToOutputSQL},
|
||||||
|
{&s.selectEventIDStmt, selectEventIDSQL},
|
||||||
|
{&s.bulkSelectStateAtEventAndReferenceStmt, bulkSelectStateAtEventAndReferenceSQL},
|
||||||
|
{&s.bulkSelectEventReferenceStmt, bulkSelectEventReferenceSQL},
|
||||||
|
{&s.bulkSelectEventIDStmt, bulkSelectEventIDSQL},
|
||||||
|
{&s.bulkSelectEventNIDStmt, bulkSelectEventNIDSQL},
|
||||||
|
{&s.selectMaxEventDepthStmt, selectMaxEventDepthSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) insertEvent(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
eventTypeNID types.EventTypeNID,
|
||||||
|
eventStateKeyNID types.EventStateKeyNID,
|
||||||
|
eventID string,
|
||||||
|
referenceSHA256 []byte,
|
||||||
|
authEventNIDs []types.EventNID,
|
||||||
|
depth int64,
|
||||||
|
) (types.EventNID, types.StateSnapshotNID, error) {
|
||||||
|
var eventNID int64
|
||||||
|
var stateNID int64
|
||||||
|
var err error
|
||||||
|
insertStmt := common.TxStmt(txn, s.insertEventStmt)
|
||||||
|
resultStmt := common.TxStmt(txn, s.insertEventResultStmt)
|
||||||
|
if _, err = insertStmt.ExecContext(
|
||||||
|
ctx, int64(roomNID), int64(eventTypeNID), int64(eventStateKeyNID),
|
||||||
|
eventID, referenceSHA256, eventNIDsAsArray(authEventNIDs), depth,
|
||||||
|
); err == nil {
|
||||||
|
err = resultStmt.QueryRowContext(ctx).Scan(&eventNID, &stateNID)
|
||||||
|
}
|
||||||
|
return types.EventNID(eventNID), types.StateSnapshotNID(stateNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) selectEvent(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventID string,
|
||||||
|
) (types.EventNID, types.StateSnapshotNID, error) {
|
||||||
|
var eventNID int64
|
||||||
|
var stateNID int64
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectEventStmt)
|
||||||
|
err := selectStmt.QueryRowContext(ctx, eventID).Scan(&eventNID, &stateNID)
|
||||||
|
return types.EventNID(eventNID), types.StateSnapshotNID(stateNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// bulkSelectStateEventByID lookups a list of state events by event ID.
|
||||||
|
// If any of the requested events are missing from the database it returns a types.MissingEventError
|
||||||
|
func (s *eventStatements) bulkSelectStateEventByID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
||||||
|
) ([]types.StateEntry, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectStateEventByIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteInStr(pq.StringArray(eventIDs)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
// We know that we will only get as many results as event IDs
|
||||||
|
// because of the unique constraint on event IDs.
|
||||||
|
// So we can allocate an array of the correct size now.
|
||||||
|
// We might get fewer results than IDs so we adjust the length of the slice before returning it.
|
||||||
|
results := make([]types.StateEntry, len(eventIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
result := &results[i]
|
||||||
|
if err = rows.Scan(
|
||||||
|
&result.EventTypeNID,
|
||||||
|
&result.EventStateKeyNID,
|
||||||
|
&result.EventNID,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i != len(eventIDs) {
|
||||||
|
// If there are fewer rows returned than IDs then we were asked to lookup event IDs we don't have.
|
||||||
|
// We don't know which ones were missing because we don't return the string IDs in the query.
|
||||||
|
// However it should be possible debug this by replaying queries or entries from the input kafka logs.
|
||||||
|
// If this turns out to be impossible and we do need the debug information here, it would be better
|
||||||
|
// to do it as a separate query rather than slowing down/complicating the common case.
|
||||||
|
return nil, types.MissingEventError(
|
||||||
|
fmt.Sprintf("storage: state event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return results, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// bulkSelectStateAtEventByID lookups the state at a list of events by event ID.
|
||||||
|
// If any of the requested events are missing from the database it returns a types.MissingEventError.
|
||||||
|
// If we do not have the state for any of the requested events it returns a types.MissingEventError.
|
||||||
|
func (s *eventStatements) bulkSelectStateAtEventByID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
||||||
|
) ([]types.StateAtEvent, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectStateAtEventByIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteInStr(pq.StringArray(eventIDs)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make([]types.StateAtEvent, len(eventIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
result := &results[i]
|
||||||
|
if err = rows.Scan(
|
||||||
|
&result.EventTypeNID,
|
||||||
|
&result.EventStateKeyNID,
|
||||||
|
&result.EventNID,
|
||||||
|
&result.BeforeStateSnapshotNID,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if result.BeforeStateSnapshotNID == 0 {
|
||||||
|
return nil, types.MissingEventError(
|
||||||
|
fmt.Sprintf("storage: missing state for event NID %d", result.EventNID),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i != len(eventIDs) {
|
||||||
|
return nil, types.MissingEventError(
|
||||||
|
fmt.Sprintf("storage: event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return results, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) updateEventState(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
||||||
|
) error {
|
||||||
|
updateStmt := common.TxStmt(txn, s.updateEventStateStmt)
|
||||||
|
_, err := updateStmt.ExecContext(ctx, int64(eventNID), int64(stateNID))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("updateEventState s.updateEventStateStmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) selectEventSentToOutput(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
||||||
|
) (sentToOutput bool, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectEventSentToOutputStmt)
|
||||||
|
err = selectStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&sentToOutput)
|
||||||
|
//err = s.selectEventSentToOutputStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&sentToOutput)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectEventSentToOutput stmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) updateEventSentToOutput(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) error {
|
||||||
|
updateStmt := common.TxStmt(txn, s.updateEventSentToOutputStmt)
|
||||||
|
_, err := updateStmt.ExecContext(ctx, int64(eventNID))
|
||||||
|
//_, err := s.updateEventSentToOutputStmt.ExecContext(ctx, int64(eventNID))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("updateEventSentToOutput stmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) selectEventID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
||||||
|
) (eventID string, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectEventIDStmt)
|
||||||
|
err = selectStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&eventID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectEventID stmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) bulkSelectStateAtEventAndReference(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
||||||
|
) ([]types.StateAtEventAndReference, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectStateAtEventAndReferenceStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteIn(eventNIDsAsArray(eventNIDs)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectStateAtEventAndREference stmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make([]types.StateAtEventAndReference, len(eventNIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
var (
|
||||||
|
eventTypeNID int64
|
||||||
|
eventStateKeyNID int64
|
||||||
|
eventNID int64
|
||||||
|
stateSnapshotNID int64
|
||||||
|
eventID string
|
||||||
|
eventSHA256 []byte
|
||||||
|
)
|
||||||
|
if err = rows.Scan(
|
||||||
|
&eventTypeNID, &eventStateKeyNID, &eventNID, &stateSnapshotNID, &eventID, &eventSHA256,
|
||||||
|
); err != nil {
|
||||||
|
fmt.Println("bulkSelectStateAtEventAndReference rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &results[i]
|
||||||
|
result.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
||||||
|
result.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
||||||
|
result.EventNID = types.EventNID(eventNID)
|
||||||
|
result.BeforeStateSnapshotNID = types.StateSnapshotNID(stateSnapshotNID)
|
||||||
|
result.EventID = eventID
|
||||||
|
result.EventSHA256 = eventSHA256
|
||||||
|
}
|
||||||
|
if i != len(eventNIDs) {
|
||||||
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) bulkSelectEventReference(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
||||||
|
) ([]gomatrixserverlib.EventReference, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectEventReferenceStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteIn(eventNIDsAsArray(eventNIDs)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventReference s.bulkSelectEventReferenceStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make([]gomatrixserverlib.EventReference, len(eventNIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
result := &results[i]
|
||||||
|
if err = rows.Scan(&result.EventID, &result.EventSHA256); err != nil {
|
||||||
|
fmt.Println("bulkSelectEventReference rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i != len(eventNIDs) {
|
||||||
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bulkSelectEventID returns a map from numeric event ID to string event ID.
|
||||||
|
func (s *eventStatements) bulkSelectEventID(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (map[types.EventNID]string, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectEventIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteIn(eventNIDsAsArray(eventNIDs)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventID s.bulkSelectEventIDStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make(map[types.EventNID]string, len(eventNIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
var eventNID int64
|
||||||
|
var eventID string
|
||||||
|
if err = rows.Scan(&eventNID, &eventID); err != nil {
|
||||||
|
fmt.Println("bulkSelectEventID rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
results[types.EventNID(eventNID)] = eventID
|
||||||
|
}
|
||||||
|
if i != len(eventNIDs) {
|
||||||
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bulkSelectEventNIDs returns a map from string event ID to numeric event ID.
|
||||||
|
// If an event ID is not in the database then it is omitted from the map.
|
||||||
|
func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventNID, error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectEventNIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteInStr(pq.StringArray(eventIDs)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectEventNID s.bulkSelectEventNIDStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make(map[string]types.EventNID, len(eventIDs))
|
||||||
|
for rows.Next() {
|
||||||
|
var eventID string
|
||||||
|
var eventNID int64
|
||||||
|
if err = rows.Scan(&eventID, &eventNID); err != nil {
|
||||||
|
fmt.Println("bulkSelectEventNID rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
results[eventID] = types.EventNID(eventNID)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventStatements) selectMaxEventDepth(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (int64, error) {
|
||||||
|
var result int64
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectMaxEventDepthStmt)
|
||||||
|
err := selectStmt.QueryRowContext(ctx, sqliteIn(eventNIDsAsArray(eventNIDs))).Scan(&result)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectMaxEventDepth stmt.QueryRowContext:", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventNIDsAsArray(eventNIDs []types.EventNID) pq.Int64Array {
|
||||||
|
nids := make([]int64, len(eventNIDs))
|
||||||
|
for i := range eventNIDs {
|
||||||
|
nids[i] = int64(eventNIDs[i])
|
||||||
|
}
|
||||||
|
return nids
|
||||||
|
}
|
149
roomserver/storage/sqlite3/invite_table.go
Normal file
149
roomserver/storage/sqlite3/invite_table.go
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const inviteSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_invites (
|
||||||
|
invite_event_id TEXT PRIMARY KEY,
|
||||||
|
room_nid INTEGER NOT NULL,
|
||||||
|
target_nid INTEGER NOT NULL,
|
||||||
|
sender_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
|
retired BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
invite_event_json TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS roomserver_invites_active_idx ON roomserver_invites (target_nid, room_nid)
|
||||||
|
WHERE NOT retired;
|
||||||
|
`
|
||||||
|
const insertInviteEventSQL = "" +
|
||||||
|
"INSERT INTO roomserver_invites (invite_event_id, room_nid, target_nid," +
|
||||||
|
" sender_nid, invite_event_json) VALUES ($1, $2, $3, $4, $5)" +
|
||||||
|
" ON CONFLICT DO NOTHING"
|
||||||
|
|
||||||
|
const selectInviteActiveForUserInRoomSQL = "" +
|
||||||
|
"SELECT sender_nid FROM roomserver_invites" +
|
||||||
|
" WHERE target_nid = $1 AND room_nid = $2" +
|
||||||
|
" AND NOT retired"
|
||||||
|
|
||||||
|
// Retire every active invite for a user in a room.
|
||||||
|
// Ideally we'd know which invite events were retired by a given update so we
|
||||||
|
// wouldn't need to remove every active invite.
|
||||||
|
// However the matrix protocol doesn't give us a way to reliably identify the
|
||||||
|
// invites that were retired, so we are forced to retire all of them.
|
||||||
|
const updateInviteRetiredSQL = `
|
||||||
|
UPDATE roomserver_invites SET retired = TRUE
|
||||||
|
WHERE room_nid = $1 AND target_nid = $2 AND NOT retired;
|
||||||
|
SELECT invite_event_id FROM roomserver_invites
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
type inviteStatements struct {
|
||||||
|
insertInviteEventStmt *sql.Stmt
|
||||||
|
selectInviteActiveForUserInRoomStmt *sql.Stmt
|
||||||
|
updateInviteRetiredStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(inviteSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertInviteEventStmt, insertInviteEventSQL},
|
||||||
|
{&s.selectInviteActiveForUserInRoomStmt, selectInviteActiveForUserInRoomSQL},
|
||||||
|
{&s.updateInviteRetiredStmt, updateInviteRetiredSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteStatements) insertInviteEvent(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx, inviteEventID string, roomNID types.RoomNID,
|
||||||
|
targetUserNID, senderUserNID types.EventStateKeyNID,
|
||||||
|
inviteEventJSON []byte,
|
||||||
|
) (bool, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.insertInviteEventStmt)
|
||||||
|
defer stmt.Close()
|
||||||
|
result, err := stmt.ExecContext(
|
||||||
|
ctx, inviteEventID, roomNID, targetUserNID, senderUserNID, inviteEventJSON,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertInviteEvent common.TxStmt.ExecContext:", err)
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
count, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertInviteEvent result.RowsAffected:", err)
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return count != 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteStatements) updateInviteRetired(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
|
||||||
|
) (eventIDs []string, err error) {
|
||||||
|
stmt := common.TxStmt(txn, s.updateInviteRetiredStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomNID, targetUserNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("updateInviteRetired stmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer (func() { err = rows.Close() })()
|
||||||
|
for rows.Next() {
|
||||||
|
var inviteEventID string
|
||||||
|
if err := rows.Scan(&inviteEventID); err != nil {
|
||||||
|
fmt.Println("updateInviteRetired rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
eventIDs = append(eventIDs, inviteEventID)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectInviteActiveForUserInRoom returns a list of sender state key NIDs
|
||||||
|
func (s *inviteStatements) selectInviteActiveForUserInRoom(
|
||||||
|
ctx context.Context,
|
||||||
|
targetUserNID types.EventStateKeyNID, roomNID types.RoomNID,
|
||||||
|
) ([]types.EventStateKeyNID, error) {
|
||||||
|
rows, err := s.selectInviteActiveForUserInRoomStmt.QueryContext(
|
||||||
|
ctx, targetUserNID, roomNID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectInviteActiveForUserInRoom s.selectInviteActiveForUserInRoomStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
var result []types.EventStateKeyNID
|
||||||
|
for rows.Next() {
|
||||||
|
var senderUserNID int64
|
||||||
|
if err := rows.Scan(&senderUserNID); err != nil {
|
||||||
|
fmt.Println("selectInviteActiveForUserInRoom rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, types.EventStateKeyNID(senderUserNID))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
22
roomserver/storage/sqlite3/list.go
Normal file
22
roomserver/storage/sqlite3/list.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SqliteList string
|
||||||
|
|
||||||
|
func sqliteIn(a pq.Int64Array) string {
|
||||||
|
var b []string
|
||||||
|
for _, n := range a {
|
||||||
|
b = append(b, strconv.FormatInt(n, 10))
|
||||||
|
}
|
||||||
|
return strings.Join(b, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func sqliteInStr(a pq.StringArray) string {
|
||||||
|
return "\"" + strings.Join(a, "\",\"") + "\""
|
||||||
|
}
|
195
roomserver/storage/sqlite3/membership_table.go
Normal file
195
roomserver/storage/sqlite3/membership_table.go
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type membershipState int64
|
||||||
|
|
||||||
|
const (
|
||||||
|
membershipStateLeaveOrBan membershipState = 1
|
||||||
|
membershipStateInvite membershipState = 2
|
||||||
|
membershipStateJoin membershipState = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
const membershipSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_membership (
|
||||||
|
room_nid INTEGER NOT NULL,
|
||||||
|
target_nid INTEGER NOT NULL,
|
||||||
|
sender_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
|
membership_nid INTEGER NOT NULL DEFAULT 1,
|
||||||
|
event_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
|
UNIQUE (room_nid, target_nid)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
// Insert a row in to membership table so that it can be locked by the
|
||||||
|
// SELECT FOR UPDATE
|
||||||
|
const insertMembershipSQL = "" +
|
||||||
|
"INSERT INTO roomserver_membership (room_nid, target_nid)" +
|
||||||
|
" VALUES ($1, $2)" +
|
||||||
|
" ON CONFLICT DO NOTHING"
|
||||||
|
|
||||||
|
const selectMembershipFromRoomAndTargetSQL = "" +
|
||||||
|
"SELECT membership_nid, event_nid FROM roomserver_membership" +
|
||||||
|
" WHERE room_nid = $1 AND target_nid = $2"
|
||||||
|
|
||||||
|
const selectMembershipsFromRoomAndMembershipSQL = "" +
|
||||||
|
"SELECT event_nid FROM roomserver_membership" +
|
||||||
|
" WHERE room_nid = $1 AND membership_nid = $2"
|
||||||
|
|
||||||
|
const selectMembershipsFromRoomSQL = "" +
|
||||||
|
"SELECT event_nid FROM roomserver_membership" +
|
||||||
|
" WHERE room_nid = $1"
|
||||||
|
|
||||||
|
const selectMembershipForUpdateSQL = "" +
|
||||||
|
"SELECT membership_nid FROM roomserver_membership" +
|
||||||
|
" WHERE room_nid = $1 AND target_nid = $2"
|
||||||
|
|
||||||
|
const updateMembershipSQL = "" +
|
||||||
|
"UPDATE roomserver_membership SET sender_nid = $3, membership_nid = $4, event_nid = $5" +
|
||||||
|
" WHERE room_nid = $1 AND target_nid = $2"
|
||||||
|
|
||||||
|
type membershipStatements struct {
|
||||||
|
insertMembershipStmt *sql.Stmt
|
||||||
|
selectMembershipForUpdateStmt *sql.Stmt
|
||||||
|
selectMembershipFromRoomAndTargetStmt *sql.Stmt
|
||||||
|
selectMembershipsFromRoomAndMembershipStmt *sql.Stmt
|
||||||
|
selectMembershipsFromRoomStmt *sql.Stmt
|
||||||
|
updateMembershipStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(membershipSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertMembershipStmt, insertMembershipSQL},
|
||||||
|
{&s.selectMembershipForUpdateStmt, selectMembershipForUpdateSQL},
|
||||||
|
{&s.selectMembershipFromRoomAndTargetStmt, selectMembershipFromRoomAndTargetSQL},
|
||||||
|
{&s.selectMembershipsFromRoomAndMembershipStmt, selectMembershipsFromRoomAndMembershipSQL},
|
||||||
|
{&s.selectMembershipsFromRoomStmt, selectMembershipsFromRoomSQL},
|
||||||
|
{&s.updateMembershipStmt, updateMembershipSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) insertMembership(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
|
||||||
|
) error {
|
||||||
|
stmt := common.TxStmt(txn, s.insertMembershipStmt)
|
||||||
|
_, err := stmt.ExecContext(ctx, roomNID, targetUserNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertMembership stmt.ExecContent:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) selectMembershipForUpdate(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
|
||||||
|
) (membership membershipState, err error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectMembershipForUpdateStmt)
|
||||||
|
err = stmt.QueryRowContext(
|
||||||
|
ctx, roomNID, targetUserNID,
|
||||||
|
).Scan(&membership)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectMembershipForUpdate common.TxStmt.Scan:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) selectMembershipFromRoomAndTarget(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
|
||||||
|
) (eventNID types.EventNID, membership membershipState, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectMembershipFromRoomAndTargetStmt)
|
||||||
|
err = selectStmt.QueryRowContext(
|
||||||
|
ctx, roomNID, targetUserNID,
|
||||||
|
).Scan(&membership, &eventNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectMembershipForUpdate s.selectMembershipFromRoomAndTargetStmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) selectMembershipsFromRoom(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
) (eventNIDs []types.EventNID, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectMembershipsFromRoomStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, roomNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectMembershipsFromRoom s.selectMembershipsFromRoomStmt.QueryContext:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var eNID types.EventNID
|
||||||
|
if err = rows.Scan(&eNID); err != nil {
|
||||||
|
fmt.Println("selectMembershipsFromRoom rows.Scan:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eventNIDs = append(eventNIDs, eNID)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func (s *membershipStatements) selectMembershipsFromRoomAndMembership(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID, membership membershipState,
|
||||||
|
) (eventNIDs []types.EventNID, err error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectMembershipsFromRoomAndMembershipStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomNID, membership)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectMembershipsFromRoomAndMembership stmt.QueryContext:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var eNID types.EventNID
|
||||||
|
if err = rows.Scan(&eNID); err != nil {
|
||||||
|
fmt.Println("selectMembershipsFromRoomAndMembership rows.Scan:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eventNIDs = append(eventNIDs, eNID)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *membershipStatements) updateMembership(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
|
||||||
|
senderUserNID types.EventStateKeyNID, membership membershipState,
|
||||||
|
eventNID types.EventNID,
|
||||||
|
) error {
|
||||||
|
stmt := common.TxStmt(txn, s.updateMembershipStmt)
|
||||||
|
_, err := stmt.ExecContext(
|
||||||
|
ctx, roomNID, targetUserNID, senderUserNID, membership, eventNID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("updateMembership common.TxStmt.ExecContent:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
36
roomserver/storage/sqlite3/prepare.go
Normal file
36
roomserver/storage/sqlite3/prepare.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// 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 (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
// a statementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement.
|
||||||
|
type statementList []struct {
|
||||||
|
statement **sql.Stmt
|
||||||
|
sql string
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare the SQL for each statement in the list and assign the result to the prepared statement.
|
||||||
|
func (s statementList) prepare(db *sql.DB) (err error) {
|
||||||
|
for _, statement := range s {
|
||||||
|
if *statement.statement, err = db.Prepare(statement.sql); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
99
roomserver/storage/sqlite3/previous_events_table.go
Normal file
99
roomserver/storage/sqlite3/previous_events_table.go
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"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 {
|
||||||
|
insertPreviousEventStmt *sql.Stmt
|
||||||
|
selectPreviousEventExistsStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *previousEventStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(previousEventSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertPreviousEventStmt, insertPreviousEventSQL},
|
||||||
|
{&s.selectPreviousEventExistsStmt, selectPreviousEventExistsSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *previousEventStatements) insertPreviousEvent(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
previousEventID string,
|
||||||
|
previousEventReferenceSHA256 []byte,
|
||||||
|
eventNID types.EventNID,
|
||||||
|
) error {
|
||||||
|
stmt := common.TxStmt(txn, s.insertPreviousEventStmt)
|
||||||
|
_, err := stmt.ExecContext(
|
||||||
|
ctx, previousEventID, previousEventReferenceSHA256, int64(eventNID),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertPreviousEvent stmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the event reference exists
|
||||||
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
||||||
|
func (s *previousEventStatements) selectPreviousEventExists(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
|
||||||
|
) error {
|
||||||
|
var ok int64
|
||||||
|
stmt := common.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
||||||
|
defer func() {
|
||||||
|
fmt.Println("SELECTED PREVIOUS EVENT EXISTS", ok)
|
||||||
|
}()
|
||||||
|
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
|
||||||
|
}
|
141
roomserver/storage/sqlite3/room_aliases_table.go
Normal file
141
roomserver/storage/sqlite3/room_aliases_table.go
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
const roomAliasesSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_room_aliases (
|
||||||
|
alias TEXT NOT NULL PRIMARY KEY,
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
creator_id TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS roomserver_room_id_idx ON roomserver_room_aliases(room_id);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertRoomAliasSQL = `
|
||||||
|
INSERT INTO roomserver_room_aliases (alias, room_id, creator_id) VALUES ($1, $2, $3)
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectRoomIDFromAliasSQL = `
|
||||||
|
SELECT room_id FROM roomserver_room_aliases WHERE alias = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectAliasesFromRoomIDSQL = `
|
||||||
|
SELECT alias FROM roomserver_room_aliases WHERE room_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectCreatorIDFromAliasSQL = `
|
||||||
|
SELECT creator_id FROM roomserver_room_aliases WHERE alias = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
const deleteRoomAliasSQL = `
|
||||||
|
DELETE FROM roomserver_room_aliases WHERE alias = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type roomAliasesStatements struct {
|
||||||
|
insertRoomAliasStmt *sql.Stmt
|
||||||
|
selectRoomIDFromAliasStmt *sql.Stmt
|
||||||
|
selectAliasesFromRoomIDStmt *sql.Stmt
|
||||||
|
selectCreatorIDFromAliasStmt *sql.Stmt
|
||||||
|
deleteRoomAliasStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(roomAliasesSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return statementList{
|
||||||
|
{&s.insertRoomAliasStmt, insertRoomAliasSQL},
|
||||||
|
{&s.selectRoomIDFromAliasStmt, selectRoomIDFromAliasSQL},
|
||||||
|
{&s.selectAliasesFromRoomIDStmt, selectAliasesFromRoomIDSQL},
|
||||||
|
{&s.selectCreatorIDFromAliasStmt, selectCreatorIDFromAliasSQL},
|
||||||
|
{&s.deleteRoomAliasStmt, deleteRoomAliasSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) insertRoomAlias(
|
||||||
|
ctx context.Context, txn *sql.Tx, alias string, roomID string, creatorUserID string,
|
||||||
|
) (err error) {
|
||||||
|
insertStmt := common.TxStmt(txn, s.insertRoomAliasStmt)
|
||||||
|
_, err = insertStmt.ExecContext(ctx, alias, roomID, creatorUserID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertRoomAlias s.insertRoomAliasStmt.ExecContent:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) selectRoomIDFromAlias(
|
||||||
|
ctx context.Context, txn *sql.Tx, alias string,
|
||||||
|
) (roomID string, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectRoomIDFromAliasStmt)
|
||||||
|
err = selectStmt.QueryRowContext(ctx, alias).Scan(&roomID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) selectAliasesFromRoomID(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomID string,
|
||||||
|
) (aliases []string, err error) {
|
||||||
|
aliases = []string{}
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectAliasesFromRoomIDStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, roomID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectAliasesFromRoomID s.selectAliasesFromRoomIDStmt.QueryContext:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var alias string
|
||||||
|
if err = rows.Scan(&alias); err != nil {
|
||||||
|
fmt.Println("selectAliasesFromRoomID rows.Scan:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
aliases = append(aliases, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) selectCreatorIDFromAlias(
|
||||||
|
ctx context.Context, txn *sql.Tx, alias string,
|
||||||
|
) (creatorID string, err error) {
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectCreatorIDFromAliasStmt)
|
||||||
|
err = selectStmt.QueryRowContext(ctx, alias).Scan(&creatorID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomAliasesStatements) deleteRoomAlias(
|
||||||
|
ctx context.Context, txn *sql.Tx, alias string,
|
||||||
|
) (err error) {
|
||||||
|
deleteStmt := common.TxStmt(txn, s.deleteRoomAliasStmt)
|
||||||
|
_, err = deleteStmt.ExecContext(ctx, alias)
|
||||||
|
return
|
||||||
|
}
|
172
roomserver/storage/sqlite3/rooms_table.go
Normal file
172
roomserver/storage/sqlite3/rooms_table.go
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const roomsSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_rooms (
|
||||||
|
room_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
room_id TEXT NOT NULL UNIQUE,
|
||||||
|
latest_event_nids TEXT NOT NULL DEFAULT '{}',
|
||||||
|
last_event_sent_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
|
state_snapshot_nid INTEGER NOT NULL DEFAULT 0
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
// Same as insertEventTypeNIDSQL
|
||||||
|
const insertRoomNIDSQL = `
|
||||||
|
INSERT INTO roomserver_rooms (room_id) VALUES ($1)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertRoomNIDResultSQL = `
|
||||||
|
SELECT room_nid FROM roomserver_rooms
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectRoomNIDSQL = "" +
|
||||||
|
"SELECT room_nid FROM roomserver_rooms WHERE room_id = $1"
|
||||||
|
|
||||||
|
const selectLatestEventNIDsSQL = "" +
|
||||||
|
"SELECT latest_event_nids, state_snapshot_nid FROM roomserver_rooms WHERE room_nid = $1"
|
||||||
|
|
||||||
|
const selectLatestEventNIDsForUpdateSQL = "" +
|
||||||
|
"SELECT latest_event_nids, last_event_sent_nid, state_snapshot_nid FROM roomserver_rooms WHERE room_nid = $1"
|
||||||
|
|
||||||
|
const updateLatestEventNIDsSQL = "" +
|
||||||
|
"UPDATE roomserver_rooms SET latest_event_nids = $2, last_event_sent_nid = $3, state_snapshot_nid = $4 WHERE room_nid = $1"
|
||||||
|
|
||||||
|
type roomStatements struct {
|
||||||
|
insertRoomNIDStmt *sql.Stmt
|
||||||
|
insertRoomNIDResultStmt *sql.Stmt
|
||||||
|
selectRoomNIDStmt *sql.Stmt
|
||||||
|
selectLatestEventNIDsStmt *sql.Stmt
|
||||||
|
selectLatestEventNIDsForUpdateStmt *sql.Stmt
|
||||||
|
updateLatestEventNIDsStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(roomsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return statementList{
|
||||||
|
{&s.insertRoomNIDStmt, insertRoomNIDSQL},
|
||||||
|
{&s.insertRoomNIDResultStmt, insertRoomNIDResultSQL},
|
||||||
|
{&s.selectRoomNIDStmt, selectRoomNIDSQL},
|
||||||
|
{&s.selectLatestEventNIDsStmt, selectLatestEventNIDsSQL},
|
||||||
|
{&s.selectLatestEventNIDsForUpdateStmt, selectLatestEventNIDsForUpdateSQL},
|
||||||
|
{&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) insertRoomNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomID string,
|
||||||
|
) (types.RoomNID, error) {
|
||||||
|
var roomNID int64
|
||||||
|
var err error
|
||||||
|
insertStmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
||||||
|
resultStmt := common.TxStmt(txn, s.insertRoomNIDResultStmt)
|
||||||
|
if _, err = insertStmt.ExecContext(ctx, roomID); err == nil {
|
||||||
|
err = resultStmt.QueryRowContext(ctx).Scan(&roomNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertRoomNID resultStmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("insertRoomNID insertStmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return types.RoomNID(roomNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectRoomNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomID string,
|
||||||
|
) (types.RoomNID, error) {
|
||||||
|
var roomNID int64
|
||||||
|
stmt := common.TxStmt(txn, s.selectRoomNIDStmt)
|
||||||
|
err := stmt.QueryRowContext(ctx, roomID).Scan(&roomNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectRoomNID stmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return types.RoomNID(roomNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectLatestEventNIDs(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||||
|
) ([]types.EventNID, types.StateSnapshotNID, error) {
|
||||||
|
var nids pq.Int64Array
|
||||||
|
var stateSnapshotNID int64
|
||||||
|
stmt := common.TxStmt(txn, s.selectLatestEventNIDsStmt)
|
||||||
|
err := stmt.QueryRowContext(ctx, int64(roomNID)).Scan(&nids, &stateSnapshotNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectLatestEventNIDs stmt.QueryRowContext:", err)
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
eventNIDs := make([]types.EventNID, len(nids))
|
||||||
|
for i := range nids {
|
||||||
|
eventNIDs[i] = types.EventNID(nids[i])
|
||||||
|
}
|
||||||
|
return eventNIDs, types.StateSnapshotNID(stateSnapshotNID), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectLatestEventsNIDsForUpdate(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||||
|
) ([]types.EventNID, types.EventNID, types.StateSnapshotNID, error) {
|
||||||
|
var nids pq.Int64Array
|
||||||
|
var lastEventSentNID int64
|
||||||
|
var stateSnapshotNID int64
|
||||||
|
stmt := common.TxStmt(txn, s.selectLatestEventNIDsForUpdateStmt)
|
||||||
|
err := stmt.QueryRowContext(ctx, int64(roomNID)).Scan(&nids, &lastEventSentNID, &stateSnapshotNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectLatestEventsNIDsForUpdate stmt.QueryRowContext:", err)
|
||||||
|
return nil, 0, 0, err
|
||||||
|
}
|
||||||
|
eventNIDs := make([]types.EventNID, len(nids))
|
||||||
|
for i := range nids {
|
||||||
|
eventNIDs[i] = types.EventNID(nids[i])
|
||||||
|
}
|
||||||
|
return eventNIDs, types.EventNID(lastEventSentNID), types.StateSnapshotNID(stateSnapshotNID), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) updateLatestEventNIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
eventNIDs []types.EventNID,
|
||||||
|
lastEventSentNID types.EventNID,
|
||||||
|
stateSnapshotNID types.StateSnapshotNID,
|
||||||
|
) error {
|
||||||
|
stmt := common.TxStmt(txn, s.updateLatestEventNIDsStmt)
|
||||||
|
_, err := stmt.ExecContext(
|
||||||
|
ctx,
|
||||||
|
roomNID,
|
||||||
|
eventNIDsAsArray(eventNIDs),
|
||||||
|
int64(lastEventSentNID),
|
||||||
|
int64(stateSnapshotNID),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("updateLatestEventNIDs stmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
60
roomserver/storage/sqlite3/sql.go
Normal file
60
roomserver/storage/sqlite3/sql.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
// 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 (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type statements struct {
|
||||||
|
eventTypeStatements
|
||||||
|
eventStateKeyStatements
|
||||||
|
roomStatements
|
||||||
|
eventStatements
|
||||||
|
eventJSONStatements
|
||||||
|
stateSnapshotStatements
|
||||||
|
stateBlockStatements
|
||||||
|
previousEventStatements
|
||||||
|
roomAliasesStatements
|
||||||
|
inviteStatements
|
||||||
|
membershipStatements
|
||||||
|
transactionStatements
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *statements) prepare(db *sql.DB) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for _, prepare := range []func(db *sql.DB) error{
|
||||||
|
s.eventTypeStatements.prepare,
|
||||||
|
s.eventStateKeyStatements.prepare,
|
||||||
|
s.roomStatements.prepare,
|
||||||
|
s.eventStatements.prepare,
|
||||||
|
s.eventJSONStatements.prepare,
|
||||||
|
s.stateSnapshotStatements.prepare,
|
||||||
|
s.stateBlockStatements.prepare,
|
||||||
|
s.previousEventStatements.prepare,
|
||||||
|
s.roomAliasesStatements.prepare,
|
||||||
|
s.inviteStatements.prepare,
|
||||||
|
s.membershipStatements.prepare,
|
||||||
|
s.transactionStatements.prepare,
|
||||||
|
} {
|
||||||
|
if err = prepare(db); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
285
roomserver/storage/sqlite3/state_block_table.go
Normal file
285
roomserver/storage/sqlite3/state_block_table.go
Normal file
|
@ -0,0 +1,285 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const stateDataSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_state_block (
|
||||||
|
state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
event_type_nid INTEGER NOT NULL,
|
||||||
|
event_state_key_nid INTEGER NOT NULL,
|
||||||
|
event_nid INTEGER NOT NULL,
|
||||||
|
UNIQUE (state_block_nid, event_type_nid, event_state_key_nid)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertStateDataSQL = "" +
|
||||||
|
"INSERT INTO roomserver_state_block (state_block_nid, event_type_nid, event_state_key_nid, event_nid)" +
|
||||||
|
" VALUES ($1, $2, $3, $4)"
|
||||||
|
|
||||||
|
const selectNextStateBlockNIDSQL = `
|
||||||
|
SELECT COALESCE((
|
||||||
|
SELECT seq+1 AS state_block_nid FROM sqlite_sequence
|
||||||
|
WHERE name = 'roomserver_state_block'), 0
|
||||||
|
) AS state_block_nid
|
||||||
|
`
|
||||||
|
|
||||||
|
// Bulk state lookup by numeric state block ID.
|
||||||
|
// Sort by the state_block_nid, event_type_nid, event_state_key_nid
|
||||||
|
// This means that all the entries for a given state_block_nid will appear
|
||||||
|
// together in the list and those entries will sorted by event_type_nid
|
||||||
|
// and event_state_key_nid. This property makes it easier to merge two
|
||||||
|
// state data blocks together.
|
||||||
|
const bulkSelectStateBlockEntriesSQL = "" +
|
||||||
|
"SELECT state_block_nid, event_type_nid, event_state_key_nid, event_nid" +
|
||||||
|
" FROM roomserver_state_block WHERE state_block_nid IN ($1)" +
|
||||||
|
" ORDER BY state_block_nid, event_type_nid, event_state_key_nid"
|
||||||
|
|
||||||
|
// Bulk state lookup by numeric state block ID.
|
||||||
|
// Filters the rows in each block to the requested types and state keys.
|
||||||
|
// We would like to restrict to particular type state key pairs but we are
|
||||||
|
// restricted by the query language to pull the cross product of a list
|
||||||
|
// of types and a list state_keys. So we have to filter the result in the
|
||||||
|
// application to restrict it to the list of event types and state keys we
|
||||||
|
// actually wanted.
|
||||||
|
const bulkSelectFilteredStateBlockEntriesSQL = "" +
|
||||||
|
"SELECT state_block_nid, event_type_nid, event_state_key_nid, event_nid" +
|
||||||
|
" FROM roomserver_state_block WHERE state_block_nid IN ($1)" +
|
||||||
|
" AND event_type_nid IN ($2) AND event_state_key_nid IN ($3)" +
|
||||||
|
" ORDER BY state_block_nid, event_type_nid, event_state_key_nid"
|
||||||
|
|
||||||
|
type stateBlockStatements struct {
|
||||||
|
insertStateDataStmt *sql.Stmt
|
||||||
|
selectNextStateBlockNIDStmt *sql.Stmt
|
||||||
|
bulkSelectStateBlockEntriesStmt *sql.Stmt
|
||||||
|
bulkSelectFilteredStateBlockEntriesStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateBlockStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(stateDataSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertStateDataStmt, insertStateDataSQL},
|
||||||
|
{&s.selectNextStateBlockNIDStmt, selectNextStateBlockNIDSQL},
|
||||||
|
{&s.bulkSelectStateBlockEntriesStmt, bulkSelectStateBlockEntriesSQL},
|
||||||
|
{&s.bulkSelectFilteredStateBlockEntriesStmt, bulkSelectFilteredStateBlockEntriesSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateBlockStatements) bulkInsertStateData(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
stateBlockNID types.StateBlockNID,
|
||||||
|
entries []types.StateEntry,
|
||||||
|
) error {
|
||||||
|
for _, entry := range entries {
|
||||||
|
_, err := common.TxStmt(txn, s.insertStateDataStmt).ExecContext(
|
||||||
|
ctx,
|
||||||
|
int64(stateBlockNID),
|
||||||
|
int64(entry.EventTypeNID),
|
||||||
|
int64(entry.EventStateKeyNID),
|
||||||
|
int64(entry.EventNID),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkInsertStateData s.insertStateDataStmt.ExecContext:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateBlockStatements) selectNextStateBlockNID(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
) (types.StateBlockNID, error) {
|
||||||
|
var stateBlockNID int64
|
||||||
|
selectStmt := common.TxStmt(txn, s.selectNextStateBlockNIDStmt)
|
||||||
|
err := selectStmt.QueryRowContext(ctx).Scan(&stateBlockNID)
|
||||||
|
return types.StateBlockNID(stateBlockNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateBlockStatements) bulkSelectStateBlockEntries(
|
||||||
|
ctx context.Context, txn *sql.Tx, stateBlockNIDs []types.StateBlockNID,
|
||||||
|
) ([]types.StateEntryList, error) {
|
||||||
|
nids := make([]int64, len(stateBlockNIDs))
|
||||||
|
for i := range stateBlockNIDs {
|
||||||
|
nids[i] = int64(stateBlockNIDs[i])
|
||||||
|
}
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectStateBlockEntriesStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteIn(pq.Int64Array(nids)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectStateBlockEntries s.bulkSelectStateBlockEntriesStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
results := make([]types.StateEntryList, len(stateBlockNIDs))
|
||||||
|
// current is a pointer to the StateEntryList to append the state entries to.
|
||||||
|
var current *types.StateEntryList
|
||||||
|
i := 0
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
stateBlockNID int64
|
||||||
|
eventTypeNID int64
|
||||||
|
eventStateKeyNID int64
|
||||||
|
eventNID int64
|
||||||
|
entry types.StateEntry
|
||||||
|
)
|
||||||
|
if err := rows.Scan(
|
||||||
|
&stateBlockNID, &eventTypeNID, &eventStateKeyNID, &eventNID,
|
||||||
|
); err != nil {
|
||||||
|
fmt.Println("bulkSelectStateBlockEntries rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fmt.Println("state block NID", stateBlockNID, "event type NID", eventTypeNID, "event state key NID", eventStateKeyNID, "event NID", eventNID)
|
||||||
|
entry.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
||||||
|
entry.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
||||||
|
entry.EventNID = types.EventNID(eventNID)
|
||||||
|
if current == nil || types.StateBlockNID(stateBlockNID) != current.StateBlockNID {
|
||||||
|
// The state entry row is for a different state data block to the current one.
|
||||||
|
// So we start appending to the next entry in the list.
|
||||||
|
current = &results[i]
|
||||||
|
current.StateBlockNID = types.StateBlockNID(stateBlockNID)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
current.StateEntries = append(current.StateEntries, entry)
|
||||||
|
}
|
||||||
|
if i != len(nids) {
|
||||||
|
return nil, fmt.Errorf("storage: state data NIDs missing from the database (%d != %d)", i, len(nids))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateBlockStatements) bulkSelectFilteredStateBlockEntries(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
stateBlockNIDs []types.StateBlockNID,
|
||||||
|
stateKeyTuples []types.StateKeyTuple,
|
||||||
|
) ([]types.StateEntryList, error) {
|
||||||
|
tuples := stateKeyTupleSorter(stateKeyTuples)
|
||||||
|
// Sort the tuples so that we can run binary search against them as we filter the rows returned by the db.
|
||||||
|
sort.Sort(tuples)
|
||||||
|
|
||||||
|
eventTypeNIDArray, eventStateKeyNIDArray := tuples.typesAndStateKeysAsArrays()
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectFilteredStateBlockEntriesStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(
|
||||||
|
ctx,
|
||||||
|
stateBlockNIDsAsArray(stateBlockNIDs),
|
||||||
|
eventTypeNIDArray,
|
||||||
|
sqliteIn(eventStateKeyNIDArray),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectFilteredStateBlockEntries s.bulkSelectFilteredStateBlockEntriesStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
var results []types.StateEntryList
|
||||||
|
var current types.StateEntryList
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
stateBlockNID int64
|
||||||
|
eventTypeNID int64
|
||||||
|
eventStateKeyNID int64
|
||||||
|
eventNID int64
|
||||||
|
entry types.StateEntry
|
||||||
|
)
|
||||||
|
if err := rows.Scan(
|
||||||
|
&stateBlockNID, &eventTypeNID, &eventStateKeyNID, &eventNID,
|
||||||
|
); err != nil {
|
||||||
|
fmt.Println("bulkSelectFilteredStateBlockEntries rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
entry.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
||||||
|
entry.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
||||||
|
entry.EventNID = types.EventNID(eventNID)
|
||||||
|
|
||||||
|
// We can use binary search here because we sorted the tuples earlier
|
||||||
|
if !tuples.contains(entry.StateKeyTuple) {
|
||||||
|
// The select will return the cross product of types and state keys.
|
||||||
|
// So we need to check if type of the entry is in the list.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if types.StateBlockNID(stateBlockNID) != current.StateBlockNID {
|
||||||
|
// The state entry row is for a different state data block to the current one.
|
||||||
|
// So we append the current entry to the results and start adding to a new one.
|
||||||
|
// The first time through the loop current will be empty.
|
||||||
|
if current.StateEntries != nil {
|
||||||
|
results = append(results, current)
|
||||||
|
}
|
||||||
|
current = types.StateEntryList{StateBlockNID: types.StateBlockNID(stateBlockNID)}
|
||||||
|
}
|
||||||
|
current.StateEntries = append(current.StateEntries, entry)
|
||||||
|
}
|
||||||
|
// Add the last entry to the list if it is not empty.
|
||||||
|
if current.StateEntries != nil {
|
||||||
|
results = append(results, current)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stateBlockNIDsAsArray(stateBlockNIDs []types.StateBlockNID) pq.Int64Array {
|
||||||
|
nids := make([]int64, len(stateBlockNIDs))
|
||||||
|
for i := range stateBlockNIDs {
|
||||||
|
nids[i] = int64(stateBlockNIDs[i])
|
||||||
|
}
|
||||||
|
return pq.Int64Array(nids)
|
||||||
|
}
|
||||||
|
|
||||||
|
type stateKeyTupleSorter []types.StateKeyTuple
|
||||||
|
|
||||||
|
func (s stateKeyTupleSorter) Len() int { return len(s) }
|
||||||
|
func (s stateKeyTupleSorter) Less(i, j int) bool { return s[i].LessThan(s[j]) }
|
||||||
|
func (s stateKeyTupleSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
|
||||||
|
// Check whether a tuple is in the list. Assumes that the list is sorted.
|
||||||
|
func (s stateKeyTupleSorter) contains(value types.StateKeyTuple) bool {
|
||||||
|
i := sort.Search(len(s), func(i int) bool { return !s[i].LessThan(value) })
|
||||||
|
return i < len(s) && s[i] == value
|
||||||
|
}
|
||||||
|
|
||||||
|
// List the unique eventTypeNIDs and eventStateKeyNIDs.
|
||||||
|
// Assumes that the list is sorted.
|
||||||
|
func (s stateKeyTupleSorter) typesAndStateKeysAsArrays() (eventTypeNIDs pq.Int64Array, eventStateKeyNIDs pq.Int64Array) {
|
||||||
|
eventTypeNIDs = make(pq.Int64Array, len(s))
|
||||||
|
eventStateKeyNIDs = make(pq.Int64Array, len(s))
|
||||||
|
for i := range s {
|
||||||
|
eventTypeNIDs[i] = int64(s[i].EventTypeNID)
|
||||||
|
eventStateKeyNIDs[i] = int64(s[i].EventStateKeyNID)
|
||||||
|
}
|
||||||
|
eventTypeNIDs = eventTypeNIDs[:util.SortAndUnique(int64Sorter(eventTypeNIDs))]
|
||||||
|
eventStateKeyNIDs = eventStateKeyNIDs[:util.SortAndUnique(int64Sorter(eventStateKeyNIDs))]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type int64Sorter []int64
|
||||||
|
|
||||||
|
func (s int64Sorter) Len() int { return len(s) }
|
||||||
|
func (s int64Sorter) Less(i, j int) bool { return s[i] < s[j] }
|
||||||
|
func (s int64Sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
86
roomserver/storage/sqlite3/state_block_table_test.go
Normal file
86
roomserver/storage/sqlite3/state_block_table_test.go
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
// 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 (
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStateKeyTupleSorter(t *testing.T) {
|
||||||
|
input := stateKeyTupleSorter{
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 2},
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 4},
|
||||||
|
{EventTypeNID: 2, EventStateKeyNID: 2},
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 1},
|
||||||
|
}
|
||||||
|
want := []types.StateKeyTuple{
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 1},
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 2},
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 4},
|
||||||
|
{EventTypeNID: 2, EventStateKeyNID: 2},
|
||||||
|
}
|
||||||
|
doNotWant := []types.StateKeyTuple{
|
||||||
|
{EventTypeNID: 0, EventStateKeyNID: 0},
|
||||||
|
{EventTypeNID: 1, EventStateKeyNID: 3},
|
||||||
|
{EventTypeNID: 2, EventStateKeyNID: 1},
|
||||||
|
{EventTypeNID: 3, EventStateKeyNID: 1},
|
||||||
|
}
|
||||||
|
wantTypeNIDs := []int64{1, 2}
|
||||||
|
wantStateKeyNIDs := []int64{1, 2, 4}
|
||||||
|
|
||||||
|
// Sort the input and check it's in the right order.
|
||||||
|
sort.Sort(input)
|
||||||
|
gotTypeNIDs, gotStateKeyNIDs := input.typesAndStateKeysAsArrays()
|
||||||
|
|
||||||
|
for i := range want {
|
||||||
|
if input[i] != want[i] {
|
||||||
|
t.Errorf("Wanted %#v at index %d got %#v", want[i], i, input[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
if !input.contains(want[i]) {
|
||||||
|
t.Errorf("Wanted %#v.contains(%#v) to be true but got false", input, want[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range doNotWant {
|
||||||
|
if input.contains(doNotWant[i]) {
|
||||||
|
t.Errorf("Wanted %#v.contains(%#v) to be false but got true", input, doNotWant[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(wantTypeNIDs) != len(gotTypeNIDs) {
|
||||||
|
t.Fatalf("Wanted type NIDs %#v got %#v", wantTypeNIDs, gotTypeNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range wantTypeNIDs {
|
||||||
|
if wantTypeNIDs[i] != gotTypeNIDs[i] {
|
||||||
|
t.Fatalf("Wanted type NIDs %#v got %#v", wantTypeNIDs, gotTypeNIDs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(wantStateKeyNIDs) != len(gotStateKeyNIDs) {
|
||||||
|
t.Fatalf("Wanted state key NIDs %#v got %#v", wantStateKeyNIDs, gotStateKeyNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range wantStateKeyNIDs {
|
||||||
|
if wantStateKeyNIDs[i] != gotStateKeyNIDs[i] {
|
||||||
|
t.Fatalf("Wanted type NIDs %#v got %#v", wantTypeNIDs, gotTypeNIDs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
124
roomserver/storage/sqlite3/state_snapshot_table.go
Normal file
124
roomserver/storage/sqlite3/state_snapshot_table.go
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const stateSnapshotSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_state_snapshots (
|
||||||
|
state_snapshot_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
room_nid INTEGER NOT NULL,
|
||||||
|
state_block_nids TEXT NOT NULL DEFAULT '{}'
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertStateSQL = `
|
||||||
|
INSERT INTO roomserver_state_snapshots (room_nid, state_block_nids)
|
||||||
|
VALUES ($1, $2);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertStateResultSQL = `
|
||||||
|
SELECT state_snapshot_nid FROM roomserver_state_snapshots
|
||||||
|
WHERE rowid = last_insert_rowid();
|
||||||
|
`
|
||||||
|
|
||||||
|
// Bulk state data NID lookup.
|
||||||
|
// Sorting by state_snapshot_nid means we can use binary search over the result
|
||||||
|
// to lookup the state data NIDs for a state snapshot NID.
|
||||||
|
const bulkSelectStateBlockNIDsSQL = "" +
|
||||||
|
"SELECT state_snapshot_nid, state_block_nids FROM roomserver_state_snapshots" +
|
||||||
|
" WHERE state_snapshot_nid IN ($1) ORDER BY state_snapshot_nid ASC"
|
||||||
|
|
||||||
|
type stateSnapshotStatements struct {
|
||||||
|
insertStateStmt *sql.Stmt
|
||||||
|
insertStateResultStmt *sql.Stmt
|
||||||
|
bulkSelectStateBlockNIDsStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateSnapshotStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(stateSnapshotSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertStateStmt, insertStateSQL},
|
||||||
|
{&s.insertStateResultStmt, insertStateResultSQL},
|
||||||
|
{&s.bulkSelectStateBlockNIDsStmt, bulkSelectStateBlockNIDsSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateSnapshotStatements) insertState(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, stateBlockNIDs []types.StateBlockNID,
|
||||||
|
) (stateNID types.StateSnapshotNID, err error) {
|
||||||
|
nids := make([]int64, len(stateBlockNIDs))
|
||||||
|
for i := range stateBlockNIDs {
|
||||||
|
nids[i] = int64(stateBlockNIDs[i])
|
||||||
|
}
|
||||||
|
insertStmt := common.TxStmt(txn, s.insertStateStmt)
|
||||||
|
resultStmt := common.TxStmt(txn, s.insertStateResultStmt)
|
||||||
|
if _, err = insertStmt.ExecContext(ctx, int64(roomNID), pq.Int64Array(nids)); err == nil {
|
||||||
|
err = resultStmt.QueryRowContext(ctx).Scan(&stateNID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertState s.insertStateResultStmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("insertState s.insertStateStmt.ExecContext:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stateSnapshotStatements) bulkSelectStateBlockNIDs(
|
||||||
|
ctx context.Context, txn *sql.Tx, stateNIDs []types.StateSnapshotNID,
|
||||||
|
) ([]types.StateBlockNIDList, error) {
|
||||||
|
nids := make([]int64, len(stateNIDs))
|
||||||
|
for i := range stateNIDs {
|
||||||
|
nids[i] = int64(stateNIDs[i])
|
||||||
|
}
|
||||||
|
selectStmt := common.TxStmt(txn, s.bulkSelectStateBlockNIDsStmt)
|
||||||
|
rows, err := selectStmt.QueryContext(ctx, sqliteIn(pq.Int64Array(nids)))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("bulkSelectStateBlockNIDs s.bulkSelectStateBlockNIDsStmt.QueryContext:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
results := make([]types.StateBlockNIDList, len(stateNIDs))
|
||||||
|
i := 0
|
||||||
|
for ; rows.Next(); i++ {
|
||||||
|
result := &results[i]
|
||||||
|
var stateBlockNIDs pq.Int64Array
|
||||||
|
if err := rows.Scan(&result.StateSnapshotNID, &stateBlockNIDs); err != nil {
|
||||||
|
fmt.Println("bulkSelectStateBlockNIDs rows.Scan:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result.StateBlockNIDs = make([]types.StateBlockNID, len(stateBlockNIDs))
|
||||||
|
for k := range stateBlockNIDs {
|
||||||
|
result.StateBlockNIDs[k] = types.StateBlockNID(stateBlockNIDs[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i != len(stateNIDs) {
|
||||||
|
return nil, fmt.Errorf("storage: state NIDs missing from the database (%d != %d)", i, len(stateNIDs))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
778
roomserver/storage/sqlite3/storage.go
Normal file
778
roomserver/storage/sqlite3/storage.go
Normal file
|
@ -0,0 +1,778 @@
|
||||||
|
// 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"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Database is used to store room events and stream offsets.
|
||||||
|
type Database struct {
|
||||||
|
statements statements
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a postgres database.
|
||||||
|
func Open(dataSourceName string) (*Database, error) {
|
||||||
|
var d Database
|
||||||
|
uri, err := url.Parse(dataSourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var cs string
|
||||||
|
if uri.Opaque != "" { // file:filename.db
|
||||||
|
cs = fmt.Sprintf("%s?cache=shared&_busy_timeout=9999999", uri.Opaque)
|
||||||
|
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||||
|
cs = fmt.Sprintf("%s?cache=shared&_busy_timeout=9999999", uri.Path)
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("no filename or path in connect string")
|
||||||
|
}
|
||||||
|
if d.db, err = sql.Open("sqlite3", cs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d.db.Exec("PRAGMA journal_mode=WAL;")
|
||||||
|
//d.db.Exec("PRAGMA parser_trace = true;")
|
||||||
|
//d.db.SetMaxOpenConns(1)
|
||||||
|
if err = d.statements.prepare(d.db); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StoreEvent implements input.EventDatabase
|
||||||
|
func (d *Database) StoreEvent(
|
||||||
|
ctx context.Context, event gomatrixserverlib.Event,
|
||||||
|
txnAndSessionID *api.TransactionID, authEventNIDs []types.EventNID,
|
||||||
|
) (types.RoomNID, types.StateAtEvent, error) {
|
||||||
|
var (
|
||||||
|
roomNID types.RoomNID
|
||||||
|
eventTypeNID types.EventTypeNID
|
||||||
|
eventStateKeyNID types.EventStateKeyNID
|
||||||
|
eventNID types.EventNID
|
||||||
|
stateNID types.StateSnapshotNID
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if txnAndSessionID != nil {
|
||||||
|
if err = d.statements.insertTransaction(
|
||||||
|
ctx, txnAndSessionID.TransactionID,
|
||||||
|
txnAndSessionID.SessionID, event.Sender(), event.EventID(),
|
||||||
|
); err != nil {
|
||||||
|
return 0, types.StateAtEvent{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = common.WithTransaction(d.db, func(tx *sql.Tx) error {
|
||||||
|
roomNID, err = d.assignRoomNID(ctx, tx, event.RoomID())
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, types.StateAtEvent{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = common.WithTransaction(d.db, func(tx *sql.Tx) error {
|
||||||
|
eventTypeNID, err = d.assignEventTypeNID(ctx, tx, event.Type())
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, types.StateAtEvent{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
eventStateKey := event.StateKey()
|
||||||
|
// Assigned a numeric ID for the state_key if there is one present.
|
||||||
|
// Otherwise set the numeric ID for the state_key to 0.
|
||||||
|
if eventStateKey != nil {
|
||||||
|
if eventStateKeyNID, err = d.assignStateKeyNID(ctx, txn, *eventStateKey); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if eventNID, stateNID, err = d.statements.insertEvent(
|
||||||
|
ctx,
|
||||||
|
txn,
|
||||||
|
roomNID,
|
||||||
|
eventTypeNID,
|
||||||
|
eventStateKeyNID,
|
||||||
|
event.EventID(),
|
||||||
|
event.EventReference().EventSHA256,
|
||||||
|
authEventNIDs,
|
||||||
|
event.Depth(),
|
||||||
|
); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We've already inserted the event so select the numeric event ID
|
||||||
|
eventNID, stateNID, err = d.statements.selectEvent(ctx, txn, event.EventID())
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = d.statements.insertEventJSON(ctx, txn, eventNID, event.JSON()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, types.StateAtEvent{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return roomNID, types.StateAtEvent{
|
||||||
|
BeforeStateSnapshotNID: stateNID,
|
||||||
|
StateEntry: types.StateEntry{
|
||||||
|
StateKeyTuple: types.StateKeyTuple{
|
||||||
|
EventTypeNID: eventTypeNID,
|
||||||
|
EventStateKeyNID: eventStateKeyNID,
|
||||||
|
},
|
||||||
|
EventNID: eventNID,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) assignRoomNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomID string,
|
||||||
|
) (roomNID types.RoomNID, err error) {
|
||||||
|
// Check if we already have a numeric ID in the database.
|
||||||
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We don't have a numeric ID so insert one into the database.
|
||||||
|
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We raced with another insert so run the select again.
|
||||||
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) assignEventTypeNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventType string,
|
||||||
|
) (eventTypeNID types.EventTypeNID, err error) {
|
||||||
|
// Check if we already have a numeric ID in the database.
|
||||||
|
eventTypeNID, err = d.statements.selectEventTypeNID(ctx, txn, eventType)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We don't have a numeric ID so insert one into the database.
|
||||||
|
eventTypeNID, err = d.statements.insertEventTypeNID(ctx, txn, eventType)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We raced with another insert so run the select again.
|
||||||
|
eventTypeNID, err = d.statements.selectEventTypeNID(ctx, txn, eventType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) assignStateKeyNID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
||||||
|
) (eventStateKeyNID types.EventStateKeyNID, err error) {
|
||||||
|
// Check if we already have a numeric ID in the database.
|
||||||
|
eventStateKeyNID, err = d.statements.selectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We don't have a numeric ID so insert one into the database.
|
||||||
|
eventStateKeyNID, err = d.statements.insertEventStateKeyNID(ctx, txn, eventStateKey)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// We raced with another insert so run the select again.
|
||||||
|
eventStateKeyNID, err = d.statements.selectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateEntriesForEventIDs implements input.EventDatabase
|
||||||
|
func (d *Database) StateEntriesForEventIDs(
|
||||||
|
ctx context.Context, eventIDs []string,
|
||||||
|
) ([]types.StateEntry, error) {
|
||||||
|
return d.statements.bulkSelectStateEventByID(ctx, nil, eventIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventTypeNIDs implements state.RoomStateDatabase
|
||||||
|
func (d *Database) EventTypeNIDs(
|
||||||
|
ctx context.Context, eventTypes []string,
|
||||||
|
) (map[string]types.EventTypeNID, error) {
|
||||||
|
return d.statements.bulkSelectEventTypeNID(ctx, nil, eventTypes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventStateKeyNIDs implements state.RoomStateDatabase
|
||||||
|
func (d *Database) EventStateKeyNIDs(
|
||||||
|
ctx context.Context, eventStateKeys []string,
|
||||||
|
) (map[string]types.EventStateKeyNID, error) {
|
||||||
|
return d.statements.bulkSelectEventStateKeyNID(ctx, nil, eventStateKeys)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventStateKeys implements query.RoomserverQueryAPIDatabase
|
||||||
|
func (d *Database) EventStateKeys(
|
||||||
|
ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID,
|
||||||
|
) (map[types.EventStateKeyNID]string, error) {
|
||||||
|
return d.statements.bulkSelectEventStateKey(ctx, nil, eventStateKeyNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventNIDs implements query.RoomserverQueryAPIDatabase
|
||||||
|
func (d *Database) EventNIDs(
|
||||||
|
ctx context.Context, eventIDs []string,
|
||||||
|
) (map[string]types.EventNID, error) {
|
||||||
|
return d.statements.bulkSelectEventNID(ctx, nil, eventIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Events implements input.EventDatabase
|
||||||
|
func (d *Database) Events(
|
||||||
|
ctx context.Context, eventNIDs []types.EventNID,
|
||||||
|
) ([]types.Event, error) {
|
||||||
|
var eventJSONs []eventJSONPair
|
||||||
|
var err error
|
||||||
|
results := make([]types.Event, len(eventJSONs))
|
||||||
|
common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
eventJSONs, err = d.statements.bulkSelectEventJSON(ctx, txn, eventNIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for i, eventJSON := range eventJSONs {
|
||||||
|
result := &results[i]
|
||||||
|
result.EventNID = eventJSON.EventNID
|
||||||
|
// TODO: Use NewEventFromTrustedJSON for efficiency
|
||||||
|
result.Event, err = gomatrixserverlib.NewEventFromUntrustedJSON(eventJSON.EventJSON)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return []types.Event{}, err
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddState implements input.EventDatabase
|
||||||
|
func (d *Database) AddState(
|
||||||
|
ctx context.Context,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
stateBlockNIDs []types.StateBlockNID,
|
||||||
|
state []types.StateEntry,
|
||||||
|
) (stateNID types.StateSnapshotNID, err error) {
|
||||||
|
common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
if len(state) > 0 {
|
||||||
|
stateBlockNID, err := d.statements.selectNextStateBlockNID(ctx, txn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = d.statements.bulkInsertStateData(ctx, txn, stateBlockNID, state); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID)
|
||||||
|
}
|
||||||
|
stateNID, err = d.statements.insertState(ctx, txn, roomNID, stateBlockNIDs)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetState implements input.EventDatabase
|
||||||
|
func (d *Database) SetState(
|
||||||
|
ctx context.Context, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
||||||
|
) error {
|
||||||
|
return d.statements.updateEventState(ctx, nil, eventNID, stateNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateAtEventIDs implements input.EventDatabase
|
||||||
|
func (d *Database) StateAtEventIDs(
|
||||||
|
ctx context.Context, eventIDs []string,
|
||||||
|
) ([]types.StateAtEvent, error) {
|
||||||
|
return d.statements.bulkSelectStateAtEventByID(ctx, nil, eventIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateBlockNIDs implements state.RoomStateDatabase
|
||||||
|
func (d *Database) StateBlockNIDs(
|
||||||
|
ctx context.Context, stateNIDs []types.StateSnapshotNID,
|
||||||
|
) ([]types.StateBlockNIDList, error) {
|
||||||
|
return d.statements.bulkSelectStateBlockNIDs(ctx, nil, stateNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateEntries implements state.RoomStateDatabase
|
||||||
|
func (d *Database) StateEntries(
|
||||||
|
ctx context.Context, stateBlockNIDs []types.StateBlockNID,
|
||||||
|
) ([]types.StateEntryList, error) {
|
||||||
|
return d.statements.bulkSelectStateBlockEntries(ctx, nil, stateBlockNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SnapshotNIDFromEventID implements state.RoomStateDatabase
|
||||||
|
func (d *Database) SnapshotNIDFromEventID(
|
||||||
|
ctx context.Context, eventID string,
|
||||||
|
) (stateNID types.StateSnapshotNID, err error) {
|
||||||
|
_, stateNID, err = d.statements.selectEvent(ctx, nil, eventID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventIDs implements input.RoomEventDatabase
|
||||||
|
func (d *Database) EventIDs(
|
||||||
|
ctx context.Context, eventNIDs []types.EventNID,
|
||||||
|
) (map[types.EventNID]string, error) {
|
||||||
|
return d.statements.bulkSelectEventID(ctx, nil, eventNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLatestEventsForUpdate implements input.EventDatabase
|
||||||
|
func (d *Database) GetLatestEventsForUpdate(
|
||||||
|
ctx context.Context, roomNID types.RoomNID,
|
||||||
|
) (types.RoomRecentEventsUpdater, error) {
|
||||||
|
txn, err := d.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err :=
|
||||||
|
d.statements.selectLatestEventsNIDsForUpdate(ctx, txn, roomNID)
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback() // nolint: errcheck
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
stateAndRefs, err := d.statements.bulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback() // nolint: errcheck
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var lastEventIDSent string
|
||||||
|
if lastEventNIDSent != 0 {
|
||||||
|
lastEventIDSent, err = d.statements.selectEventID(ctx, txn, lastEventNIDSent)
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback() // nolint: errcheck
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &roomRecentEventsUpdater{
|
||||||
|
transaction{ctx, txn}, d, roomNID, stateAndRefs, lastEventIDSent, currentStateSnapshotNID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTransactionEventID implements input.EventDatabase
|
||||||
|
func (d *Database) GetTransactionEventID(
|
||||||
|
ctx context.Context, transactionID string,
|
||||||
|
sessionID int64, userID string,
|
||||||
|
) (string, error) {
|
||||||
|
eventID, err := d.statements.selectTransactionEventID(ctx, transactionID, sessionID, userID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return eventID, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type roomRecentEventsUpdater struct {
|
||||||
|
transaction
|
||||||
|
d *Database
|
||||||
|
roomNID types.RoomNID
|
||||||
|
latestEvents []types.StateAtEventAndReference
|
||||||
|
lastEventIDSent string
|
||||||
|
currentStateSnapshotNID types.StateSnapshotNID
|
||||||
|
}
|
||||||
|
|
||||||
|
// LatestEvents implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) LatestEvents() []types.StateAtEventAndReference {
|
||||||
|
return u.latestEvents
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastEventIDSent implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) LastEventIDSent() string {
|
||||||
|
return u.lastEventIDSent
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentStateSnapshotNID implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) CurrentStateSnapshotNID() types.StateSnapshotNID {
|
||||||
|
return u.currentStateSnapshotNID
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorePreviousEvents implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
||||||
|
for _, ref := range previousEventReferences {
|
||||||
|
if err := u.d.statements.insertPreviousEvent(u.ctx, u.txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsReferenced implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (bool, error) {
|
||||||
|
err := u.d.statements.selectPreviousEventExists(u.ctx, u.txn, eventReference.EventID, eventReference.EventSHA256)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLatestEvents implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) SetLatestEvents(
|
||||||
|
roomNID types.RoomNID, latest []types.StateAtEventAndReference, lastEventNIDSent types.EventNID,
|
||||||
|
currentStateSnapshotNID types.StateSnapshotNID,
|
||||||
|
) error {
|
||||||
|
eventNIDs := make([]types.EventNID, len(latest))
|
||||||
|
for i := range latest {
|
||||||
|
eventNIDs[i] = latest[i].EventNID
|
||||||
|
}
|
||||||
|
// TODO: transaction was removed here - is this wise?
|
||||||
|
return u.d.statements.updateLatestEventNIDs(u.ctx, nil, roomNID, eventNIDs, lastEventNIDSent, currentStateSnapshotNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasEventBeenSent implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) HasEventBeenSent(eventNID types.EventNID) (bool, error) {
|
||||||
|
// TODO: transaction was removed here - is this wise?
|
||||||
|
return u.d.statements.selectEventSentToOutput(u.ctx, nil, eventNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkEventAsSent implements types.RoomRecentEventsUpdater
|
||||||
|
func (u *roomRecentEventsUpdater) MarkEventAsSent(eventNID types.EventNID) error {
|
||||||
|
// TODO: transaction was removed here - is this wise?
|
||||||
|
return u.d.statements.updateEventSentToOutput(u.ctx, nil, eventNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *roomRecentEventsUpdater) MembershipUpdater(targetUserNID types.EventStateKeyNID) (types.MembershipUpdater, error) {
|
||||||
|
// TODO: transaction was removed here - is this wise?
|
||||||
|
return u.d.membershipUpdaterTxn(u.ctx, nil, u.roomNID, targetUserNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoomNID implements query.RoomserverQueryAPIDB
|
||||||
|
func (d *Database) RoomNID(ctx context.Context, roomID string) (types.RoomNID, error) {
|
||||||
|
roomNID, err := d.statements.selectRoomNID(ctx, nil, roomID)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return roomNID, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// LatestEventIDs implements query.RoomserverQueryAPIDatabase
|
||||||
|
func (d *Database) LatestEventIDs(
|
||||||
|
ctx context.Context, roomNID types.RoomNID,
|
||||||
|
) (references []gomatrixserverlib.EventReference, currentStateSnapshotNID types.StateSnapshotNID, depth int64, err error) {
|
||||||
|
err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
var eventNIDs []types.EventNID
|
||||||
|
eventNIDs, currentStateSnapshotNID, err = d.statements.selectLatestEventNIDs(ctx, txn, roomNID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
references, err = d.statements.bulkSelectEventReference(ctx, txn, eventNIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
depth, err = d.statements.selectMaxEventDepth(ctx, txn, eventNIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInvitesForUser implements query.RoomserverQueryAPIDatabase
|
||||||
|
func (d *Database) GetInvitesForUser(
|
||||||
|
ctx context.Context,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
targetUserNID types.EventStateKeyNID,
|
||||||
|
) (senderUserIDs []types.EventStateKeyNID, err error) {
|
||||||
|
return d.statements.selectInviteActiveForUserInRoom(ctx, targetUserNID, roomNID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRoomAlias implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) SetRoomAlias(ctx context.Context, alias string, roomID string, creatorUserID string) error {
|
||||||
|
return d.statements.insertRoomAlias(ctx, nil, alias, roomID, creatorUserID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRoomIDForAlias implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) GetRoomIDForAlias(ctx context.Context, alias string) (string, error) {
|
||||||
|
return d.statements.selectRoomIDFromAlias(ctx, nil, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAliasesForRoomID implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) {
|
||||||
|
return d.statements.selectAliasesFromRoomID(ctx, nil, roomID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCreatorIDForAlias implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) GetCreatorIDForAlias(
|
||||||
|
ctx context.Context, alias string,
|
||||||
|
) (string, error) {
|
||||||
|
return d.statements.selectCreatorIDFromAlias(ctx, nil, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveRoomAlias implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) RemoveRoomAlias(ctx context.Context, alias string) error {
|
||||||
|
return d.statements.deleteRoomAlias(ctx, nil, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateEntriesForTuples implements state.RoomStateDatabase
|
||||||
|
func (d *Database) StateEntriesForTuples(
|
||||||
|
ctx context.Context,
|
||||||
|
stateBlockNIDs []types.StateBlockNID,
|
||||||
|
stateKeyTuples []types.StateKeyTuple,
|
||||||
|
) ([]types.StateEntryList, error) {
|
||||||
|
return d.statements.bulkSelectFilteredStateBlockEntries(
|
||||||
|
ctx, nil, stateBlockNIDs, stateKeyTuples,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MembershipUpdater implements input.RoomEventDatabase
|
||||||
|
func (d *Database) MembershipUpdater(
|
||||||
|
ctx context.Context, roomID, targetUserID string,
|
||||||
|
) (types.MembershipUpdater, error) {
|
||||||
|
txn, err := d.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
succeeded := false
|
||||||
|
defer func() {
|
||||||
|
if !succeeded {
|
||||||
|
txn.Rollback() // nolint: errcheck
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
targetUserNID, err := d.assignStateKeyNID(ctx, txn, targetUserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updater, err := d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
succeeded = true
|
||||||
|
return updater, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type membershipUpdater struct {
|
||||||
|
transaction
|
||||||
|
d *Database
|
||||||
|
roomNID types.RoomNID
|
||||||
|
targetUserNID types.EventStateKeyNID
|
||||||
|
membership membershipState
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) membershipUpdaterTxn(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
roomNID types.RoomNID,
|
||||||
|
targetUserNID types.EventStateKeyNID,
|
||||||
|
) (types.MembershipUpdater, error) {
|
||||||
|
|
||||||
|
if err := d.statements.insertMembership(ctx, txn, roomNID, targetUserNID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
membership, err := d.statements.selectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &membershipUpdater{
|
||||||
|
transaction{ctx, txn}, d, roomNID, targetUserNID, membership,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsInvite implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) IsInvite() bool {
|
||||||
|
return u.membership == membershipStateInvite
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsJoin implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) IsJoin() bool {
|
||||||
|
return u.membership == membershipStateJoin
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLeave implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) IsLeave() bool {
|
||||||
|
return u.membership == membershipStateLeaveOrBan
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetToInvite implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) SetToInvite(event gomatrixserverlib.Event) (bool, error) {
|
||||||
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, event.Sender())
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
inserted, err := u.d.statements.insertInviteEvent(
|
||||||
|
u.ctx, u.txn, event.EventID(), u.roomNID, u.targetUserNID, senderUserNID, event.JSON(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if u.membership != membershipStateInvite {
|
||||||
|
if err = u.d.statements.updateMembership(
|
||||||
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, membershipStateInvite, 0,
|
||||||
|
); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inserted, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetToJoin implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) SetToJoin(senderUserID string, eventID string, isUpdate bool) ([]string, error) {
|
||||||
|
var inviteEventIDs []string
|
||||||
|
|
||||||
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a join event update, there is no invite to update
|
||||||
|
if !isUpdate {
|
||||||
|
inviteEventIDs, err = u.d.statements.updateInviteRetired(
|
||||||
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up the NID of the new join event
|
||||||
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.membership != membershipStateJoin || isUpdate {
|
||||||
|
if err = u.d.statements.updateMembership(
|
||||||
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID,
|
||||||
|
membershipStateJoin, nIDs[eventID],
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return inviteEventIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetToLeave implements types.MembershipUpdater
|
||||||
|
func (u *membershipUpdater) SetToLeave(senderUserID string, eventID string) ([]string, error) {
|
||||||
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inviteEventIDs, err := u.d.statements.updateInviteRetired(
|
||||||
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up the NID of the new leave event
|
||||||
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.membership != membershipStateLeaveOrBan {
|
||||||
|
if err = u.d.statements.updateMembership(
|
||||||
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID,
|
||||||
|
membershipStateLeaveOrBan, nIDs[eventID],
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inviteEventIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMembership implements query.RoomserverQueryAPIDB
|
||||||
|
func (d *Database) GetMembership(
|
||||||
|
ctx context.Context, roomNID types.RoomNID, requestSenderUserID string,
|
||||||
|
) (membershipEventNID types.EventNID, stillInRoom bool, err error) {
|
||||||
|
err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
requestSenderUserNID, err := d.assignStateKeyNID(ctx, txn, requestSenderUserID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
membershipEventNID, _, err =
|
||||||
|
d.statements.selectMembershipFromRoomAndTarget(
|
||||||
|
ctx, txn, roomNID, requestSenderUserNID,
|
||||||
|
)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// The user has never been a member of that room
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stillInRoom = true
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMembershipEventNIDsForRoom implements query.RoomserverQueryAPIDB
|
||||||
|
func (d *Database) GetMembershipEventNIDsForRoom(
|
||||||
|
ctx context.Context, roomNID types.RoomNID, joinOnly bool,
|
||||||
|
) (eventNIDs []types.EventNID, err error) {
|
||||||
|
common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
|
if joinOnly {
|
||||||
|
eventNIDs, err = d.statements.selectMembershipsFromRoomAndMembership(
|
||||||
|
ctx, txn, roomNID, membershipStateJoin,
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
eventNIDs, err = d.statements.selectMembershipsFromRoom(ctx, txn, roomNID)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventsFromIDs implements query.RoomserverQueryAPIEventDB
|
||||||
|
func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error) {
|
||||||
|
nidMap, err := d.EventNIDs(ctx, eventIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var nids []types.EventNID
|
||||||
|
for _, nid := range nidMap {
|
||||||
|
nids = append(nids, nid)
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.Events(ctx, nids)
|
||||||
|
}
|
||||||
|
|
||||||
|
type transaction struct {
|
||||||
|
ctx context.Context
|
||||||
|
txn *sql.Tx
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit implements types.Transaction
|
||||||
|
func (t *transaction) Commit() error {
|
||||||
|
return t.txn.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback implements types.Transaction
|
||||||
|
func (t *transaction) Rollback() error {
|
||||||
|
return t.txn.Rollback()
|
||||||
|
}
|
89
roomserver/storage/sqlite3/transactions_table.go
Normal file
89
roomserver/storage/sqlite3/transactions_table.go
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
// 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"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const transactionsSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS roomserver_transactions (
|
||||||
|
transaction_id TEXT NOT NULL,
|
||||||
|
session_id INTEGER NOT NULL,
|
||||||
|
user_id TEXT NOT NULL,
|
||||||
|
event_id TEXT NOT NULL,
|
||||||
|
PRIMARY KEY (transaction_id, session_id, user_id)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
const insertTransactionSQL = `
|
||||||
|
INSERT INTO roomserver_transactions (transaction_id, session_id, user_id, event_id)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectTransactionEventIDSQL = `
|
||||||
|
SELECT event_id FROM roomserver_transactions
|
||||||
|
WHERE transaction_id = $1 AND session_id = $2 AND user_id = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
type transactionStatements struct {
|
||||||
|
insertTransactionStmt *sql.Stmt
|
||||||
|
selectTransactionEventIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transactionStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(transactionsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return statementList{
|
||||||
|
{&s.insertTransactionStmt, insertTransactionSQL},
|
||||||
|
{&s.selectTransactionEventIDStmt, selectTransactionEventIDSQL},
|
||||||
|
}.prepare(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transactionStatements) insertTransaction(
|
||||||
|
ctx context.Context,
|
||||||
|
transactionID string,
|
||||||
|
sessionID int64,
|
||||||
|
userID string,
|
||||||
|
eventID string,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.insertTransactionStmt.ExecContext(
|
||||||
|
ctx, transactionID, sessionID, userID, eventID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("insertTransaction s.insertTransactionStmt.ExecContent:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transactionStatements) selectTransactionEventID(
|
||||||
|
ctx context.Context,
|
||||||
|
transactionID string,
|
||||||
|
sessionID int64,
|
||||||
|
userID string,
|
||||||
|
) (eventID string, err error) {
|
||||||
|
err = s.selectTransactionEventIDStmt.QueryRowContext(
|
||||||
|
ctx, transactionID, sessionID, userID,
|
||||||
|
).Scan(&eventID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("selectTransactionEventID s.selectTransactionEventIDStmt.QueryRowContext:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
|
@ -19,25 +19,20 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/state"
|
||||||
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
|
state.RoomStateDatabase
|
||||||
StoreEvent(ctx context.Context, event gomatrixserverlib.Event, txnAndSessionID *api.TransactionID, authEventNIDs []types.EventNID) (types.RoomNID, types.StateAtEvent, error)
|
StoreEvent(ctx context.Context, event gomatrixserverlib.Event, txnAndSessionID *api.TransactionID, authEventNIDs []types.EventNID) (types.RoomNID, types.StateAtEvent, error)
|
||||||
StateEntriesForEventIDs(ctx context.Context, eventIDs []string) ([]types.StateEntry, error)
|
StateEntriesForEventIDs(ctx context.Context, eventIDs []string) ([]types.StateEntry, error)
|
||||||
EventTypeNIDs(ctx context.Context, eventTypes []string) (map[string]types.EventTypeNID, error)
|
|
||||||
EventStateKeyNIDs(ctx context.Context, eventStateKeys []string) (map[string]types.EventStateKeyNID, error)
|
|
||||||
EventStateKeys(ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID) (map[types.EventStateKeyNID]string, error)
|
EventStateKeys(ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID) (map[types.EventStateKeyNID]string, error)
|
||||||
EventNIDs(ctx context.Context, eventIDs []string) (map[string]types.EventNID, error)
|
EventNIDs(ctx context.Context, eventIDs []string) (map[string]types.EventNID, error)
|
||||||
Events(ctx context.Context, eventNIDs []types.EventNID) ([]types.Event, error)
|
|
||||||
AddState(ctx context.Context, roomNID types.RoomNID, stateBlockNIDs []types.StateBlockNID, state []types.StateEntry) (types.StateSnapshotNID, error)
|
|
||||||
SetState(ctx context.Context, eventNID types.EventNID, stateNID types.StateSnapshotNID) error
|
SetState(ctx context.Context, eventNID types.EventNID, stateNID types.StateSnapshotNID) error
|
||||||
StateAtEventIDs(ctx context.Context, eventIDs []string) ([]types.StateAtEvent, error)
|
|
||||||
StateBlockNIDs(ctx context.Context, stateNIDs []types.StateSnapshotNID) ([]types.StateBlockNIDList, error)
|
|
||||||
StateEntries(ctx context.Context, stateBlockNIDs []types.StateBlockNID) ([]types.StateEntryList, error)
|
|
||||||
SnapshotNIDFromEventID(ctx context.Context, eventID string) (types.StateSnapshotNID, error)
|
|
||||||
EventIDs(ctx context.Context, eventNIDs []types.EventNID) (map[types.EventNID]string, error)
|
EventIDs(ctx context.Context, eventNIDs []types.EventNID) (map[types.EventNID]string, error)
|
||||||
GetLatestEventsForUpdate(ctx context.Context, roomNID types.RoomNID) (types.RoomRecentEventsUpdater, error)
|
GetLatestEventsForUpdate(ctx context.Context, roomNID types.RoomNID) (types.RoomRecentEventsUpdater, error)
|
||||||
GetTransactionEventID(ctx context.Context, transactionID string, sessionID int64, userID string) (string, error)
|
GetTransactionEventID(ctx context.Context, transactionID string, sessionID int64, userID string) (string, error)
|
||||||
|
@ -49,7 +44,6 @@ type Database interface {
|
||||||
GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error)
|
GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error)
|
||||||
GetCreatorIDForAlias(ctx context.Context, alias string) (string, error)
|
GetCreatorIDForAlias(ctx context.Context, alias string) (string, error)
|
||||||
RemoveRoomAlias(ctx context.Context, alias string) error
|
RemoveRoomAlias(ctx context.Context, alias string) error
|
||||||
StateEntriesForTuples(ctx context.Context, stateBlockNIDs []types.StateBlockNID, stateKeyTuples []types.StateKeyTuple) ([]types.StateEntryList, error)
|
|
||||||
MembershipUpdater(ctx context.Context, roomID, targetUserID string) (types.MembershipUpdater, error)
|
MembershipUpdater(ctx context.Context, roomID, targetUserID string) (types.MembershipUpdater, error)
|
||||||
GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
||||||
GetMembershipEventNIDsForRoom(ctx context.Context, roomNID types.RoomNID, joinOnly bool) ([]types.EventNID, error)
|
GetMembershipEventNIDsForRoom(ctx context.Context, roomNID types.RoomNID, joinOnly bool) ([]types.EventNID, error)
|
||||||
|
@ -65,6 +59,8 @@ func Open(dataSourceName string) (Database, error) {
|
||||||
switch uri.Scheme {
|
switch uri.Scheme {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return postgres.Open(dataSourceName)
|
return postgres.Open(dataSourceName)
|
||||||
|
case "file":
|
||||||
|
return sqlite3.Open(dataSourceName)
|
||||||
default:
|
default:
|
||||||
return postgres.Open(dataSourceName)
|
return postgres.Open(dataSourceName)
|
||||||
}
|
}
|
||||||
|
|
139
syncapi/storage/sqlite3/account_data_table.go
Normal file
139
syncapi/storage/sqlite3/account_data_table.go
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const accountDataSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_account_data_type (
|
||||||
|
id BIGINT PRIMARY KEY AUTOINCREMENT, -- DEFAULT nextval('syncapi_stream_id'),
|
||||||
|
user_id TEXT NOT NULL,
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
CONSTRAINT syncapi_account_data_unique UNIQUE (user_id, room_id, type)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CREATE UNIQUE INDEX IF NOT EXISTS syncapi_account_data_id_idx ON syncapi_account_data_type(id, type);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertAccountDataSQL = "" +
|
||||||
|
"INSERT INTO syncapi_account_data_type (user_id, room_id, type) VALUES ($1, $2, $3)" +
|
||||||
|
" ON CONFLICT DO UPDATE" +
|
||||||
|
" SET id = EXCLUDED.id" +
|
||||||
|
" RETURNING id"
|
||||||
|
|
||||||
|
const selectAccountDataInRangeSQL = "" +
|
||||||
|
"SELECT room_id, type FROM syncapi_account_data_type" +
|
||||||
|
" WHERE user_id = $1 AND id > $2 AND id <= $3" +
|
||||||
|
" 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"
|
||||||
|
|
||||||
|
const selectMaxAccountDataIDSQL = "" +
|
||||||
|
"SELECT MAX(id) FROM syncapi_account_data_type"
|
||||||
|
|
||||||
|
type accountDataStatements struct {
|
||||||
|
insertAccountDataStmt *sql.Stmt
|
||||||
|
selectAccountDataInRangeStmt *sql.Stmt
|
||||||
|
selectMaxAccountDataIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
if s.selectMaxAccountDataIDStmt, err = db.Prepare(selectMaxAccountDataIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *accountDataStatements) insertAccountData(
|
||||||
|
ctx context.Context,
|
||||||
|
userID, roomID, dataType string,
|
||||||
|
) (pos types.StreamPosition, err error) {
|
||||||
|
err = s.insertAccountDataStmt.QueryRowContext(ctx, userID, roomID, dataType).Scan(&pos)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *accountDataStatements) selectAccountDataInRange(
|
||||||
|
ctx context.Context,
|
||||||
|
userID string,
|
||||||
|
oldPos, newPos types.StreamPosition,
|
||||||
|
accountDataFilterPart *gomatrix.FilterPart,
|
||||||
|
) (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--
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.selectAccountDataInRangeStmt.QueryContext(ctx, userID, oldPos, newPos,
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.Types)),
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.NotTypes)),
|
||||||
|
accountDataFilterPart.Limit,
|
||||||
|
)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
119
syncapi/storage/sqlite3/backward_extremities_table.go
Normal file
119
syncapi/storage/sqlite3/backward_extremities_table.go
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
// Copyright 2018 New Vector 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 sqlite3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
const backwardExtremitiesSchema = `
|
||||||
|
-- Stores output room events received from the roomserver.
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_backward_extremities (
|
||||||
|
-- The 'room_id' key for the event.
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
-- The event ID for the event.
|
||||||
|
event_id TEXT NOT NULL,
|
||||||
|
|
||||||
|
PRIMARY KEY(room_id, event_id)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertBackwardExtremitySQL = "" +
|
||||||
|
"INSERT INTO syncapi_backward_extremities (room_id, event_id)" +
|
||||||
|
" VALUES ($1, $2)" +
|
||||||
|
" ON CONFLICT DO NOTHING"
|
||||||
|
|
||||||
|
const selectBackwardExtremitiesForRoomSQL = "" +
|
||||||
|
"SELECT event_id FROM syncapi_backward_extremities WHERE room_id = $1"
|
||||||
|
|
||||||
|
const isBackwardExtremitySQL = "" +
|
||||||
|
"SELECT EXISTS (" +
|
||||||
|
" SELECT TRUE FROM syncapi_backward_extremities" +
|
||||||
|
" WHERE room_id = $1 AND event_id = $2" +
|
||||||
|
")"
|
||||||
|
|
||||||
|
const deleteBackwardExtremitySQL = "" +
|
||||||
|
"DELETE FROM syncapi_backward_extremities WHERE room_id = $1 AND event_id = $2"
|
||||||
|
|
||||||
|
type backwardExtremitiesStatements struct {
|
||||||
|
insertBackwardExtremityStmt *sql.Stmt
|
||||||
|
selectBackwardExtremitiesForRoomStmt *sql.Stmt
|
||||||
|
isBackwardExtremityStmt *sql.Stmt
|
||||||
|
deleteBackwardExtremityStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *backwardExtremitiesStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(backwardExtremitiesSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertBackwardExtremityStmt, err = db.Prepare(insertBackwardExtremitySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectBackwardExtremitiesForRoomStmt, err = db.Prepare(selectBackwardExtremitiesForRoomSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.isBackwardExtremityStmt, err = db.Prepare(isBackwardExtremitySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.deleteBackwardExtremityStmt, err = db.Prepare(deleteBackwardExtremitySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *backwardExtremitiesStatements) insertsBackwardExtremity(
|
||||||
|
ctx context.Context, roomID, eventID string,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.insertBackwardExtremityStmt.ExecContext(ctx, roomID, eventID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *backwardExtremitiesStatements) selectBackwardExtremitiesForRoom(
|
||||||
|
ctx context.Context, roomID string,
|
||||||
|
) (eventIDs []string, err error) {
|
||||||
|
eventIDs = make([]string, 0)
|
||||||
|
|
||||||
|
rows, err := s.selectBackwardExtremitiesForRoomStmt.QueryContext(ctx, roomID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var eID string
|
||||||
|
if err = rows.Scan(&eID); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eventIDs = append(eventIDs, eID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *backwardExtremitiesStatements) isBackwardExtremity(
|
||||||
|
ctx context.Context, roomID, eventID string,
|
||||||
|
) (isBE bool, err error) {
|
||||||
|
err = s.isBackwardExtremityStmt.QueryRowContext(ctx, roomID, eventID).Scan(&isBE)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *backwardExtremitiesStatements) deleteBackwardExtremity(
|
||||||
|
ctx context.Context, roomID, eventID string,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.insertBackwardExtremityStmt.ExecContext(ctx, roomID, eventID)
|
||||||
|
return
|
||||||
|
}
|
287
syncapi/storage/sqlite3/current_room_state_table.go
Normal file
287
syncapi/storage/sqlite3/current_room_state_table.go
Normal file
|
@ -0,0 +1,287 @@
|
||||||
|
// 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"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const currentRoomStateSchema = `
|
||||||
|
-- Stores the current room state for every room.
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_current_room_state (
|
||||||
|
-- The 'room_id' key for the state event.
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
-- The state event ID
|
||||||
|
event_id TEXT NOT NULL,
|
||||||
|
-- The state event type e.g 'm.room.member'
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
-- The 'sender' property of the event.
|
||||||
|
sender TEXT NOT NULL,
|
||||||
|
-- true if the event content contains a url key
|
||||||
|
contains_url BOOL NOT NULL,
|
||||||
|
-- The state_key value for this state event e.g ''
|
||||||
|
state_key TEXT NOT NULL,
|
||||||
|
-- The JSON for the event. Stored as TEXT because this should be valid UTF-8.
|
||||||
|
event_json TEXT NOT NULL,
|
||||||
|
-- The 'content.membership' value if this event is an m.room.member event. For other
|
||||||
|
-- events, this will be NULL.
|
||||||
|
membership TEXT,
|
||||||
|
-- The serial ID of the output_room_events table when this event became
|
||||||
|
-- part of the current state of the room.
|
||||||
|
added_at BIGINT,
|
||||||
|
-- Clobber based on 3-uple of room_id, type and state_key
|
||||||
|
CONSTRAINT syncapi_room_state_unique UNIQUE (room_id, type, state_key)
|
||||||
|
);
|
||||||
|
-- for event deletion
|
||||||
|
-- CREATE UNIQUE INDEX IF NOT EXISTS syncapi_event_id_idx ON syncapi_current_room_state(event_id, room_id, type, sender, contains_url);
|
||||||
|
-- for querying membership states of users
|
||||||
|
-- CREATE INDEX IF NOT EXISTS syncapi_membership_idx ON syncapi_current_room_state(type, state_key, membership) WHERE membership IS NOT NULL AND membership != 'leave';
|
||||||
|
`
|
||||||
|
|
||||||
|
const upsertRoomStateSQL = "" +
|
||||||
|
"INSERT INTO syncapi_current_room_state (room_id, event_id, type, sender, contains_url, state_key, event_json, membership, added_at)" +
|
||||||
|
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)" +
|
||||||
|
" ON CONFLICT" + // ON CONSTRAINT syncapi_room_state_unique" +
|
||||||
|
" DO UPDATE SET event_id = $2, sender=$4, contains_url=$5, event_json = $7, membership = $8, added_at = $9"
|
||||||
|
|
||||||
|
const deleteRoomStateByEventIDSQL = "" +
|
||||||
|
"DELETE FROM syncapi_current_room_state WHERE event_id = $1"
|
||||||
|
|
||||||
|
const selectRoomIDsWithMembershipSQL = "" +
|
||||||
|
"SELECT room_id FROM syncapi_current_room_state WHERE type = 'm.room.member' AND state_key = $1 AND membership = $2"
|
||||||
|
|
||||||
|
const selectCurrentStateSQL = "" +
|
||||||
|
"SELECT event_json FROM syncapi_current_room_state WHERE room_id = $1" +
|
||||||
|
" AND ( $2::text[] IS NULL OR sender = ANY($2) )" +
|
||||||
|
" AND ( $3::text[] IS NULL OR NOT(sender = ANY($3)) )" +
|
||||||
|
" AND ( $4::text[] IS NULL OR type LIKE ANY($4) )" +
|
||||||
|
" AND ( $5::text[] IS NULL OR NOT(type LIKE ANY($5)) )" +
|
||||||
|
" AND ( $6::bool IS NULL OR contains_url = $6 )" +
|
||||||
|
" LIMIT $7"
|
||||||
|
|
||||||
|
const selectJoinedUsersSQL = "" +
|
||||||
|
"SELECT room_id, state_key FROM syncapi_current_room_state WHERE type = 'm.room.member' AND membership = 'join'"
|
||||||
|
|
||||||
|
const selectStateEventSQL = "" +
|
||||||
|
"SELECT event_json FROM syncapi_current_room_state WHERE room_id = $1 AND type = $2 AND state_key = $3"
|
||||||
|
|
||||||
|
const selectEventsWithEventIDsSQL = "" +
|
||||||
|
// TODO: The session_id and transaction_id blanks are here because otherwise
|
||||||
|
// the rowsToStreamEvents expects there to be exactly five columns. We need to
|
||||||
|
// figure out if these really need to be in the DB, and if so, we need a
|
||||||
|
// better permanent fix for this. - neilalexander, 2 Jan 2020
|
||||||
|
"SELECT added_at, event_json, 0 AS session_id, false AS exclude_from_sync, '' AS transaction_id" +
|
||||||
|
" FROM syncapi_current_room_state WHERE event_id = ANY($1)"
|
||||||
|
|
||||||
|
type currentRoomStateStatements struct {
|
||||||
|
upsertRoomStateStmt *sql.Stmt
|
||||||
|
deleteRoomStateByEventIDStmt *sql.Stmt
|
||||||
|
selectRoomIDsWithMembershipStmt *sql.Stmt
|
||||||
|
selectCurrentStateStmt *sql.Stmt
|
||||||
|
selectJoinedUsersStmt *sql.Stmt
|
||||||
|
selectEventsWithEventIDsStmt *sql.Stmt
|
||||||
|
selectStateEventStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *currentRoomStateStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(currentRoomStateSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.upsertRoomStateStmt, err = db.Prepare(upsertRoomStateSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.deleteRoomStateByEventIDStmt, err = db.Prepare(deleteRoomStateByEventIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectRoomIDsWithMembershipStmt, err = db.Prepare(selectRoomIDsWithMembershipSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectCurrentStateStmt, err = db.Prepare(selectCurrentStateSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectJoinedUsersStmt, err = db.Prepare(selectJoinedUsersSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventsWithEventIDsStmt, err = db.Prepare(selectEventsWithEventIDsSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectStateEventStmt, err = db.Prepare(selectStateEventSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// JoinedMemberLists returns a map of room ID to a list of joined user IDs.
|
||||||
|
func (s *currentRoomStateStatements) selectJoinedUsers(
|
||||||
|
ctx context.Context,
|
||||||
|
) (map[string][]string, error) {
|
||||||
|
rows, err := s.selectJoinedUsersStmt.QueryContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
result := make(map[string][]string)
|
||||||
|
for rows.Next() {
|
||||||
|
var roomID string
|
||||||
|
var userID string
|
||||||
|
if err := rows.Scan(&roomID, &userID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
users := result[roomID]
|
||||||
|
users = append(users, userID)
|
||||||
|
result[roomID] = users
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectRoomIDsWithMembership returns the list of room IDs which have the given user in the given membership state.
|
||||||
|
func (s *currentRoomStateStatements) selectRoomIDsWithMembership(
|
||||||
|
ctx context.Context,
|
||||||
|
txn *sql.Tx,
|
||||||
|
userID string,
|
||||||
|
membership string, // nolint: unparam
|
||||||
|
) ([]string, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectRoomIDsWithMembershipStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, userID, membership)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
var result []string
|
||||||
|
for rows.Next() {
|
||||||
|
var roomID string
|
||||||
|
if err := rows.Scan(&roomID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, roomID)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentState returns all the current state events for the given room.
|
||||||
|
func (s *currentRoomStateStatements) selectCurrentState(
|
||||||
|
ctx context.Context, txn *sql.Tx, roomID string,
|
||||||
|
stateFilterPart *gomatrix.FilterPart,
|
||||||
|
) ([]gomatrixserverlib.Event, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectCurrentStateStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomID,
|
||||||
|
pq.StringArray(stateFilterPart.Senders),
|
||||||
|
pq.StringArray(stateFilterPart.NotSenders),
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.Types)),
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.NotTypes)),
|
||||||
|
stateFilterPart.ContainsURL,
|
||||||
|
stateFilterPart.Limit,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
|
||||||
|
return rowsToEvents(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *currentRoomStateStatements) deleteRoomStateByEventID(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventID string,
|
||||||
|
) error {
|
||||||
|
stmt := common.TxStmt(txn, s.deleteRoomStateByEventIDStmt)
|
||||||
|
_, err := stmt.ExecContext(ctx, eventID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *currentRoomStateStatements) upsertRoomState(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
event gomatrixserverlib.Event, membership *string, addedAt types.StreamPosition,
|
||||||
|
) error {
|
||||||
|
// Parse content as JSON and search for an "url" key
|
||||||
|
containsURL := false
|
||||||
|
var content map[string]interface{}
|
||||||
|
if json.Unmarshal(event.Content(), &content) != nil {
|
||||||
|
// Set containsURL to true if url is present
|
||||||
|
_, containsURL = content["url"]
|
||||||
|
}
|
||||||
|
|
||||||
|
// upsert state event
|
||||||
|
stmt := common.TxStmt(txn, s.upsertRoomStateStmt)
|
||||||
|
_, err := stmt.ExecContext(
|
||||||
|
ctx,
|
||||||
|
event.RoomID(),
|
||||||
|
event.EventID(),
|
||||||
|
event.Type(),
|
||||||
|
event.Sender(),
|
||||||
|
containsURL,
|
||||||
|
*event.StateKey(),
|
||||||
|
event.JSON(),
|
||||||
|
membership,
|
||||||
|
addedAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *currentRoomStateStatements) selectEventsWithEventIDs(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
||||||
|
) ([]types.StreamEvent, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectEventsWithEventIDsStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
return rowsToStreamEvents(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rowsToEvents(rows *sql.Rows) ([]gomatrixserverlib.Event, error) {
|
||||||
|
result := []gomatrixserverlib.Event{}
|
||||||
|
for rows.Next() {
|
||||||
|
var eventBytes []byte
|
||||||
|
if err := rows.Scan(&eventBytes); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// TODO: Handle redacted events
|
||||||
|
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(eventBytes, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, ev)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *currentRoomStateStatements) selectStateEvent(
|
||||||
|
ctx context.Context, roomID, evType, stateKey string,
|
||||||
|
) (*gomatrixserverlib.Event, error) {
|
||||||
|
stmt := s.selectStateEventStmt
|
||||||
|
var res []byte
|
||||||
|
err := stmt.QueryRowContext(ctx, roomID, evType, stateKey).Scan(&res)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(res, false)
|
||||||
|
return &ev, err
|
||||||
|
}
|
36
syncapi/storage/sqlite3/filtering.go
Normal file
36
syncapi/storage/sqlite3/filtering.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// Copyright 2017 Thibaut CHARLES
|
||||||
|
//
|
||||||
|
// 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 (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// filterConvertWildcardToSQL converts wildcards as defined in
|
||||||
|
// https://matrix.org/docs/spec/client_server/r0.3.0.html#post-matrix-client-r0-user-userid-filter
|
||||||
|
// to SQL wildcards that can be used with LIKE()
|
||||||
|
func filterConvertTypeWildcardToSQL(values []string) []string {
|
||||||
|
if values == nil {
|
||||||
|
// Return nil instead of []string{} so IS NULL can work correctly when
|
||||||
|
// the return value is passed into SQL queries
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := make([]string, len(values))
|
||||||
|
for i := range values {
|
||||||
|
ret[i] = strings.Replace(values[i], "*", "%", -1)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
149
syncapi/storage/sqlite3/invites_table.go
Normal file
149
syncapi/storage/sqlite3/invites_table.go
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const inviteEventsSchema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_invite_events (
|
||||||
|
id BIGINT PRIMARY KEY DEFAULT AUTOINCREMENT, -- nextval('syncapi_stream_id'),
|
||||||
|
event_id TEXT NOT NULL,
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
target_user_id TEXT NOT NULL,
|
||||||
|
event_json TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- For looking up the invites for a given user.
|
||||||
|
-- CREATE INDEX IF NOT EXISTS syncapi_invites_target_user_id_idx
|
||||||
|
-- ON syncapi_invite_events (target_user_id, id);
|
||||||
|
|
||||||
|
-- For deleting old invites
|
||||||
|
-- CREATE INDEX IF NOT EXISTS syncapi_invites_event_id_idx
|
||||||
|
-- ON syncapi_invite_events (event_id);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertInviteEventSQL = "" +
|
||||||
|
"INSERT INTO syncapi_invite_events (" +
|
||||||
|
" room_id, event_id, target_user_id, event_json" +
|
||||||
|
") VALUES ($1, $2, $3, $4) RETURNING id"
|
||||||
|
|
||||||
|
const deleteInviteEventSQL = "" +
|
||||||
|
"DELETE FROM syncapi_invite_events WHERE event_id = $1"
|
||||||
|
|
||||||
|
const selectInviteEventsInRangeSQL = "" +
|
||||||
|
"SELECT room_id, event_json FROM syncapi_invite_events" +
|
||||||
|
" WHERE target_user_id = $1 AND id > $2 AND id <= $3" +
|
||||||
|
" ORDER BY id DESC"
|
||||||
|
|
||||||
|
const selectMaxInviteIDSQL = "" +
|
||||||
|
"SELECT MAX(id) FROM syncapi_invite_events"
|
||||||
|
|
||||||
|
type inviteEventsStatements struct {
|
||||||
|
insertInviteEventStmt *sql.Stmt
|
||||||
|
selectInviteEventsInRangeStmt *sql.Stmt
|
||||||
|
deleteInviteEventStmt *sql.Stmt
|
||||||
|
selectMaxInviteIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteEventsStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(inviteEventsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertInviteEventStmt, err = db.Prepare(insertInviteEventSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectInviteEventsInRangeStmt, err = db.Prepare(selectInviteEventsInRangeSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.deleteInviteEventStmt, err = db.Prepare(deleteInviteEventSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectMaxInviteIDStmt, err = db.Prepare(selectMaxInviteIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteEventsStatements) insertInviteEvent(
|
||||||
|
ctx context.Context, inviteEvent gomatrixserverlib.Event,
|
||||||
|
) (streamPos types.StreamPosition, err error) {
|
||||||
|
err = s.insertInviteEventStmt.QueryRowContext(
|
||||||
|
ctx,
|
||||||
|
inviteEvent.RoomID(),
|
||||||
|
inviteEvent.EventID(),
|
||||||
|
*inviteEvent.StateKey(),
|
||||||
|
inviteEvent.JSON(),
|
||||||
|
).Scan(&streamPos)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteEventsStatements) deleteInviteEvent(
|
||||||
|
ctx context.Context, inviteEventID string,
|
||||||
|
) error {
|
||||||
|
_, err := s.deleteInviteEventStmt.ExecContext(ctx, inviteEventID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectInviteEventsInRange returns a map of room ID to invite event for the
|
||||||
|
// active invites for the target user ID in the supplied range.
|
||||||
|
func (s *inviteEventsStatements) selectInviteEventsInRange(
|
||||||
|
ctx context.Context, txn *sql.Tx, targetUserID string, startPos, endPos types.StreamPosition,
|
||||||
|
) (map[string]gomatrixserverlib.Event, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectInviteEventsInRangeStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, targetUserID, startPos, endPos)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
result := map[string]gomatrixserverlib.Event{}
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
roomID string
|
||||||
|
eventJSON []byte
|
||||||
|
)
|
||||||
|
if err = rows.Scan(&roomID, &eventJSON); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
event, err := gomatrixserverlib.NewEventFromTrustedJSON(eventJSON, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result[roomID] = event
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inviteEventsStatements) selectMaxInviteID(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
) (id int64, err error) {
|
||||||
|
var nullableID sql.NullInt64
|
||||||
|
stmt := common.TxStmt(txn, s.selectMaxInviteIDStmt)
|
||||||
|
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
|
||||||
|
if nullableID.Valid {
|
||||||
|
id = nullableID.Int64
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
396
syncapi/storage/sqlite3/output_room_events_table.go
Normal file
396
syncapi/storage/sqlite3/output_room_events_table.go
Normal file
|
@ -0,0 +1,396 @@
|
||||||
|
// 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"
|
||||||
|
"encoding/json"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const outputRoomEventsSchema = `
|
||||||
|
-- This sequence is shared between all the tables generated from kafka logs.
|
||||||
|
CREATE SEQUENCE IF NOT EXISTS syncapi_stream_id;
|
||||||
|
|
||||||
|
-- Stores output room events received from the roomserver.
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_output_room_events (
|
||||||
|
-- An incrementing ID which denotes the position in the log that this event resides at.
|
||||||
|
-- NB: 'serial' makes no guarantees to increment by 1 every time, only that it increments.
|
||||||
|
-- This isn't a problem for us since we just want to order by this field.
|
||||||
|
id BIGINT PRIMARY KEY AUTOINCREMENT, -- DEFAULT nextval('syncapi_stream_id'),
|
||||||
|
-- The event ID for the event
|
||||||
|
event_id TEXT NOT NULL UNIQUE, -- CONSTRAINT syncapi_event_id_idx UNIQUE,
|
||||||
|
-- The 'room_id' key for the event.
|
||||||
|
room_id TEXT NOT NULL,
|
||||||
|
-- The JSON for the event. Stored as TEXT because this should be valid UTF-8.
|
||||||
|
event_json TEXT NOT NULL,
|
||||||
|
-- The event type e.g 'm.room.member'.
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
-- The 'sender' property of the event.
|
||||||
|
sender TEXT NOT NULL,
|
||||||
|
-- true if the event content contains a url key.
|
||||||
|
contains_url BOOL NOT NULL,
|
||||||
|
-- A list of event IDs which represent a delta of added/removed room state. This can be NULL
|
||||||
|
-- if there is no delta.
|
||||||
|
add_state_ids TEXT[],
|
||||||
|
remove_state_ids TEXT[],
|
||||||
|
-- The client session that sent the event, if any
|
||||||
|
session_id BIGINT,
|
||||||
|
-- The transaction id used to send the event, if any
|
||||||
|
transaction_id TEXT,
|
||||||
|
-- Should the event be excluded from responses to /sync requests. Useful for
|
||||||
|
-- events retrieved through backfilling that have a position in the stream
|
||||||
|
-- that relates to the moment these were retrieved rather than the moment these
|
||||||
|
-- were emitted.
|
||||||
|
exclude_from_sync BOOL DEFAULT FALSE
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventSQL = "" +
|
||||||
|
"INSERT INTO syncapi_output_room_events (" +
|
||||||
|
"room_id, event_id, event_json, type, sender, contains_url, add_state_ids, remove_state_ids, session_id, transaction_id, exclude_from_sync" +
|
||||||
|
") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) " +
|
||||||
|
"ON CONFLICT DO UPDATE SET exclude_from_sync = $11 " +
|
||||||
|
"RETURNING id"
|
||||||
|
|
||||||
|
const selectEventsSQL = "" +
|
||||||
|
"SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events WHERE event_id = ANY($1)"
|
||||||
|
|
||||||
|
const selectRecentEventsSQL = "" +
|
||||||
|
"SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
||||||
|
" WHERE room_id = $1 AND id > $2 AND id <= $3" +
|
||||||
|
" ORDER BY id DESC LIMIT $4"
|
||||||
|
|
||||||
|
const selectRecentEventsForSyncSQL = "" +
|
||||||
|
"SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
||||||
|
" WHERE room_id = $1 AND id > $2 AND id <= $3 AND exclude_from_sync = FALSE" +
|
||||||
|
" ORDER BY id DESC LIMIT $4"
|
||||||
|
|
||||||
|
const selectEarlyEventsSQL = "" +
|
||||||
|
"SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
||||||
|
" WHERE room_id = $1 AND id > $2 AND id <= $3" +
|
||||||
|
" ORDER BY id ASC LIMIT $4"
|
||||||
|
|
||||||
|
const selectMaxEventIDSQL = "" +
|
||||||
|
"SELECT MAX(id) FROM syncapi_output_room_events"
|
||||||
|
|
||||||
|
// In order for us to apply the state updates correctly, rows need to be ordered in the order they were received (id).
|
||||||
|
const selectStateInRangeSQL = "" +
|
||||||
|
"SELECT id, event_json, exclude_from_sync, add_state_ids, remove_state_ids" +
|
||||||
|
" FROM syncapi_output_room_events" +
|
||||||
|
" WHERE (id > $1 AND id <= $2) AND (add_state_ids IS NOT NULL OR remove_state_ids IS NOT NULL)" +
|
||||||
|
" AND ( $3::text[] IS NULL OR sender = ANY($3) )" +
|
||||||
|
" AND ( $4::text[] IS NULL OR NOT(sender = ANY($4)) )" +
|
||||||
|
" AND ( $5::text[] IS NULL OR type LIKE ANY($5) )" +
|
||||||
|
" AND ( $6::text[] IS NULL OR NOT(type LIKE ANY($6)) )" +
|
||||||
|
" AND ( $7::bool IS NULL OR contains_url = $7 )" +
|
||||||
|
" ORDER BY id ASC" +
|
||||||
|
" LIMIT $8"
|
||||||
|
|
||||||
|
type outputRoomEventsStatements struct {
|
||||||
|
insertEventStmt *sql.Stmt
|
||||||
|
selectEventsStmt *sql.Stmt
|
||||||
|
selectMaxEventIDStmt *sql.Stmt
|
||||||
|
selectRecentEventsStmt *sql.Stmt
|
||||||
|
selectRecentEventsForSyncStmt *sql.Stmt
|
||||||
|
selectEarlyEventsStmt *sql.Stmt
|
||||||
|
selectStateInRangeStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *outputRoomEventsStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(outputRoomEventsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertEventStmt, err = db.Prepare(insertEventSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventsStmt, err = db.Prepare(selectEventsSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectMaxEventIDStmt, err = db.Prepare(selectMaxEventIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectRecentEventsStmt, err = db.Prepare(selectRecentEventsSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectRecentEventsForSyncStmt, err = db.Prepare(selectRecentEventsForSyncSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEarlyEventsStmt, err = db.Prepare(selectEarlyEventsSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectStateInRangeStmt, err = db.Prepare(selectStateInRangeSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectStateInRange returns the state events between the two given PDU stream positions, exclusive of oldPos, inclusive of newPos.
|
||||||
|
// Results are bucketed based on the room ID. If the same state is overwritten multiple times between the
|
||||||
|
// two positions, only the most recent state is returned.
|
||||||
|
func (s *outputRoomEventsStatements) selectStateInRange(
|
||||||
|
ctx context.Context, txn *sql.Tx, oldPos, newPos types.StreamPosition,
|
||||||
|
stateFilterPart *gomatrix.FilterPart,
|
||||||
|
) (map[string]map[string]bool, map[string]types.StreamEvent, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectStateInRangeStmt)
|
||||||
|
|
||||||
|
rows, err := stmt.QueryContext(
|
||||||
|
ctx, oldPos, newPos,
|
||||||
|
pq.StringArray(stateFilterPart.Senders),
|
||||||
|
pq.StringArray(stateFilterPart.NotSenders),
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.Types)),
|
||||||
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.NotTypes)),
|
||||||
|
stateFilterPart.ContainsURL,
|
||||||
|
stateFilterPart.Limit,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
// Fetch all the state change events for all rooms between the two positions then loop each event and:
|
||||||
|
// - Keep a cache of the event by ID (99% of state change events are for the event itself)
|
||||||
|
// - For each room ID, build up an array of event IDs which represents cumulative adds/removes
|
||||||
|
// For each room, map cumulative event IDs to events and return. This may need to a batch SELECT based on event ID
|
||||||
|
// if they aren't in the event ID cache. We don't handle state deletion yet.
|
||||||
|
eventIDToEvent := make(map[string]types.StreamEvent)
|
||||||
|
|
||||||
|
// RoomID => A set (map[string]bool) of state event IDs which are between the two positions
|
||||||
|
stateNeeded := make(map[string]map[string]bool)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
streamPos types.StreamPosition
|
||||||
|
eventBytes []byte
|
||||||
|
excludeFromSync bool
|
||||||
|
addIDs pq.StringArray
|
||||||
|
delIDs pq.StringArray
|
||||||
|
)
|
||||||
|
if err := rows.Scan(&streamPos, &eventBytes, &excludeFromSync, &addIDs, &delIDs); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
// Sanity check for deleted state and whine if we see it. We don't need to do anything
|
||||||
|
// since it'll just mark the event as not being needed.
|
||||||
|
if len(addIDs) < len(delIDs) {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"since": oldPos,
|
||||||
|
"current": newPos,
|
||||||
|
"adds": addIDs,
|
||||||
|
"dels": delIDs,
|
||||||
|
}).Warn("StateBetween: ignoring deleted state")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Handle redacted events
|
||||||
|
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(eventBytes, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
needSet := stateNeeded[ev.RoomID()]
|
||||||
|
if needSet == nil { // make set if required
|
||||||
|
needSet = make(map[string]bool)
|
||||||
|
}
|
||||||
|
for _, id := range delIDs {
|
||||||
|
needSet[id] = false
|
||||||
|
}
|
||||||
|
for _, id := range addIDs {
|
||||||
|
needSet[id] = true
|
||||||
|
}
|
||||||
|
stateNeeded[ev.RoomID()] = needSet
|
||||||
|
|
||||||
|
eventIDToEvent[ev.EventID()] = types.StreamEvent{
|
||||||
|
Event: ev,
|
||||||
|
StreamPosition: streamPos,
|
||||||
|
ExcludeFromSync: excludeFromSync,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stateNeeded, eventIDToEvent, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxID returns the ID of the last inserted event in this table. 'txn' is optional. If it is not supplied,
|
||||||
|
// then this function should only ever be used at startup, as it will race with inserting events if it is
|
||||||
|
// done afterwards. If there are no inserted events, 0 is returned.
|
||||||
|
func (s *outputRoomEventsStatements) selectMaxEventID(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
) (id int64, err error) {
|
||||||
|
var nullableID sql.NullInt64
|
||||||
|
stmt := common.TxStmt(txn, s.selectMaxEventIDStmt)
|
||||||
|
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
|
||||||
|
if nullableID.Valid {
|
||||||
|
id = nullableID.Int64
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertEvent into the output_room_events table. addState and removeState are an optional list of state event IDs. Returns the position
|
||||||
|
// of the inserted event.
|
||||||
|
func (s *outputRoomEventsStatements) insertEvent(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
event *gomatrixserverlib.Event, addState, removeState []string,
|
||||||
|
transactionID *api.TransactionID, excludeFromSync bool,
|
||||||
|
) (streamPos types.StreamPosition, err error) {
|
||||||
|
var txnID *string
|
||||||
|
var sessionID *int64
|
||||||
|
if transactionID != nil {
|
||||||
|
sessionID = &transactionID.SessionID
|
||||||
|
txnID = &transactionID.TransactionID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse content as JSON and search for an "url" key
|
||||||
|
containsURL := false
|
||||||
|
var content map[string]interface{}
|
||||||
|
if json.Unmarshal(event.Content(), &content) != nil {
|
||||||
|
// Set containsURL to true if url is present
|
||||||
|
_, containsURL = content["url"]
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := common.TxStmt(txn, s.insertEventStmt)
|
||||||
|
err = stmt.QueryRowContext(
|
||||||
|
ctx,
|
||||||
|
event.RoomID(),
|
||||||
|
event.EventID(),
|
||||||
|
event.JSON(),
|
||||||
|
event.Type(),
|
||||||
|
event.Sender(),
|
||||||
|
containsURL,
|
||||||
|
pq.StringArray(addState),
|
||||||
|
pq.StringArray(removeState),
|
||||||
|
sessionID,
|
||||||
|
txnID,
|
||||||
|
excludeFromSync,
|
||||||
|
).Scan(&streamPos)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectRecentEvents returns the most recent events in the given room, up to a maximum of 'limit'.
|
||||||
|
// If onlySyncEvents has a value of true, only returns the events that aren't marked as to exclude
|
||||||
|
// from sync.
|
||||||
|
func (s *outputRoomEventsStatements) selectRecentEvents(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, fromPos, toPos types.StreamPosition, limit int,
|
||||||
|
chronologicalOrder bool, onlySyncEvents bool,
|
||||||
|
) ([]types.StreamEvent, error) {
|
||||||
|
var stmt *sql.Stmt
|
||||||
|
if onlySyncEvents {
|
||||||
|
stmt = common.TxStmt(txn, s.selectRecentEventsForSyncStmt)
|
||||||
|
} else {
|
||||||
|
stmt = common.TxStmt(txn, s.selectRecentEventsStmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomID, fromPos, toPos, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
events, err := rowsToStreamEvents(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if chronologicalOrder {
|
||||||
|
// The events need to be returned from oldest to latest, which isn't
|
||||||
|
// necessary the way the SQL query returns them, so a sort is necessary to
|
||||||
|
// ensure the events are in the right order in the slice.
|
||||||
|
sort.SliceStable(events, func(i int, j int) bool {
|
||||||
|
return events[i].StreamPosition < events[j].StreamPosition
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return events, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectEarlyEvents returns the earliest events in the given room, starting
|
||||||
|
// from a given position, up to a maximum of 'limit'.
|
||||||
|
func (s *outputRoomEventsStatements) selectEarlyEvents(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, fromPos, toPos types.StreamPosition, limit int,
|
||||||
|
) ([]types.StreamEvent, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectEarlyEventsStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomID, fromPos, toPos, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
events, err := rowsToStreamEvents(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// The events need to be returned from oldest to latest, which isn't
|
||||||
|
// necessarily the way the SQL query returns them, so a sort is necessary to
|
||||||
|
// ensure the events are in the right order in the slice.
|
||||||
|
sort.SliceStable(events, func(i int, j int) bool {
|
||||||
|
return events[i].StreamPosition < events[j].StreamPosition
|
||||||
|
})
|
||||||
|
return events, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectEvents returns the events for the given event IDs. If an event is
|
||||||
|
// missing from the database, it will be omitted.
|
||||||
|
func (s *outputRoomEventsStatements) selectEvents(
|
||||||
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
||||||
|
) ([]types.StreamEvent, error) {
|
||||||
|
stmt := common.TxStmt(txn, s.selectEventsStmt)
|
||||||
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // nolint: errcheck
|
||||||
|
return rowsToStreamEvents(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rowsToStreamEvents(rows *sql.Rows) ([]types.StreamEvent, error) {
|
||||||
|
var result []types.StreamEvent
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
streamPos types.StreamPosition
|
||||||
|
eventBytes []byte
|
||||||
|
excludeFromSync bool
|
||||||
|
sessionID *int64
|
||||||
|
txnID *string
|
||||||
|
transactionID *api.TransactionID
|
||||||
|
)
|
||||||
|
if err := rows.Scan(&streamPos, &eventBytes, &sessionID, &excludeFromSync, &txnID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// TODO: Handle redacted events
|
||||||
|
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(eventBytes, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if sessionID != nil && txnID != nil {
|
||||||
|
transactionID = &api.TransactionID{
|
||||||
|
SessionID: *sessionID,
|
||||||
|
TransactionID: *txnID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, types.StreamEvent{
|
||||||
|
Event: ev,
|
||||||
|
StreamPosition: streamPos,
|
||||||
|
TransactionID: transactionID,
|
||||||
|
ExcludeFromSync: excludeFromSync,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
188
syncapi/storage/sqlite3/output_room_events_topology_table.go
Normal file
188
syncapi/storage/sqlite3/output_room_events_topology_table.go
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
// Copyright 2018 New Vector 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 sqlite3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const outputRoomEventsTopologySchema = `
|
||||||
|
-- Stores output room events received from the roomserver.
|
||||||
|
CREATE TABLE IF NOT EXISTS syncapi_output_room_events_topology (
|
||||||
|
-- The event ID for the event.
|
||||||
|
event_id TEXT PRIMARY KEY,
|
||||||
|
-- The place of the event in the room's topology. This can usually be determined
|
||||||
|
-- from the event's depth.
|
||||||
|
topological_position BIGINT NOT NULL,
|
||||||
|
-- The 'room_id' key for the event.
|
||||||
|
room_id TEXT NOT NULL
|
||||||
|
);
|
||||||
|
-- The topological order will be used in events selection and ordering
|
||||||
|
-- CREATE UNIQUE INDEX IF NOT EXISTS syncapi_event_topological_position_idx ON syncapi_output_room_events_topology(topological_position, room_id);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertEventInTopologySQL = "" +
|
||||||
|
"INSERT INTO syncapi_output_room_events_topology (event_id, topological_position, room_id)" +
|
||||||
|
" VALUES ($1, $2, $3)" +
|
||||||
|
" ON CONFLICT (topological_position, room_id) DO UPDATE SET event_id = $1"
|
||||||
|
|
||||||
|
const selectEventIDsInRangeASCSQL = "" +
|
||||||
|
"SELECT event_id FROM syncapi_output_room_events_topology" +
|
||||||
|
" WHERE room_id = $1 AND topological_position > $2 AND topological_position <= $3" +
|
||||||
|
" ORDER BY topological_position ASC LIMIT $4"
|
||||||
|
|
||||||
|
const selectEventIDsInRangeDESCSQL = "" +
|
||||||
|
"SELECT event_id FROM syncapi_output_room_events_topology" +
|
||||||
|
" WHERE room_id = $1 AND topological_position > $2 AND topological_position <= $3" +
|
||||||
|
" ORDER BY topological_position DESC LIMIT $4"
|
||||||
|
|
||||||
|
const selectPositionInTopologySQL = "" +
|
||||||
|
"SELECT topological_position FROM syncapi_output_room_events_topology" +
|
||||||
|
" WHERE event_id = $1"
|
||||||
|
|
||||||
|
const selectMaxPositionInTopologySQL = "" +
|
||||||
|
"SELECT MAX(topological_position) FROM syncapi_output_room_events_topology" +
|
||||||
|
" WHERE room_id = $1"
|
||||||
|
|
||||||
|
const selectEventIDsFromPositionSQL = "" +
|
||||||
|
"SELECT event_id FROM syncapi_output_room_events_topology" +
|
||||||
|
" WHERE room_id = $1 AND topological_position = $2"
|
||||||
|
|
||||||
|
type outputRoomEventsTopologyStatements struct {
|
||||||
|
insertEventInTopologyStmt *sql.Stmt
|
||||||
|
selectEventIDsInRangeASCStmt *sql.Stmt
|
||||||
|
selectEventIDsInRangeDESCStmt *sql.Stmt
|
||||||
|
selectPositionInTopologyStmt *sql.Stmt
|
||||||
|
selectMaxPositionInTopologyStmt *sql.Stmt
|
||||||
|
selectEventIDsFromPositionStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *outputRoomEventsTopologyStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(outputRoomEventsTopologySchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertEventInTopologyStmt, err = db.Prepare(insertEventInTopologySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventIDsInRangeASCStmt, err = db.Prepare(selectEventIDsInRangeASCSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventIDsInRangeDESCStmt, err = db.Prepare(selectEventIDsInRangeDESCSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectPositionInTopologyStmt, err = db.Prepare(selectPositionInTopologySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectMaxPositionInTopologyStmt, err = db.Prepare(selectMaxPositionInTopologySQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventIDsFromPositionStmt, err = db.Prepare(selectEventIDsFromPositionSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// insertEventInTopology inserts the given event in the room's topology, based
|
||||||
|
// on the event's depth.
|
||||||
|
func (s *outputRoomEventsTopologyStatements) insertEventInTopology(
|
||||||
|
ctx context.Context, event *gomatrixserverlib.Event,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.insertEventInTopologyStmt.ExecContext(
|
||||||
|
ctx, event.EventID(), event.Depth(), event.RoomID(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectEventIDsInRange selects the IDs of events which positions are within a
|
||||||
|
// given range in a given room's topological order.
|
||||||
|
// Returns an empty slice if no events match the given range.
|
||||||
|
func (s *outputRoomEventsTopologyStatements) selectEventIDsInRange(
|
||||||
|
ctx context.Context, roomID string, fromPos, toPos types.StreamPosition,
|
||||||
|
limit int, chronologicalOrder bool,
|
||||||
|
) (eventIDs []string, err error) {
|
||||||
|
// Decide on the selection's order according to whether chronological order
|
||||||
|
// is requested or not.
|
||||||
|
var stmt *sql.Stmt
|
||||||
|
if chronologicalOrder {
|
||||||
|
stmt = s.selectEventIDsInRangeASCStmt
|
||||||
|
} else {
|
||||||
|
stmt = s.selectEventIDsInRangeDESCStmt
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query the event IDs.
|
||||||
|
rows, err := stmt.QueryContext(ctx, roomID, fromPos, toPos, limit)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// If no event matched the request, return an empty slice.
|
||||||
|
return []string{}, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the IDs.
|
||||||
|
var eventID string
|
||||||
|
for rows.Next() {
|
||||||
|
if err = rows.Scan(&eventID); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eventIDs = append(eventIDs, eventID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectPositionInTopology returns the position of a given event in the
|
||||||
|
// topology of the room it belongs to.
|
||||||
|
func (s *outputRoomEventsTopologyStatements) selectPositionInTopology(
|
||||||
|
ctx context.Context, eventID string,
|
||||||
|
) (pos types.StreamPosition, err error) {
|
||||||
|
err = s.selectPositionInTopologyStmt.QueryRowContext(ctx, eventID).Scan(&pos)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *outputRoomEventsTopologyStatements) selectMaxPositionInTopology(
|
||||||
|
ctx context.Context, roomID string,
|
||||||
|
) (pos types.StreamPosition, err error) {
|
||||||
|
err = s.selectMaxPositionInTopologyStmt.QueryRowContext(ctx, roomID).Scan(&pos)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectEventIDsFromPosition returns the IDs of all events that have a given
|
||||||
|
// position in the topology of a given room.
|
||||||
|
func (s *outputRoomEventsTopologyStatements) selectEventIDsFromPosition(
|
||||||
|
ctx context.Context, roomID string, pos types.StreamPosition,
|
||||||
|
) (eventIDs []string, err error) {
|
||||||
|
// Query the event IDs.
|
||||||
|
rows, err := s.selectEventIDsFromPositionStmt.QueryContext(ctx, roomID, pos)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// If no event matched the request, return an empty slice.
|
||||||
|
return []string{}, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Return the IDs.
|
||||||
|
var eventID string
|
||||||
|
for rows.Next() {
|
||||||
|
if err = rows.Scan(&eventID); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eventIDs = append(eventIDs, eventID)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
1153
syncapi/storage/sqlite3/syncserver.go
Normal file
1153
syncapi/storage/sqlite3/syncserver.go
Normal file
File diff suppressed because it is too large
Load diff
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
"github.com/matrix-org/dendrite/syncapi/storage/postgres"
|
"github.com/matrix-org/dendrite/syncapi/storage/postgres"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/storage/sqlite3"
|
||||||
"github.com/matrix-org/dendrite/syncapi/types"
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
"github.com/matrix-org/dendrite/typingserver/cache"
|
"github.com/matrix-org/dendrite/typingserver/cache"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
|
@ -64,6 +65,8 @@ func NewSyncServerDatasource(dataSourceName string) (Database, error) {
|
||||||
switch uri.Scheme {
|
switch uri.Scheme {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return postgres.NewSyncServerDatasource(dataSourceName)
|
return postgres.NewSyncServerDatasource(dataSourceName)
|
||||||
|
case "file":
|
||||||
|
return sqlite3.NewSyncServerDatasource(dataSourceName)
|
||||||
default:
|
default:
|
||||||
return postgres.NewSyncServerDatasource(dataSourceName)
|
return postgres.NewSyncServerDatasource(dataSourceName)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue