Skip to content

Commit

Permalink
Fix AggregationRequest Builder (#259)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shacharPash authored Mar 7, 2024
1 parent 5f8b0b0 commit ee56859
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 262 deletions.
251 changes: 52 additions & 199 deletions src/NRedisStack/Search/AggregationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,6 @@ public class AggregationRequest
private List<object> args = new List<object>(); // Check if Readonly
private bool isWithCursor = false;

// Parameters:

private bool? verbatim = null;

// Load
private List<FieldName> fieldNames = new List<FieldName>(); // TODO: Check if the new list suposed to be here
private bool? loadAll = null;

private long? timeout = null;

// GroupBy:
private List<Group> groups = new List<Group>();

// SotrBy:
private List<SortedField> sortedFields = new List<SortedField>();
private int? max = null;

// Apply:
private List<Tuple<string, string>> apply = new List<Tuple<string, string>>();

// Limit:
private int? offset = null;
private int? num = null;

private string? filter = null;

// WithCursor:
private int? count = null;
private long? maxIdle = null;

// Params:
private Dictionary<string, object> nameValue = new Dictionary<string, object>();
public int? dialect { get; private set; } = null;

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

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

public AggregationRequest Verbatim(bool verbatim = true)
public AggregationRequest Verbatim()
{
this.verbatim = true;
args.Add(SearchArgs.VERBATIM);
return this;
}

private void Verbatim()
{
if (verbatim == true)
args.Add("VERBATIM");
}

public AggregationRequest Load(params FieldName[] fields)
{
this.fieldNames.AddRange(fields);
return this;
}

public AggregationRequest LoadAll()
{
loadAll = true;
return this;
}

private void Load()
{
if (loadAll == true)
if (fields.Length > 0)
{
args.Add("LOAD");
args.Add("*");
return;
}
else if (fieldNames.Count > 0)
{
args.Add("LOAD");
args.Add(SearchArgs.LOAD);
int loadCountIndex = args.Count;
//args.Add(null);
int loadCount = 0;
foreach (FieldName fn in fieldNames)
foreach (FieldName fn in fields)
{
loadCount += fn.AddCommandArguments(args);
}

args.Insert(loadCountIndex, loadCount);
// args[loadCountIndex] = loadCount.ToString();
}
return this;
}

public AggregationRequest LoadAll()
{
args.Add(SearchArgs.LOAD);
args.Add("*");
return this;
}

public AggregationRequest Timeout(long timeout)
{
this.timeout = timeout;
args.Add(SearchArgs.TIMEOUT);
args.Add(timeout);
return this;
}

private void Timeout()


public AggregationRequest GroupBy(string field, params Reducer[] reducers)
{
if (timeout != null)
{
args.Add("TIMEOUT");
args.Add(timeout);
}
return GroupBy(new string[] { field }, reducers);
}

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

public AggregationRequest GroupBy(string field, params Reducer[] reducers)
{
return GroupBy(new string[] { field }, reducers);
}

public AggregationRequest GroupBy(Group group)
{
this.groups.Add(group);
args.Add(SearchArgs.GROUPBY);
group.SerializeRedisArgs(args);
return this;
}

private void GroupBy()
{
if (groups.Count > 0)
{
args.Add("GROUPBY");
foreach (Group group in groups)
{
group.SerializeRedisArgs(args);
}
}
}

public AggregationRequest SortBy(string property) => SortBy(SortedField.Asc(property));

public AggregationRequest SortBy(params SortedField[] fields)
{
this.sortedFields.AddRange(fields);
return this;
}
public AggregationRequest SortBy(params SortedField[] fields) => SortBy(-1, fields);

private void SortBy()
public AggregationRequest SortBy(int max, params SortedField[] fields)
{
if (sortedFields.Count > 0)
args.Add(SearchArgs.SORTBY);
args.Add(fields.Length * 2);

foreach (SortedField field in fields)
{
args.Add("SORTBY");
args.Add(sortedFields.Count * 2);
foreach (SortedField field in sortedFields)
{
args.Add(field.FieldName);
args.Add(field.Order.ToString());
}
args.Add(field.FieldName);
args.Add(field.Order.ToString());
}

if (max > 0)
{
args.Add("MAX");
args.Add(max);
}
if (max > 0)
{
args.Add(SearchArgs.MAX);
args.Add(max);
}
}

public AggregationRequest SortBy(int max, params SortedField[] fields)
{
this.max = max;
SortBy(fields);
return this;
}

public AggregationRequest Apply(string projection, string alias)
{
apply.Add(new Tuple<string, string>(projection, alias));
args.Add(SearchArgs.APPLY);
args.Add(projection);
args.Add(SearchArgs.AS);
args.Add(alias);
return this;
}

private void Apply()
{
if (apply.Count > 0)
{
foreach (Tuple<string, string> tuple in apply)
{
args.Add("APPLY");
args.Add(tuple.Item1);
args.Add("AS");
args.Add(tuple.Item2);
}
}
}

public AggregationRequest Limit(int count) => Limit(0, count);

public AggregationRequest Limit(int offset, int count)
{
this.offset = offset;
this.num = count;

new Limit(offset, count).SerializeRedisArgs(args);
return this;
}

private void Limit()
{
if (offset != null && num != null)
{
new Limit(offset.Value, num.Value).SerializeRedisArgs(args);
}
}

public AggregationRequest Filter(string filter)
{
this.filter = filter;
args.Add(SearchArgs.FILTER);
args.Add(filter!);
return this;
}

private void Filter()
{
if (filter != null)
{
args.Add(SearchArgs.FILTER);
args.Add(filter!);
}

}

public AggregationRequest Cursor(int? count = null, long? maxIdle = null)
{
isWithCursor = true;
if (count != null)
this.count = count;
if (maxIdle != null)
this.maxIdle = maxIdle;
return this;
}
args.Add(SearchArgs.WITHCURSOR);

private void Cursor()
{
if (isWithCursor)
if (count != null)
{
args.Add("WITHCURSOR");

if (count != null)
{
args.Add("COUNT");
args.Add(count);
}

if (maxIdle != null && maxIdle < long.MaxValue && maxIdle >= 0)
{
args.Add("MAXIDLE");
args.Add(maxIdle);
}
args.Add(SearchArgs.COUNT);
args.Add(count);
}
}

public AggregationRequest Params(Dictionary<string, object> nameValue)
{
foreach (var entry in nameValue)
if (maxIdle != null && maxIdle < long.MaxValue && maxIdle >= 0)
{
this.nameValue.Add(entry.Key, entry.Value);
args.Add(SearchArgs.MAXIDLE);
args.Add(maxIdle);
}
return this;
}

private void Params()
public AggregationRequest Params(Dictionary<string, object> nameValue)
{
if (nameValue.Count > 0)
{
args.Add("PARAMS");
args.Add(SearchArgs.PARAMS);
args.Add(nameValue.Count * 2);
foreach (var entry in nameValue)
{
args.Add(entry.Key);
args.Add(entry.Value);
}
}
return this;
}

public AggregationRequest Dialect(int dialect)
Expand All @@ -298,7 +171,7 @@ private void Dialect()
{
if (dialect != null)
{
args.Add("DIALECT");
args.Add(SearchArgs.DIALECT);
args.Add(dialect);
}
}
Expand All @@ -310,29 +183,9 @@ public List<object> GetArgs()

public void SerializeRedisArgs()
{
Verbatim();
Load();
Timeout();
Apply();
GroupBy();
SortBy();
Limit();
Filter();
Cursor();
Params();
Dialect();
}

// public string getArgsstring()
// {
// StringBuilder sj = new StringBuilder(" ");
// foreach (var s in GetArgs())
// {
// sj.Add(s.ToString());
// }
// return sj.tostring();
// }

public bool IsWithCursor()
{
return isWithCursor;
Expand Down
Loading

0 comments on commit ee56859

Please sign in to comment.