Skip to content

Commit 8eb7bc7

Browse files
committed
more z
1 parent 6483d0a commit 8eb7bc7

File tree

2 files changed

+510
-354
lines changed

2 files changed

+510
-354
lines changed

src/RESPite.StackExchange.Redis/RedisCommands.SortedSetCommands.cs

Lines changed: 175 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ public static partial RespOperation<RedisValue[]> ZDiff(
136136
RedisKey first,
137137
RedisKey second);
138138

139-
[RespCommand("zdiff")]
139+
[RespCommand(nameof(ZDiff))]
140140
public static partial RespOperation<SortedSetEntry[]> ZDiffWithScores(
141141
this in SortedSetCommands context,
142142
[RespSuffix("WITHSCORES")] RedisKey[] keys);
143143

144-
[RespCommand("zdiff")]
144+
[RespCommand(nameof(ZDiff))]
145145
public static partial RespOperation<SortedSetEntry[]> ZDiffWithScores(
146146
this in SortedSetCommands context,
147147
RedisKey first,
@@ -194,14 +194,14 @@ public static partial RespOperation<long> ZInterCard(
194194
RedisKey second,
195195
[RespPrefix("LIMIT"), RespIgnore(0)] long limit = 0);
196196

197-
[RespCommand("zinter")]
197+
[RespCommand(nameof(ZInter))]
198198
public static partial RespOperation<SortedSetEntry[]> ZInterWithScores(
199199
this in SortedSetCommands context,
200200
[RespSuffix("WITHSCORES")] RedisKey[] keys,
201201
[RespPrefix("WEIGHTS")] double[]? weights = null,
202202
[RespPrefix("AGGREGATE")] Aggregate? aggregate = null);
203203

204-
[RespCommand("zinter")]
204+
[RespCommand(nameof(ZInter))]
205205
public static partial RespOperation<SortedSetEntry[]> ZInterWithScores(
206206
this in SortedSetCommands context,
207207
RedisKey first,
@@ -268,25 +268,163 @@ public static partial RespOperation<RedisValue[]> ZRandMember(
268268
RedisKey key,
269269
long count);
270270

271-
[RespCommand]
271+
[RespCommand(nameof(ZRandMember))]
272272
public static partial RespOperation<SortedSetEntry[]> ZRandMemberWithScores(
273273
this in SortedSetCommands context,
274274
RedisKey key,
275-
long count);
275+
[RespSuffix("WITHSCORES")] long count);
276276

277-
[RespCommand]
277+
[RespCommand(Formatter = "ZRangeFormatter.NoScores")]
278278
public static partial RespOperation<RedisValue[]> ZRange(
279279
this in SortedSetCommands context,
280280
RedisKey key,
281281
long start,
282-
long stop);
282+
long stop,
283+
Order order = Order.Ascending,
284+
long offset = 0,
285+
long count = long.MaxValue);
283286

