|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +// using NRediSearch.Aggregation.Reducers; |
| 5 | +using StackExchange.Redis; |
| 6 | + |
| 7 | +namespace NRedisStack.Search.Aggregation |
| 8 | +{ |
| 9 | + public class AggregationRequest |
| 10 | + { |
| 11 | + private List<object> args = new List<object>(); // Check if Readonly |
| 12 | + private bool isWithCursor = false; |
| 13 | + |
| 14 | + public AggregationRequest(string query) |
| 15 | + { |
| 16 | + args.Add(query); |
| 17 | + } |
| 18 | + |
| 19 | + public AggregationRequest() : this("*") { } |
| 20 | + |
| 21 | + // public AggregationRequest load(params string[] fields) |
| 22 | + // { |
| 23 | + // return load(FieldName.Convert(fields)); |
| 24 | + // } |
| 25 | + |
| 26 | + public AggregationRequest Load(params FieldName[] fields) |
| 27 | + { |
| 28 | + args.Add("LOAD"); |
| 29 | + int loadCountIndex = args.Count(); |
| 30 | + args.Add(null); |
| 31 | + int loadCount = 0; |
| 32 | + foreach (FieldName fn in fields) |
| 33 | + { |
| 34 | + loadCount += fn.AddCommandArguments(args); |
| 35 | + } |
| 36 | + args.Insert(loadCountIndex, loadCount.ToString()); |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + public AggregationRequest LoadAll() |
| 41 | + { |
| 42 | + args.Add("LOAD"); |
| 43 | + args.Add("*"); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public AggregationRequest Limit(int offset, int count) |
| 48 | + { |
| 49 | + Limit limit = new Limit(offset, count); |
| 50 | + limit.SerializeRedisArgs(args); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public AggregationRequest Limit(int count) |
| 55 | + { |
| 56 | + return Limit(0, count); |
| 57 | + } |
| 58 | + |
| 59 | + public AggregationRequest SortBy(params SortedField[] Fields) |
| 60 | + { |
| 61 | + args.Add("SORTBY"); |
| 62 | + args.Add(Fields.Length * 2); |
| 63 | + foreach (SortedField field in Fields) |
| 64 | + { |
| 65 | + args.Add(field.FieldName); |
| 66 | + args.Add(field.Order); |
| 67 | + } |
| 68 | + |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public AggregationRequest SortBy(int max, params SortedField[] Fields) |
| 73 | + { |
| 74 | + SortBy(Fields); |
| 75 | + if (max > 0) |
| 76 | + { |
| 77 | + args.Add("MAX"); |
| 78 | + args.Add(max); |
| 79 | + } |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public AggregationRequest SortByAsc(string field) |
| 84 | + { |
| 85 | + return SortBy(SortedField.Asc(field)); |
| 86 | + } |
| 87 | + |
| 88 | + public AggregationRequest SortByDesc(string field) |
| 89 | + { |
| 90 | + return SortBy(SortedField.Desc(field)); |
| 91 | + } |
| 92 | + |
| 93 | + public AggregationRequest Apply(string projection, string alias) |
| 94 | + { |
| 95 | + args.Add("APPLY"); |
| 96 | + args.Add(projection); |
| 97 | + args.Add("AS"); |
| 98 | + args.Add(alias); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers) |
| 103 | + { |
| 104 | + // string[] fieldsArr = new string[fields.size()]; |
| 105 | + Group g = new Group(fields); |
| 106 | + foreach (Reducer r in reducers) |
| 107 | + { |
| 108 | + g.Reduce(r); |
| 109 | + } |
| 110 | + GroupBy(g); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public AggregationRequest GroupBy(string field, params Reducer[] reducers) |
| 115 | + { |
| 116 | + return GroupBy(new string[] { field }, reducers); |
| 117 | + } |
| 118 | + |
| 119 | + public AggregationRequest GroupBy(Group group) |
| 120 | + { |
| 121 | + args.Add("GROUPBY"); |
| 122 | + group.SerializeRedisArgs(args); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public AggregationRequest Filter(string expression) |
| 127 | + { |
| 128 | + args.Add("FILTER"); |
| 129 | + args.Add(expression); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public AggregationRequest Cursor(int count, long maxIdle) |
| 134 | + { |
| 135 | + isWithCursor = true; |
| 136 | + if (count > 0) |
| 137 | + { |
| 138 | + args.Add("WITHCURSOR"); |
| 139 | + args.Add("COUNT"); |
| 140 | + args.Add(count); |
| 141 | + if (maxIdle < long.MaxValue && maxIdle >= 0) |
| 142 | + { |
| 143 | + args.Add("MAXIDLE"); |
| 144 | + args.Add(maxIdle); |
| 145 | + } |
| 146 | + } |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public AggregationRequest Verbatim() |
| 151 | + { |
| 152 | + args.Add("VERBATIM"); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public AggregationRequest Timeout(long timeout) |
| 157 | + { |
| 158 | + if (timeout >= 0) |
| 159 | + { |
| 160 | + args.Add("TIMEOUT"); |
| 161 | + args.Add(timeout); |
| 162 | + } |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + public AggregationRequest Params(Dictionary<string, object> nameValue) |
| 167 | + { |
| 168 | + if (nameValue.Count >= 1) |
| 169 | + { |
| 170 | + args.Add("PARAMS"); |
| 171 | + args.Add(nameValue.Count * 2); |
| 172 | + foreach (var entry in nameValue) |
| 173 | + { |
| 174 | + args.Add(entry.Key); |
| 175 | + args.Add(entry.Value); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + public AggregationRequest Dialect(int dialect) |
| 183 | + { |
| 184 | + args.Add("DIALECT"); |
| 185 | + args.Add(dialect); |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + public List<object> GetArgs() |
| 190 | + { |
| 191 | + return args; |
| 192 | + } |
| 193 | + |
| 194 | + public void SerializeRedisArgs(List<object> redisArgs) |
| 195 | + { |
| 196 | + foreach (var s in GetArgs()) |
| 197 | + { |
| 198 | + redisArgs.Add(s); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // public string getArgsstring() |
| 203 | + // { |
| 204 | + // StringBuilder sj = new StringBuilder(" "); |
| 205 | + // foreach (var s in GetArgs()) |
| 206 | + // { |
| 207 | + // sj.Add(s.ToString()); |
| 208 | + // } |
| 209 | + // return sj.tostring(); |
| 210 | + // } |
| 211 | + |
| 212 | + public bool IsWithCursor() |
| 213 | + { |
| 214 | + return isWithCursor; |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments