-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathexpression.go
More file actions
162 lines (132 loc) · 3.91 KB
/
Copy pathexpression.go
File metadata and controls
162 lines (132 loc) · 3.91 KB
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
package reindexer
import (
"reflect"
"github.com/restream/reindexer/v5/cjson"
)
type TimeUnit string
const (
Sec TimeUnit = "sec"
Msec TimeUnit = "msec"
Usec TimeUnit = "usec"
Nsec TimeUnit = "nsec"
)
// IExpression is a serializable operand for WhereExpressions: a field reference, literal values,
// a nested query, or a built-in function (see Field, Values, SubQuery, FlatArrayLen, Now, etc.).
type IExpression interface {
Type() int
Serialize(ser *cjson.Serializer)
}
// ==================== IExpressions implemetations list begins here ====================
// Field names an indexed field for use as the left or right side of an expression condition.
type Field struct {
Name string
}
// Values is a list of literal values serialized as the right-hand side of an expression (or other supported positions).
type Values struct {
Values []any
}
// SubQuery embeds another Query’s serialized body as an expression operand (e.g. correlated filters).
type SubQuery struct {
SubQuery *Query
}
// FlatArrayLen is the flat_array_len(field) expression; implements IFunction and IExpression.
type FlatArrayLen struct {
Field string
}
// Now is the now(time_unit) expression; implements IFunction and IExpression.
type Now struct {
TimeUnit TimeUnit
}
// ==================== IExpressions implemetations list ends here ====================
// Type returns expressionTypeField.
func (f Field) Type() int {
return expressionTypeField
}
// Serialize writes the field expression tag and name.
func (f Field) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(f.Type()))
ser.PutVString(f.Name)
}
// Type returns expressionTypeValues.
func (v Values) Type() int {
return expressionTypeValues
}
// Serialize writes the values tag, count, and each element with PutValue.
func (v Values) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(v.Type()))
ser.PutVarCUInt(len(v.Values))
for _, v := range v.Values {
if err := ser.PutValue(reflect.ValueOf(v)); err != nil {
panic(err)
}
}
}
// Type returns expressionTypeSubQuery.
func (q SubQuery) Type() int {
return expressionTypeSubQuery
}
// Serialize writes the subquery tag and the nested query’s serialized buffer.
func (s SubQuery) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(s.Type()))
serializeSubQuery(s.SubQuery, ser, s.SubQuery.queryFormatVersion)
}
func (f FlatArrayLen) FunctionType() int {
return functionFlatArrayLen
}
func (f FlatArrayLen) Fields() []string {
return []string{f.Field}
}
func (f FlatArrayLen) Args() []any {
return []any{}
}
func (f Now) FunctionType() int {
return functionNow
}
func (f Now) Fields() []string {
return []string{}
}
func (f Now) Args() []any {
return []any{f.TimeUnit}
}
type IFunction interface {
FunctionType() int
Fields() []string
Args() []any
}
func SerializeFunction(fn IFunction, ser *cjson.Serializer) {
ser.PutVarCUInt(len(fn.Fields()))
for _, field := range fn.Fields() {
ser.PutVString(field)
}
ser.PutVarCUInt(len(fn.Args()))
for _, arg := range fn.Args() {
if err := ser.PutValue(reflect.ValueOf(arg)); err != nil {
panic(err)
}
}
ser.PutVarCUInt(fn.FunctionType())
}
// Type reports expressionTypeExpression for the flat_array_len function node.
func (f FlatArrayLen) Type() int {
return expressionTypeExpression
}
// Serialize writes the function expression tag and flat_array_len payload.
func (f FlatArrayLen) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(f.Type()))
ser.PutVarCUInt(1)
ser.PutVString(f.Field)
ser.PutVarCUInt(0)
ser.PutVarCUInt(functionFlatArrayLen)
}
// Type reports expressionTypeExpression for the now function node.
func (n Now) Type() int {
return expressionTypeExpression
}
// Serialize writes the function expression tag and now() payload.
func (n Now) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(n.Type()))
ser.PutVarCUInt(0)
ser.PutVarCUInt(1)
ser.PutVarCUInt(valueString).PutVString(string(n.TimeUnit))
ser.PutVarCUInt(functionNow)
}