/
aggregate.go
313 lines (272 loc) · 6.79 KB
/
aggregate.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package redisearch
import (
"fmt"
"github.com/gomodule/redigo/redis"
"log"
"reflect"
)
// Projection
type Projection struct {
Expression string
Alias string
}
func NewProjection(expression string, alias string) *Projection {
return &Projection{
Expression: expression,
Alias: alias,
}
}
func (p Projection) Serialize() redis.Args {
args := redis.Args{"APPLY", p.Expression, "AS", p.Alias}
return args
}
// Cursor
type Cursor struct {
Id int
Count int
MaxIdle int
}
func NewCursor() *Cursor {
return &Cursor{
Id: 0,
Count: 0,
MaxIdle: 0,
}
}
func (c *Cursor) SetId(id int) *Cursor {
c.Id = id
return c
}
func (c *Cursor) SetCount(count int) *Cursor {
c.Count = count
return c
}
func (c *Cursor) SetMaxIdle(maxIdle int) *Cursor {
c.MaxIdle = maxIdle
return c
}
func (c Cursor) Serialize() redis.Args {
args := redis.Args{"WITHCURSOR"}
if c.Count > 0 {
args = args.Add("COUNT", c.Count)
}
if c.MaxIdle > 0 {
args = args.Add("MAXIDLE", c.MaxIdle)
}
return args
}
// GroupBy
type GroupBy struct {
Fields []string
Reducers []Reducer
Paging *Paging
}
func NewGroupBy() *GroupBy {
return &GroupBy{
Fields: make([]string, 0),
Reducers: make([]Reducer, 0),
Paging: nil,
}
}
func (g *GroupBy) AddFields(fields interface{}) *GroupBy {
switch fields.(type) {
case string:
g.Fields = append(g.Fields, fields.(string))
case []string:
g.Fields = append(g.Fields, fields.([]string)...)
default:
return g
}
return g
}
func (g *GroupBy) Reduce(reducer Reducer) *GroupBy {
g.Reducers = append(g.Reducers, reducer)
return g
}
func (g *GroupBy) Limit(offset int, num int) *GroupBy {
g.Paging = NewPaging(offset, num)
return g
}
func (g GroupBy) Serialize() redis.Args {
ret := len(g.Fields)
args := redis.Args{"GROUPBY", ret}.AddFlat(g.Fields)
for _, reducer := range g.Reducers {
args = args.AddFlat(reducer.Serialize())
}
if g.Paging != nil {
args = args.AddFlat(g.Paging.serialize())
}
return args
}
// AggregateQuery
type AggregateQuery struct {
Query *Query
AggregatePlan redis.Args
Paging *Paging
Max int
WithSchema bool
Verbatim bool
WithCursor bool
Cursor *Cursor
// TODO: add load fields
}
func NewAggregateQuery() *AggregateQuery {
return &AggregateQuery{
Query: nil,
Paging: nil,
Max: 0,
WithSchema: false,
Verbatim: false,
WithCursor: false,
}
}
func (a *AggregateQuery) SetQuery(query *Query) *AggregateQuery {
a.Query = query
return a
}
func (a *AggregateQuery) SetWithSchema(value bool) *AggregateQuery {
a.WithSchema = value
return a
}
func (a *AggregateQuery) SetVerbatim(value bool) *AggregateQuery {
a.Verbatim = value
return a
}
func (a *AggregateQuery) SetMax(value int) *AggregateQuery {
a.Max = value
return a
}
func (a *AggregateQuery) SetCursor(cursor *Cursor) *AggregateQuery {
a.WithCursor = true
a.Cursor = cursor
return a
}
func (a *AggregateQuery) CursorHasResults() (res bool) {
res = false
if !reflect.ValueOf(a.Cursor).IsNil() {
res = a.Cursor.Id > 0
}
return
}
//Adds a APPLY clause to the aggregate plan
func (a *AggregateQuery) Apply(expression Projection) *AggregateQuery {
a.AggregatePlan = a.AggregatePlan.AddFlat(expression.Serialize())
return a
}
//Sets the limit for the initial pool of results from the query.
func (a *AggregateQuery) Limit(offset int, num int) *AggregateQuery {
a.Paging = NewPaging(offset, num)
return a
}
//Load document fields from the document HASH objects (if they are not in the sortables)
func (a *AggregateQuery) Load(Properties []string) *AggregateQuery {
nproperties := len(Properties)
if nproperties > 0 {
a.AggregatePlan = a.AggregatePlan.Add("LOAD", nproperties)
for _, property := range Properties {
a.AggregatePlan = a.AggregatePlan.Add(fmt.Sprintf("@%s", property))
}
}
return a
}
//Adds a GROUPBY clause to the aggregate plan
func (a *AggregateQuery) GroupBy(group GroupBy) *AggregateQuery {
a.AggregatePlan = a.AggregatePlan.AddFlat(group.Serialize())
return a
}
//Adds a SORTBY clause to the aggregate plan
func (a *AggregateQuery) SortBy(SortByProperties []SortingKey) *AggregateQuery {
nsort := len(SortByProperties)
if nsort > 0 {
a.AggregatePlan = a.AggregatePlan.Add("SORTBY", nsort*2)
for _, sortby := range SortByProperties {
a.AggregatePlan = a.AggregatePlan.AddFlat(sortby.Serialize())
}
if a.Max > 0 {
a.AggregatePlan = a.AggregatePlan.Add("MAX", a.Max)
}
}
return a
}
//Specify filters to filter the results using predicates relating to values in the result set.
func (a *AggregateQuery) Filter(expression string) *AggregateQuery {
a.AggregatePlan = a.AggregatePlan.Add("FILTER", expression)
//a.Filters = append(a.Filters, expression)
return a
}
func (q AggregateQuery) Serialize() redis.Args {
args := redis.Args{}
if q.Query != nil {
args = args.AddFlat(q.Query.serialize())
} else {
args = args.Add("*")
}
// WITHSCHEMA
if q.WithSchema {
args = args.AddFlat("WITHSCHEMA")
}
// VERBATIM
if q.Verbatim {
args = args.Add("VERBATIM")
}
// WITHCURSOR
if q.WithCursor {
args = args.AddFlat(q.Cursor.Serialize())
}
// TODO: add load fields
//Add the aggregation plan with ( GROUPBY and REDUCE | SORTBY | APPLY | FILTER ).+ clauses
args = args.AddFlat(q.AggregatePlan)
// LIMIT
if !reflect.ValueOf(q.Paging).IsNil() {
args = args.Add("LIMIT", q.Paging.Offset, q.Paging.Num)
}
return args
}
// Deprecated: Please use processAggReply() instead
func ProcessAggResponse(res []interface{}) [][]string {
aggregateReply := make([][]string, len(res), len(res))
for i := 0; i < len(res); i++ {
if d, e := redis.Strings(res[i], nil); e == nil {
aggregateReply[i] = d
} else {
log.Print("Error parsing Aggregate Reply: ", e)
aggregateReply[i] = nil
}
}
return aggregateReply
}
func processAggReply(res []interface{}) (total int, aggregateReply [][]string, err error) {
aggregateReply = [][]string{}
total = 0
aggregate_results := len(res) - 1
if aggregate_results > 0 {
total = aggregate_results
aggregateReply = make([][]string, aggregate_results, aggregate_results)
for i := 0; i < aggregate_results; i++ {
if d, e := redis.Strings(res[i+1], nil); e == nil {
aggregateReply[i] = d
} else {
err = fmt.Errorf("Error parsing Aggregate Reply: %v on reply position %d", e, i)
aggregateReply[i] = nil
}
}
}
return
}
func ProcessAggResponseSS(res []interface{}) [][]string {
var lout = len(res)
aggregateReply := make([][]string, lout, lout)
for i := 0; i < lout; i++ {
reply := res[i].([]interface{})
linner := len(reply)
aggregateReply[i] = make([]string, linner, linner)
for j := 0; j < linner; j++ {
if reply[j] == nil {
log.Print(fmt.Sprintf("Error parsing Aggregate Reply on position (%d,%d)", i, j))
} else {
aggregateReply[i][j] = reply[j].(string)
}
}
}
return aggregateReply
}