2020-02-13 17:27:33 +00: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 (
|
2022-08-11 16:23:35 +00:00
|
|
|
"context"
|
2020-02-13 17:27:33 +00:00
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-05-03 15:35:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-05-13 16:28:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/shared"
|
2022-08-11 16:23:35 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/sqlite3/deltas"
|
2020-02-13 17:27:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SyncServerDatasource represents a sync server datasource which manages
|
|
|
|
// both the database for PDUs and caches for EDUs.
|
|
|
|
type SyncServerDatasource struct {
|
2020-05-14 08:53:55 +00:00
|
|
|
shared.Database
|
2022-03-21 09:47:41 +00:00
|
|
|
db *sql.DB
|
|
|
|
writer sqlutil.Writer
|
2022-04-08 16:53:24 +00:00
|
|
|
streamID StreamIDStatements
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:11:37 +00:00
|
|
|
// NewDatabase creates a new sync server database
|
2020-02-13 17:27:33 +00:00
|
|
|
// nolint: gocyclo
|
2022-05-03 15:35:06 +00:00
|
|
|
func NewDatabase(base *base.BaseDendrite, dbProperties *config.DatabaseOptions) (*SyncServerDatasource, error) {
|
2020-02-13 17:27:33 +00:00
|
|
|
var d SyncServerDatasource
|
2020-08-10 13:18:04 +00:00
|
|
|
var err error
|
2022-05-03 15:35:06 +00:00
|
|
|
if d.db, d.writer, err = base.DatabaseConnection(dbProperties, sqlutil.NewExclusiveWriter()); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-11 16:23:35 +00:00
|
|
|
if err = d.prepare(base.Context()); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
func (d *SyncServerDatasource) NewDatabaseSnapshot(ctx context.Context) (*shared.DatabaseTransaction, error) {
|
|
|
|
return &shared.DatabaseTransaction{
|
|
|
|
Database: &d.Database,
|
|
|
|
// not setting a transaction because SQLite doesn't support it
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SyncServerDatasource) NewDatabaseTransaction(ctx context.Context) (*shared.DatabaseTransaction, error) {
|
|
|
|
return &shared.DatabaseTransaction{
|
|
|
|
Database: &d.Database,
|
|
|
|
// not setting a transaction because SQLite doesn't support it
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:23:35 +00:00
|
|
|
func (d *SyncServerDatasource) prepare(ctx context.Context) (err error) {
|
2022-04-08 16:53:24 +00:00
|
|
|
if err = d.streamID.Prepare(d.db); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 09:39:22 +00:00
|
|
|
accountData, err := NewSqliteAccountDataTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 09:39:22 +00:00
|
|
|
events, err := NewSqliteEventsTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 09:39:22 +00:00
|
|
|
roomState, err := NewSqliteCurrentRoomStateTable(d.db, &d.streamID)
|
2020-05-14 15:11:37 +00:00
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-13 16:28:42 +00:00
|
|
|
invites, err := NewSqliteInvitesTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-09-10 13:39:18 +00:00
|
|
|
peeks, err := NewSqlitePeeksTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-14 15:11:37 +00:00
|
|
|
topology, err := NewSqliteTopologyTable(d.db)
|
2020-04-28 14:50:24 +00:00
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-14 15:11:37 +00:00
|
|
|
bwExtrem, err := NewSqliteBackwardsExtremitiesTable(d.db)
|
2020-05-13 11:14:50 +00:00
|
|
|
if err != nil {
|
2020-02-20 09:28:03 +00:00
|
|
|
return err
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 16:50:19 +00:00
|
|
|
sendToDevice, err := NewSqliteSendToDeviceTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-26 14:34:41 +00:00
|
|
|
filter, err := NewSqliteFilterTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
receipts, err := NewSqliteReceiptsTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-20 20:43:20 +00:00
|
|
|
memberships, err := NewSqliteMembershipsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-05 11:44:20 +00:00
|
|
|
notificationData, err := NewSqliteNotificationDataTable(d.db, &d.streamID)
|
2022-03-03 11:40:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-07 14:08:19 +00:00
|
|
|
ignores, err := NewSqliteIgnoresTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-06 11:11:19 +00:00
|
|
|
presence, err := NewSqlitePresenceTable(d.db, &d.streamID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-11 16:23:35 +00:00
|
|
|
|
|
|
|
// apply migrations which need multiple tables
|
|
|
|
m := sqlutil.NewMigrator(d.db)
|
|
|
|
m.AddMigrations(
|
|
|
|
sqlutil.Migration{
|
|
|
|
Version: "syncapi: set history visibility for existing events",
|
|
|
|
Up: deltas.UpSetHistoryVisibility, // Requires current_room_state and output_room_events to be created.
|
|
|
|
},
|
|
|
|
)
|
|
|
|
err = m.Up(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-14 15:11:37 +00:00
|
|
|
d.Database = shared.Database{
|
|
|
|
DB: d.db,
|
2020-09-01 15:58:21 +00:00
|
|
|
Writer: d.writer,
|
2020-05-14 15:11:37 +00:00
|
|
|
Invites: invites,
|
2020-09-10 13:39:18 +00:00
|
|
|
Peeks: peeks,
|
2020-05-14 15:11:37 +00:00
|
|
|
AccountData: accountData,
|
|
|
|
OutputEvents: events,
|
|
|
|
BackwardExtremities: bwExtrem,
|
|
|
|
CurrentRoomState: roomState,
|
|
|
|
Topology: topology,
|
2020-06-26 14:34:41 +00:00
|
|
|
Filter: filter,
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 16:50:19 +00:00
|
|
|
SendToDevice: sendToDevice,
|
2020-11-09 18:46:11 +00:00
|
|
|
Receipts: receipts,
|
2021-01-20 20:43:20 +00:00
|
|
|
Memberships: memberships,
|
2022-03-03 11:40:53 +00:00
|
|
|
NotificationData: notificationData,
|
2022-04-07 14:08:19 +00:00
|
|
|
Ignores: ignores,
|
2022-04-06 11:11:19 +00:00
|
|
|
Presence: presence,
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|