|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using RESPite.Messages; |
| 3 | +using StackExchange.Redis; |
| 4 | + |
| 5 | +// ReSharper disable MemberCanBePrivate.Global |
| 6 | +// ReSharper disable InconsistentNaming |
| 7 | +namespace RESPite.StackExchange.Redis; |
| 8 | + |
| 9 | +internal static partial class RedisCommands |
| 10 | +{ |
| 11 | + // this is just a "type pun" - it should be an invisible/magic pointer cast to the JIT |
| 12 | + public static ref readonly HashCommands Hashes(this in RespContext context) |
| 13 | + => ref Unsafe.As<RespContext, HashCommands>(ref Unsafe.AsRef(in context)); |
| 14 | +} |
| 15 | + |
| 16 | +public readonly struct HashCommands(in RespContext context) |
| 17 | +{ |
| 18 | + public readonly RespContext Context = context; // important: this is the only field |
| 19 | +} |
| 20 | + |
| 21 | +internal static partial class HashCommandsExtensions |
| 22 | +{ |
| 23 | + [RespCommand] |
| 24 | + public static partial RespOperation<bool> HDel(this in HashCommands context, RedisKey key, RedisValue hashField); |
| 25 | + |
| 26 | + [RespCommand] |
| 27 | + public static partial RespOperation<long> HDel(this in HashCommands context, RedisKey key, RedisValue[] hashFields); |
| 28 | + |
| 29 | + [RespCommand] |
| 30 | + public static partial RespOperation<bool> HExists(this in HashCommands context, RedisKey key, RedisValue hashField); |
| 31 | + |
| 32 | + [RespCommand] |
| 33 | + public static partial RespOperation<RedisValue> HGet(this in HashCommands context, RedisKey key, RedisValue hashField); |
| 34 | + |
| 35 | + [RespCommand] |
| 36 | + public static partial RespOperation<HashEntry[]> HGetAll(this in HashCommands context, RedisKey key); |
| 37 | + |
| 38 | + [RespCommand] |
| 39 | + public static partial RespOperation<long> HIncrBy(this in HashCommands context, RedisKey key, RedisValue hashField, long value = 1); |
| 40 | + |
| 41 | + [RespCommand] |
| 42 | + public static partial RespOperation<double> HIncrByFloat(this in HashCommands context, RedisKey key, RedisValue hashField, double value); |
| 43 | + |
| 44 | + [RespCommand] |
| 45 | + public static partial RespOperation<RedisValue[]> HKeys(this in HashCommands context, RedisKey key); |
| 46 | + |
| 47 | + [RespCommand] |
| 48 | + public static partial RespOperation<long> HLen(this in HashCommands context, RedisKey key); |
| 49 | + |
| 50 | + [RespCommand] |
| 51 | + public static partial RespOperation<RedisValue[]> HMGet(this in HashCommands context, RedisKey key, RedisValue[] hashFields); |
| 52 | + |
| 53 | + [RespCommand] |
| 54 | + public static partial RespOperation<RedisValue> HRandField(this in HashCommands context, RedisKey key); |
| 55 | + |
| 56 | + [RespCommand] |
| 57 | + public static partial RespOperation<RedisValue[]> HRandField(this in HashCommands context, RedisKey key, long count); |
| 58 | + |
| 59 | + [RespCommand] |
| 60 | + public static partial RespOperation<HashEntry[]> HRandFieldWithValues(this in HashCommands context, RedisKey key, [RespSuffix("WITHVALUES")] long count); |
| 61 | + |
| 62 | + [RespCommand] |
| 63 | + public static partial RespOperation<bool> HSet(this in HashCommands context, RedisKey key, RedisValue hashField, RedisValue value); |
| 64 | + |
| 65 | + internal static RespOperation<bool> HSet( |
| 66 | + this in HashCommands context, |
| 67 | + RedisKey key, |
| 68 | + RedisValue hashField, |
| 69 | + RedisValue value, |
| 70 | + When when) |
| 71 | + { |
| 72 | + switch (when) |
| 73 | + { |
| 74 | + case When.Always: |
| 75 | + return HSet(context, key, hashField, value); |
| 76 | + case When.NotExists: |
| 77 | + return HSetNX(context, key, hashField, value); |
| 78 | + default: |
| 79 | + when.AlwaysOrNotExists(); // throws |
| 80 | + return default; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + [RespCommand(Formatter = "HSetFormatter.Instance")] |
| 85 | + public static partial RespOperation HSet(this in HashCommands context, RedisKey key, HashEntry[] hashFields); |
| 86 | + |
| 87 | + private sealed class HSetFormatter : IRespFormatter<(RedisKey Key, HashEntry[] HashFields)> |
| 88 | + { |
| 89 | + private HSetFormatter() { } |
| 90 | + public static readonly HSetFormatter Instance = new(); |
| 91 | + |
| 92 | + public void Format( |
| 93 | + scoped ReadOnlySpan<byte> command, |
| 94 | + ref RespWriter writer, |
| 95 | + in (RedisKey Key, HashEntry[] HashFields) request) |
| 96 | + { |
| 97 | + writer.WriteCommand(command, 1 + (request.HashFields.Length * 2)); |
| 98 | + writer.Write(request.Key); |
| 99 | + foreach (var entry in request.HashFields) |
| 100 | + { |
| 101 | + writer.Write(entry.Name); |
| 102 | + writer.Write(entry.Value); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + [RespCommand] |
| 108 | + public static partial RespOperation<bool> HSetNX(this in HashCommands context, RedisKey key, RedisValue hashField, RedisValue value); |
| 109 | + |
| 110 | + [RespCommand] |
| 111 | + public static partial RespOperation<long> HStrLen(this in HashCommands context, RedisKey key, RedisValue hashField); |
| 112 | + |
| 113 | + [RespCommand] |
| 114 | + public static partial RespOperation<RedisValue[]> HVals(this in HashCommands context, RedisKey key); |
| 115 | +} |
0 commit comments