summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2023-01-04 18:44:58 +0300
committerNeonXP <i@neonxp.dev>2023-01-04 18:44:58 +0300
commit8716ac3e650075525cab7fb5caf1aa62b3efe55b (patch)
treef34dcb33400ef6bfd7f01b55a04f59784505c506 /internal
parente91712e388c530dd5bdfb46f028157a62a60b1e3 (diff)
rewriteHEADmaster
Diffstat (limited to 'internal')
-rw-r--r--internal/command/commandtype_string.go (renamed from internal/model/commandtype_string.go)2
-rw-r--r--internal/command/mutations.go21
-rw-r--r--internal/command/objectid.go47
-rw-r--r--internal/config/config.go30
-rw-r--r--internal/core/core.go203
-rw-r--r--internal/events/contract.go6
-rw-r--r--internal/events/events.go8
-rw-r--r--internal/events/node.go12
-rw-r--r--internal/handler/auth.go15
-rw-r--r--internal/handler/contract.go12
-rw-r--r--internal/handler/crud.go88
-rw-r--r--internal/handler/events.go16
-rw-r--r--internal/handler/handler.go11
-rw-r--r--internal/handler/hearthbeat.go15
-rw-r--r--internal/logger/logger.go27
-rw-r--r--internal/model/mutations.go36
-rw-r--r--internal/node/acl.go12
-rw-r--r--internal/node/array.go23
-rw-r--r--internal/node/bool.go9
-rw-r--r--internal/node/node.go71
-rw-r--r--internal/node/number.go9
-rw-r--r--internal/node/object.go22
-rw-r--r--internal/node/string.go9
-rw-r--r--internal/storage/contract.go14
-rw-r--r--internal/storage/file.go79
-rw-r--r--internal/tree/contract.go23
-rw-r--r--internal/tree/core.go50
-rw-r--r--internal/tree/mutations.go89
28 files changed, 545 insertions, 414 deletions
diff --git a/internal/model/commandtype_string.go b/internal/command/commandtype_string.go
index 1ad4635..ef5080a 100644
--- a/internal/model/commandtype_string.go
+++ b/internal/command/commandtype_string.go
@@ -1,6 +1,6 @@
// Code generated by "stringer -type=CommandType"; DO NOT EDIT.
-package model
+package command
import "strconv"
diff --git a/internal/command/mutations.go b/internal/command/mutations.go
new file mode 100644
index 0000000..3fe1e3f
--- /dev/null
+++ b/internal/command/mutations.go
@@ -0,0 +1,21 @@
+package command
+
+import (
+ "go.neonxp.dev/objectid"
+)
+
+type Mutation struct {
+ ID objectid.ID
+ Type CommandType
+ Path []string
+ Data string
+}
+
+//go:generate stringer -type=CommandType
+type CommandType int
+
+const (
+ Create CommandType = iota
+ Merge
+ Remove
+)
diff --git a/internal/command/objectid.go b/internal/command/objectid.go
new file mode 100644
index 0000000..6eda868
--- /dev/null
+++ b/internal/command/objectid.go
@@ -0,0 +1,47 @@
+package command
+
+import (
+ "encoding/binary"
+ "encoding/hex"
+ "math/rand"
+ "time"
+)
+
+var iterator uint64
+
+func init() {
+ rand.Seed(time.Now().UnixMicro())
+ iterator = rand.Uint64()
+}
+
+func NewObjectID() ObjectID {
+ iterator++
+ p1 := uint64(time.Now().UnixMicro())
+ p2 := iterator
+ p3 := rand.Uint64()
+ r := ObjectID{}
+ r = binary.BigEndian.AppendUint64(r, p1)
+ r = binary.BigEndian.AppendUint64(r, p2)
+ r = binary.BigEndian.AppendUint64(r, p3)
+ return r
+}
+
+type ObjectID []byte
+
+func (o ObjectID) MarshalJSON() ([]byte, error) {
+ res := make([]byte, 0, hex.EncodedLen(len(o)))
+ hex.Encode(res, o)
+ return res, nil
+}
+
+func (o ObjectID) String() string {
+ return hex.EncodeToString(o)
+}
+
+func (o ObjectID) Time() time.Time {
+ if len(o) < 8 {
+ return time.Time{}
+ }
+ t := o[:8]
+ return time.UnixMicro(int64(binary.BigEndian.Uint64(t)))
+}
diff --git a/internal/config/config.go b/internal/config/config.go
index 0c48a87..9f581de 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -3,18 +3,20 @@ package config
import (
"os"
+ "go.neonxp.dev/djson/internal/node"
+
"go.neonxp.dev/json"
- "go.neonxp.dev/json/model"
)
type Config struct {
Listen string
- DB dbConfig
+ Log logConfig
+ DB string
JWT jwtConfig
}
-type dbConfig struct {
- Path string
+type logConfig struct {
+ Level string
}
type jwtConfig struct {
@@ -28,23 +30,23 @@ func Parse(file string) (*Config, error) {
return nil, err
}
- cfgNode, err := json.Unmarshal(fb)
+ j := json.New(node.Factory)
+ root, err := j.Unmarshal(string(fb))
if err != nil {
return nil, err
}
- listen := model.MustQuery(cfgNode, []string{"listen"}).(*model.StringNode).Value
- dbPath := model.MustQuery(cfgNode, []string{"db", "path"}).(*model.StringNode).Value
- algorithm := model.MustQuery(cfgNode, []string{"jwt", "algorithm"}).(*model.StringNode).Value
- privateKey := model.MustQuery(cfgNode, []string{"jwt", "privateKey"}).(*model.StringNode).Value
cfg := &Config{
- Listen: listen,
- DB: dbConfig{
- Path: dbPath,
+ Listen: json.MustQuery(root, []string{"listen"}).(*node.Node).GetString(),
+ Log: logConfig{
+ Level: json.MustQuery(root, []string{"log", "level"}).(*node.Node).GetString(),
},
+ DB: json.MustQuery(root, []string{"db"}).(*node.Node).GetString(),
JWT: jwtConfig{
- Algorithm: algorithm,
- PrivateKey: []byte(privateKey),
+ Algorithm: json.MustQuery(root, []string{"jwt", "algorithm"}).(*node.Node).GetString(),
+ PrivateKey: []byte(
+ json.MustQuery(root, []string{"jwt", "privateKey"}).(*node.Node).GetString(),
+ ),
},
}
diff --git a/internal/core/core.go b/internal/core/core.go
new file mode 100644
index 0000000..34718bd
--- /dev/null
+++ b/internal/core/core.go
@@ -0,0 +1,203 @@
+package core
+
+import (
+ "context"
+ "fmt"
+ "strconv"
+ "sync"
+
+ "github.com/dgraph-io/badger/v3"
+ "github.com/vmihailenco/msgpack/v5"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/djson/internal/node"
+ "go.neonxp.dev/json"
+)
+
+type Core struct {
+ storage *badger.DB
+ root *node.Node
+ mu sync.RWMutex
+ json *json.JSON
+}
+
+func New(storage *badger.DB) *Core {
+ return &Core{
+ storage: storage,
+ root: nil,
+ json: json.New(node.Factory),
+ }
+}
+
+func (c *Core) Init(ctx context.Context) error {
+ return c.storage.View(func(txn *badger.Txn) error {
+ opts := badger.DefaultIteratorOptions
+ opts.PrefetchSize = 10
+ it := txn.NewIterator(opts)
+ defer it.Close()
+ for it.Rewind(); it.Valid(); it.Next() {
+ item := it.Item()
+ err := item.Value(func(v []byte) error {
+ mut := &command.Mutation{}
+ if err := msgpack.Unmarshal(v, mut); err != nil {
+ return err
+ }
+ return c.apply(ctx, mut)
+ })
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+}
+
+func (c *Core) Apply(ctx context.Context, mutation *command.Mutation) error {
+ return c.storage.Update(func(txn *badger.Txn) error {
+ if err := c.apply(ctx, mutation); err != nil {
+ return err
+ }
+ mb, err := msgpack.Marshal(mutation)
+ if err != nil {
+ return err
+ }
+
+ e := &badger.Entry{
+ Key: mutation.ID,
+ Value: mb,
+ }
+
+ return txn.SetEntry(e)
+ })
+}
+
+func (c *Core) apply(ctx context.Context, mutation *command.Mutation) error {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
+ switch mutation.Type {
+ case command.Create:
+ n, err := c.json.Unmarshal(mutation.Data)
+ if err != nil {
+ return err
+ }
+ if err := c.create(mutation.Path, n.(*node.Node)); err != nil {
+ return err
+ }
+ case command.Merge:
+ n, err := c.json.Unmarshal(mutation.Data)
+ if err != nil {
+ return err
+ }
+ if err := c.merge(mutation.Path, n.(*node.Node)); err != nil {
+ return err
+ }
+ case command.Remove:
+ if err := c.remove(mutation.Path); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (c *Core) Query(ctx context.Context, query []string) (json.Node, error) {
+ return json.Query(c.root, query)
+}
+
+func (c *Core) create(path []string, n *node.Node) error {
+ if len(path) == 0 {
+ c.root = n
+ return nil
+ }
+
+ path, last := path[:len(path)-1], path[len(path)-1]
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+
+ switch parentNode.Type {
+ case json.ArrayType:
+ if last == "[]" {
+ parentNode.Append(n)
+ return nil
+ }
+ idx, err := strconv.Atoi(last)
+ if err != nil {
+ return fmt.Errorf("cant use %s as array index", last)
+ }
+ if idx < 0 || idx >= parentNode.Len() {
+ return fmt.Errorf("index %d out of bounds [0, %d]", idx, parentNode.Len()-1)
+ }
+ parentNode.SetByIndex(idx, n)
+ case json.ObjectType:
+ parentNode.SetKeyValue(last, n)
+ default:
+ return fmt.Errorf("cant add node to node of type %s", parentNode.Type)
+ }
+ return nil
+}
+
+func (c *Core) merge(path []string, n *node.Node) error {
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+
+ if n.Type != parentNode.Type {
+ return fmt.Errorf("merging nodes must be same type")
+ }
+
+ switch n.Type {
+ case json.ObjectType:
+ parentNode.Merge(n)
+ case json.ArrayType:
+ for i := 0; i < n.Len(); i++ {
+ parentNode.Append(n.Index(i))
+ }
+ default:
+ return fmt.Errorf("can merge only objects or arrays")
+ }
+ return nil
+}
+
+func (c *Core) remove(path []string) error {
+ if len(path) == 0 {
+ c.root = nil
+ return nil
+ }
+ path, last := path[:len(path)-1], path[len(path)-1]
+ parent, err := json.Query(c.root, path)
+ if err != nil {
+ return fmt.Errorf("parent node not found")
+ }
+ parentNode, ok := parent.(*node.Node)
+ if !ok {
+ return fmt.Errorf("invalid node")
+ }
+ switch parentNode.Type {
+ case json.ObjectType:
+ parentNode.RemoveByKey(last)
+ case json.ArrayType:
+ idx, err := strconv.Atoi(last)
+ if err != nil {
+ return fmt.Errorf("cant use %s as array index", last)
+ }
+ if idx < 0 || idx >= parentNode.Len() {
+ return fmt.Errorf("index %d out of bounds [0, %d]", idx, parentNode.Len()-1)
+ }
+ parentNode.RemoveByIndex(idx)
+ default:
+ return fmt.Errorf("can remove children only from object or array")
+ }
+ return nil
+}
diff --git a/internal/events/contract.go b/internal/events/contract.go
index dc5003f..5bea6ac 100644
--- a/internal/events/contract.go
+++ b/internal/events/contract.go
@@ -1,11 +1,11 @@
package events
-import "go.neonxp.dev/djson/internal/model"
+import "go.neonxp.dev/djson/internal/command"
type Dispatcher interface {
- Subscribe(path []string, id string, ch chan model.Mutation)
+ Subscribe(path []string, id string, ch chan command.Mutation)
Unsubscribe(path []string, id string)
- Notify(path []string, event *model.Mutation)
+ Notify(path []string, event *command.Mutation)
}
diff --git a/internal/events/events.go b/internal/events/events.go
index 49731b8..3ee87e4 100644
--- a/internal/events/events.go
+++ b/internal/events/events.go
@@ -3,7 +3,7 @@ package events
import (
"sync"
- "go.neonxp.dev/djson/internal/model"
+ "go.neonxp.dev/djson/internal/command"
)
type stdDispatcher struct {
@@ -14,14 +14,14 @@ func New() Dispatcher {
return &stdDispatcher{
tree: subscriberNode{
children: make(map[string]*subscriberNode),
- channels: make(map[string]chan model.Mutation),
+ channels: make(map[string]chan command.Mutation),
parent: nil,
mu: sync.RWMutex{},
},
}
}
-func (ed *stdDispatcher) Subscribe(path []string, id string, ch chan model.Mutation) {
+func (ed *stdDispatcher) Subscribe(path []string, id string, ch chan command.Mutation) {
ed.tree.subscribe(path, id, ch)
}
@@ -29,6 +29,6 @@ func (ed *stdDispatcher) Unsubscribe(path []string, id string) {
ed.tree.unsubscribe(path, id)
}
-func (ed *stdDispatcher) Notify(path []string, event *model.Mutation) {
+func (ed *stdDispatcher) Notify(path []string, event *command.Mutation) {
ed.tree.notify(path, event)
}
diff --git a/internal/events/node.go b/internal/events/node.go
index a1d9c3e..62d45dd 100644
--- a/internal/events/node.go
+++ b/internal/events/node.go
@@ -3,17 +3,17 @@ package events
import (
"sync"
- "go.neonxp.dev/djson/internal/model"
+ "go.neonxp.dev/djson/internal/command"
)
type subscriberNode struct {
parent *subscriberNode
children map[string]*subscriberNode
- channels map[string]chan model.Mutation
+ channels map[string]chan command.Mutation
mu sync.RWMutex
}
-func (sn *subscriberNode) subscribe(path []string, id string, ch chan model.Mutation) {
+func (sn *subscriberNode) subscribe(path []string, id string, ch chan command.Mutation) {
sn.mu.Lock()
defer sn.mu.Unlock()
if len(path) == 0 {
@@ -26,7 +26,7 @@ func (sn *subscriberNode) subscribe(path []string, id string, ch chan model.Muta
child = &subscriberNode{
parent: sn,
children: make(map[string]*subscriberNode),
- channels: make(map[string]chan model.Mutation),
+ channels: make(map[string]chan command.Mutation),
}
sn.children[head] = child
}
@@ -51,11 +51,11 @@ func (sn *subscriberNode) unsubscribe(path []string, id string) {
}
}
-func (sn *subscriberNode) notify(path []string, event *model.Mutation) {
+func (sn *subscriberNode) notify(path []string, event *command.Mutation) {
sn.mu.RLock()
defer sn.mu.RUnlock()
for _, ch := range sn.channels {
- go func(ch chan model.Mutation) {
+ go func(ch chan command.Mutation) {
ch <- *event
}(ch)
}
diff --git a/internal/handler/auth.go b/internal/handler/auth.go
deleted file mode 100644
index c66eeb7..0000000
--- a/internal/handler/auth.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package handler
-
-import (
- "fmt"
-
- "github.com/go-chi/jwtauth/v5"
-)
-
-var tokenAuth *jwtauth.JWTAuth
-
-func init() {
- tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
- _, tokenString, _ := tokenAuth.Encode(map[string]interface{}{"user_id": 123})
- fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
-}
diff --git a/internal/handler/contract.go b/internal/handler/contract.go
index 8887be8..38280cf 100644
--- a/internal/handler/contract.go
+++ b/internal/handler/contract.go
@@ -1,13 +1,13 @@
package handler
import (
- "net/http"
+ "context"
- "github.com/go-chi/chi/v5"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/json"
)
-type Handler interface {
- HandleCRUD(r chi.Router)
- HandleEvents(r chi.Router)
- Hearthbeat(w http.ResponseWriter, r *http.Request)
+type Core interface {
+ Apply(ctx context.Context, mutation *command.Mutation) error
+ Query(ctx context.Context, query []string) (json.Node, error)
}
diff --git a/internal/handler/crud.go b/internal/handler/crud.go
index 4ab0c7f..b12f68a 100644
--- a/internal/handler/crud.go
+++ b/internal/handler/crud.go
@@ -1,37 +1,35 @@
package handler
import (
+ "fmt"
"io"
"net/http"
- "time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
- "go.neonxp.dev/djson/internal/model"
- "go.neonxp.dev/json"
+ "go.neonxp.dev/djson/internal/command"
+ "go.neonxp.dev/objectid"
)
-func (h *handler) HandleCRUD(r chi.Router) {
- r.Use(middleware.CleanPath)
- r.Use(middleware.StripSlashes)
+func (h *handler) HandleCRUD(router chi.Router) {
+ router.Use(middleware.CleanPath)
+ router.Use(middleware.StripSlashes)
- r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
- res, err := h.core.Get(parsePath(rctx.RoutePath))
- if err != nil {
- writeError(http.StatusNotFound, err, w)
- return
- }
- result, err := res.MarshalJSON()
+ path := parsePath(rctx.RoutePath)
+
+ node, err := h.core.Query(r.Context(), path)
if err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
w.WriteHeader(http.StatusOK)
- _, _ = w.Write(result)
+ _, _ = w.Write([]byte(node.String()))
})
- r.Post("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Post("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
jsonBody, err := io.ReadAll(r.Body)
@@ -40,25 +38,23 @@ func (h *handler) HandleCRUD(r chi.Router) {
return
}
r.Body.Close()
- node, err := json.Unmarshal(jsonBody)
- if err != nil {
- writeError(http.StatusBadRequest, err, w)
- return
- }
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Create,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Create,
Path: path,
- Body: node,
+ Data: string(jsonBody),
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusCreated)
})
- r.Patch("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Patch("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
jsonBody, err := io.ReadAll(r.Body)
@@ -67,37 +63,37 @@ func (h *handler) HandleCRUD(r chi.Router) {
return
}
r.Body.Close()
- node, err := json.Unmarshal(jsonBody)
- if err != nil {
- writeError(http.StatusBadRequest, err, w)
- return
- }
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Merge,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Merge,
Path: path,
- Body: node,
+ Data: string(jsonBody),
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusOK)
})
- r.Delete("/*", func(w http.ResponseWriter, r *http.Request) {
+ router.Delete("/*", func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
- mutation := &model.Mutation{
- Date: time.Now(),
- Type: model.Remove,
+
+ mutation := command.Mutation{
+ ID: objectid.New(),
+ Type: command.Remove,
Path: path,
- Body: nil,
}
- if err := h.core.Mutate(r.Context(), mutation); err != nil {
- writeError(http.StatusInternalServerError, err, w)
+ if err := h.core.Apply(r.Context(), &mutation); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error())))
return
}
+
w.WriteHeader(http.StatusNoContent)
})
}
diff --git a/internal/handler/events.go b/internal/handler/events.go
index 299971d..43de3c4 100644
--- a/internal/handler/events.go
+++ b/internal/handler/events.go
@@ -1,20 +1,22 @@
package handler
import (
+ "encoding/json"
"fmt"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
- dmodel "go.neonxp.dev/djson/internal/model"
+
+ "go.neonxp.dev/djson/internal/command"
)
func (h *handler) HandleEvents(r chi.Router) {
r.Route("/sse", func(r chi.Router) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
- h.receiveEvents(w, r, func(ev *dmodel.Mutation) {
- message, _ := ev.Body.MarshalJSON()
+ h.receiveEvents(w, r, func(ev *command.Mutation) {
+ message, _ := json.Marshal(ev.Data)
fmt.Fprintf(
w,
`event: %s\ndata: {"path":"%s","data":%s}\n\n`,
@@ -27,8 +29,8 @@ func (h *handler) HandleEvents(r chi.Router) {
})
r.Route("/json", func(r chi.Router) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
- h.receiveEvents(w, r, func(ev *dmodel.Mutation) {
- message, _ := ev.Body.MarshalJSON()
+ h.receiveEvents(w, r, func(ev *command.Mutation) {
+ message, _ := json.Marshal(ev.Data)
fmt.Fprintf(
w,
`{"event":"%s","path":"%s","data":"%s"}\n`,
@@ -44,7 +46,7 @@ func (h *handler) HandleEvents(r chi.Router) {
func (h *handler) receiveEvents(
w http.ResponseWriter,
r *http.Request,
- render func(ev *dmodel.Mutation),
+ render func(ev *command.Mutation),
) {
flusher := w.(http.Flusher)
w.Header().Set("Content-Type", "application/x-ndjson")
@@ -53,7 +55,7 @@ func (h *handler) receiveEvents(
w.Header().Set("Access-Control-Allow-Origin", "*")
rctx := chi.RouteContext(r.Context())
path := parsePath(rctx.RoutePath)
- ch := make(chan dmodel.Mutation)
+ ch := make(chan command.Mutation)
reqID := middleware.GetReqID(r.Context())
h.events.Subscribe(path, reqID, ch)
defer h.events.Unsubscribe(path, reqID)
diff --git a/internal/handler/handler.go b/internal/handler/handler.go
index 11c1936..bd8a2c1 100644
--- a/internal/handler/handler.go
+++ b/internal/handler/handler.go
@@ -4,14 +4,10 @@ import (
"net/http"
"strings"
- "go.neonxp.dev/json"
- jsonModel "go.neonxp.dev/json/model"
-
"go.neonxp.dev/djson/internal/events"
- "go.neonxp.dev/djson/internal/tree"
)
-func New(core tree.Core, eventsDispatcher events.Dispatcher) Handler {
+func New(core Core, eventsDispatcher events.Dispatcher) *handler {
return &handler{
core: core,
events: eventsDispatcher,
@@ -19,13 +15,12 @@ func New(core tree.Core, eventsDispatcher events.Dispatcher) Handler {
}
type handler struct {
- core tree.Core
+ core Core
events events.Dispatcher
}
func writeError(code int, err error, w http.ResponseWriter) {
- jsonErr, _ := json.Marshal(jsonModel.NewNode(err.Error()))
- _, _ = w.Write(jsonErr)
+ _, _ = w.Write([]byte(err.Error()))
}
func parsePath(nodePath string) []string {
diff --git a/internal/handler/hearthbeat.go b/internal/handler/hearthbeat.go
index 014880e..22f6c3c 100644
--- a/internal/handler/hearthbeat.go
+++ b/internal/handler/hearthbeat.go
@@ -2,21 +2,10 @@ package handler
import (
"net/http"
-
- "go.neonxp.dev/djson/internal/tree"
)
func (h *handler) Hearthbeat(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
- switch h.core.State() {
- case tree.Ready:
- w.WriteHeader(http.StatusOK)
- _, _ = w.Write([]byte("."))
- case tree.Failed:
- w.WriteHeader(http.StatusInternalServerError)
- _, _ = w.Write([]byte("start failed"))
- case tree.Running:
- w.WriteHeader(http.StatusServiceUnavailable)
- _, _ = w.Write([]byte("starting..."))
- }
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte("."))
}
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
new file mode 100644
index 0000000..bd90308
--- /dev/null
+++ b/internal/logger/logger.go
@@ -0,0 +1,27 @@
+package logger
+
+import (
+ "fmt"
+
+ "github.com/rs/zerolog"
+)
+
+type Logger struct {
+ zerolog.Logger
+}
+
+func (l Logger) Errorf(msg string, args ...interface{}) {
+ l.Error().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Warningf(msg string, args ...interface{}) {
+ l.Warn().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Infof(msg string, args ...interface{}) {
+ l.Info().Msg(fmt.Sprintf(msg, args...))
+}
+
+func (l Logger) Debugf(msg string, args ...interface{}) {
+ l.Debug().Msg(fmt.Sprintf(msg, args...))
+}
diff --git a/internal/model/mutations.go b/internal/model/mutations.go
deleted file mode 100644
index 6463823..0000000
--- a/internal/model/mutations.go
+++ /dev/null
@@ -1,36 +0,0 @@
-package model
-
-import (
- "fmt"
- "strings"
- "time"
-
- "go.neonxp.dev/json/model"
-)
-
-type Mutation struct {
- Date time.Time
- Type CommandType
- Path []string
- Body model.Node
-}
-
-func (m *Mutation) String() string {
- body, _ := m.Body.MarshalJSON()
- return fmt.Sprintf(
- "Date=%s Type=%s Path='%s' Body=%s",
- m.Date.Format(time.RFC3339),
- m.Type,
- strings.Join(m.Path, "/"),
- string(body),
- )
-}
-
-//go:generate stringer -type=CommandType
-type CommandType int
-
-const (
- Create CommandType = iota
- Merge
- Remove
-)
diff --git a/internal/node/acl.go b/internal/node/acl.go
new file mode 100644
index 0000000..ca2e3dc
--- /dev/null
+++ b/internal/node/acl.go
@@ -0,0 +1,12 @@
+package node
+
+type ACL struct {
+ Read []Actor
+ Write []Actor
+ Delete []Actor
+}
+
+type Actor struct {
+ UserID string
+ Group string
+}
diff --git a/internal/node/array.go b/internal/node/array.go
new file mode 100644
index 0000000..780f99f
--- /dev/null
+++ b/internal/node/array.go
@@ -0,0 +1,23 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) Append(v json.Node) {
+ n.arrayValue = append(n.arrayValue, v.(*Node))
+}
+
+func (n *Node) Index(i int) json.Node {
+ return n.arrayValue[i]
+}
+
+func (n *Node) SetByIndex(i int, v *Node) {
+ n.arrayValue[i] = v
+}
+
+func (n *Node) RemoveByIndex(i int) {
+ n.arrayValue = append(n.arrayValue[:i], n.arrayValue[i:]...)
+}
+
+func (n *Node) Len() int {
+ return len(n.arrayValue)
+}
diff --git a/internal/node/bool.go b/internal/node/bool.go
new file mode 100644
index 0000000..91edaf9
--- /dev/null
+++ b/internal/node/bool.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetBool(v bool) {
+ n.boolValue = v
+}
+
+func (n *Node) GetBool() bool {
+ return n.boolValue
+}
diff --git a/internal/node/node.go b/internal/node/node.go
new file mode 100644
index 0000000..b8bec52
--- /dev/null
+++ b/internal/node/node.go
@@ -0,0 +1,71 @@
+package node
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ "go.neonxp.dev/json"
+ "go.neonxp.dev/objectid"
+)
+
+func Factory(typ json.NodeType) (json.Node, error) {
+ n := &Node{
+ ID: objectid.New(),
+ Type: typ,
+ }
+ switch typ {
+ case json.ObjectType:
+ n.objectValue = make(map[string]*Node)
+ case json.ArrayType:
+ n.arrayValue = make([]*Node, 0)
+ }
+ return n, nil
+}
+
+type Node struct {
+ ID objectid.ID
+ Type json.NodeType
+ Parent objectid.ID
+ stringValue string
+ numberValue float64
+ boolValue bool
+ objectValue map[string]*Node
+ arrayValue []*Node
+ ACL ACL
+}
+
+func (n *Node) String() string {
+ switch n.Type {
+ case json.NullType:
+ return "null"
+ case json.StringType:
+ return `"` + n.stringValue + `"`
+ case json.NumberType:
+ return strconv.FormatFloat(n.numberValue, 'g', 15, 64)
+ case json.BooleanType:
+ if n.boolValue {
+ return "true"
+ }
+ return "false"
+ case json.ObjectType:
+ res := make([]string, 0, len(n.objectValue))
+ for k, n := range n.objectValue {
+ res = append(res, fmt.Sprintf(`"%s":%s`, k, n.String()))
+ }
+ return fmt.Sprintf(`{%s}`, strings.Join(res, ","))
+ case json.ArrayType:
+ res := make([]string, 0, len(n.arrayValue))
+ for _, v := range n.arrayValue {
+ res = append(res, v.String())
+ }
+ return fmt.Sprintf(`[%s]`, strings.Join(res, ","))
+ }
+ return ""
+}
+
+func (n *Node) SetParent(v json.Node) {
+ if v != nil {
+ n.Parent = v.(*Node).ID
+ }
+}
diff --git a/internal/node/number.go b/internal/node/number.go
new file mode 100644
index 0000000..055ae00
--- /dev/null
+++ b/internal/node/number.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetNumber(v float64) {
+ n.numberValue = v
+}
+
+func (n *Node) GetNumber() float64 {
+ return n.numberValue
+}
diff --git a/internal/node/object.go b/internal/node/object.go
new file mode 100644
index 0000000..35a3141
--- /dev/null
+++ b/internal/node/object.go
@@ -0,0 +1,22 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) SetKeyValue(k string, v json.Node) {
+ n.objectValue[k] = v.(*Node)
+}
+
+func (n *Node) GetByKey(k string) (json.Node, bool) {
+ node, ok := n.objectValue[k]
+ return node, ok
+}
+
+func (n *Node) Merge(n2 *Node) {
+ for k, v := range n2.objectValue {
+ n.objectValue[k] = v
+ }
+}
+
+func (n *Node) RemoveByKey(k string) {
+ delete(n.objectValue, k)
+}
diff --git a/internal/node/string.go b/internal/node/string.go
new file mode 100644
index 0000000..90de4a7
--- /dev/null
+++ b/internal/node/string.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetString(v string) {
+ n.stringValue = v
+}
+
+func (n *Node) GetString() string {
+ return n.stringValue
+}
diff --git a/internal/storage/contract.go b/internal/storage/contract.go
deleted file mode 100644
index 5847bcc..0000000
--- a/internal/storage/contract.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package storage
-
-import (
- "context"
- "io"
-
- "go.neonxp.dev/djson/internal/model"
-)
-
-type Storage interface {
- io.Closer
- Commit(ctx context.Context, mut model.Mutation) error
- Load() chan model.Mutation
-}
diff --git a/internal/storage/file.go b/internal/storage/file.go
deleted file mode 100644
index 885d5ef..0000000
--- a/internal/storage/file.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package storage
-
-import (
- "context"
- "encoding/gob"
- "fmt"
- "io"
- "os"
-
- "github.com/rs/zerolog"
- "go.neonxp.dev/djson/internal/model"
- jsonModel "go.neonxp.dev/json/model"
-)
-
-func init() {
- gob.Register(new(jsonModel.ArrayNode))
- gob.Register(new(jsonModel.ObjectNode))
- gob.Register(new(jsonModel.StringNode))
- gob.Register(new(jsonModel.BooleanNode))
- gob.Register(new(jsonModel.NullNode))
- gob.Register(new(jsonModel.NumberNode))
-}
-
-type fsStorage struct {
- logger zerolog.Logger
- enc *gob.Encoder
- dec *gob.Decoder
- fh *os.File
- fileName string
- mutationsLog []model.Mutation
-}
-
-func New(fileName string, logger zerolog.Logger) (Storage, error) {
- logger.Info().Str("path", fileName).Msg("loading db")
- fh, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
- if err != nil {
- return nil, err
- }
-
- return &fsStorage{
- logger: logger,
- fileName: fileName,
- fh: fh,
- enc: gob.NewEncoder(fh),
- dec: gob.NewDecoder(fh),
- mutationsLog: []model.Mutation{},
- }, nil
-}
-
-func (fs *fsStorage) Commit(ctx context.Context, mut model.Mutation) error {
- if fs.enc == nil {
- return fmt.Errorf("file storage not initiated")
- }
- return fs.enc.Encode(mut)
-}
-
-func (fs *fsStorage) Load() chan model.Mutation {
- ch := make(chan model.Mutation)
- go func() {
- for {
- m := model.Mutation{}
- if err := fs.dec.Decode(&m); err != nil {
- if err != io.EOF {
- fs.logger.Err(err)
- }
- close(ch)
- return
- }
- fs.logger.Debug().RawJSON("json", []byte(m.String())).Msg("loaded mutation")
- fs.mutationsLog = append(fs.mutationsLog, m)
- ch <- m
- }
- }()
- return ch
-}
-
-func (fs *fsStorage) Close() error {
- return fs.fh.Close()
-}
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
-}