diff options
Diffstat (limited to 'internal/tree/engine.go')
-rw-r--r-- | internal/tree/engine.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/tree/engine.go b/internal/tree/engine.go new file mode 100644 index 0000000..0f28eb7 --- /dev/null +++ b/internal/tree/engine.go @@ -0,0 +1,42 @@ +package tree + +import ( + "context" + "sync" + + "go.neonxp.dev/djson/internal/storage" + "go.neonxp.dev/json/model" +) + +type Engine struct { + Root model.Node + mu sync.RWMutex + storage storage.Storage +} + +func New(storage storage.Storage) *Engine { + return &Engine{ + Root: model.Node{}, + mu: sync.RWMutex{}, + storage: storage, + } +} + +func (t *Engine) Run(ctx context.Context) error { + // Load initial mutations + for m := range t.storage.Load() { + if err := t.execute(&m); err != nil { + return err + } + } + + <-ctx.Done() + return nil +} + +func (t *Engine) Get(nodes []string) (*model.Node, error) { + if len(nodes) == 0 { + return &t.Root, nil + } + return t.Root.Query(nodes) +} |