Skip to content

Commit 4a5ce1f

Browse files
AviAvnishacharPash
andauthored
Review (#28)
* review * Changing the way the function is called * Make ResponeParser mothod to be extension Methods and Changing the way of calling them * Extract common logic to new BloomAux Class * Shorting Bloom Insert Code * Change Literal classes to contain const strings instead of methods that returns the literal string * Add public void TestModulePrefixs() Co-authored-by: shacharPash <[email protected]>
1 parent dacc16a commit 4a5ce1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1971
-1500
lines changed

src/NRedisStack/Auxiliary.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static class Auxiliary
66
{
77
public static List<object> MergeArgs(RedisKey key, params RedisValue[] items)
88
{
9-
var args = new List<object> { key };
9+
var args = new List<object>(items.Length + 1) { key };
1010
foreach (var item in items) args.Add(item);
1111
return args;
1212
}

src/NRedisStack/Bloom/BloomAux.cs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NRedisStack.Literals;
4+
using NRedisStack.Literals.Enums;
5+
using NRedisStack.DataTypes;
6+
using NRedisStack.Extensions;
7+
using StackExchange.Redis;
8+
9+
namespace NRedisStack
10+
{
11+
public static class BloomAux
12+
{
13+
public static List<object> BuildInsertArgs(RedisKey key, RedisValue[] items, int? capacity,
14+
double? error, int? expansion, bool nocreate, bool nonscaling)
15+
{
16+
var args = new List<object> { key };
17+
args.AddCapacity(capacity);
18+
args.AddError(error);
19+
args.AddExpansion(expansion);
20+
args.AddNoCreate(nocreate);
21+
args.AddNoScaling(nonscaling);
22+
args.AddItems(items);
23+
24+
return args;
25+
}
26+
27+
private static void AddItems(this List<object> args, RedisValue[] items)
28+
{
29+
args.Add(BloomArgs.ITEMS);
30+
foreach (var item in items)
31+
{
32+
args.Add(item);
33+
}
34+
}
35+
36+
private static void AddNoScaling(this List<object> args, bool nonscaling)
37+
{
38+
if (nonscaling)
39+
{
40+
args.Add(BloomArgs.NONSCALING);
41+
}
42+
}
43+
44+
private static void AddNoCreate(this List<object> args, bool nocreate)
45+
{
46+
if (nocreate)
47+
{
48+
args.Add(BloomArgs.NOCREATE);
49+
}
50+
}
51+
52+
private static void AddExpansion(this List<object> args, int? expansion)
53+
{
54+
if (expansion != null)
55+
{
56+
args.Add(BloomArgs.EXPANSION);
57+
args.Add(expansion);
58+
}
59+
}
60+
61+
private static void AddError(this List<object> args, double? error)
62+
{
63+
if (error != null)
64+
{
65+
args.Add(BloomArgs.ERROR);
66+
args.Add(error);
67+
}
68+
}
69+
70+
private static void AddCapacity(this List<object> args, int? capacity)
71+
{
72+
if (capacity != null)
73+
{
74+
args.Add(BloomArgs.CAPACITY);
75+
args.Add(capacity);
76+
}
77+
}
78+
}
79+
}

src/NRedisStack/Bloom/BloomCommands.cs

+17-89
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public async Task<bool> ExistsAsync(RedisKey key, RedisValue item)
7272
/// <remarks><seealso href="https://redis.io/commands/bf.info"/></remarks>
7373
public BloomInformation Info(RedisKey key)
7474
{
75-
var info = _db.Execute(BF.INFO, key);
76-
return ResponseParser.ToBloomInfo(info);
75+
return _db.Execute(BF.INFO, key).ToBloomInfo();
7776
}
7877

7978
/// <summary>
@@ -85,7 +84,7 @@ public BloomInformation Info(RedisKey key)
8584
public async Task<BloomInformation> InfoAsync(RedisKey key)
8685
{
8786
var info = await _db.ExecuteAsync(BF.INFO, key);
88-
return ResponseParser.ToBloomInfo(info);
87+
return info.ToBloomInfo();
8988
}
9089

9190
/// <summary>
@@ -111,45 +110,9 @@ public bool[] Insert(RedisKey key, RedisValue[] items, int? capacity = null,
111110
if (items.Length < 1)
112111
throw new ArgumentOutOfRangeException(nameof(items));
113112

114-
List<object> args = new List<object> { key };
115-
116-
if (capacity != null)
117-
{
118-
args.Add(BloomArgs.CAPACITY);
119-
args.Add(capacity);
120-
}
121-
122-
123-
if (error != null)
124-
{
125-
args.Add(BloomArgs.ERROR);
126-
args.Add(error);
127-
}
128-
129-
if (expansion != null)
130-
{
131-
args.Add(BloomArgs.EXPANSION);
132-
args.Add(expansion);
133-
}
134-
135-
if (nocreate)
136-
{
137-
args.Add(BloomArgs.NOCREATE);
138-
139-
}
140-
141-
if (nonscaling)
142-
{
143-
args.Add(BloomArgs.NONSCALING);
144-
}
145-
146-
args.Add(BloomArgs.ITEMS);
147-
foreach (var item in items)
148-
{
149-
args.Add(item);
150-
}
151-
152-
return ResponseParser.ToBooleanArray(_db.Execute(BF.INSERT, args));
113+
var args = BloomAux.BuildInsertArgs(key, items, capacity, error, expansion, nocreate, nonscaling);
114+
115+
return _db.Execute(BF.INSERT, args).ToBooleanArray();
153116
}
154117

155118
/// <summary>
@@ -175,45 +138,10 @@ public async Task<bool[]> InsertAsync(RedisKey key, RedisValue[] items, int? cap
175138
if (items.Length < 1)
176139
throw new ArgumentOutOfRangeException(nameof(items));
177140

178-
List<object> args = new List<object> { key };
179-
180-
if (capacity != null)
181-
{
182-
args.Add(BloomArgs.CAPACITY);
183-
args.Add(capacity);
184-
}
185-
186-
if (error != null)
187-
{
188-
args.Add(BloomArgs.ERROR);
189-
args.Add(error);
190-
}
191-
192-
if (expansion != null)
193-
{
194-
args.Add(BloomArgs.EXPANSION);
195-
args.Add(expansion);
196-
}
197-
198-
if (nocreate)
199-
{
200-
args.Add(BloomArgs.NOCREATE);
201-
202-
}
203-
204-
if (nonscaling)
205-
{
206-
args.Add(BloomArgs.NONSCALING);
207-
}
208-
209-
args.Add(BloomArgs.ITEMS);
210-
foreach (var item in items)
211-
{
212-
args.Add(item);
213-
}
141+
var args = BloomAux.BuildInsertArgs(key, items, capacity, error, expansion, nocreate, nonscaling);
214142

215143
var result = await _db.ExecuteAsync(BF.INSERT, args);
216-
return ResponseParser.ToBooleanArray(result);
144+
return result.ToBooleanArray();
217145
}
218146

219147
/// <summary>
@@ -226,7 +154,7 @@ public async Task<bool[]> InsertAsync(RedisKey key, RedisValue[] items, int? cap
226154
/// <remarks><seealso href="https://redis.io/commands/bf.loadchunk"/></remarks>
227155
public bool LoadChunk(RedisKey key, long iterator, Byte[] data)
228156
{
229-
return ResponseParser.OKtoBoolean(_db.Execute(BF.LOADCHUNK, key, iterator, data));
157+
return _db.Execute(BF.LOADCHUNK, key, iterator, data).OKtoBoolean();
230158
}
231159

232160
/// <summary>
@@ -240,7 +168,7 @@ public bool LoadChunk(RedisKey key, long iterator, Byte[] data)
240168
public async Task<bool> LoadChunkAsync(RedisKey key, long iterator, Byte[] data)
241169
{
242170
var result = await _db.ExecuteAsync(BF.LOADCHUNK, key, iterator, data);
243-
return ResponseParser.OKtoBoolean(result);
171+
return result.OKtoBoolean();
244172
}
245173

246174
/// <summary>
@@ -263,7 +191,7 @@ public bool[] MAdd(RedisKey key, params RedisValue[] items)
263191
args.Add(item);
264192
}
265193

266-
return ResponseParser.ToBooleanArray(_db.Execute(BF.MADD, args));
194+
return _db.Execute(BF.MADD, args).ToBooleanArray();
267195
}
268196

269197
/// <summary>
@@ -287,7 +215,7 @@ public async Task<bool[]> MAddAsync(RedisKey key, params RedisValue[] items)
287215
}
288216

