Only update required entries if we already got some

This commit is contained in:
Till Faelligen 2023-08-03 17:38:34 +02:00
parent ca1ac770f0
commit 9359bd5497
No known key found for this signature in database
GPG Key ID: ACCDC9606D472758
3 changed files with 36 additions and 3 deletions

View File

@ -23,7 +23,7 @@ func isBitSet(n int, bit int) bool {
type DeviceData struct {
// Contains the latest device_one_time_keys_count values.
// Set whenever this field arrives down the v2 poller, and it replaces what was previously there.
OTKCounts map[string]int `json:"otk"`
OTKCounts MapStringInt `json:"otk"`
// Contains the latest device_unused_fallback_key_types value
// Set whenever this field arrives down the v2 poller, and it replaces what was previously there.
FallbackKeyTypes []string `json:"fallback"`

View File

@ -1,5 +1,10 @@
package internal
import (
"database/sql/driver"
"encoding/json"
)
const (
DeviceListChanged = 1
DeviceListLeft = 2
@ -7,8 +12,19 @@ const (
type DeviceLists struct {
// map user_id -> DeviceList enum
New map[string]int `json:"n"`
Sent map[string]int `json:"s"`
New MapStringInt `json:"n"`
Sent MapStringInt `json:"s"`
}
type MapStringInt map[string]int
// Value implements driver.Valuer
func (dl MapStringInt) Value() (driver.Value, error) {
if len(dl) == 0 {
return "{}", nil
}
v, err := json.Marshal(dl)
return v, err
}
func (dl DeviceLists) Combine(newer DeviceLists) DeviceLists {

View File

@ -6,6 +6,7 @@ import (
"reflect"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/matrix-org/sliding-sync/internal"
"github.com/matrix-org/sliding-sync/sqlutil"
)
@ -119,6 +120,22 @@ func (t *DeviceDataTable) Upsert(dd *internal.DeviceData) (err error) {
}
tempDD.DeviceLists = tempDD.DeviceLists.Combine(dd.DeviceLists)
// we already got something in the database - update by just sending the new data
if len(row.Data) > 0 {
if tempDD.FallbackKeyTypes == nil {
// If fallback is null, it would, for some reason, nuke the whole
// column, so make sure we have something set.
tempDD.FallbackKeyTypes = []string{}
}
_, err = txn.Exec(`UPDATE syncv3_device_data SET data =
jsonb_set(jsonb_set(jsonb_set(jsonb_set(jsonb_set(data, '{otk}', $3, false), '{fallback}', to_jsonb($4::text[]), false), '{c}', $5, false), '{dl,s}', $6, false),'{dl,n}', $7, false)
WHERE user_id = $1 AND device_id = $2
`,
dd.UserID, dd.DeviceID, tempDD.OTKCounts, pq.StringArray(tempDD.FallbackKeyTypes), tempDD.ChangedBits, tempDD.DeviceLists.Sent, tempDD.DeviceLists.New,
)
return err
}
data, err := json.Marshal(tempDD)
if err != nil {
return err