1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
}
|