289217
var result = await _db.ExecuteAsync(BF.MADD, args);
290-
return ResponseParser.ToBooleanArray(result);
218+
return result.ToBooleanArray();
291219
}
292220

293221
/// <summary>
@@ -310,7 +238,7 @@ public bool[] MExists(RedisKey key, RedisValue[] items)
310238
args.Add(item);
311239
}
312240

313-
return ResponseParser.ToBooleanArray(_db.Execute(BF.MEXISTS, args));
241+
return _db.Execute(BF.MEXISTS, args).ToBooleanArray();
314242

315243
}
316244

@@ -335,7 +263,7 @@ public async Task<bool[]> MExistsAsync(RedisKey key, RedisValue[] items)
335263
}
336264

337265
var result = await _db.ExecuteAsync(BF.MEXISTS, args);
338-
return ResponseParser.ToBooleanArray(result);
266+
return result.ToBooleanArray();
339267

340268
}
341269

@@ -366,7 +294,7 @@ public bool Reserve(RedisKey key, double errorRate, long capacity,
366294
args.Add(BloomArgs.NONSCALING);
367295
}
368296

369-
return ResponseParser.OKtoBoolean(_db.Execute(BF.RESERVE, args));
297+
return _db.Execute(BF.RESERVE, args).OKtoBoolean();
370298
}
371299

