forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastMatcherDef.go
247 lines (210 loc) · 4.29 KB
/
fastMatcherDef.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
package gojsonsm
import (
"fmt"
"sort"
"strings"
)
type SlotID int
func (id SlotID) String() string {
return fmt.Sprintf("#%d", id)
}
type BucketID int
func (id BucketID) String() string {
return fmt.Sprintf("%%%d", id)
}
type DataRef interface {
String() string
}
func dataRefToString(ref DataRef) string {
if ref == nil {
return activeLitRef{}.String()
}
return ref.String()
}
type activeLitRef struct {
}
func (activeLitRef) String() string {
return "@"
}
type SlotRef struct {
Slot SlotID
}
func (ref SlotRef) String() string {
return fmt.Sprintf("$%d", ref.Slot)
}
type FuncRef struct {
FuncName string
Params []DataRef
}
func (ref FuncRef) String() string {
value := fmt.Sprintf("func:%s(", ref.FuncName)
for paramIdx, param := range ref.Params {
if paramIdx != 0 {
value += ", "
}
value += param.String()
}
value += ")"
return value
}
type OpType int
const (
OpTypeEquals OpType = iota
OpTypeLessThan
OpTypeLessEquals
OpTypeGreaterThan
OpTypeGreaterEquals
OpTypeExists
OpTypeIn
OpTypeMatches
)
func (value OpType) String() string {
switch value {
case OpTypeEquals:
return "eq"
case OpTypeLessThan:
return "lt"
case OpTypeLessEquals:
return "le"
case OpTypeGreaterThan:
return "gt"
case OpTypeGreaterEquals:
return "gte"
case OpTypeIn:
return "in"
case OpTypeExists:
return "exists"
case OpTypeMatches:
return "matches"
}
return "??unknown??"
}
type OpNode struct {
BucketIdx BucketID
Op OpType
Lhs DataRef
Rhs DataRef
}
func (op OpNode) String() string {
return fmt.Sprintf("[%d] %s %s %s",
op.BucketIdx,
dataRefToString(op.Lhs),
op.Op,
dataRefToString(op.Rhs))
}
type LoopType int
const (
LoopTypeAny LoopType = iota
LoopTypeEvery
LoopTypeAnyEvery
)
func (value LoopType) String() string {
switch value {
case LoopTypeAny:
return "any"
case LoopTypeEvery:
return "every"
case LoopTypeAnyEvery:
return "anyevery"
}
return "??unknown??"
}
type LoopNode struct {
BucketIdx BucketID
Mode LoopType
Target DataRef
Node *ExecNode
}
func (node *LoopNode) String() string {
out := ""
out += fmt.Sprintf("[%d] :%s in %s:\n", node.BucketIdx, node.Mode, dataRefToString(node.Target))
out += reindentString(node.Node.String(), " ")
return out
}
type AfterNode struct {
Ops []OpNode
Loops []LoopNode
}
type ExecNode struct {
StoreId SlotID
Elems map[string]*ExecNode
Ops []OpNode
Loops []LoopNode
After *AfterNode
}
type MatchDef struct {
ParseNode *ExecNode
MatchTree binTree
MatchBuckets []int
NumBuckets int
NumSlots int
}
func (def MatchDef) String() string {
var out string
out += "match tree:\n"
out += " $doc:\n"
out += reindentString(def.ParseNode.String(), " ")
out += "\n"
out += "bin tree:\n"
out += reindentString(def.MatchTree.String(), " ")
out += "\n"
out += "match buckets:\n"
for i, bucketID := range def.MatchBuckets {
out += fmt.Sprintf(" %d: %d\n", i, bucketID)
}
out += fmt.Sprintf("num buckets: %d\n", def.NumBuckets)
out += fmt.Sprintf("num slots: %d\n", def.NumSlots)
return strings.TrimRight(out, "\n")
}
func (node ExecNode) String() string {
var out string
if node.StoreId > 0 {
out += fmt.Sprintf(":store $%d\n", node.StoreId)
}
if len(node.Ops) > 0 {
out += fmt.Sprintf(":ops\n")
for _, op := range node.Ops {
out += reindentString(op.String(), " ")
out += "\n"
}
}
// For debugging, lets sort the elements by name first
var ks []string
for k := range node.Elems {
ks = append(ks, k)
}
sort.Strings(ks)
if len(ks) > 0 {
out += fmt.Sprintf(":elems\n")
for _, k := range ks {
elem := node.Elems[k]
out += fmt.Sprintf(" `%s`:\n", k)
out += reindentString(elem.String(), " ")
out += "\n"
}
}
if len(node.Loops) > 0 {
out += fmt.Sprintf(":loops\n")
for _, loop := range node.Loops {
out += reindentString(loop.String(), " ")
out += "\n"
}
}
if node.After != nil {
if len(node.After.Ops) > 0 {
out += fmt.Sprintf(":after-ops:\n")
for _, anode := range node.After.Ops {
out += reindentString(anode.String(), " ")
out += "\n"
}
}
if len(node.After.Loops) > 0 {
out += fmt.Sprintf(":after-loops:\n")
for _, loop := range node.After.Loops {
out += reindentString(loop.String(), " ")
out += "\n"
}
}
}
return strings.TrimRight(out, "\n")
}