mirror of
https://github.com/1f349/dendrite.git
synced 2024-12-23 16:54:08 +00:00
b6ea1bc67a
* Move current work into single branch * Initial massaging of clientapi etc (not working yet) * Interfaces for accounts/devices databases * Duplicate postgres package for sqlite3 (no changes made to it yet) * Some keydb, accountdb, devicedb, common partition fixes, some more syncapi tweaking * Fix accounts DB, device DB * Update naffka dependency for SQLite * Naffka SQLite * Update naffka to latest master * SQLite support for federationsender * Mostly not-bad support for SQLite in syncapi (although there are problems where lots of events get classed incorrectly as backward extremities, probably because of IN/ANY clauses that are badly supported) * Update Dockerfile -> Go 1.13.7, add build-base (as gcc and friends are needed for SQLite) * Implement GET endpoints for account_data in clientapi * Nuke filtering for now... * Revert "Implement GET endpoints for account_data in clientapi" This reverts commit 4d80dff4583d278620d9b3ed437e9fcd8d4674ee. * Implement GET endpoints for account_data in clientapi (#861) * Implement GET endpoints for account_data in clientapi * Fix accountDB parameter * Remove fmt.Println * Fix insertAccountData SQLite query * Fix accountDB storage interfaces * Add empty push rules into account data on account creation (#862) * Put SaveAccountData into the right function this time * Not sure if roomserver is better or worse now * sqlite work * Allow empty last sent ID for the first event * sqlite: room creation works * Support sending messages * Nuke fmt.println * Move QueryVariadic etc into common, other device fixes * Fix some linter issues * Fix bugs * Fix some linting errors * Fix errcheck lint errors * Make naffka use postgres as fallback, fix couple of compile errors * What on earth happened to the /rooms/{roomID}/send/{eventType} routing Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
193 lines
6.4 KiB
Go
193 lines
6.4 KiB
Go
// 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/common"
|
|
"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 (
|
|
event_id TEXT PRIMARY KEY,
|
|
topological_position BIGINT NOT NULL,
|
|
room_id TEXT NOT NULL,
|
|
|
|
UNIQUE(topological_position, room_id)
|
|
);
|
|
-- 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, txn *sql.Tx, event *gomatrixserverlib.Event,
|
|
) (err error) {
|
|
stmt := common.TxStmt(txn, s.insertEventInTopologyStmt)
|
|
_, err = stmt.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, txn *sql.Tx, 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 = common.TxStmt(txn, s.selectEventIDsInRangeASCStmt)
|
|
} else {
|
|
stmt = common.TxStmt(txn, 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, txn *sql.Tx, eventID string,
|
|
) (pos types.StreamPosition, err error) {
|
|
stmt := common.TxStmt(txn, s.selectPositionInTopologyStmt)
|
|
err = stmt.QueryRowContext(ctx, eventID).Scan(&pos)
|
|
return
|
|
}
|
|
|
|
func (s *outputRoomEventsTopologyStatements) selectMaxPositionInTopology(
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
) (pos types.StreamPosition, err error) {
|
|
stmt := common.TxStmt(txn, s.selectMaxPositionInTopologyStmt)
|
|
err = stmt.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, txn *sql.Tx, roomID string, pos types.StreamPosition,
|
|
) (eventIDs []string, err error) {
|
|
// Query the event IDs.
|
|
stmt := common.TxStmt(txn, s.selectEventIDsFromPositionStmt)
|
|
rows, err := stmt.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
|
|
}
|