284-
[RespCommand]
287+
[RespCommand(Formatter = "ZRangeFormatter.WithScores")]
285288
public static partial RespOperation<SortedSetEntry[]> ZRangeWithScores(
286289
this in SortedSetCommands context,
287290
RedisKey key,
288291
long start,
289-
long stop);
292+
long stop,
293+
Order order = Order.Ascending,
294+
long offset = 0,
295+
long count = long.MaxValue);
296+
297+
[RespCommand(Formatter = "ZRangeFormatter.NoScores")] // by lex
298+
internal static partial RespOperation<RedisValue[]> ZRange(
299+
this in SortedSetCommands context,
300+
RedisKey key,
301+
BoundedRedisValue start,
302+
BoundedRedisValue stop,
303+
Order order = Order.Ascending,
304+
long offset = 0,
305+
long count = long.MaxValue);
306+
307+
[RespCommand(Formatter = "ZRangeFormatter.WithScores")] // by lex
308+
internal static partial RespOperation<SortedSetEntry[]> ZRangeWithScores(
309+
this in SortedSetCommands context,
310+
RedisKey key,
311+
BoundedRedisValue start,
312+
BoundedRedisValue stop,
313+
Order order = Order.Ascending,
314+
long offset = 0,
315+
long count = long.MaxValue);
316+
317+
[RespCommand(Formatter = "ZRangeFormatter.NoScores")] // by score
318+
internal static partial RespOperation<RedisValue[]> ZRange(
319+
this in SortedSetCommands context,
320+
RedisKey key,
321+
BoundedDouble start,
322+
BoundedDouble stop,
323+
Order order = Order.Ascending,
324+
long offset = 0,
325+
long count = long.MaxValue);
326+
327+
[RespCommand(Formatter = "ZRangeFormatter.WithScores")] // byscore
328+
internal static partial RespOperation<SortedSetEntry[]> ZRangeWithScores(
329+
this in SortedSetCommands context,
330+
RedisKey key,
331+
BoundedDouble start,
332+
BoundedDouble stop,
333+
Order order = Order.Ascending,
334+
long offset = 0,
335+
long count = long.MaxValue);
336+
337+
private sealed class ZRangeFormatter :
338+
IRespFormatter<(RedisKey Key, long Start, long Stop, Order Order, long Offset, long Count)>,
339+
IRespFormatter<(RedisKey Key, BoundedDouble Start, BoundedDouble Stop, Order Order, long Offset, long Count)>,
340+
IRespFormatter<(RedisKey Key, BoundedRedisValue Start, BoundedRedisValue Stop, Order Order, long Offset, long Count)>
341+
{
342+
private readonly bool _withScores;
343+
private ZRangeFormatter(bool withScores) => _withScores = withScores;
344+
public static readonly ZRangeFormatter WithScores = new(true), NoScores = new(false);
345+
public void Format(
346+
scoped ReadOnlySpan<byte> command,
347+
ref RespWriter writer,
348+
in (RedisKey Key, long Start, long Stop, Order Order, long Offset, long Count) request)
349+
{
350+
bool writeLimit = request.Offset != 0 || request.Count != long.MaxValue;
351+
var argCount = 3 + (writeLimit ? 3 : 0) + (_withScores ? 1 : 0) + (request.Order == Order.Descending ? 1 : 0);
352+
writer.WriteCommand(command, argCount);
353+
writer.Write(request.Key);
354+
writer.WriteBulkString(request.Start);
355+
writer.WriteBulkString(request.Stop);
356+
if (request.Order == Order.Descending)
357+
{
358+
writer.WriteRaw("$3\r\nREV\r\n"u8);
359+
}
360+
if (writeLimit)
361+
{
362+
writer.WriteRaw("$5\r\nLIMIT\r\n"u8);
363+
writer.WriteBulkString(request.Offset);
364+
writer.WriteBulkString(request.Count);
365+
}
366+
if (_withScores)
367+
{
368+
writer.WriteRaw("$10\r\nWITHSCORES\r\n"u8);
369+
}
370+
}
371+
372+
public void Format(
373+
scoped ReadOnlySpan<byte> command,
374+
ref RespWriter writer,
375+
in (RedisKey Key, BoundedDouble Start, BoundedDouble Stop, Order Order, long Offset, long Count) request)
376+
{
377+
bool writeLimit = request.Offset != 0 || request.Count != long.MaxValue;
378+
var argCount = 4 + (writeLimit ? 3 : 0) + (_withScores ? 1 : 0) + (request.Order == Order.Descending ? 1 : 0);
379+
writer.WriteCommand(command, argCount);
380+
writer.Write(request.Key);
381+
writer.WriteBulkString(request.Start);
382+
writer.WriteBulkString(request.Stop);
383+
writer.WriteRaw("$7\r\nBYSCORE\r\n"u8);
384+
if (request.Order == Order.Descending)
385+
{
386+
writer.WriteRaw("$3\r\nREV\r\n"u8);
387+
}
388+
if (writeLimit)
389+
{
390+
writer.WriteRaw("$5\r\nLIMIT\r\n"u8);
391+
writer.WriteBulkString(request.Offset);
392+
writer.WriteBulkString(request.Count);
393+
}
394+
if (_withScores)
395+
{
396+
writer.WriteRaw("$10\r\nWITHSCORES\r\n"u8);
397+
}
398+
}
399+
400+
public void Format(
401+
scoped ReadOnlySpan<byte> command,
402+
ref RespWriter writer,
403+
in (RedisKey Key, BoundedRedisValue Start, BoundedRedisValue Stop, Order Order, long Offset, long Count) request)
404+
{
405+
bool writeLimit = request.Offset != 0 || request.Count != long.MaxValue;
406+
var argCount = 4 + (writeLimit ? 3 : 0) + (_withScores ? 1 : 0) + (request.Order == Order.Descending ? 1 : 0);
407+
writer.WriteCommand(command, argCount);
408+
writer.Write(request.Key);
409+
writer.WriteBulkString(request.Start);
410+
writer.WriteBulkString(request.Stop);
411+
writer.WriteRaw("$5\r\nBYLEX\r\n"u8);
412+
if (request.Order == Order.Descending)
413+
{
414+
writer.WriteRaw("$3\r\nREV\r\n"u8);
415+
}
416+
if (writeLimit)
417+
{
418+
writer.WriteRaw("$5\r\nLIMIT\r\n"u8);
419+
writer.WriteBulkString(request.Offset);
420+
writer.WriteBulkString(request.Count);
421+
}
422+
if (_withScores)
423+
{
424+
writer.WriteRaw("$10\r\nWITHSCORES\r\n"u8);
425+
}
426+
}
427+
}
290428

291429
[RespCommand]
292430
public static partial RespOperation<RedisValue[]> ZRangeByLex(
@@ -302,12 +440,12 @@ public static partial RespOperation<RedisValue[]> ZRangeByScore(
302440
BoundedDouble min,
303441
BoundedDouble max);
304442

305-
[RespCommand]
443+
[RespCommand(nameof(ZRangeByScore))]
306444
public static partial RespOperation<SortedSetEntry[]> ZRangeByScoreWithScores(
307445
this in SortedSetCommands context,
308446
RedisKey key,
309447
BoundedDouble min,
310-
BoundedDouble max);
448+
[RespSuffix("WITHSCORES")] BoundedDouble max);
311449

312450
[RespCommand]
313451
public static partial RespOperation<long> ZRangeStore(
@@ -363,12 +501,12 @@ public static partial RespOperation<RedisValue[]> ZRevRange(
363501
long start,
364502
long stop);
365503

366-
[RespCommand]
504+
[RespCommand(nameof(ZRevRange))]
367505
public static partial RespOperation<SortedSetEntry[]> ZRevRangeWithScores(
368506
this in SortedSetCommands context,
369507
RedisKey key,
370508
long start,
371-
long stop);
509+
[RespSuffix("WITHSCORES")] long stop);
372510

373511
[RespCommand]
374512
public static partial RespOperation<RedisValue[]> ZRevRangeByLex(
@@ -384,12 +522,12 @@ public static partial RespOperation<RedisValue[]> ZRevRangeByScore(
384522
BoundedDouble max,
385523
BoundedDouble min);
386524

387-
[RespCommand]
525+
[RespCommand(nameof(ZRevRangeByScore))]
388526
public static partial RespOperation<SortedSetEntry[]> ZRevRangeByScoreWithScores(
389527
this in SortedSetCommands context,
390528
RedisKey key,
391529
BoundedDouble max,
392-
BoundedDouble min);
530+
[RespSuffix("WITHSCORES")] BoundedDouble min);
393531

394532
[RespCommand]
395533
public static partial RespOperation<long?> ZRevRank(
@@ -423,14 +561,14 @@ public static partial RespOperation<RedisValue[]> ZUnion(
423561
RedisKey second,
424562
[RespPrefix("AGGREGATE")] Aggregate? aggregate = null);
425563

426-
[RespCommand("zunion")]
564+
[RespCommand(nameof(ZUnion))]
427565
public static partial RespOperation<SortedSetEntry[]> ZUnionWithScores(
428566
this in SortedSetCommands context,
429567
[RespSuffix("WITHSCORES")] RedisKey[] keys,
430568
[RespPrefix("WEIGHTS")] double[]? weights = null,
431569
[RespPrefix("AGGREGATE")] Aggregate? aggregate = null);
432570

433-
[RespCommand("zunion")]
571+
[RespCommand(nameof(ZUnion))]
434572
public static partial RespOperation<SortedSetEntry[]> ZUnionWithScores(
435573
this in SortedSetCommands context,
436574
RedisKey first,
@@ -539,6 +677,25 @@ internal static RespOperation<long> CombineAndStore(
539677
_ => throw new ArgumentOutOfRangeException(nameof(operation)),
540678
};
541679

680+
public static RespOperation<SortedSetPopResult> ZMPop(
681+
this in SortedSetCommands context,
682+
RedisKey[] keys,
683+
Order order = Order.Ascending,
684+
long count = 1)
685+
=> order == Order.Ascending ? context.ZMPopMin(keys, count) : context.ZMPopMax(keys, count);
686+
687+
[RespCommand(nameof(ZMPop))]
688+
private static partial RespOperation<SortedSetPopResult> ZMPopMin(
689+
this in SortedSetCommands context,
690+
[RespPrefix, RespSuffix("MIN")] RedisKey[] keys,
691+
[RespIgnore(1), RespPrefix("COUNT")] long count);
692+
693+
[RespCommand(nameof(ZMPop))]
694+
private static partial RespOperation<SortedSetPopResult> ZMPopMax(
695+
this in SortedSetCommands context,
696+
[RespPrefix, RespSuffix("MAX")] RedisKey[] keys,
697+
[RespIgnore(1), RespPrefix("COUNT")] long count);
698+
542699
internal static RespOperation<SortedSetEntry?> ZPop(
543700
this in SortedSetCommands context,
544701
RedisKey key,

0 commit comments

Comments
 (0)