2020-02-13 18:27:33 +01:00
|
|
|
// 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"
|
2021-04-26 14:25:57 +02:00
|
|
|
"encoding/json"
|
2020-02-13 18:27:33 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const stateDataSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_state_block (
|
2021-04-26 14:25:57 +02:00
|
|
|
-- The state snapshot NID that identifies this snapshot.
|
|
|
|
state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
-- The hash of the state block, which is used to enforce uniqueness. The hash is
|
|
|
|
-- generated in Dendrite and passed through to the database, as a btree index over
|
|
|
|
-- this column is cheap and fits within the maximum index size.
|
|
|
|
state_block_hash BLOB UNIQUE,
|
|
|
|
-- The event NIDs contained within the state block, encoded as JSON.
|
|
|
|
event_nids TEXT NOT NULL DEFAULT '[]'
|
2020-02-13 18:27:33 +01:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
2021-04-26 14:25:57 +02:00
|
|
|
// Insert a new state block. If we conflict on the hash column then
|
|
|
|
// we must perform an update so that the RETURNING statement returns the
|
|
|
|
// ID of the row that we conflicted with, so that we can then refer to
|
|
|
|
// the original block.
|
|
|
|
const insertStateDataSQL = `
|
|
|
|
INSERT INTO roomserver_state_block (state_block_hash, event_nids)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (state_block_hash) DO UPDATE SET event_nids=$2
|
|
|
|
RETURNING state_block_nid
|
2020-02-13 18:27:33 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const bulkSelectStateBlockEntriesSQL = "" +
|
2021-04-26 14:25:57 +02:00
|
|
|
"SELECT state_block_nid, event_nids" +
|
2021-07-09 11:49:49 +02:00
|
|
|
" FROM roomserver_state_block WHERE state_block_nid IN ($1) ORDER BY state_block_nid ASC"
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
type stateBlockStatements struct {
|
2021-04-26 14:25:57 +02:00
|
|
|
db *sql.DB
|
|
|
|
insertStateDataStmt *sql.Stmt
|
|
|
|
bulkSelectStateBlockEntriesStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:33:16 +02:00
|
|
|
func CreateStateBlockTable(db *sql.DB) error {
|
2021-04-26 14:25:57 +02:00
|
|
|
_, err := db.Exec(stateDataSchema)
|
|
|
|
return err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
func PrepareStateBlockTable(db *sql.DB) (*stateBlockStatements, error) {
|
2020-07-21 11:48:49 +02:00
|
|
|
s := &stateBlockStatements{
|
2020-08-19 16:38:27 +02:00
|
|
|
db: db,
|
2020-07-21 11:48:49 +02:00
|
|
|
}
|
2020-02-13 18:27:33 +01:00
|
|
|
|
2021-07-28 19:30:04 +02:00
|
|
|
return s, sqlutil.StatementList{
|
2020-02-13 18:27:33 +01:00
|
|
|
{&s.insertStateDataStmt, insertStateDataSQL},
|
|
|
|
{&s.bulkSelectStateBlockEntriesStmt, bulkSelectStateBlockEntriesSQL},
|
2020-05-27 12:03:47 +02:00
|
|
|
}.Prepare(db)
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (s *stateBlockStatements) BulkInsertStateData(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2021-04-26 14:25:57 +02:00
|
|
|
entries types.StateEntries,
|
|
|
|
) (id types.StateBlockNID, err error) {
|
|
|
|
entries = entries[:util.SortAndUnique(entries)]
|
2022-05-16 19:33:16 +02:00
|
|
|
nids := make(types.EventNIDs, entries.Len())
|
|
|
|
for i := range entries {
|
|
|
|
nids[i] = entries[i].EventNID
|
2021-04-26 14:25:57 +02:00
|
|
|
}
|
|
|
|
js, err := json.Marshal(nids)
|
2020-08-19 16:38:27 +02:00
|
|
|
if err != nil {
|
2021-04-26 14:25:57 +02:00
|
|
|
return 0, fmt.Errorf("json.Marshal: %w", err)
|
2020-08-19 16:38:27 +02:00
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertStateDataStmt)
|
|
|
|
err = stmt.QueryRowContext(
|
2021-04-26 14:25:57 +02:00
|
|
|
ctx, nids.Hash(), js,
|
|
|
|
).Scan(&id)
|
|
|
|
return
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (s *stateBlockStatements) BulkSelectStateBlockEntries(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, stateBlockNIDs types.StateBlockNIDs,
|
2021-04-26 14:25:57 +02:00
|
|
|
) ([][]types.EventNID, error) {
|
|
|
|
intfs := make([]interface{}, len(stateBlockNIDs))
|
|
|
|
for i := range stateBlockNIDs {
|
|
|
|
intfs[i] = int64(stateBlockNIDs[i])
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
selectOrig := strings.Replace(bulkSelectStateBlockEntriesSQL, "($1)", sqlutil.QueryVariadic(len(intfs)), 1)
|
2022-03-04 16:05:42 +01:00
|
|
|
selectPrep, err := s.db.Prepare(selectOrig)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-04 16:05:42 +01:00
|
|
|
defer selectPrep.Close() // nolint:errcheck
|
|
|
|
selectStmt := sqlutil.TxStmt(txn, selectPrep)
|
2021-04-26 14:25:57 +02:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, intfs...)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateBlockEntries: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
|
2021-04-26 14:25:57 +02:00
|
|
|
results := make([][]types.EventNID, len(stateBlockNIDs))
|
2020-02-13 18:27:33 +01:00
|
|
|
i := 0
|
2022-05-16 19:33:16 +02:00
|
|
|
var stateBlockNID types.StateBlockNID
|
|
|
|
var result json.RawMessage
|
2021-04-26 14:25:57 +02:00
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
if err = rows.Scan(&stateBlockNID, &result); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-16 19:33:16 +02:00
|
|
|
var r []types.EventNID
|
2021-04-26 14:25:57 +02:00
|
|
|
if err = json.Unmarshal(result, &r); err != nil {
|
|
|
|
return nil, fmt.Errorf("json.Unmarshal: %w", err)
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
results[i] = r
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
if err = rows.Err(); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
if i != len(stateBlockNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: state data NIDs missing from the database (%d != %d)", len(results), len(stateBlockNIDs))
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
return results, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|