@@ -7,38 +7,6 @@ public class AggregationRequest
7
7
private List < object > args = new List < object > ( ) ; // Check if Readonly
8
8
private bool isWithCursor = false ;
9
9
10
- // Parameters:
11
-
12
- private bool ? verbatim = null ;
13
-
14
- // Load
15
- private List < FieldName > fieldNames = new List < FieldName > ( ) ; // TODO: Check if the new list suposed to be here
16
- private bool ? loadAll = null ;
17
-
18
- private long ? timeout = null ;
19
-
20
- // GroupBy:
21
- private List < Group > groups = new List < Group > ( ) ;
22
-
23
- // SotrBy:
24
- private List < SortedField > sortedFields = new List < SortedField > ( ) ;
25
- private int ? max = null ;
26
-
27
- // Apply:
28
- private List < Tuple < string , string > > apply = new List < Tuple < string , string > > ( ) ;
29
-
30
- // Limit:
31
- private int ? offset = null ;
32
- private int ? num = null ;
33
-
34
- private string ? filter = null ;
35
-
36
- // WithCursor:
37
- private int ? count = null ;
38
- private long ? maxIdle = null ;
39
-
40
- // Params:
41
- private Dictionary < string , object > nameValue = new Dictionary < string , object > ( ) ;
42
10
public int ? dialect { get ; private set ; } = null ;
43
11
44
12
public AggregationRequest ( string query , int ? defaultDialect = null )
@@ -49,67 +17,48 @@ public AggregationRequest(string query, int? defaultDialect = null)
49
17
50
18
public AggregationRequest ( ) : this ( "*" ) { }
51
19
52
- public AggregationRequest Verbatim ( bool verbatim = true )
20
+ public AggregationRequest Verbatim ( )
53
21
{
54
- this . verbatim = true ;
22
+ args . Add ( SearchArgs . VERBATIM ) ;
55
23
return this ;
56
24
}
57
25
58
- private void Verbatim ( )
59
- {
60
- if ( verbatim == true )
61
- args . Add ( "VERBATIM" ) ;
62
- }
63
-
64
26
public AggregationRequest Load ( params FieldName [ ] fields )
65
27
{
66
- this . fieldNames . AddRange ( fields ) ;
67
- return this ;
68
- }
69
-
70
- public AggregationRequest LoadAll ( )
71
- {
72
- loadAll = true ;
73
- return this ;
74
- }
75
-
76
- private void Load ( )
77
- {
78
- if ( loadAll == true )
28
+ if ( fields . Length > 0 )
79
29
{
80
- args . Add ( "LOAD" ) ;
81
- args . Add ( "*" ) ;
82
- return ;
83
- }
84
- else if ( fieldNames . Count > 0 )
85
- {
86
- args . Add ( "LOAD" ) ;
30
+ args . Add ( SearchArgs . LOAD ) ;
87
31
int loadCountIndex = args . Count ;
88
- //args.Add(null);
89
32
int loadCount = 0 ;
90
- foreach ( FieldName fn in fieldNames )
33
+ foreach ( FieldName fn in fields )
91
34
{
92
35
loadCount += fn . AddCommandArguments ( args ) ;
93
36
}
94
37
95
38
args . Insert ( loadCountIndex , loadCount ) ;
96
- // args[loadCountIndex] = loadCount.ToString();
97
39
}
40
+ return this ;
41
+ }
42
+
43
+ public AggregationRequest LoadAll ( )
44
+ {
45
+ args . Add ( SearchArgs . LOAD ) ;
46
+ args . Add ( "*" ) ;
47
+ return this ;
98
48
}
99
49
100
50
public AggregationRequest Timeout ( long timeout )
101
51
{
102
- this . timeout = timeout ;
52
+ args . Add ( SearchArgs . TIMEOUT ) ;
53
+ args . Add ( timeout ) ;
103
54
return this ;
104
55
}
105
56
106
- private void Timeout ( )
57
+
58
+
59
+ public AggregationRequest GroupBy ( string field , params Reducer [ ] reducers )
107
60
{
108
- if ( timeout != null )
109
- {
110
- args . Add ( "TIMEOUT" ) ;
111
- args . Add ( timeout ) ;
112
- }
61
+ return GroupBy ( new string [ ] { field } , reducers ) ;
113
62
}
114
63
115
64
public AggregationRequest GroupBy ( IList < string > fields , IList < Reducer > reducers )
@@ -123,169 +72,93 @@ public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers)
123
72
return this ;
124
73
}
125
74
126
- public AggregationRequest GroupBy ( string field , params Reducer [ ] reducers )
127
- {
128
- return GroupBy ( new string [ ] { field } , reducers ) ;
129
- }
130
-
131
75
public AggregationRequest GroupBy ( Group group )
132
76
{
133
- this . groups . Add ( group ) ;
77
+ args . Add ( SearchArgs . GROUPBY ) ;
78
+ group . SerializeRedisArgs ( args ) ;
134
79
return this ;
135
80
}
136
81
137
- private void GroupBy ( )
138
- {
139
- if ( groups . Count > 0 )
140
- {
141
- args . Add ( "GROUPBY" ) ;
142
- foreach ( Group group in groups )
143
- {
144
- group . SerializeRedisArgs ( args ) ;
145
- }
146
- }
147
- }
148
-
149
82
public AggregationRequest SortBy ( string property ) => SortBy ( SortedField . Asc ( property ) ) ;
150
83
151
- public AggregationRequest SortBy ( params SortedField [ ] fields )
152
- {
153
- this . sortedFields . AddRange ( fields ) ;
154
- return this ;
155
- }
84
+ public AggregationRequest SortBy ( params SortedField [ ] fields ) => SortBy ( - 1 , fields ) ;
156
85
157
- private void SortBy ( )
86
+ public AggregationRequest SortBy ( int max , params SortedField [ ] fields )
158
87
{
159
- if ( sortedFields . Count > 0 )
88
+ args . Add ( SearchArgs . SORTBY ) ;
89
+ args . Add ( fields . Length * 2 ) ;
90
+
91
+ foreach ( SortedField field in fields )
160
92
{
161
- args . Add ( "SORTBY" ) ;
162
- args . Add ( sortedFields . Count * 2 ) ;
163
- foreach ( SortedField field in sortedFields )
164
- {
165
- args . Add ( field . FieldName ) ;
166
- args . Add ( field . Order . ToString ( ) ) ;
167
- }
93
+ args . Add ( field . FieldName ) ;
94
+ args . Add ( field . Order . ToString ( ) ) ;
95
+ }
168
96
169
- if ( max > 0 )
170
- {
171
- args . Add ( "MAX" ) ;
172
- args . Add ( max ) ;
173
- }
97
+ if ( max > 0 )
98
+ {
99
+ args . Add ( SearchArgs . MAX ) ;
100
+ args . Add ( max ) ;
174
101
}
175
- }
176
102
177
- public AggregationRequest SortBy ( int max , params SortedField [ ] fields )
178
- {
179
- this . max = max ;
180
- SortBy ( fields ) ;
181
103
return this ;
182
104
}
183
105
184
106
public AggregationRequest Apply ( string projection , string alias )
185
107
{
186
- apply . Add ( new Tuple < string , string > ( projection , alias ) ) ;
108
+ args . Add ( SearchArgs . APPLY ) ;
109
+ args . Add ( projection ) ;
110
+ args . Add ( SearchArgs . AS ) ;
111
+ args . Add ( alias ) ;
187
112
return this ;
188
113
}
189
114
190
- private void Apply ( )
191
- {
192
- if ( apply . Count > 0 )
193
- {
194
- foreach ( Tuple < string , string > tuple in apply )
195
- {
196
- args . Add ( "APPLY" ) ;
197
- args . Add ( tuple . Item1 ) ;
198
- args . Add ( "AS" ) ;
199
- args . Add ( tuple . Item2 ) ;
200
- }
201
- }
202
- }
203
-
204
115
public AggregationRequest Limit ( int count ) => Limit ( 0 , count ) ;
205
116
206
117
public AggregationRequest Limit ( int offset , int count )
207
118
{
208
- this . offset = offset ;
209
- this . num = count ;
210
-
119
+ new Limit ( offset , count ) . SerializeRedisArgs ( args ) ;
211
120
return this ;
212
121
}
213
122
214
- private void Limit ( )
215
- {
216
- if ( offset != null && num != null )
217
- {
218
- new Limit ( offset . Value , num . Value ) . SerializeRedisArgs ( args ) ;
219
- }
220
- }
221
-
222
123
public AggregationRequest Filter ( string filter )
223
124
{
224
- this . filter = filter ;
125
+ args . Add ( SearchArgs . FILTER ) ;
126
+ args . Add ( filter ! ) ;
225
127
return this ;
226
128
}
227
129
228
- private void Filter ( )
229
- {
230
- if ( filter != null )
231
- {
232
- args . Add ( SearchArgs . FILTER ) ;
233
- args . Add ( filter ! ) ;
234
- }
235
-
236
- }
237
-
238
130
public AggregationRequest Cursor ( int ? count = null , long ? maxIdle = null )
239
131
{
240
132
isWithCursor = true ;
241
- if ( count != null )
242
- this . count = count ;
243
- if ( maxIdle != null )
244
- this . maxIdle = maxIdle ;
245
- return this ;
246
- }
133
+ args . Add ( SearchArgs . WITHCURSOR ) ;
247
134
248
- private void Cursor ( )
249
- {
250
- if ( isWithCursor )
135
+ if ( count != null )
251
136
{
252
- args . Add ( "WITHCURSOR" ) ;
253
-
254
- if ( count != null )
255
- {
256
- args . Add ( "COUNT" ) ;
257
- args . Add ( count ) ;
258
- }
259
-
260
- if ( maxIdle != null && maxIdle < long . MaxValue && maxIdle >= 0 )
261
- {
262
- args . Add ( "MAXIDLE" ) ;
263
- args . Add ( maxIdle ) ;
264
- }
137
+ args . Add ( SearchArgs . COUNT ) ;
138
+ args . Add ( count ) ;
265
139
}
266
- }
267
140
268
- public AggregationRequest Params ( Dictionary < string , object > nameValue )
269
- {
270
- foreach ( var entry in nameValue )
141
+ if ( maxIdle != null && maxIdle < long . MaxValue && maxIdle >= 0 )
271
142
{
272
- this . nameValue . Add ( entry . Key , entry . Value ) ;
143
+ args . Add ( SearchArgs . MAXIDLE ) ;
144
+ args . Add ( maxIdle ) ;
273
145
}
274
146
return this ;
275
147
}
276
148
277
- private void Params ( )
149
+ public AggregationRequest Params ( Dictionary < string , object > nameValue )
278
150
{
279
151
if ( nameValue . Count > 0 )
280
152
{
281
- args . Add ( " PARAMS" ) ;
153
+ args . Add ( SearchArgs . PARAMS ) ;
282
154
args . Add ( nameValue . Count * 2 ) ;
283
155
foreach ( var entry in nameValue )
284
156
{
285
157
args . Add ( entry . Key ) ;
286
158
args . Add ( entry . Value ) ;
287
159
}
288
160
}
161
+ return this ;
289
162
}
290
163
291
164
public AggregationRequest Dialect ( int dialect )
@@ -298,7 +171,7 @@ private void Dialect()
298
171
{
299
172
if ( dialect != null )
300
173
{
301
- args . Add ( " DIALECT" ) ;
174
+ args . Add ( SearchArgs . DIALECT ) ;
302
175
args . Add ( dialect ) ;
303
176
}
304
177
}
@@ -310,29 +183,9 @@ public List<object> GetArgs()
310
183
311
184
public void SerializeRedisArgs ( )
312
185
{
313
- Verbatim ( ) ;
314
- Load ( ) ;
315
- Timeout ( ) ;
316
- Apply ( ) ;
317
- GroupBy ( ) ;
318
- SortBy ( ) ;
319
- Limit ( ) ;
320
- Filter ( ) ;
321
- Cursor ( ) ;
322
- Params ( ) ;
323
186
Dialect ( ) ;
324
187
}
325
188
326
- // public string getArgsstring()
327
- // {
328
- // StringBuilder sj = new StringBuilder(" ");
329
- // foreach (var s in GetArgs())
330
- // {
331
- // sj.Add(s.ToString());
332
- // }
333
- // return sj.tostring();
334
- // }
335
-
336
189
public bool IsWithCursor ( )
337
190
{
338
191
return isWithCursor ;
0 commit comments