mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 13:22:46 +00:00
State storage refactor (#1839)
* Hash-deduplicated state storage (and migrations) for PostgreSQL and SQLite * Refactor droomserver database setup for migrations * Fix conflict statements * Update migration names * Set a boundary for old to new block/snapshot IDs so we don't rewrite them more than once accidentally * Create sequence if not exists * Fix boundary queries * Fix boundary queries * Use Query * Break out queries a bit * More sequence tweaks * Query parameters are not playing the game * Injection escaping may not work for CREATE SEQUENCE after all * Fix snapshot sequence name * Use boundaried IDs in SQLite too * Use IFNULL for SQLite * Use COALESCE in PostgreSQL * Review comments @Kegsay
This commit is contained in:
parent
d6e9b7b307
commit
5ce1fe80de
39 changed files with 1076 additions and 554 deletions
|
@ -16,9 +16,11 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
// EventTypeNID is a numeric ID for an event type.
|
||||
|
@ -40,6 +42,38 @@ type StateSnapshotNID int64
|
|||
// These blocks of state data are combined to form the actual state.
|
||||
type StateBlockNID int64
|
||||
|
||||
// EventNIDs is used to sort and dedupe event NIDs.
|
||||
type EventNIDs []EventNID
|
||||
|
||||
func (a EventNIDs) Len() int { return len(a) }
|
||||
func (a EventNIDs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a EventNIDs) Less(i, j int) bool { return a[i] < a[j] }
|
||||
|
||||
func (a EventNIDs) Hash() []byte {
|
||||
j, err := json.Marshal(a)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
h := blake2b.Sum256(j)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// StateBlockNIDs is used to sort and dedupe state block NIDs.
|
||||
type StateBlockNIDs []StateBlockNID
|
||||
|
||||
func (a StateBlockNIDs) Len() int { return len(a) }
|
||||
func (a StateBlockNIDs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a StateBlockNIDs) Less(i, j int) bool { return a[i] < a[j] }
|
||||
|
||||
func (a StateBlockNIDs) Hash() []byte {
|
||||
j, err := json.Marshal(a)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
h := blake2b.Sum256(j)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// A StateKeyTuple is a pair of a numeric event type and a numeric state key.
|
||||
// It is used to lookup state entries.
|
||||
type StateKeyTuple struct {
|
||||
|
@ -65,6 +99,12 @@ type StateEntry struct {
|
|||
EventNID EventNID
|
||||
}
|
||||
|
||||
type StateEntries []StateEntry
|
||||
|
||||
func (a StateEntries) Len() int { return len(a) }
|
||||
func (a StateEntries) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a StateEntries) Less(i, j int) bool { return a[i].EventNID < a[j].EventNID }
|
||||
|
||||
// LessThan returns true if this state entry is less than the other state entry.
|
||||
// The ordering is arbitrary and is used to implement binary search and to efficiently deduplicate entries.
|
||||
func (a StateEntry) LessThan(b StateEntry) bool {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue