|
| 1 | +using System.ComponentModel; |
1 | 2 | using NRedisStack.Search; |
| 3 | +using NRedisStack.Search.Aggregation; |
2 | 4 | using NRedisStack.Search.DataTypes; |
3 | 5 | using StackExchange.Redis; |
4 | 6 | namespace NRedisStack; |
@@ -40,20 +42,71 @@ public async Task<RedisResult[]> _ListAsync() |
40 | 42 | return (await _db.ExecuteAsync(SearchCommandBuilder._List())).ToArray(); |
41 | 43 | } |
42 | 44 |
|
| 45 | + internal static IServer? GetRandomServerForCluster(IDatabaseAsync db, out int? database) |
| 46 | + { |
| 47 | + var server = db.Multiplexer.GetServer(key: default(RedisKey)); |
| 48 | + // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract |
| 49 | + if (server is null || server.ServerType != ServerType.Cluster) |
| 50 | + { |
| 51 | + database = null; |
| 52 | + return null; |
| 53 | + } |
| 54 | + // This is vexingly misplaced, but: it doesn't actually matter for cluster |
| 55 | + database = db is IDatabase nonAsync ? nonAsync.Database : null; |
| 56 | + return server; |
| 57 | + } |
| 58 | + |
43 | 59 | /// <inheritdoc/> |
44 | 60 | public async Task<AggregationResult> AggregateAsync(string index, AggregationRequest query) |
45 | 61 | { |
46 | 62 | SetDefaultDialectIfUnset(query); |
47 | | - var result = await _db.ExecuteAsync(SearchCommandBuilder.Aggregate(index, query)); |
| 63 | + IServer? server = null; |
| 64 | + int? database = null; |
| 65 | + |
| 66 | + var command = SearchCommandBuilder.Aggregate(index, query); |
48 | 67 | if (query.IsWithCursor()) |
49 | 68 | { |
50 | | - var results = (RedisResult[])result!; |
| 69 | + // we can issue this anywhere, but follow-up calls need to be on the same server |
| 70 | + server = GetRandomServerForCluster(_db, out database); |
| 71 | + } |
51 | 72 |
|
52 | | - return new(results[0], (long)results[1]); |
| 73 | + RedisResult result; |
| 74 | + if (server is not null) |
| 75 | + { |
| 76 | + result = await server.ExecuteAsync(database, command); |
53 | 77 | } |
54 | 78 | else |
55 | 79 | { |
56 | | - return new(result); |
| 80 | + result = await _db.ExecuteAsync(command); |
| 81 | + } |
| 82 | + |
| 83 | + return result.ToAggregationResult(index, query, server, database); |
| 84 | + } |
| 85 | + |
| 86 | + public async IAsyncEnumerable<Row> AggregateEnumerableAsync(string index, AggregationRequest query) |
| 87 | + { |
| 88 | + if (!query.IsWithCursor()) query.Cursor(); |
| 89 | + |
| 90 | + var result = await AggregateAsync(index, query); |
| 91 | + try |
| 92 | + { |
| 93 | + while (true) |
| 94 | + { |
| 95 | + var count = checked((int)result.TotalResults); |
| 96 | + for (int i = 0; i < count; i++) |
| 97 | + { |
| 98 | + yield return result.GetRow(i); |
| 99 | + } |
| 100 | + if (result.CursorId == 0) break; |
| 101 | + result = await CursorReadAsync(result, query.Count); |
| 102 | + } |
| 103 | + } |
| 104 | + finally |
| 105 | + { |
| 106 | + if (result.CursorId != 0) |
| 107 | + { |
| 108 | + await CursorDelAsync(result); |
| 109 | + } |
57 | 110 | } |
58 | 111 | } |
59 | 112 |
|
@@ -108,18 +161,52 @@ public async Task<bool> CreateAsync(string indexName, Schema schema) |
108 | 161 | } |
109 | 162 |
|
110 | 163 | /// <inheritdoc/> |
| 164 | + [Obsolete("When possible, use CursorDelAsync(AggregationResult, int?) instead.")] |
| 165 | + [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] |
111 | 166 | public async Task<bool> CursorDelAsync(string indexName, long cursorId) |
112 | 167 | { |
113 | 168 | return (await _db.ExecuteAsync(SearchCommandBuilder.CursorDel(indexName, cursorId))).OKtoBoolean(); |
114 | 169 | } |
115 | 170 |
|
| 171 | + public async Task<bool> CursorDelAsync(AggregationResult result) |
| 172 | + { |
| 173 | + if (result is not AggregationResult.WithCursorAggregationResult withCursor) |
| 174 | + { |
| 175 | + throw new ArgumentException( |
| 176 | + message: $"{nameof(CursorDelAsync)} must be called with a value returned from a previous call to {nameof(AggregateAsync)} with a cursor.", |
| 177 | + paramName: nameof(result)); |
| 178 | + } |
| 179 | + |
| 180 | + var command = SearchCommandBuilder.CursorDel(withCursor.IndexName, withCursor.CursorId); |
| 181 | + var pending = withCursor.Server is { } server |
| 182 | + ? server.ExecuteAsync(withCursor.Database, command) |
| 183 | + : _db.ExecuteAsync(command); |
| 184 | + return (await pending).OKtoBoolean(); |
| 185 | + } |
| 186 | + |
116 | 187 | /// <inheritdoc/> |
| 188 | + [Obsolete("When possible, use CursorReadAsync(AggregationResult, int?) instead.")] |
| 189 | + [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] |
117 | 190 | public async Task<AggregationResult> CursorReadAsync(string indexName, long cursorId, int? count = null) |
118 | 191 | { |
119 | 192 | var resp = (await _db.ExecuteAsync(SearchCommandBuilder.CursorRead(indexName, cursorId, count))).ToArray(); |
120 | 193 | return new(resp[0], (long)resp[1]); |
121 | 194 | } |
122 | 195 |
|
| 196 | + public async Task<AggregationResult> CursorReadAsync(AggregationResult result, int? count = null) |
| 197 | + { |
| 198 | + if (result is not AggregationResult.WithCursorAggregationResult withCursor) |
| 199 | + { |
| 200 | + throw new ArgumentException(message: $"{nameof(CursorReadAsync)} must be called with a value returned from a previous call to {nameof(AggregateAsync)} with a cursor.", paramName: nameof(result)); |
| 201 | + } |
| 202 | + var command = SearchCommandBuilder.CursorRead(withCursor.IndexName, withCursor.CursorId, count); |
| 203 | + var pending = withCursor.Server is { } server |
| 204 | + ? server.ExecuteAsync(withCursor.Database, command) |
| 205 | + : _db.ExecuteAsync(command); |
| 206 | + var resp = (await pending).ToArray(); |
| 207 | + return new AggregationResult.WithCursorAggregationResult(withCursor.IndexName, resp[0], (long)resp[1], withCursor.Server, withCursor.Database); |
| 208 | + } |
| 209 | + |
123 | 210 | /// <inheritdoc/> |
124 | 211 | public async Task<long> DictAddAsync(string dict, params string[] terms) |
125 | 212 | { |
|
0 commit comments