Skip to content

Commit ee56859

Browse files
authored
Fix AggregationRequest Builder (#259)
* change aggregationRequest builder * add args to SearchArgs * check issue 230 * use SearchArgs in Schema * comment deletions * fix SortBy * fix accident change * fix Regex mistakes * delete unnessesary definition * deleted a test that was slipped in by mistake
1 parent 5f8b0b0 commit ee56859

File tree

4 files changed

+138
-262
lines changed

4 files changed

+138
-262
lines changed

src/NRedisStack/Search/AggregationRequest.cs

+52-199
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,6 @@ public class AggregationRequest
77
private List<object> args = new List<object>(); // Check if Readonly
88
private bool isWithCursor = false;
99

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>();
4210
public int? dialect { get; private set; } = null;
4311

4412
public AggregationRequest(string query, int? defaultDialect = null)
@@ -49,67 +17,48 @@ public AggregationRequest(string query, int? defaultDialect = null)
4917

5018
public AggregationRequest() : this("*") { }
5119

52-
public AggregationRequest Verbatim(bool verbatim = true)
20+
public AggregationRequest Verbatim()
5321
{
54-
this.verbatim = true;
22+
args.Add(SearchArgs.VERBATIM);
5523
return this;
5624
}
5725

58-
private void Verbatim()
59-
{
60-
if (verbatim == true)
61-
args.Add("VERBATIM");
62-
}
63-
6426
public AggregationRequest Load(params FieldName[] fields)
6527
{
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)
7929
{
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);
8731
int loadCountIndex = args.Count;
88-
//args.Add(null);
8932
int loadCount = 0;
90-
foreach (FieldName fn in fieldNames)
33+
foreach (FieldName fn in fields)
9134
{
9235
loadCount += fn.AddCommandArguments(args);
9336
}
9437

9538
args.Insert(loadCountIndex, loadCount);
96-
// args[loadCountIndex] = loadCount.ToString();
9739
}
40+
return this;
41+
}
42+
43+
public AggregationRequest LoadAll()
44+
{
45+
args.Add(SearchArgs.LOAD);
46+
args.Add("*");
47+
return this;
9848
}
9949

10050
public AggregationRequest Timeout(long timeout)
10151
{
102-
this.timeout = timeout;
52+
args.Add(SearchArgs.TIMEOUT);
53+
args.Add(timeout);
10354
return this;
10455
}
10556

106-
private void Timeout()
57+
58+
59+
public AggregationRequest GroupBy(string field, params Reducer[] reducers)
10760
{
108-
if (timeout != null)
109-
{
110-
args.Add("TIMEOUT");
111-
args.Add(timeout);
112-
}
61+
return GroupBy(new string[] { field }, reducers);
11362
}
11463

11564
public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers)
@@ -123,169 +72,93 @@ public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers)
12372
return this;
12473
}
12574

126-
public AggregationRequest GroupBy(string field, params Reducer[] reducers)
127-
{
128-
return GroupBy(new string[] { field }, reducers);
129-
}
130-
13175
public AggregationRequest GroupBy(Group group)
13276
{
133-
this.groups.Add(group);
77+
args.Add(SearchArgs.GROUPBY);
78+
group.SerializeRedisArgs(args);
13479
return this;
13580
}
13681

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-
14982
public AggregationRequest SortBy(string property) => SortBy(SortedField.Asc(property));
15083

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);
15685

157-
private void SortBy()
86+
public AggregationRequest SortBy(int max, params SortedField[] fields)
15887
{
159-
if (sortedFields.Count > 0)
88+
args.Add(SearchArgs.SORTBY);
89+
args.Add(fields.Length * 2);
90+
91+
foreach (SortedField field in fields)
16092
{
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+
}
16896

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);
174101
}
175-
}
176102

177-
public AggregationRequest SortBy(int max, params SortedField[] fields)
178-
{
179-
this.max = max;
180-
SortBy(fields);
181103
return this;
182104
}
183105

184106
public AggregationRequest Apply(string projection, string alias)
185107
{
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);
187112
return this;
188113
}
189114

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-
204115
public AggregationRequest Limit(int count) => Limit(0, count);
205116

206117
public AggregationRequest Limit(int offset, int count)
207118
{
208-
this.offset = offset;
209-
this.num = count;
210-
119+
new Limit(offset, count).SerializeRedisArgs(args);
211120
return this;
212121
}
213122

214-
private void Limit()
215-
{
216-
if (offset != null && num != null)
217-
{
218-
new Limit(offset.Value, num.Value).SerializeRedisArgs(args);
219-
}
220-
}
221-
222123
public AggregationRequest Filter(string filter)
223124
{
224-
this.filter = filter;
125+
args.Add(SearchArgs.FILTER);
126+
args.Add(filter!);
225127
return this;
226128
}
227129

228-
private void Filter()
229-
{
230-
if (filter != null)
231-
{
232-
args.Add(SearchArgs.FILTER);
233-
args.Add(filter!);
234-
}
235-
236-
}
237-
238130
public AggregationRequest Cursor(int? count = null, long? maxIdle = null)
239131
{
240132
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);
247134

248-
private void Cursor()
249-
{
250-
if (isWithCursor)
135+
if (count != null)
251136
{
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);
265139
}
266-
}
267140

268-
public AggregationRequest Params(Dictionary<string, object> nameValue)
269-
{
270-
foreach (var entry in nameValue)
141+
if (maxIdle != null && maxIdle < long.MaxValue && maxIdle >= 0)
271142
{
272-
this.nameValue.Add(entry.Key, entry.Value);
143+
args.Add(SearchArgs.MAXIDLE);
144+
args.Add(maxIdle);
273145
}
274146
return this;
275147
}
276148

277-
private void Params()
149+
public AggregationRequest Params(Dictionary<string, object> nameValue)
278150
{
279151
if (nameValue.Count > 0)
280152
{
281-
args.Add("PARAMS");
153+
args.Add(SearchArgs.PARAMS);
282154
args.Add(nameValue.Count * 2);
283155
foreach (var entry in nameValue)
284156
{
285157
args.Add(entry.Key);
286158
args.Add(entry.Value);
287159
}
288160
}
161+
return this;
289162
}
290163

291164
public AggregationRequest Dialect(int dialect)
@@ -298,7 +171,7 @@ private void Dialect()
298171
{
299172
if (dialect != null)
300173
{
301-
args.Add("DIALECT");
174+
args.Add(SearchArgs.DIALECT);
302175
args.Add(dialect);
303176
}
304177
}
@@ -310,29 +183,9 @@ public List<object> GetArgs()
310183

311184
public void SerializeRedisArgs()
312185
{
313-
Verbatim();
314-
Load();
315-
Timeout();
316-
Apply();
317-
GroupBy();
318-
SortBy();
319-
Limit();
320-
Filter();
321-
Cursor();
322-
Params();
323186
Dialect();
324187
}
325188

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-
336189
public bool IsWithCursor()
337190
{
338191
return isWithCursor;

0 commit comments

Comments
 (0)