2024-05-22 06:15:11 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package wstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-06-14 08:54:04 +02:00
|
|
|
"log"
|
2024-06-14 23:43:47 +02:00
|
|
|
"reflect"
|
2024-11-26 03:07:29 +01:00
|
|
|
"regexp"
|
2024-06-14 19:09:28 +02:00
|
|
|
"time"
|
2024-05-24 23:08:24 +02:00
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/filestore"
|
2024-11-21 03:05:13 +01:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/panichandler"
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/dbutil"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
2024-05-26 20:59:14 +02:00
|
|
|
)
|
2024-05-24 23:08:24 +02:00
|
|
|
|
2024-05-27 22:59:58 +02:00
|
|
|
var ErrNotFound = fmt.Errorf("not found")
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func waveObjTableName(w waveobj.WaveObj) string {
|
|
|
|
return "db_" + w.GetOType()
|
2024-05-24 23:08:24 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func tableNameFromOType(otype string) string {
|
|
|
|
return "db_" + otype
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func tableNameGen[T waveobj.WaveObj]() string {
|
|
|
|
var zeroObj T
|
2024-05-27 08:05:11 +02:00
|
|
|
return tableNameFromOType(zeroObj.GetOType())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOTypeGen[T waveobj.WaveObj]() string {
|
|
|
|
var zeroObj T
|
|
|
|
return zeroObj.GetOType()
|
2024-05-24 23:08:24 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func DBGetCount[T waveobj.WaveObj](ctx context.Context) (int, error) {
|
2024-05-22 06:15:11 +02:00
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (int, error) {
|
2024-05-26 20:59:14 +02:00
|
|
|
table := tableNameGen[T]()
|
2024-05-24 23:08:24 +02:00
|
|
|
query := fmt.Sprintf("SELECT count(*) FROM %s", table)
|
2024-05-22 06:15:11 +02:00
|
|
|
return tx.GetInt(query), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-26 03:07:29 +01:00
|
|
|
var viewRe = regexp.MustCompile(`^[a-z0-9]{1,20}$`)
|
|
|
|
|
|
|
|
func DBGetBlockViewCounts(ctx context.Context) (map[string]int, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (map[string]int, error) {
|
|
|
|
query := `SELECT COALESCE(json_extract(data, '$.meta.view'), '') AS view FROM db_block`
|
|
|
|
views := tx.SelectStrings(query)
|
|
|
|
rtn := make(map[string]int)
|
|
|
|
for _, view := range views {
|
|
|
|
if view == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !viewRe.MatchString(view) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rtn[view]++
|
|
|
|
}
|
|
|
|
return rtn, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
type idDataType struct {
|
|
|
|
OId string
|
|
|
|
Version int
|
|
|
|
Data []byte
|
2024-05-22 06:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func genericCastWithErr[T any](v any, err error) (T, error) {
|
|
|
|
if err != nil {
|
|
|
|
var zeroVal T
|
|
|
|
return zeroVal, err
|
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
if v == nil {
|
|
|
|
var zeroVal T
|
|
|
|
return zeroVal, nil
|
|
|
|
}
|
2024-05-27 08:05:11 +02:00
|
|
|
return v.(T), err
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func DBGetSingleton[T waveobj.WaveObj](ctx context.Context) (T, error) {
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn, err := DBGetSingletonByType(ctx, getOTypeGen[T]())
|
|
|
|
return genericCastWithErr[T](rtn, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBGetSingletonByType(ctx context.Context, otype string) (waveobj.WaveObj, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (waveobj.WaveObj, error) {
|
|
|
|
table := tableNameFromOType(otype)
|
2024-05-26 20:59:14 +02:00
|
|
|
query := fmt.Sprintf("SELECT oid, version, data FROM %s LIMIT 1", table)
|
|
|
|
var row idDataType
|
2024-06-20 04:10:53 +02:00
|
|
|
found := tx.Get(&row, query)
|
|
|
|
if !found {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn, err := waveobj.FromJson(row.Data)
|
2024-05-26 20:59:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return rtn, err
|
2024-05-24 23:08:24 +02:00
|
|
|
}
|
2024-05-26 20:59:14 +02:00
|
|
|
waveobj.SetVersion(rtn, row.Version)
|
|
|
|
return rtn, nil
|
2024-05-29 00:41:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBExistsORef(ctx context.Context, oref waveobj.ORef) (bool, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (bool, error) {
|
|
|
|
table := tableNameFromOType(oref.OType)
|
|
|
|
query := fmt.Sprintf("SELECT oid FROM %s WHERE oid = ?", table)
|
|
|
|
return tx.Exists(query, oref.OID), nil
|
2024-05-22 06:15:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func DBGet[T waveobj.WaveObj](ctx context.Context, id string) (T, error) {
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn, err := DBGetORef(ctx, waveobj.ORef{OType: getOTypeGen[T](), OID: id})
|
|
|
|
return genericCastWithErr[T](rtn, err)
|
|
|
|
}
|
|
|
|
|
2024-05-27 22:59:58 +02:00
|
|
|
func DBMustGet[T waveobj.WaveObj](ctx context.Context, id string) (T, error) {
|
|
|
|
rtn, err := DBGetORef(ctx, waveobj.ORef{OType: getOTypeGen[T](), OID: id})
|
|
|
|
if err != nil {
|
|
|
|
var zeroVal T
|
|
|
|
return zeroVal, err
|
|
|
|
}
|
|
|
|
if rtn == nil {
|
|
|
|
var zeroVal T
|
|
|
|
return zeroVal, ErrNotFound
|
|
|
|
}
|
|
|
|
return rtn.(T), nil
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func DBGetORef(ctx context.Context, oref waveobj.ORef) (waveobj.WaveObj, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (waveobj.WaveObj, error) {
|
|
|
|
table := tableNameFromOType(oref.OType)
|
2024-05-26 20:59:14 +02:00
|
|
|
query := fmt.Sprintf("SELECT oid, version, data FROM %s WHERE oid = ?", table)
|
|
|
|
var row idDataType
|
2024-06-03 22:43:50 +02:00
|
|
|
found := tx.Get(&row, query, oref.OID)
|
|
|
|
if !found {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn, err := waveobj.FromJson(row.Data)
|
2024-05-26 20:59:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return rtn, err
|
|
|
|
}
|
|
|
|
waveobj.SetVersion(rtn, row.Version)
|
|
|
|
return rtn, nil
|
|
|
|
})
|
2024-05-22 06:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func dbSelectOIDs(ctx context.Context, otype string, oids []string) ([]waveobj.WaveObj, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) ([]waveobj.WaveObj, error) {
|
|
|
|
table := tableNameFromOType(otype)
|
2024-05-26 20:59:14 +02:00
|
|
|
query := fmt.Sprintf("SELECT oid, version, data FROM %s WHERE oid IN (SELECT value FROM json_each(?))", table)
|
2024-05-27 08:05:11 +02:00
|
|
|
var rows []idDataType
|
2024-06-12 02:42:10 +02:00
|
|
|
tx.Select(&rows, query, dbutil.QuickJson(oids))
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn := make([]waveobj.WaveObj, 0, len(rows))
|
2024-05-24 23:08:24 +02:00
|
|
|
for _, row := range rows {
|
2024-05-27 08:05:11 +02:00
|
|
|
waveObj, err := waveobj.FromJson(row.Data)
|
2024-05-26 20:59:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-05-24 23:08:24 +02:00
|
|
|
}
|
2024-05-26 20:59:14 +02:00
|
|
|
waveobj.SetVersion(waveObj, row.Version)
|
2024-05-27 08:05:11 +02:00
|
|
|
rtn = append(rtn, waveObj)
|
|
|
|
}
|
|
|
|
return rtn, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBSelectORefs(ctx context.Context, orefs []waveobj.ORef) ([]waveobj.WaveObj, error) {
|
|
|
|
oidsByType := make(map[string][]string)
|
|
|
|
for _, oref := range orefs {
|
|
|
|
oidsByType[oref.OType] = append(oidsByType[oref.OType], oref.OID)
|
|
|
|
}
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) ([]waveobj.WaveObj, error) {
|
|
|
|
rtn := make([]waveobj.WaveObj, 0, len(orefs))
|
|
|
|
for otype, oids := range oidsByType {
|
|
|
|
rtnArr, err := dbSelectOIDs(tx.Context(), otype, oids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rtn = append(rtn, rtnArr...)
|
2024-05-22 06:15:11 +02:00
|
|
|
}
|
2024-05-27 08:05:11 +02:00
|
|
|
return rtn, nil
|
2024-05-22 06:15:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-14 23:43:47 +02:00
|
|
|
func DBResolveEasyOID(ctx context.Context, oid string) (*waveobj.ORef, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (*waveobj.ORef, error) {
|
2024-08-20 23:56:48 +02:00
|
|
|
for _, rtype := range waveobj.AllWaveObjTypes() {
|
2024-06-14 23:43:47 +02:00
|
|
|
otype := reflect.Zero(rtype).Interface().(waveobj.WaveObj).GetOType()
|
|
|
|
table := tableNameFromOType(otype)
|
|
|
|
var fullOID string
|
|
|
|
if len(oid) == 8 {
|
|
|
|
query := fmt.Sprintf("SELECT oid FROM %s WHERE oid LIKE ?", table)
|
|
|
|
fullOID = tx.GetString(query, oid+"%")
|
|
|
|
} else {
|
|
|
|
query := fmt.Sprintf("SELECT oid FROM %s WHERE oid = ?", table)
|
|
|
|
fullOID = tx.GetString(query, oid)
|
|
|
|
}
|
|
|
|
if fullOID != "" {
|
|
|
|
oref := waveobj.MakeORef(otype, fullOID)
|
|
|
|
return &oref, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrNotFound
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func DBSelectMap[T waveobj.WaveObj](ctx context.Context, ids []string) (map[string]T, error) {
|
|
|
|
rtnArr, err := dbSelectOIDs(ctx, getOTypeGen[T](), ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rtnMap := make(map[string]T)
|
|
|
|
for _, obj := range rtnArr {
|
|
|
|
rtnMap[waveobj.GetOID(obj)] = obj.(T)
|
|
|
|
}
|
|
|
|
return rtnMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBDelete(ctx context.Context, otype string, id string) error {
|
2024-06-14 08:54:04 +02:00
|
|
|
err := WithTx(ctx, func(tx *TxWrap) error {
|
2024-05-27 08:05:11 +02:00
|
|
|
table := tableNameFromOType(otype)
|
2024-05-26 20:59:14 +02:00
|
|
|
query := fmt.Sprintf("DELETE FROM %s WHERE oid = ?", table)
|
2024-05-24 23:08:24 +02:00
|
|
|
tx.Exec(query, id)
|
2024-08-20 23:56:48 +02:00
|
|
|
waveobj.ContextAddUpdate(ctx, waveobj.WaveObjUpdate{UpdateType: waveobj.UpdateType_Delete, OType: otype, OID: id})
|
2024-05-24 23:08:24 +02:00
|
|
|
return nil
|
2024-05-22 06:15:11 +02:00
|
|
|
})
|
2024-06-14 08:54:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-14 19:09:28 +02:00
|
|
|
go func() {
|
2024-11-21 03:05:13 +01:00
|
|
|
defer panichandler.PanicHandler("DBDelete:filestore.DeleteZone")
|
2024-06-14 19:09:28 +02:00
|
|
|
// we spawn a go routine here because we don't want to reuse the DB connection
|
|
|
|
// since DBDelete is called in a transaction from DeleteTab
|
|
|
|
deleteCtx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
err := filestore.WFS.DeleteZone(deleteCtx, id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error deleting filestore zone (after deleting block): %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-06-14 08:54:04 +02:00
|
|
|
return nil
|
2024-05-22 06:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 20:59:14 +02:00
|
|
|
func DBUpdate(ctx context.Context, val waveobj.WaveObj) error {
|
|
|
|
oid := waveobj.GetOID(val)
|
|
|
|
if oid == "" {
|
2024-05-24 23:08:24 +02:00
|
|
|
return fmt.Errorf("cannot update %T value with empty id", val)
|
|
|
|
}
|
2024-05-26 20:59:14 +02:00
|
|
|
jsonData, err := waveobj.ToJson(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-22 06:15:11 +02:00
|
|
|
return WithTx(ctx, func(tx *TxWrap) error {
|
2024-05-26 20:59:14 +02:00
|
|
|
table := waveObjTableName(val)
|
2024-05-27 22:59:58 +02:00
|
|
|
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)
|
2024-08-20 23:56:48 +02:00
|
|
|
waveobj.ContextAddUpdate(ctx, waveobj.WaveObjUpdate{UpdateType: waveobj.UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val})
|
2024-05-22 06:15:11 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:05:11 +02:00
|
|
|
func DBInsert(ctx context.Context, val waveobj.WaveObj) error {
|
2024-05-26 20:59:14 +02:00
|
|
|
oid := waveobj.GetOID(val)
|
|
|
|
if oid == "" {
|
2024-05-24 23:08:24 +02:00
|
|
|
return fmt.Errorf("cannot insert %T value with empty id", val)
|
2024-05-22 06:15:11 +02:00
|
|
|
}
|
2024-05-26 20:59:14 +02:00
|
|
|
jsonData, err := waveobj.ToJson(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-22 06:15:11 +02:00
|
|
|
return WithTx(ctx, func(tx *TxWrap) error {
|
2024-05-26 20:59:14 +02:00
|
|
|
table := waveObjTableName(val)
|
2024-05-27 22:59:58 +02:00
|
|
|
waveobj.SetVersion(val, 1)
|
2024-05-26 20:59:14 +02:00
|
|
|
query := fmt.Sprintf("INSERT INTO %s (oid, version, data) VALUES (?, ?, ?)", table)
|
|
|
|
tx.Exec(query, oid, 1, jsonData)
|
2024-08-20 23:56:48 +02:00
|
|
|
waveobj.ContextAddUpdate(ctx, waveobj.WaveObjUpdate{UpdateType: waveobj.UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val})
|
2024-05-22 06:15:11 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2024-06-25 04:04:08 +02:00
|
|
|
|
|
|
|
func DBFindWindowForTabId(ctx context.Context, tabId string) (string, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (string, error) {
|
|
|
|
query := "SELECT oid FROM db_window WHERE data->>'activetabid' = ?"
|
|
|
|
return tx.GetString(query, tabId), nil
|
|
|
|
})
|
|
|
|
}
|
2024-07-19 03:13:59 +02:00
|
|
|
|
|
|
|
func DBFindTabForBlockId(ctx context.Context, blockId string) (string, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (string, error) {
|
2024-10-25 22:45:00 +02:00
|
|
|
iterNum := 1
|
|
|
|
for {
|
|
|
|
if iterNum > 5 {
|
|
|
|
return "", fmt.Errorf("too many iterations looking for tab in block parents")
|
|
|
|
}
|
|
|
|
query := `
|
|
|
|
SELECT json_extract(b.data, '$.parentoref') AS parentoref
|
|
|
|
FROM db_block b
|
|
|
|
WHERE b.oid = ?;`
|
|
|
|
parentORef := tx.GetString(query, blockId)
|
|
|
|
oref, err := waveobj.ParseORef(parentORef)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("bad block parent oref: %v", err)
|
|
|
|
}
|
|
|
|
if oref.OType == "tab" {
|
|
|
|
return oref.OID, nil
|
|
|
|
}
|
|
|
|
if oref.OType == "block" {
|
|
|
|
blockId = oref.OID
|
|
|
|
iterNum++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("bad parent oref type: %v", oref.OType)
|
|
|
|
}
|
2024-07-19 03:13:59 +02:00
|
|
|
})
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
|
|
|
|
func DBFindWorkspaceForTabId(ctx context.Context, tabId string) (string, error) {
|
|
|
|
return WithTxRtn(ctx, func(tx *TxWrap) (string, error) {
|
|
|
|
query := `
|
|
|
|
SELECT w.oid
|
|
|
|
FROM db_workspace w, json_each(data->'tabids') je
|
|
|
|
WHERE je.value = ?`
|
|
|
|
return tx.GetString(query, tabId), nil
|
|
|
|
})
|
|
|
|
}
|