372300
/// <summary>
@@ -397,7 +325,7 @@ public async Task<bool> ReserveAsync(RedisKey key, double errorRate, long capaci
397325
}
398326

399327
var result = await _db.ExecuteAsync(BF.RESERVE, args);
400-
return ResponseParser.OKtoBoolean(result);
328+
return result.OKtoBoolean();
401329
}
402330

403331
/// <summary>
@@ -409,7 +337,7 @@ public async Task<bool> ReserveAsync(RedisKey key, double errorRate, long capaci
409337
/// <remarks><seealso href="https://redis.io/commands/bf.scandump"/></remarks>
410338
public Tuple<long,Byte[]> ScanDump(RedisKey key, long iterator)
411339
{
412-
return ResponseParser.ToScanDumpTuple(_db.Execute(BF.SCANDUMP, key, iterator));
340+
return _db.Execute(BF.SCANDUMP, key, iterator).ToScanDumpTuple();
413341
}
414342

415343
/// <summary>
@@ -422,7 +350,7 @@ public Tuple<long,Byte[]> ScanDump(RedisKey key, long iterator)
422350
public async Task<Tuple<long,Byte[]>> ScanDumpAsync(RedisKey key, long iterator)
423351
{
424352
var result = await _db.ExecuteAsync(BF.SCANDUMP, key, iterator);
425-
return ResponseParser.ToScanDumpTuple(result);
353+
return result.ToScanDumpTuple();
426354
}
427355
}
428356
}

src/NRedisStack/Bloom/Literals/CommandArgs.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ namespace NRedisStack.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";
5+
public const string CAPACITY = "CAPACITY";
6+
public const string ERROR = "ERROR";
7+
public const string EXPANSION = "EXPANSION";
8+
public const string NOCREATE = "NOCREATE";
9+
public const string NONSCALING = "NONSCALING";
10+
public const string ITEMS = "ITEMS";
1111
}
1212
}

src/NRedisStack/Bloom/Literals/Commands.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
{
33
internal class BF
44
{
5-
public static string ADD => "BF.ADD";
6-
public static string EXISTS => "BF.EXISTS";
7-
public static string INFO => "BF.INFO";
8-
public static string INSERT => "BF.INSERT";
9-
public static string LOADCHUNK => "BF.LOADCHUNK";
10-
public static string MADD => "BF.MADD";
11-
public static string MEXISTS => "BF.MEXISTS";
12-
public static string RESERVE => "BF.RESERVE";
13-
public static string SCANDUMP => "BF.SCANDUMP";
5+
public const string ADD = "BF.ADD";
6+
public const string EXISTS = "BF.EXISTS";
7+
public const string INFO = "BF.INFO";
8+
public const string INSERT = "BF.INSERT";
9+
public const string LOADCHUNK = "BF.LOADCHUNK";
10+
public const string MADD = "BF.MADD";
11+
public const string MEXISTS = "BF.MEXISTS";
12+
public const string RESERVE = "BF.RESERVE";
13+
public const string SCANDUMP = "BF.SCANDUMP";
1414
}
1515
}

0 commit comments

Comments
 (0)