2020-01-03 08:07:05 -06:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 17:40:52 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-01-03 08:07:05 -06:00
|
|
|
package postgres
|
2017-03-29 08:05:43 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-12-15 09:42:55 -06:00
|
|
|
|
2020-04-16 04:06:55 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2019-06-19 08:05:03 -05:00
|
|
|
|
2017-03-29 08:05:43 -05:00
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
2017-03-30 09:29:23 -05:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2020-03-30 09:02:20 -05:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/cache"
|
2020-05-13 11:28:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/shared"
|
2017-03-29 08:05:43 -05:00
|
|
|
)
|
|
|
|
|
2020-01-23 11:51:10 -06:00
|
|
|
// SyncServerDatasource represents a sync server datasource which manages
|
2019-07-12 09:59:53 -05:00
|
|
|
// both the database for PDUs and caches for EDUs.
|
|
|
|
type SyncServerDatasource struct {
|
2020-05-14 03:53:55 -05:00
|
|
|
shared.Database
|
2017-09-21 09:09:19 -05:00
|
|
|
db *sql.DB
|
|
|
|
common.PartitionOffsetStatements
|
2017-03-29 08:05:43 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:11:37 -05:00
|
|
|
// NewDatabase creates a new sync server database
|
|
|
|
func NewDatabase(dbDataSourceName string, dbProperties common.DbProperties) (*SyncServerDatasource, error) {
|
2019-07-12 09:59:53 -05:00
|
|
|
var d SyncServerDatasource
|
2017-03-29 08:05:43 -05:00
|
|
|
var err error
|
2020-05-01 07:34:53 -05:00
|
|
|
if d.db, err = sqlutil.Open("postgres", dbDataSourceName, dbProperties); err != nil {
|
2017-03-29 08:05:43 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-21 09:09:19 -05:00
|
|
|
if err = d.PartitionOffsetStatements.Prepare(d.db, "syncapi"); err != nil {
|
2017-03-29 08:05:43 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 03:53:55 -05:00
|
|
|
accountData, err := NewPostgresAccountDataTable(d.db)
|
|
|
|
if err != nil {
|
2017-08-02 10:21:35 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 03:53:55 -05:00
|
|
|
events, err := NewPostgresEventsTable(d.db)
|
|
|
|
if err != nil {
|
2017-03-30 09:29:23 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:11:37 -05:00
|
|
|
currState, err := NewPostgresCurrentRoomStateTable(d.db)
|
|
|
|
if err != nil {
|
2017-04-05 04:30:13 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 11:28:42 -05:00
|
|
|
invites, err := NewPostgresInvitesTable(d.db)
|
|
|
|
if err != nil {
|
2017-09-20 09:36:41 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:11:37 -05:00
|
|
|
topology, err := NewPostgresTopologyTable(d.db)
|
|
|
|
if err != nil {
|
2020-01-23 11:51:10 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:11:37 -05:00
|
|
|
backwardExtremities, err := NewPostgresBackwardsExtremitiesTable(d.db)
|
2020-04-28 09:50:24 -05:00
|
|
|
if err != nil {
|
2020-01-23 11:51:10 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 03:53:55 -05:00
|
|
|
d.Database = shared.Database{
|
2020-05-14 10:11:37 -05:00
|
|
|
DB: d.db,
|
|
|
|
Invites: invites,
|
|
|
|
AccountData: accountData,
|
|
|
|
OutputEvents: events,
|
|
|
|
Topology: topology,
|
|
|
|
CurrentRoomState: currState,
|
|
|
|
BackwardExtremities: backwardExtremities,
|
|
|
|
EDUCache: cache.New(),
|
2020-05-13 11:28:42 -05:00
|
|
|
}
|
2017-09-20 09:36:41 -05:00
|
|
|
return &d, nil
|
2017-03-30 09:29:23 -05:00
|
|
|
}
|