-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDredisCommandHandler.Json.cs
More file actions
597 lines (532 loc) · 20.2 KB
/
DredisCommandHandler.Json.cs
File metadata and controls
597 lines (532 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
using System.Collections.Generic;
using System.Linq;
using DotNetty.Buffers;
using DotNetty.Codecs.Redis.Messages;
using DotNetty.Transport.Channels;
using Dredis.Abstractions.Storage;
namespace Dredis
{
/// <summary>
/// JSON command handlers for Dredis.
/// Implements RedisJSON-compatible commands for storing and manipulating JSON documents.
/// </summary>
/// <remarks>
/// <para>
/// This partial class extends <see cref="DredisCommandHandler"/> with JSON document support.
/// All commands use JSONPath syntax for navigating and manipulating JSON structures.
/// </para>
/// <para>
/// Supported commands:
/// <list type="bullet">
/// <item><description><c>JSON.SET key path value</c> - Sets JSON value at the specified path</description></item>
/// <item><description><c>JSON.GET key [path ...]</c> - Gets JSON values from one or more paths (defaults to root "$")</description></item>
/// <item><description><c>JSON.DEL key [path ...]</c> - Deletes JSON values at specified paths</description></item>
/// <item><description><c>JSON.TYPE key [path ...]</c> - Returns JSON type(s) at specified path(s)</description></item>
/// <item><description><c>JSON.STRLEN key [path ...]</c> - Returns string length(s) at specified path(s)</description></item>
/// <item><description><c>JSON.ARRLEN key [path ...]</c> - Returns array length(s) at specified path(s)</description></item>
/// <item><description><c>JSON.ARRAPPEND key path value [value ...]</c> - Appends values to array at path</description></item>
/// <item><description><c>JSON.ARRINDEX key path value</c> - Returns index of value in array, or -1 if not found</description></item>
/// <item><description><c>JSON.ARRINSERT key path index value [value ...]</c> - Inserts values at index in array</description></item>
/// <item><description><c>JSON.ARRREM key path [index]</c> - Removes element at index (or last element if no index)</description></item>
/// <item><description><c>JSON.ARRTRIM key path start stop</c> - Trims array to specified range</description></item>
/// <item><description><c>JSON.MGET key [key ...] path</c> - Gets JSON values from multiple keys at specified path</description></item>
/// </list>
/// </para>
/// <para>
/// JSONPath syntax supports standard path expressions:
/// <list type="bullet">
/// <item><description><c>$</c> - Root element</description></item>
/// <item><description><c>$.property</c> - Object property access</description></item>
/// <item><description><c>$[0]</c> - Array index access</description></item>
/// <item><description><c>$.users[0].name</c> - Nested path traversal</description></item>
/// </list>
/// </para>
/// <para>
/// JSON types returned by JSON.TYPE:
/// <c>object</c>, <c>array</c>, <c>string</c>, <c>number</c>, <c>boolean</c>, <c>null</c>
/// </para>
/// </remarks>
public partial class DredisCommandHandler
{
/// <summary>
/// Handles the JSON.SET command.
/// </summary>
private async Task HandleJsonSetAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 4)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.set' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
if (!TryGetString(args[2], out var path))
{
WriteError(ctx, "ERR null bulk string");
return;
}
if (!TryGetBytes(args[3], out var value))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var result = await _store.JsonSetAsync(key, path, value).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok)
{
WriteSimpleString(ctx, "OK");
Transactions.NotifyKeyModified(key, _store);
}
else if (result.Status == JsonResultStatus.InvalidJson)
{
WriteError(ctx, "ERR invalid JSON");
}
else if (result.Status == JsonResultStatus.InvalidPath)
{
WriteError(ctx, "ERR invalid path");
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.GET command.
/// </summary>
private async Task HandleJsonGetAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 2)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.get' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var paths = new List<string>();
for (int i = 2; i < args.Count; i++)
{
if (TryGetString(args[i], out var path))
{
paths.Add(path);
}
}
if (paths.Count == 0)
{
paths.Add("$");
}
var result = await _store.JsonGetAsync(key, paths.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok)
{
if (result.Value != null)
{
WriteBulkString(ctx, result.Value);
}
else if (result.Values != null)
{
var array = new IRedisMessage[result.Values.Length];
for (int i = 0; i < result.Values.Length; i++)
{
array[i] = new FullBulkStringRedisMessage(Unpooled.WrappedBuffer(result.Values[i]));
}
WriteArray(ctx, array);
}
}
else if (result.Status == JsonResultStatus.PathNotFound)
{
WriteNullBulkString(ctx);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.DEL command.
/// </summary>
private async Task HandleJsonDelAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 2)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.del' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var paths = new List<string>();
for (int i = 2; i < args.Count; i++)
{
if (TryGetString(args[i], out var path))
{
paths.Add(path);
}
}
if (paths.Count == 0)
{
paths.Add("$");
}
var result = await _store.JsonDelAsync(key, paths.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok)
{
WriteInteger(ctx, result.Deleted);
Transactions.NotifyKeyModified(key, _store);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.TYPE command.
/// </summary>
private async Task HandleJsonTypeAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 2)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.type' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var paths = new List<string>();
for (int i = 2; i < args.Count; i++)
{
if (TryGetString(args[i], out var path))
{
paths.Add(path);
}
}
if (paths.Count == 0)
{
paths.Add("$");
}
var result = await _store.JsonTypeAsync(key, paths.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Types != null)
{
if (result.Types.Length == 1)
{
WriteBulkString(ctx, System.Text.Encoding.UTF8.GetBytes(result.Types[0]));
}
else
{
var array = new IRedisMessage[result.Types.Length];
for (int i = 0; i < result.Types.Length; i++)
{
array[i] = new FullBulkStringRedisMessage(Unpooled.WrappedBuffer(System.Text.Encoding.UTF8.GetBytes(result.Types[i])));
}
WriteArray(ctx, array);
}
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.STRLEN command.
/// </summary>
private async Task HandleJsonStrlenAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 2)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.strlen' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var paths = new List<string>();
for (int i = 2; i < args.Count; i++)
{
if (TryGetString(args[i], out var path))
{
paths.Add(path);
}
}
if (paths.Count == 0)
{
paths.Add("$");
}
var result = await _store.JsonStrlenAsync(key, paths.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok)
{
if (result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
}
else if (result.Counts != null)
{
var array = new List<IRedisMessage>();
foreach (var count in result.Counts)
{
array.Add(new IntegerRedisMessage(count));
}
WriteArray(ctx, array);
}
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRLEN command.
/// </summary>
private async Task HandleJsonArrlenAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 2)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrlen' command");
return;
}
if (!TryGetString(args[1], out var key))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var paths = new List<string>();
for (int i = 2; i < args.Count; i++)
{
if (TryGetString(args[i], out var path))
{
paths.Add(path);
}
}
if (paths.Count == 0)
{
paths.Add("$");
}
var result = await _store.JsonArrlenAsync(key, paths.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok)
{
if (result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
}
else if (result.Counts != null)
{
var array = new List<IRedisMessage>();
foreach (var count in result.Counts)
{
array.Add(new IntegerRedisMessage(count));
}
WriteArray(ctx, array);
}
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRAPPEND command.
/// </summary>
private async Task HandleJsonArrappendAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 4)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrappend' command");
return;
}
if (!TryGetString(args[1], out var key) || !TryGetString(args[2], out var path))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var values = new List<byte[]>();
for (int i = 3; i < args.Count; i++)
{
if (TryGetBytes(args[i], out var value))
{
values.Add(value);
}
}
var result = await _store.JsonArrappendAsync(key, path, values.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
Transactions.NotifyKeyModified(key, _store);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRINDEX command.
/// </summary>
private async Task HandleJsonArrindexAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 4)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrindex' command");
return;
}
if (!TryGetString(args[1], out var key) || !TryGetString(args[2], out var path) || !TryGetBytes(args[3], out var value))
{
WriteError(ctx, "ERR null bulk string");
return;
}
var result = await _store.JsonArrindexAsync(key, path, value).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Value != null)
{
WriteBulkString(ctx, result.Value);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRINSERT command.
/// </summary>
private async Task HandleJsonArrinsertAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 5)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrinsert' command");
return;
}
if (!TryGetString(args[1], out var key) || !TryGetString(args[2], out var path) ||
!TryGetString(args[3], out var indexStr) || !long.TryParse(indexStr, out var indexVal))
{
WriteError(ctx, "ERR invalid argument");
return;
}
var values = new List<byte[]>();
for (int i = 4; i < args.Count; i++)
{
if (TryGetBytes(args[i], out var value))
{
values.Add(value);
}
}
var result = await _store.JsonArrinsertAsync(key, path, (int)indexVal, values.ToArray()).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
Transactions.NotifyKeyModified(key, _store);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRREM command.
/// </summary>
private async Task HandleJsonArrremAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 3)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrrem' command");
return;
}
if (!TryGetString(args[1], out var key) || !TryGetString(args[2], out var path))
{
WriteError(ctx, "ERR null bulk string");
return;
}
int? index = null;
if (args.Count >= 4 && TryGetString(args[3], out var indexStr) && long.TryParse(indexStr, out var indexVal))
{
index = (int)indexVal;
}
var result = await _store.JsonArrremAsync(key, path, index).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
Transactions.NotifyKeyModified(key, _store);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.ARRTRIM command.
/// </summary>
private async Task HandleJsonArrtrimAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 5)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.arrtrim' command");
return;
}
if (!TryGetString(args[1], out var key) || !TryGetString(args[2], out var path) ||
!TryGetString(args[3], out var startStr) || !long.TryParse(startStr, out var startVal) ||
!TryGetString(args[4], out var stopStr) || !long.TryParse(stopStr, out var stopVal))
{
WriteError(ctx, "ERR invalid argument");
return;
}
var result = await _store.JsonArrtrimAsync(key, path, (int)startVal, (int)stopVal).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Count.HasValue)
{
WriteInteger(ctx, result.Count.Value);
Transactions.NotifyKeyModified(key, _store);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
/// <summary>
/// Handles the JSON.MGET command.
/// </summary>
private async Task HandleJsonMgetAsync(IChannelHandlerContext ctx, IList<IRedisMessage> args)
{
if (args.Count < 3)
{
WriteError(ctx, "ERR wrong number of arguments for 'json.mget' command");
return;
}
var keys = new List<string>();
string path = "$";
for (int i = 1; i < args.Count - 1; i++)
{
if (TryGetString(args[i], out var key))
{
keys.Add(key);
}
}
if (TryGetString(args[args.Count - 1], out var lastArg))
{
if (lastArg.StartsWith("$") || lastArg.Contains("."))
{
path = lastArg;
}
else
{
keys.Add(lastArg);
}
}
var result = await _store.JsonMgetAsync(keys.ToArray(), path).ConfigureAwait(false);
if (result.Status == JsonResultStatus.Ok && result.Values != null)
{
var array = new IRedisMessage[result.Values.Length];
for (int i = 0; i < result.Values.Length; i++)
{
array[i] = new FullBulkStringRedisMessage(Unpooled.WrappedBuffer(result.Values[i]));
}
WriteArray(ctx, array);
}
else
{
WriteError(ctx, "ERR operation failed");
}
}
}
}