mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Initial syncapi storage refactor to share pq/sqlite code (#1030)
* Initial syncapi storage refactor to share pq/sqlite code This goes down a different route than https://github.com/matrix-org/dendrite/pull/985 which tried to even reduce the boilerplate of `ExecContext` etc. The previous pattern fails badly when there are subtle differences in parameters and hence the shared boilerplate to read from `QueryContext` breaks. Rather than attacking it at that level, the main place where we want to reuse code is for the `syncserver.go` itself - the database implementation which has lots of complex logic. So instead, this commit: - Makes `invites_table.go` an interface. - Makes `SyncServerDatasource` use that interface - This means some functions are now identical for pq/sqlite, so factor them out to a temporary `shared.Database` struct which will grow until it replaces all of `SyncServerDatasource`. * Missing files
This commit is contained in:
parent
bdddd83753
commit
a25d477cdb
6 changed files with 127 additions and 65 deletions
|
@ -35,6 +35,7 @@ import (
|
|||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/eduserver/cache"
|
||||
"github.com/matrix-org/dendrite/syncapi/storage/shared"
|
||||
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
||||
"github.com/matrix-org/dendrite/syncapi/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
@ -58,10 +59,10 @@ type SyncServerDatasource struct {
|
|||
accountData accountDataStatements
|
||||
events outputRoomEventsStatements
|
||||
roomstate currentRoomStateStatements
|
||||
invites inviteEventsStatements
|
||||
eduCache *cache.EDUCache
|
||||
topology outputRoomEventsTopologyStatements
|
||||
backwardExtremities tables.BackwardsExtremities
|
||||
shared *shared.Database
|
||||
}
|
||||
|
||||
// NewSyncServerDatasource creates a new sync server database
|
||||
|
@ -106,7 +107,8 @@ func (d *SyncServerDatasource) prepare() (err error) {
|
|||
if err = d.roomstate.prepare(d.db, &d.streamID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = d.invites.prepare(d.db, &d.streamID); err != nil {
|
||||
invites, err := NewSqliteInvitesTable(d.db, &d.streamID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = d.topology.prepare(d.db); err != nil {
|
||||
|
@ -116,6 +118,10 @@ func (d *SyncServerDatasource) prepare() (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.shared = &shared.Database{
|
||||
DB: d.db,
|
||||
Invites: invites,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -404,7 +410,7 @@ func (d *SyncServerDatasource) syncStreamPositionTx(
|
|||
if maxAccountDataID > maxID {
|
||||
maxID = maxAccountDataID
|
||||
}
|
||||
maxInviteID, err := d.invites.selectMaxInviteID(ctx, txn)
|
||||
maxInviteID, err := d.shared.Invites.SelectMaxInviteID(ctx, txn)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -429,7 +435,7 @@ func (d *SyncServerDatasource) syncPositionTx(
|
|||
if maxAccountDataID > maxEventID {
|
||||
maxEventID = maxAccountDataID
|
||||
}
|
||||
maxInviteID, err := d.invites.selectMaxInviteID(ctx, txn)
|
||||
maxInviteID, err := d.shared.Invites.SelectMaxInviteID(ctx, txn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -756,15 +762,8 @@ func (d *SyncServerDatasource) UpsertAccountData(
|
|||
// Returns an error if there was a problem communicating with the database.
|
||||
func (d *SyncServerDatasource) AddInviteEvent(
|
||||
ctx context.Context, inviteEvent gomatrixserverlib.HeaderedEvent,
|
||||
) (streamPos types.StreamPosition, err error) {
|
||||
err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||
streamPos, err = d.streamID.nextStreamID(ctx, txn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.invites.insertInviteEvent(ctx, txn, inviteEvent, streamPos)
|
||||
})
|
||||
return
|
||||
) (sp types.StreamPosition, err error) {
|
||||
return d.shared.AddInviteEvent(ctx, inviteEvent)
|
||||
}
|
||||
|
||||
// RetireInviteEvent removes an old invite event from the database.
|
||||
|
@ -772,10 +771,7 @@ func (d *SyncServerDatasource) AddInviteEvent(
|
|||
func (d *SyncServerDatasource) RetireInviteEvent(
|
||||
ctx context.Context, inviteEventID string,
|
||||
) error {
|
||||
// TODO: Record that invite has been retired in a stream so that we can
|
||||
// notify the user in an incremental sync.
|
||||
err := d.invites.deleteInviteEvent(ctx, inviteEventID)
|
||||
return err
|
||||
return d.shared.RetireInviteEvent(ctx, inviteEventID)
|
||||
}
|
||||
|
||||
func (d *SyncServerDatasource) SetTypingTimeoutCallback(fn cache.TimeoutCallbackFn) {
|
||||
|
@ -804,7 +800,7 @@ func (d *SyncServerDatasource) addInvitesToResponse(
|
|||
fromPos, toPos types.StreamPosition,
|
||||
res *types.Response,
|
||||
) error {
|
||||
invites, err := d.invites.selectInviteEventsInRange(
|
||||
invites, err := d.shared.Invites.SelectInviteEventsInRange(
|
||||
ctx, txn, userID, fromPos, toPos,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue