-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathupdate.go
262 lines (216 loc) · 5.97 KB
/
update.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package plan
import (
"encoding/json"
"fmt"
"github.com/couchbaselabs/query/algebra"
"github.com/couchbaselabs/query/datastore"
"github.com/couchbaselabs/query/expression"
"github.com/couchbaselabs/query/expression/parser"
)
// Enable copy-before-write, so that all reads use old values
type Clone struct {
readonly
}
// Write to copy
type Set struct {
readonly
node *algebra.Set
}
// Write to copy
type Unset struct {
readonly
node *algebra.Unset
}
// Send to keyspace
type SendUpdate struct {
readwrite
keyspace datastore.Keyspace
alias string
limit expression.Expression
}
func NewClone() *Clone {
return &Clone{}
}
func (this *Clone) Accept(visitor Visitor) (interface{}, error) {
return visitor.VisitClone(this)
}
func (this *Clone) New() Operator {
return &Clone{}
}
func (this *Clone) MarshalJSON() ([]byte, error) {
r := map[string]interface{}{"#operator": "Clone"}
return json.Marshal(r)
}
func (this *Clone) UnmarshalJSON([]byte) error {
// NOP: Clone has no data structure
return nil
}
func NewSet(node *algebra.Set) *Set {
return &Set{
node: node,
}
}
func (this *Set) Accept(visitor Visitor) (interface{}, error) {
return visitor.VisitSet(this)
}
func (this *Set) New() Operator {
return &Set{}
}
func (this *Set) Node() *algebra.Set {
return this.node
}
func (this *Set) MarshalJSON() ([]byte, error) {
r := map[string]interface{}{"#operator": "Set"}
s := make([]interface{}, 0, len(this.node.Terms()))
for _, term := range this.node.Terms() {
t := make(map[string]interface{})
t["path"] = expression.NewStringer().Visit(term.Path())
t["expr"] = expression.NewStringer().Visit(term.Value())
s = append(s, t)
}
r["set_terms"] = s
return json.Marshal(r)
}
func (this *Set) UnmarshalJSON(body []byte) error {
var _unmarshalled struct {
_ string `json:"#operator"`
SetTerms []struct {
Path string `json:"path"`
Expr string `json:"expr"`
} `json:"set_terms"`
}
err := json.Unmarshal(body, &_unmarshalled)
if err != nil {
return err
}
terms := make([]*algebra.SetTerm, len(_unmarshalled.SetTerms))
for i, SetTerm := range _unmarshalled.SetTerms {
path_expr, err := parser.Parse(SetTerm.Path)
if err != nil {
return err
}
path, is_path := path_expr.(expression.Path)
if !is_path {
return fmt.Errorf("Set.UnmarshalJSON: cannot resolve path expression from %s", SetTerm.Path)
}
expr, err := parser.Parse(SetTerm.Expr)
if err != nil {
return err
}
terms[i] = algebra.NewSetTerm(path, expr, nil)
}
this.node = algebra.NewSet(terms)
return nil
}
func NewUnset(node *algebra.Unset) *Unset {
return &Unset{
node: node,
}
}
func (this *Unset) Accept(visitor Visitor) (interface{}, error) {
return visitor.VisitUnset(this)
}
func (this *Unset) New() Operator {
return &Unset{}
}
func (this *Unset) Node() *algebra.Unset {
return this.node
}
func (this *Unset) MarshalJSON() ([]byte, error) {
r := map[string]interface{}{"#operator": "Unset"}
s := make([]interface{}, 0, len(this.node.Terms()))
for _, term := range this.node.Terms() {
t := make(map[string]interface{})
t["path"] = expression.NewStringer().Visit(term.Path())
// FIXME
//t["expr"] = expression.NewStringer().Visit(term.UpdateFor().Bindings())
t["expr"] = "FIXME"
s = append(s, t)
}
r["unset_terms"] = s
return json.Marshal(r)
}
func (this *Unset) UnmarshalJSON(body []byte) error {
var _unmarshalled struct {
_ string `json:"#operator"`
UnsetTerms []struct {
Path string `json:"path"`
Expr string `json:"expr"`
} `json:"unset_terms"`
}
err := json.Unmarshal(body, &_unmarshalled)
if err != nil {
return err
}
terms := make([]*algebra.UnsetTerm, len(_unmarshalled.UnsetTerms))
for i, UnsetTerm := range _unmarshalled.UnsetTerms {
path_expr, err := parser.Parse(UnsetTerm.Path)
if err != nil {
return err
}
path, is_path := path_expr.(expression.Path)
if !is_path {
return fmt.Errorf("Unset.UnmarshalJSON: cannot resolve path expression from %s", UnsetTerm.Path)
}
// is expr needed in Unset?
_, err = parser.Parse(UnsetTerm.Expr)
if err != nil {
return err
}
terms[i] = algebra.NewUnsetTerm(path, nil)
}
this.node = algebra.NewUnset(terms)
return nil
}
func NewSendUpdate(keyspace datastore.Keyspace, alias string, limit expression.Expression) *SendUpdate {
return &SendUpdate{
keyspace: keyspace,
alias: alias,
limit: limit,
}
}
func (this *SendUpdate) Accept(visitor Visitor) (interface{}, error) {
return visitor.VisitSendUpdate(this)
}
func (this *SendUpdate) New() Operator {
return &SendUpdate{}
}
func (this *SendUpdate) Keyspace() datastore.Keyspace {
return this.keyspace
}
func (this *SendUpdate) Alias() string {
return this.alias
}
func (this *SendUpdate) Limit() expression.Expression {
return this.limit
}
func (this *SendUpdate) MarshalJSON() ([]byte, error) {
r := map[string]interface{}{"#operator": "SendUpdate"}
r["keyspace"] = this.keyspace.Name()
r["namespace"] = this.keyspace.NamespaceId()
r["alias"] = this.alias
return json.Marshal(r)
}
func (this *SendUpdate) UnmarshalJSON(body []byte) error {
var _unmarshalled struct {
_ string `json:"#operator"`
Keys string `json:"keyspace"`
Names string `json:"namespace"`
Alias string `json:"alias"`
}
err := json.Unmarshal(body, &_unmarshalled)
if err != nil {
return err
}
this.alias = _unmarshalled.Alias
this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys)
return nil
}