setactivetab working, removed tombstones, created updatetype

This commit is contained in:
sawka
2024-05-27 14:31:12 -07:00
parent b87786febf
commit abedca2364
7 changed files with 89 additions and 115 deletions
+42 -11
View File
@@ -6,6 +6,7 @@ package wstore
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"reflect"
@@ -25,7 +26,7 @@ func init() {
}
type contextUpdatesType struct {
UpdatesStack []map[waveobj.ORef]waveobj.WaveObj
UpdatesStack []map[waveobj.ORef]WaveObjUpdate
}
func dumpUpdateStack(updates *contextUpdatesType) {
@@ -47,11 +48,11 @@ func ContextWithUpdates(ctx context.Context) context.Context {
return ctx
}
return context.WithValue(ctx, waveObjUpdateKey, &contextUpdatesType{
UpdatesStack: []map[waveobj.ORef]waveobj.WaveObj{make(map[waveobj.ORef]waveobj.WaveObj)},
UpdatesStack: []map[waveobj.ORef]WaveObjUpdate{make(map[waveobj.ORef]WaveObjUpdate)},
})
}
func ContextGetUpdates(ctx context.Context) map[waveobj.ORef]waveobj.WaveObj {
func ContextGetUpdates(ctx context.Context) map[waveobj.ORef]WaveObjUpdate {
updatesVal := ctx.Value(waveObjUpdateKey)
if updatesVal == nil {
return nil
@@ -60,7 +61,7 @@ func ContextGetUpdates(ctx context.Context) map[waveobj.ORef]waveobj.WaveObj {
if len(updates.UpdatesStack) == 1 {
return updates.UpdatesStack[0]
}
rtn := make(map[waveobj.ORef]waveobj.WaveObj)
rtn := make(map[waveobj.ORef]WaveObjUpdate)
for _, update := range updates.UpdatesStack {
for k, v := range update {
rtn[k] = v
@@ -69,7 +70,7 @@ func ContextGetUpdates(ctx context.Context) map[waveobj.ORef]waveobj.WaveObj {
return rtn
}
func ContextGetUpdate(ctx context.Context, oref waveobj.ORef) waveobj.WaveObj {
func ContextGetUpdate(ctx context.Context, oref waveobj.ORef) *WaveObjUpdate {
updatesVal := ctx.Value(waveObjUpdateKey)
if updatesVal == nil {
return nil
@@ -77,23 +78,23 @@ func ContextGetUpdate(ctx context.Context, oref waveobj.ORef) waveobj.WaveObj {
updates := updatesVal.(*contextUpdatesType)
for idx := len(updates.UpdatesStack) - 1; idx >= 0; idx-- {
if obj, ok := updates.UpdatesStack[idx][oref]; ok {
return obj
return &obj
}
}
return nil
}
func ContextAddUpdate(ctx context.Context, obj waveobj.WaveObj) {
func ContextAddUpdate(ctx context.Context, update WaveObjUpdate) {
updatesVal := ctx.Value(waveObjUpdateKey)
if updatesVal == nil {
return
}
updates := updatesVal.(*contextUpdatesType)
oref := waveobj.ORef{
OType: obj.GetOType(),
OID: waveobj.GetOID(obj),
OType: update.OType,
OID: update.OID,
}
updates.UpdatesStack[len(updates.UpdatesStack)-1][oref] = obj
updates.UpdatesStack[len(updates.UpdatesStack)-1][oref] = update
}
func ContextUpdatesBeginTx(ctx context.Context) context.Context {
@@ -102,7 +103,7 @@ func ContextUpdatesBeginTx(ctx context.Context) context.Context {
return ctx
}
updates := updatesVal.(*contextUpdatesType)
updates.UpdatesStack = append(updates.UpdatesStack, make(map[waveobj.ORef]waveobj.WaveObj))
updates.UpdatesStack = append(updates.UpdatesStack, make(map[waveobj.ORef]WaveObjUpdate))
return ctx
}
@@ -136,6 +137,36 @@ func ContextUpdatesRollbackTx(ctx context.Context) {
updates.UpdatesStack = updates.UpdatesStack[:len(updates.UpdatesStack)-1]
}
type WaveObjTombstone struct {
OType string `json:"otype"`
OID string `json:"oid"`
}
const (
UpdateType_Update = "update"
UpdateType_Delete = "delete"
)
type WaveObjUpdate struct {
UpdateType string `json:"updatetype"`
OType string `json:"otype"`
OID string `json:"oid"`
Obj waveobj.WaveObj `json:"obj,omitempty"`
}
func (update WaveObjUpdate) MarshalJSON() ([]byte, error) {
rtn := make(map[string]any)
rtn["updatetype"] = update.UpdateType
rtn["otype"] = update.OType
rtn["oid"] = update.OID
var err error
rtn["obj"], err = waveobj.ToJsonMap(update.Obj)
if err != nil {
return nil, err
}
return json.Marshal(rtn)
}
type UIContext struct {
WindowId string `json:"windowid"`
}
+3 -9
View File
@@ -159,15 +159,12 @@ func DBDelete(ctx context.Context, otype string, id string) error {
table := tableNameFromOType(otype)
query := fmt.Sprintf("DELETE FROM %s WHERE oid = ?", table)
tx.Exec(query, id)
ContextAddUpdate(ctx, &waveobj.WaveObjTombstone{OType: otype, OID: id})
ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Delete, OType: otype, OID: id})
return nil
})
}
func DBUpdate(ctx context.Context, val waveobj.WaveObj) error {
if waveobj.IsTombstone(val) {
return fmt.Errorf("cannot update deleted object")
}
oid := waveobj.GetOID(val)
if oid == "" {
return fmt.Errorf("cannot update %T value with empty id", val)
@@ -181,15 +178,12 @@ func DBUpdate(ctx context.Context, val waveobj.WaveObj) error {
query := fmt.Sprintf("UPDATE %s SET data = ?, version = version+1 WHERE oid = ? RETURNING version", table)
newVersion := tx.GetInt(query, jsonData, oid)
waveobj.SetVersion(val, newVersion)
ContextAddUpdate(ctx, val)
ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val})
return nil
})
}
func DBInsert(ctx context.Context, val waveobj.WaveObj) error {
if waveobj.IsTombstone(val) {
return fmt.Errorf("cannot insert deleted object")
}
oid := waveobj.GetOID(val)
if oid == "" {
return fmt.Errorf("cannot insert %T value with empty id", val)
@@ -203,7 +197,7 @@ func DBInsert(ctx context.Context, val waveobj.WaveObj) error {
waveobj.SetVersion(val, 1)
query := fmt.Sprintf("INSERT INTO %s (oid, version, data) VALUES (?, ?, ?)", table)
tx.Exec(query, oid, 1, jsonData)
ContextAddUpdate(ctx, val)
ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val})
return nil
})
}