Skip to content

Commit 630d6af

Browse files
committed
hyperloglog
1 parent b711666 commit 630d6af

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Runtime.CompilerServices;
2+
using StackExchange.Redis;
3+
4+
// ReSharper disable MemberCanBePrivate.Global
5+
// ReSharper disable InconsistentNaming
6+
namespace RESPite.StackExchange.Redis;
7+
8+
internal static partial class RedisCommands
9+
{
10+
// this is just a "type pun" - it should be an invisible/magic pointer cast to the JIT
11+
public static ref readonly HyperLogLogCommands HyperLogLogs(this in RespContext context)
12+
=> ref Unsafe.As<RespContext, HyperLogLogCommands>(ref Unsafe.AsRef(in context));
13+
}
14+
15+
public readonly struct HyperLogLogCommands(in RespContext context)
16+
{
17+
public readonly RespContext Context = context; // important: this is the only field
18+
}
19+
20+
internal static partial class HyperLogLogCommandsExtensions
21+
{
22+
[RespCommand]
23+
public static partial RespOperation<bool> PfAdd(this in HyperLogLogCommands context, RedisKey key, RedisValue value);
24+
25+
[RespCommand]
26+
public static partial RespOperation<bool> PfAdd(this in HyperLogLogCommands context, RedisKey key, RedisValue[] values);
27+
28+
[RespCommand]
29+
public static partial RespOperation<long> PfCount(this in HyperLogLogCommands context, RedisKey key);
30+
31+
[RespCommand]
32+
public static partial RespOperation<long> PfCount(this in HyperLogLogCommands context, RedisKey[] keys);
33+
34+
[RespCommand]
35+
public static partial RespOperation PfMerge(this in HyperLogLogCommands context, RedisKey destination, RedisKey first, RedisKey second);
36+
37+
[RespCommand]
38+
public static partial RespOperation PfMerge(this in HyperLogLogCommands context, RedisKey destination, RedisKey[] sourceKeys);
39+
}
Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,79 @@
1-
using StackExchange.Redis;
1+
using RESPite.Messages;
2+
using StackExchange.Redis;
23

34
namespace RESPite.StackExchange.Redis;
45

56
internal partial class RespContextDatabase
67
{
78
// Async HyperLogLog methods
8-
public Task<bool> HyperLogLogAddAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None) =>
9-
throw new NotImplementedException();
9+
public Task<bool> HyperLogLogAddAsync(
10+
RedisKey key,
11+
RedisValue value,
12+
CommandFlags flags = CommandFlags.None) =>
13+
Context(flags).HyperLogLogs().PfAdd(key, value).AsTask();
1014

11-
public Task<bool> HyperLogLogAddAsync(RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None) =>
12-
throw new NotImplementedException();
15+
public Task<bool> HyperLogLogAddAsync(
16+
RedisKey key,
17+
RedisValue[] values,
18+
CommandFlags flags = CommandFlags.None) =>
19+
Context(flags).HyperLogLogs().PfAdd(key, values).AsTask();
1320

14-
public Task<long> HyperLogLogLengthAsync(RedisKey key, CommandFlags flags = CommandFlags.None) =>
15-
throw new NotImplementedException();
21+
public Task<long> HyperLogLogLengthAsync(
22+
RedisKey key,
23+
CommandFlags flags = CommandFlags.None) =>
24+
Context(flags).HyperLogLogs().PfCount(key).AsTask();
1625

17-
public Task<long> HyperLogLogLengthAsync(RedisKey[] keys, CommandFlags flags = CommandFlags.None) =>
18-
throw new NotImplementedException();
26+
public Task<long> HyperLogLogLengthAsync(
27+
RedisKey[] keys,
28+
CommandFlags flags = CommandFlags.None) =>
29+
Context(flags).HyperLogLogs().PfCount(keys).AsTask();
1930

2031
public Task HyperLogLogMergeAsync(
2132
RedisKey destination,
2233
RedisKey first,
2334
RedisKey second,
2435
CommandFlags flags = CommandFlags.None) =>
25-
throw new NotImplementedException();
36+
Context(flags).HyperLogLogs().PfMerge(destination, first, second).AsTask();
2637

2738
public Task HyperLogLogMergeAsync(
2839
RedisKey destination,
2940
RedisKey[] sourceKeys,
3041
CommandFlags flags = CommandFlags.None) =>
31-
throw new NotImplementedException();
42+
Context(flags).HyperLogLogs().PfMerge(destination, sourceKeys).AsTask();
3243

3344
// Synchronous HyperLogLog methods
34-
public bool HyperLogLogAdd(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None) =>
35-
throw new NotImplementedException();
45+
public bool HyperLogLogAdd(
46+
RedisKey key,
47+
RedisValue value,
48+
CommandFlags flags = CommandFlags.None) =>
49+
Context(flags).HyperLogLogs().PfAdd(key, value).Wait(SyncTimeout);
3650

37-
public bool HyperLogLogAdd(RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None) =>
38-
throw new NotImplementedException();
51+
public bool HyperLogLogAdd(
52+
RedisKey key,
53+
RedisValue[] values,
54+
CommandFlags flags = CommandFlags.None) =>
55+
Context(flags).HyperLogLogs().PfAdd(key, values).Wait(SyncTimeout);
3956

40-
public long HyperLogLogLength(RedisKey key, CommandFlags flags = CommandFlags.None) =>
41-
throw new NotImplementedException();
57+
public long HyperLogLogLength(
58+
RedisKey key,
59+
CommandFlags flags = CommandFlags.None) =>
60+
Context(flags).HyperLogLogs().PfCount(key).Wait(SyncTimeout);
4261

43-
public long HyperLogLogLength(RedisKey[] keys, CommandFlags flags = CommandFlags.None) =>
44-
throw new NotImplementedException();
62+
public long HyperLogLogLength(
63+
RedisKey[] keys,
64+
CommandFlags flags = CommandFlags.None) =>
65+
Context(flags).HyperLogLogs().PfCount(keys).Wait(SyncTimeout);
4566

4667
public void HyperLogLogMerge(
4768
RedisKey destination,
4869
RedisKey first,
4970
RedisKey second,
5071
CommandFlags flags = CommandFlags.None) =>
51-
throw new NotImplementedException();
72+
Context(flags).HyperLogLogs().PfMerge(destination, first, second).Wait(SyncTimeout);
5273

5374
public void HyperLogLogMerge(
5475
RedisKey destination,
5576
RedisKey[] sourceKeys,
5677
CommandFlags flags = CommandFlags.None) =>
57-
throw new NotImplementedException();
78+
Context(flags).HyperLogLogs().PfMerge(destination, sourceKeys).Wait(SyncTimeout);
5879
}

0 commit comments

Comments
 (0)