From d5425b554d0c96592a72d0b49ef6ce4e7909b787 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Tue, 10 Jan 2023 14:45:53 +0200 Subject: [PATCH] support BF.CARD + tests --- src/NRedisStack/Bloom/BloomCommandBuilder.cs | 5 +++ src/NRedisStack/Bloom/BloomCommands.cs | 12 +++++++ src/NRedisStack/Bloom/IBloomCommands.cs | 16 +++++++++ src/NRedisStack/Bloom/Literals/Commands.cs | 1 + tests/NRedisStack.Tests/Bloom/BloomTests.cs | 38 ++++++++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/src/NRedisStack/Bloom/BloomCommandBuilder.cs b/src/NRedisStack/Bloom/BloomCommandBuilder.cs index 9f06ed23..117c9f9d 100644 --- a/src/NRedisStack/Bloom/BloomCommandBuilder.cs +++ b/src/NRedisStack/Bloom/BloomCommandBuilder.cs @@ -11,6 +11,11 @@ public static SerializedCommand Add(RedisKey key, RedisValue item) return new SerializedCommand(BF.ADD, key, item); } + public static SerializedCommand Card(RedisKey key) + { + return new SerializedCommand(BF.CARD, key); + } + public static SerializedCommand Exists(RedisKey key, RedisValue item) { return new SerializedCommand(BF.EXISTS, key, item); diff --git a/src/NRedisStack/Bloom/BloomCommands.cs b/src/NRedisStack/Bloom/BloomCommands.cs index c919ad1d..041b1406 100644 --- a/src/NRedisStack/Bloom/BloomCommands.cs +++ b/src/NRedisStack/Bloom/BloomCommands.cs @@ -23,6 +23,18 @@ public async Task AddAsync(RedisKey key, RedisValue item) return (await _db.ExecuteAsync(BloomCommandBuilder.Add(key, item))).ToString() == "1"; } + /// + public long Card(RedisKey key) + { + return _db.Execute(BloomCommandBuilder.Card(key)).ToLong(); + } + + /// + public async Task CardAsync(RedisKey key) + { + return (await _db.ExecuteAsync(BloomCommandBuilder.Card(key))).ToLong(); + } + /// public bool Exists(RedisKey key, RedisValue item) { diff --git a/src/NRedisStack/Bloom/IBloomCommands.cs b/src/NRedisStack/Bloom/IBloomCommands.cs index 5995803a..14f96481 100644 --- a/src/NRedisStack/Bloom/IBloomCommands.cs +++ b/src/NRedisStack/Bloom/IBloomCommands.cs @@ -23,6 +23,22 @@ public interface IBloomCommands /// Task AddAsync(RedisKey key, RedisValue item); + /// + /// Returns the cardinality of a Bloom filter. + /// + /// The name of the filter. + /// number of items that were added to a Bloom filter and detected as unique. + /// + long Card(RedisKey key); + + /// + /// Returns the cardinality of a Bloom filter. + /// + /// The name of the filter. + /// number of items that were added to a Bloom filter and detected as unique. + /// + Task CardAsync(RedisKey key); + /// /// Checks whether an item exist in the Bloom Filter or not. /// diff --git a/src/NRedisStack/Bloom/Literals/Commands.cs b/src/NRedisStack/Bloom/Literals/Commands.cs index 48092b42..43485df4 100644 --- a/src/NRedisStack/Bloom/Literals/Commands.cs +++ b/src/NRedisStack/Bloom/Literals/Commands.cs @@ -3,6 +3,7 @@ internal class BF { public const string ADD = "BF.ADD"; + public const string CARD = "BF.CARD"; public const string EXISTS = "BF.EXISTS"; public const string INFO = "BF.INFO"; public const string INSERT = "BF.INSERT"; diff --git a/tests/NRedisStack.Tests/Bloom/BloomTests.cs b/tests/NRedisStack.Tests/Bloom/BloomTests.cs index 33bd5135..eb878e1b 100644 --- a/tests/NRedisStack.Tests/Bloom/BloomTests.cs +++ b/tests/NRedisStack.Tests/Bloom/BloomTests.cs @@ -331,6 +331,44 @@ public void TestModulePrefixs() Assert.NotEqual(bf1.GetHashCode(), bf2.GetHashCode()); } + [Fact] + public void TestCard() + { + IDatabase db = redisFixture.Redis.GetDatabase(); + db.Execute("FLUSHALL"); + var bf = db.BF(); + + // return 0 if the key does not exist: + Assert.Equal(0, bf.Card("notExist")); + + // Store a filter: + Assert.True(bf.Add("bf1", "item_foo")); + Assert.Equal(1, bf.Card("bf1")); + + // Error when key is of a type other than Bloom filter: + db.StringSet("setKey", "value"); + Assert.Throws(() => bf.Card("setKey")); + } + + [Fact] + public async Task TestCardAsync() + { + IDatabase db = redisFixture.Redis.GetDatabase(); + db.Execute("FLUSHALL"); + var bf = db.BF(); + + // return 0 if the key does not exist: + Assert.Equal(0, await bf.CardAsync("notExist")); + + // Store a filter: + Assert.True(await bf.AddAsync("bf1", "item_foo")); + Assert.Equal(1, await bf.CardAsync("bf1")); + + // Error when key is of a type other than Bloom filter: + db.StringSet("setKey", "value"); + await Assert.ThrowsAsync(() => bf.CardAsync("setKey")); + } + [Fact] public void TestModulePrefixs1() {