Skip to content

Commit be4f29d

Browse files
committed
BF.Insert + Tests
1 parent ada4e34 commit be4f29d

File tree

5 files changed

+83
-26
lines changed

5 files changed

+83
-26
lines changed

src/NRedisStack.Core/Bloom/BloomCommands.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,66 @@ public RedisResult Add(RedisKey key, string item)
1616
return _db.Execute(BF.ADD, key, item);
1717
}
1818

19-
public RedisResult Exists(RedisKey key, string item)
19+
public bool Exists(RedisKey key, string item)
2020
{
21-
return _db.Execute(BF.EXISTS, key, item);
21+
return _db.Execute(BF.EXISTS, key, item).ToString() == "1";
2222
}
2323

2424
public RedisResult Info(RedisKey key)
2525
{
2626
return _db.Execute(BF.INFO, key);
2727
}
2828

29-
public RedisResult Insert(RedisKey key)
29+
public RedisResult Insert(RedisKey key, RedisValue[] items, int? capacity = null,
30+
double? error = null, int? expansion = null,
31+
bool nocreate = false, bool nonscaling = false) //NOT DONE
3032
{
31-
return _db.Execute(BF.INFO, key);
33+
if (items == null)
34+
throw new ArgumentNullException(nameof(items));
35+
36+
List<object> args = new List<object> { key };
37+
38+
if (capacity != null)
39+
{
40+
args.Add(BloomArgs.CAPACITY);
41+
args.Add(capacity);
42+
}
43+
44+
if (error != null)
45+
{
46+
args.Add(BloomArgs.ERROR);
47+
args.Add(error);
48+
}
49+
50+
if (expansion != null)
51+
{
52+
args.Add(BloomArgs.EXPANSION);
53+
args.Add(expansion);
54+
}
55+
56+
if (nocreate)
57+
args.Add(BloomArgs.NOCREATE);
58+
59+
if (nonscaling)
60+
args.Add(BloomArgs.NONSCALING);
61+
62+
args.Add(BloomArgs.ITEMS);
63+
foreach (var item in items)
64+
{
65+
args.Add(item);
66+
}
67+
68+
return _db.Execute(BF.INSERT, args);
3269
}
3370

3471
public RedisResult ScanDump(RedisKey key, int iterator)
3572
{
3673
return _db.Execute(BF.SCANDUMP, key, iterator);
3774
}
3875

39-
public RedisResult LoadChunk(RedisKey key, int iterator, string data)
76+
public RedisResult LoadChunk(RedisKey key, int iterator, RedisValue data)
4077
{
41-
return _db.Execute(BF.INFO, key, iterator, data);
78+
return _db.Execute(BF.LOADCHUNK, key, iterator, data);
4279
}
4380

4481
}

src/NRedisStack.Core/Bloom/Literals/CommandArgs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ namespace NRedisStack.Core.Literals
22
{
33
internal class BloomArgs
44
{
5+
public static string CAPACITY => "CAPACITY";
6+
public static string ERROR => "ERROR";
7+
public static string EXPANSION => "EXPANSION";
8+
public static string NOCREATE => "NOCREATE";
9+
public static string NONSCALING => "NONSCALING";
10+
public static string ITEMS => "ITEMS";
511

612
}
713
}

src/NRedisStack.Core/Json/JsonCommands.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public JsonCommands(IDatabase db)
1616
{
1717
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
1818
};
19-
public RedisResult Set(RedisKey key, string path, object obj, When when = When.Always)
19+
public RedisResult Set(RedisKey key, RedisValue path, object obj, When when = When.Always)
2020
{
2121
string json = JsonSerializer.Serialize(obj);
2222
return Set(key, path, json, when);
2323
}
2424

25-
public RedisResult Set(RedisKey key, string path, string json, When when = When.Always)
25+
public RedisResult Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always)
2626
{
2727
switch (when)
2828
{
@@ -35,33 +35,34 @@ public RedisResult Set(RedisKey key, string path, string json, When when = When.
3535
}
3636
}
3737

38-
public RedisResult Get(RedisKey key, string indent = "",
39-
string newLine = "", string space = "", string path = "")
38+
public RedisResult Get(RedisKey key, RedisValue? indent = null,
39+
RedisValue? newLine = null, RedisValue? space = null, RedisValue? path = null)
4040
{
41-
List<object> subcommands = new List<object>();
42-
subcommands.Add(key);
43-
if (indent != "")
41+
List<object> args = new List<object>();
42+
args.Add(key);
43+
if (indent != null)
4444
{
45-
subcommands.Add(JsonArgs.INDENT);
46-
subcommands.Add(indent);
45+
args.Add(JsonArgs.INDENT);
46+
args.Add(indent);
4747
}
4848

49-
if (newLine != "")
49+
if (newLine != null)
5050
{
51-
subcommands.Add(JsonArgs.NEWLINE);
52-
subcommands.Add(newLine);
51+
args.Add(JsonArgs.NEWLINE);
52+
args.Add(newLine);
5353
}
5454

55-
if (space != "")
55+
if (space != null)
5656
{
57-
subcommands.Add(JsonArgs.SPACE);
58-
subcommands.Add(space);
57+
args.Add(JsonArgs.SPACE);
58+
args.Add(space);
5959
}
6060

61-
if (path != "")
61+
if (path != null)
6262
{
63-
subcommands.Add(path);
63+
args.Add(path);
6464
}
65-
return _db.Execute(JSON.GET, subcommands.ToArray());
65+
66+
return _db.Execute(JSON.GET, args);
6667
}
6768
}

src/NRedisStack.Core/Search/SearchCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public SearchCommands(IDatabase db)
99
{
1010
_db = db;
1111
}
12-
public RedisResult Info(string index)
12+
public RedisResult Info(RedisValue index)
1313
{
1414
return _db.Execute(FT.INFO, index);
1515
}

tests/NRedisStack.Tests/Bloom/BloomTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public void TestBfAddExists()
3232
IDatabase db = redisFixture.Redis.GetDatabase();
3333

3434
db.BF().Add(key, "item1");
35-
Assert.True(db.BF().Exists(key, "item1").ToString() == "1");
35+
Assert.True(db.BF().Exists(key, "item1"));
36+
}
37+
38+
[Fact]
39+
public void TestBfInsert()
40+
{
41+
IDatabase db = redisFixture.Redis.GetDatabase();
42+
RedisValue[] items = new RedisValue[] { "item1" , "item2", "item3"};
43+
44+
db.BF().Insert("key", items);
45+
46+
Assert.True(db.BF().Exists("key", "item1"));
47+
Assert.True(db.BF().Exists("key", "item2"));
48+
Assert.True(db.BF().Exists("key", "item3"));
3649
}
3750
}

0 commit comments

Comments
 (0)