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"
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01: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-05-27 10:36:09 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2023-02-07 14:31:23 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2021-04-26 14:25:57 +02:00
|
|
|
"github.com/matrix-org/util"
|
2020-02-13 18:27:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const stateSnapshotSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_state_snapshots (
|
2021-04-26 14:25:57 +02:00
|
|
|
-- The state snapshot NID that identifies this snapshot.
|
2020-02-13 18:27:33 +01:00
|
|
|
state_snapshot_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
2021-04-26 14:25:57 +02:00
|
|
|
-- The hash of the state snapshot, 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_snapshot_hash BLOB UNIQUE,
|
|
|
|
-- The room NID that the snapshot belongs to.
|
2020-02-13 18:27:33 +01:00
|
|
|
room_nid INTEGER NOT NULL,
|
2021-04-26 14:25:57 +02:00
|
|
|
-- The state blocks contained within this snapshot, encoded as JSON.
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
state_block_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 snapshot. 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 snapshot.
|
2020-02-13 18:27:33 +01:00
|
|
|
const insertStateSQL = `
|
2021-04-26 14:25:57 +02:00
|
|
|
INSERT INTO roomserver_state_snapshots (state_snapshot_hash, room_nid, state_block_nids)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT (state_snapshot_hash) DO UPDATE SET room_nid=$2
|
|
|
|
RETURNING state_snapshot_nid
|
|
|
|
`
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
// Bulk state data NID lookup.
|
|
|
|
// Sorting by state_snapshot_nid means we can use binary search over the result
|
|
|
|
// to lookup the state data NIDs for a state snapshot NID.
|
|
|
|
const bulkSelectStateBlockNIDsSQL = "" +
|
|
|
|
"SELECT state_snapshot_nid, state_block_nids FROM roomserver_state_snapshots" +
|
|
|
|
" WHERE state_snapshot_nid IN ($1) ORDER BY state_snapshot_nid ASC"
|
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
const selectStateBlockNIDsForRoomNID = "" +
|
|
|
|
"SELECT state_block_nids FROM roomserver_state_snapshots WHERE room_nid = $1"
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
type stateSnapshotStatements struct {
|
|
|
|
db *sql.DB
|
|
|
|
insertStateStmt *sql.Stmt
|
|
|
|
bulkSelectStateBlockNIDsStmt *sql.Stmt
|
2023-01-19 21:02:32 +01:00
|
|
|
selectStateBlockNIDsStmt *sql.Stmt
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:33:16 +02:00
|
|
|
func CreateStateSnapshotTable(db *sql.DB) error {
|
2021-04-26 14:25:57 +02:00
|
|
|
_, err := db.Exec(stateSnapshotSchema)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
func PrepareStateSnapshotTable(db *sql.DB) (*stateSnapshotStatements, error) {
|
2020-07-21 11:48:49 +02:00
|
|
|
s := &stateSnapshotStatements{
|
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.insertStateStmt, insertStateSQL},
|
|
|
|
{&s.bulkSelectStateBlockNIDsStmt, bulkSelectStateBlockNIDsSQL},
|
2023-01-19 21:02:32 +01:00
|
|
|
{&s.selectStateBlockNIDsStmt, selectStateBlockNIDsForRoomNID},
|
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 *stateSnapshotStatements) InsertState(
|
2021-04-26 14:25:57 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, stateBlockNIDs types.StateBlockNIDs,
|
2020-02-13 18:27:33 +01:00
|
|
|
) (stateNID types.StateSnapshotNID, err error) {
|
2021-08-04 18:08:17 +02:00
|
|
|
if stateBlockNIDs == nil {
|
|
|
|
stateBlockNIDs = []types.StateBlockNID{} // zero slice to not store 'null' in the DB
|
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
stateBlockNIDs = stateBlockNIDs[:util.SortAndUnique(stateBlockNIDs)]
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
stateBlockNIDsJSON, err := json.Marshal(stateBlockNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2020-09-24 12:10:14 +02:00
|
|
|
insertStmt := sqlutil.TxStmt(txn, s.insertStateStmt)
|
2022-05-16 19:33:16 +02:00
|
|
|
err = insertStmt.QueryRowContext(ctx, stateBlockNIDs.Hash(), int64(roomNID), string(stateBlockNIDsJSON)).Scan(&stateNID)
|
2020-08-19 16:38:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-02-13 18:27:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (s *stateSnapshotStatements) BulkSelectStateBlockNIDs(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, stateNIDs []types.StateSnapshotNID,
|
2020-02-13 18:27:33 +01:00
|
|
|
) ([]types.StateBlockNIDList, error) {
|
|
|
|
nids := make([]interface{}, len(stateNIDs))
|
|
|
|
for k, v := range stateNIDs {
|
|
|
|
nids[k] = v
|
|
|
|
}
|
2020-06-12 15:55:57 +02:00
|
|
|
selectOrig := strings.Replace(bulkSelectStateBlockNIDsSQL, "($1)", sqlutil.QueryVariadic(len(nids)), 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)
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
rows, err := selectStmt.QueryContext(ctx, nids...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateBlockNIDs: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
results := make([]types.StateBlockNIDList, len(stateNIDs))
|
|
|
|
i := 0
|
2022-05-16 19:33:16 +02:00
|
|
|
var stateBlockNIDsJSON string
|
2020-02-13 18:27:33 +01:00
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
if err := rows.Scan(&result.StateSnapshotNID, &stateBlockNIDsJSON); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
if err := json.Unmarshal([]byte(stateBlockNIDsJSON), &result.StateBlockNIDs); err != nil {
|
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i != len(stateNIDs) {
|
2022-02-21 17:22:29 +01:00
|
|
|
return nil, types.MissingStateError(fmt.Sprintf("storage: state NIDs missing from the database (%d != %d)", i, len(stateNIDs)))
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
2022-08-01 15:11:00 +02:00
|
|
|
|
|
|
|
func (s *stateSnapshotStatements) BulkSelectStateForHistoryVisibility(
|
|
|
|
ctx context.Context, txn *sql.Tx, stateSnapshotNID types.StateSnapshotNID, domain string,
|
|
|
|
) ([]types.EventNID, error) {
|
|
|
|
return nil, tables.OptimisationNotSupportedError
|
|
|
|
}
|
2023-01-19 21:02:32 +01:00
|
|
|
|
2023-02-07 14:31:23 +01:00
|
|
|
func (s *stateSnapshotStatements) BulkSelectMembershipForHistoryVisibility(ctx context.Context, txn *sql.Tx, userNID types.EventStateKeyNID, roomInfo *types.RoomInfo, eventIDs ...string) (map[string]*gomatrixserverlib.HeaderedEvent, error) {
|
|
|
|
return nil, tables.OptimisationNotSupportedError
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
func (s *stateSnapshotStatements) selectStateBlockNIDsForRoomNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
|
|
|
) ([]types.StateBlockNID, error) {
|
|
|
|
var res []types.StateBlockNID
|
|
|
|
rows, err := sqlutil.TxStmt(txn, s.selectStateBlockNIDsStmt).QueryContext(ctx, roomNID)
|
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectStateBlockNIDsForRoomNID: rows.close() failed")
|
|
|
|
|
|
|
|
var stateBlockNIDs []types.StateBlockNID
|
|
|
|
var stateBlockNIDsJSON string
|
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Scan(&stateBlockNIDsJSON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(stateBlockNIDsJSON), &stateBlockNIDs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, stateBlockNIDs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, rows.Err()
|
|
|
|
}
|