summaryrefslogtreecommitdiff
path: root/internal/tree
diff options
context:
space:
mode:
Diffstat (limited to 'internal/tree')
-rw-r--r--internal/tree/contract.go23
-rw-r--r--internal/tree/core.go50
-rw-r--r--internal/tree/mutations.go89
3 files changed, 0 insertions, 162 deletions
diff --git a/internal/tree/contract.go b/internal/tree/contract.go
deleted file mode 100644
index 29748cb..0000000
--- a/internal/tree/contract.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package tree
-
-import (
- "context"
-
- dmodel "go.neonxp.dev/djson/internal/model"
- "go.neonxp.dev/json/model"
-)
-
-type Core interface {
- Init() error
- Get(nodes []string) (model.Node, error)
- Mutate(ctx context.Context, mut *dmodel.Mutation) error
- State() CoreState
-}
-
-type CoreState int
-
-const (
- Running CoreState = iota
- Ready
- Failed
-)
diff --git a/internal/tree/core.go b/internal/tree/core.go
deleted file mode 100644
index d2cace3..0000000
--- a/internal/tree/core.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package tree
-
-import (
- "sync"
-
- "go.neonxp.dev/djson/internal/events"
- "go.neonxp.dev/djson/internal/storage"
- "go.neonxp.dev/json/model"
-)
-
-type stdCore struct {
- Root model.ObjectNode
- state CoreState
- mu sync.RWMutex
- storage storage.Storage
- eventDispatcher events.Dispatcher
-}
-
-func New(storage storage.Storage, eventsDispatcher events.Dispatcher) Core {
- return &stdCore{
- Root: model.ObjectNode{},
- state: Running,
- mu: sync.RWMutex{},
- storage: storage,
- eventDispatcher: eventsDispatcher,
- }
-}
-
-func (t *stdCore) Init() error {
- // Load initial mutations
- for m := range t.storage.Load() {
- if err := t.execute(&m); err != nil {
- t.state = Failed
- return err
- }
- }
- t.state = Ready
- return nil
-}
-
-func (t *stdCore) Get(nodes []string) (model.Node, error) {
- if len(nodes) == 0 {
- return &t.Root, nil
- }
- return model.Query(&t.Root, nodes)
-}
-
-func (t *stdCore) State() CoreState {
- return t.state
-}
diff --git a/internal/tree/mutations.go b/internal/tree/mutations.go
deleted file mode 100644
index 173828d..0000000
--- a/internal/tree/mutations.go
+++ /dev/null
@@ -1,89 +0,0 @@
-package tree
-
-import (
- "context"
- "fmt"
- "strings"
-
- "go.neonxp.dev/djson/internal/model"
- json "go.neonxp.dev/json/model"
-)
-
-func (t *stdCore) Mutate(ctx context.Context, mut *model.Mutation) error {
- t.mu.Lock()
- defer t.mu.Unlock()
- if err := t.execute(mut); err != nil {
- return err
- }
- return t.storage.Commit(ctx, *mut)
-}
-
-func (t *stdCore) execute(mut *model.Mutation) error {
- switch mut.Type {
- case model.Create:
- if len(mut.Path) == 0 {
- // create root node
- inObject, ok := mut.Body.(*json.ObjectNode)
- if !ok {
- return fmt.Errorf("root node must be object")
- }
- t.Root = *inObject
- return nil
- }
- key := mut.Path[len(mut.Path)-1]
- path := mut.Path[:len(mut.Path)-1]
- target, err := json.Query(&t.Root, path)
- if err != nil {
- return err
- }
- targetObject, ok := target.(*json.ObjectNode)
- if !ok {
- return fmt.Errorf("node %s is not object", strings.Join(path, "/"))
- }
- if err := targetObject.Value.Set(key, mut.Body); err != nil {
- return err
- }
- case model.Merge:
- inObject, ok := mut.Body.(*json.ObjectNode)
- if !ok {
- return fmt.Errorf("patch allowed only for objects")
- }
- if len(mut.Path) == 0 {
- // patch root node
- t.Root.Merge(inObject)
- return nil
- }
- target, err := json.Query(&t.Root, mut.Path)
- if err != nil {
- return err
- }
- targetObject, ok := target.(*json.ObjectNode)
- if !ok {
- return fmt.Errorf("patch allowed only for objects")
- }
- targetObject.Merge(inObject)
- case model.Remove:
- if len(mut.Path) == 0 {
- return fmt.Errorf("can't remove root node. Only replace or create avaliable")
- }
- key := mut.Path[len(mut.Path)-1]
- if len(mut.Path) == 1 {
- t.Root.Remove(key)
- return nil
- }
- path := mut.Path[:len(mut.Path)-1]
- target, err := json.Query(&t.Root, path)
- if err != nil {
- return err
- }
- targetObject, ok := target.(*json.ObjectNode)
- if !ok {
- return fmt.Errorf("remove allowed only from objects")
- }
- targetObject.Remove(key)
- default:
- return fmt.Errorf("invalid command type: %d", mut.Type)
- }
- t.eventDispatcher.Notify(mut.Path, mut)
- return nil
-}