Skip to content

Commit fc40221

Browse files
nimanikoogerzseshacharPash
authored
Enhance code readability (#241)
* Formatting and typos Fix some typos in the markdown files and run `dotnet format` on existing code. * Include documentation in the build Change the settings so that documentation is included in the build. * Add support for BZMPOP (#233) Add support for the BZMPOP command. This command is blocking on the server, so it goes against the current policy of the StackExchange.Redis library. Therefore make it obvious in the code documentation that attention must be given to the timeout in the connection multiplexer. The StackExchange.Redis library already defines a type for the payload returned by BZMPOP (which is the same as for ZMPOP), namely the SortedSetPopResult class. However, the constructor of that class is internal in the library, so we can't create instances of it. Therefore roll our out type for a <value, score> pair, and use Tuple to pair a key with a list of such <value, score> pairs. * Skip new tests for Redis Cluster * Refactor and Change the List Collection to ICollection for better performance and memory usage * Change NameSpace to Scoped * Add Db to xml summary doc * Fix Typo problem * Change constructor to primary constructor based on new feature in .net 8 * Fix Type for previously * refactor and Use Collection Expression for list initializing and change foreach to linq expression * use primary constructor in CmsCommands.cs * use primary constructor in CmsCommands.cs * Refactor and change constructor to primary * Fix Typo and indent in CommandArgs.cs and IJsonCommands.cs * Fix Typo in ExampleTests.cs and IJsonCommandsAsync.cs and JsonCommandsAsync.cs and JsonTests.cs * Use Linq and List Expression * Refactor * Improve the signatures of the new methods Improve the signatures of the new methods, to transparently replicate the structure of the Redis command, i.e. timeout is mandatory and the first argument, order is mandatory. Add a version of the method that works for a single argument, in the spirit of the StackExchange.Redis library. * Add test for BzmPop without keys * Add dedicated enum for MIN/MAX Add a dedicated enum for the MIN and MAX arguments. It feels cleaner than using Order and having to mentally map Ascending to Min and Descending to Max. * Remove primary constructor syntax The primary constructor syntax seems to be not supported by older dotnet versions, so don't use it. Also switch from `class` to `struct`, to better reflect the fact that RedisValueWithScore is just a data storage item. * Separate concurrency groups for GitHub workflows It seems that workflows cancel each other out if they are in the same concurrency group: https://github.com/orgs/community/discussions/41518 Try to separate four workflows that run on PRs into separate concurency groups. Ideally this will avoid the cancelation. They should still cancel runs from previous pushes, each in their own group. * Fix indent in CuckooInformation.cs * RollBack to original version of naming NumIncrbyAsync method in JsonCommandsAsync.cs * Remove use of collection literals Collection literals are not supported by older dotnet versions, it seems, so stop using them for now. * Increase multiplexer timeout in tests Some tests (for the Gears module) seem flaky, they fails sometime with RedisTimeoutException. Try to double the timeout in the connection multiplexer, from 5 to 10 seconds, hopefully this helps. * Try to fix the linter workflow * [Fix] 1.I fixed typo problems in this class at first After that, 2. I used collection expression to better understand the code and avoid additional coding and optimize coding based on c# doc: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions 3.then Remove unused usings 4.Then I commented the places that were not Handled for Try Catch 5.refactor to primary constructor * Use Linq Expression in JsonCommandBuilder.cs for reduce the amount of code in a file and make the code easier to read, and allow different data sources to have similar query expression * RollBack to original naming for NumIncrbyAsync * RollBack to original naming * Refactored C# code using collection expressions and the :? operator for improved clarity and efficiency. * Used Inverted If technique and removed redundant else for code optimization and improved clarity. * Used Invert If technique and removed redundant else for code optimization and improved clarity * Sync Namespace and use scoped namespace and use operator instead if * ConvertMultiBulkToObject for handle multi-bulk based on your todo in AggregationResult.cs * Optimized codebase by removing redundant elements for improved efficiency and maintainability * Sort Modifier and inverting if in Auxiliary.cs ExecuteAsync Method and RedisResult method and change foreach in MergeArgs to linq expression I think methods like "ExecuteBroadcastAsync" can be private * Sync NameSpaces also you can use ITestOutputHelper for output your test results based on xUnit Doc: https://xunit.net/docs/capturing-output * fix CoreCommand file * Fix bug for build Doc Tests and return to orginal version beacuse the old version of .net doesnt support test console for xunit and its failed * fix error * Roleback ' List<object> args = [key];' to previous version becuase the old version of .net does not support the .net 8 syntax * fix compilation error * format * Fix Indent and Solve Problem of old .net version with feature primary constructor in .net 8 * Fix Issues for compiler error in .net 8 error CS8652: The feature 'collection literals' * Fix Compiler Error in JsonCommandsAsync.cs and JsonCommands.cs for Error : error CS8652: The feature 'collection literals' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version * return ExampleTests's ctor * Fix typo for TransactionsTests.cs in method naming TestModulesTransactionWithoutGraph * Fix Typo and namespace also Set Private Access Modifier RedisFixture.cs for readonly strings * Fix Typo and set const for readonly key PipelineTests.cs * Fix Typo and Change Namespace to scoped AbstractNRedisStackTest.cs and Person.cs * Return to Collection Initializer for collections in ExampleTests.cs * Return `ExecuteAsync` method in Auxiliary.cs to original version for sole complexity * Revert ToString() in code snippets Trying to keep code snippets as short as possible, since they get published on redis.io as documentation. Hence reverting the addition of the `ToString()` calls in some places. --------- Co-authored-by: Gabriel Erzse <gabriel.erzse@redis.com> Co-authored-by: shacharPash <shachar.pashchur@redis.com>
1 parent ccd74b5 commit fc40221

37 files changed

+2130
-2128
lines changed

src/NRedisStack/Auxiliary.cs

+96-99
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,130 @@
22
using NRedisStack.RedisStackCommands;
33
using StackExchange.Redis;
44

5-
namespace NRedisStack
5+
namespace NRedisStack;
6+
7+
public static class Auxiliary
68
{
7-
public static class Auxiliary
9+
private static string? _libraryName = $"NRedisStack(.NET_v{Environment.Version})";
10+
private static bool _setInfo = true;
11+
public static void ResetInfoDefaults()
812
{
9-
private static string? _libraryName = $"NRedisStack(.NET_v{Environment.Version})";
10-
private static bool _setInfo = true;
11-
public static void ResetInfoDefaults()
12-
{
13-
_setInfo = true;
14-
_libraryName = $"NRedisStack(.NET_v{Environment.Version})";
15-
}
16-
public static List<object> MergeArgs(RedisKey key, params RedisValue[] items)
17-
{
18-
var args = new List<object>(items.Length + 1) { key };
19-
foreach (var item in items) args.Add(item);
20-
return args;
21-
}
13+
_setInfo = true;
14+
_libraryName = $"NRedisStack(.NET_v{Environment.Version})";
15+
}
16+
public static List<object> MergeArgs(RedisKey key, params RedisValue[] items)
17+
{
18+
var args = new List<object>(items.Length + 1) { key };
19+
args.AddRange(items.Cast<object>());
20+
return args;
21+
}
2222

23-
public static object[] AssembleNonNullArguments(params object?[] arguments)
23+
public static object[] AssembleNonNullArguments(params object?[] arguments)
24+
{
25+
var args = new List<object>();
26+
foreach (var arg in arguments)
2427
{
25-
var args = new List<object>();
26-
foreach (var arg in arguments)
28+
if (arg != null)
2729
{
28-
if (arg != null)
29-
{
30-
args.Add(arg);
31-
}
30+
args.Add(arg);
3231
}
33-
34-
return args.ToArray();
3532
}
3633

37-
// TODO: add all the signatures of GetDatabase
38-
public static IDatabase GetDatabase(this ConnectionMultiplexer redis,
39-
string? LibraryName)
40-
{
41-
var _db = redis.GetDatabase();
42-
if (LibraryName == null) // the user wants to disable the library name and version sending
43-
_setInfo = false;
34+
return args.ToArray();
35+
}
4436

45-
else // the user set his own the library name
46-
_libraryName = $"NRedisStack({LibraryName};.NET_v{Environment.Version})";
37+
// TODO: add all the signatures of GetDatabase
38+
public static IDatabase GetDatabase(this ConnectionMultiplexer redis,
39+
string? LibraryName)
40+
{
41+
var _db = redis.GetDatabase();
42+
if (LibraryName == null) // the user wants to disable the library name and version sending
43+
_setInfo = false;
4744

48-
return _db;
49-
}
45+
else // the user set his own the library name
46+
_libraryName = $"NRedisStack({LibraryName};.NET_v{Environment.Version})";
5047

51-
private static void SetInfoInPipeline(this IDatabase db)
52-
{
53-
if (_libraryName == null) return;
54-
Pipeline pipeline = new Pipeline(db);
55-
_ = pipeline.Db.ClientSetInfoAsync(SetInfoAttr.LibraryName, _libraryName!);
56-
_ = pipeline.Db.ClientSetInfoAsync(SetInfoAttr.LibraryVersion, GetNRedisStackVersion());
57-
pipeline.Execute();
58-
}
48+
return _db;
49+
}
5950

60-
public static RedisResult Execute(this IDatabase db, SerializedCommand command)
61-
{
62-
if (_setInfo)
63-
{
64-
_setInfo = false;
65-
db.SetInfoInPipeline();
66-
}
67-
return db.Execute(command.Command, command.Args);
68-
}
51+
private static void SetInfoInPipeline(this IDatabase db)
52+
{
53+
if (_libraryName == null) return;
54+
Pipeline pipeline = new Pipeline(db);
55+
_ = pipeline.Db.ClientSetInfoAsync(SetInfoAttr.LibraryName, _libraryName!);
56+
_ = pipeline.Db.ClientSetInfoAsync(SetInfoAttr.LibraryVersion, GetNRedisStackVersion());
57+
pipeline.Execute();
58+
}
6959

70-
public async static Task<RedisResult> ExecuteAsync(this IDatabaseAsync db, SerializedCommand command)
60+
public static RedisResult Execute(this IDatabase db, SerializedCommand command)
61+
{
62+
if (!_setInfo) return db.Execute(command.Command, command.Args);
63+
_setInfo = false;
64+
db.SetInfoInPipeline();
65+
return db.Execute(command.Command, command.Args);
66+
}
67+
68+
public static async Task<RedisResult> ExecuteAsync(this IDatabaseAsync db, SerializedCommand command)
69+
{
70+
if (_setInfo)
7171
{
72-
if (_setInfo)
73-
{
74-
_setInfo = false;
75-
((IDatabase)db).SetInfoInPipeline();
76-
}
77-
return await db.ExecuteAsync(command.Command, command.Args);
72+
_setInfo = false;
73+
((IDatabase)db).SetInfoInPipeline();
7874
}
75+
return await db.ExecuteAsync(command.Command, command.Args);
76+
}
77+
78+
public static List<RedisResult> ExecuteBroadcast(this IDatabase db, string command)
79+
=> db.ExecuteBroadcast(new SerializedCommand(command));
7980

80-
public static List<RedisResult> ExecuteBroadcast(this IDatabase db, string command)
81-
=> db.ExecuteBroadcast(new SerializedCommand(command));
81+
public static List<RedisResult> ExecuteBroadcast(this IDatabase db, SerializedCommand command)
82+
{
83+
var redis = db.Multiplexer;
84+
var endpoints = redis.GetEndPoints();
85+
var results = new List<RedisResult>(endpoints.Length);
8286

83-
public static List<RedisResult> ExecuteBroadcast(this IDatabase db, SerializedCommand command)
87+
foreach (var endPoint in endpoints)
8488
{
85-
var redis = db.Multiplexer;
86-
var endpoints = redis.GetEndPoints();
87-
var results = new List<RedisResult>(endpoints.Length);
89+
var server = redis.GetServer(endPoint);
8890

89-
foreach (var endPoint in endpoints)
91+
if (server.IsReplica)
9092
{
91-
var server = redis.GetServer(endPoint);
92-
93-
if (server.IsReplica)
94-
{
95-
continue; // Skip replica nodes
96-
}
97-
// Send your command to the master node
98-
99-
results.Add(server.Multiplexer.GetDatabase().Execute(command));
93+
continue; // Skip replica nodes
10094
}
101-
return results;
95+
// Send your command to the master node
96+
97+
results.Add(server.Multiplexer.GetDatabase().Execute(command));
10298
}
99+
return results;
100+
}
101+
102+
public static async Task<List<RedisResult>> ExecuteBroadcastAsync(this IDatabaseAsync db, string command)
103+
=> await db.ExecuteBroadcastAsync(new SerializedCommand(command));
103104

104-
public async static Task<List<RedisResult>> ExecuteBroadcastAsync(this IDatabaseAsync db, string command)
105-
=> await db.ExecuteBroadcastAsync(new SerializedCommand(command));
105+
private static async Task<List<RedisResult>> ExecuteBroadcastAsync(this IDatabaseAsync db, SerializedCommand command)
106+
{
107+
var redis = db.Multiplexer;
108+
var endpoints = redis.GetEndPoints();
109+
var results = new List<RedisResult>(endpoints.Length);
106110

107-
public async static Task<List<RedisResult>> ExecuteBroadcastAsync(this IDatabaseAsync db, SerializedCommand command)
111+
foreach (var endPoint in endpoints)
108112
{
109-
var redis = db.Multiplexer;
110-
var endpoints = redis.GetEndPoints();
111-
var results = new List<RedisResult>(endpoints.Length);
113+
var server = redis.GetServer(endPoint);
112114

113-
foreach (var endPoint in endpoints)
115+
if (server.IsReplica)
114116
{
115-
var server = redis.GetServer(endPoint);
116-
117-
if (server.IsReplica)
118-
{
119-
continue; // Skip replica nodes
120-
}
121-
// Send your command to the master node
122-
123-
results.Add(await server.Multiplexer.GetDatabase().ExecuteAsync(command));
117+
continue; // Skip replica nodes
124118
}
125-
return results;
126-
}
119+
// Send your command to the master node
127120

128-
public static string GetNRedisStackVersion()
129-
{
130-
Version version = typeof(Auxiliary).Assembly.GetName().Version!;
131-
return $"{version.Major}.{version.Minor}.{version.Build}";
121+
results.Add(await server.Multiplexer.GetDatabase().ExecuteAsync(command));
132122
}
123+
return results;
124+
}
125+
126+
public static string GetNRedisStackVersion()
127+
{
128+
Version version = typeof(Auxiliary).Assembly.GetName().Version!;
129+
return $"{version.Major}.{version.Minor}.{version.Build}";
133130
}
134-
}
131+
}

src/NRedisStack/Bloom/BloomAux.cs

+47-57
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,64 @@
11
using NRedisStack.Bloom.Literals;
22
using StackExchange.Redis;
33

4-
namespace NRedisStack
4+
namespace NRedisStack;
5+
6+
public static class BloomAux
57
{
6-
public static class BloomAux
8+
public static List<object> BuildInsertArgs(RedisKey key, IEnumerable<RedisValue> items, int? capacity,
9+
double? error, int? expansion, bool nocreate, bool nonscaling)
710
{
8-
public static List<object> BuildInsertArgs(RedisKey key, RedisValue[] items, int? capacity,
9-
double? error, int? expansion, bool nocreate, bool nonscaling)
10-
{
11-
var args = new List<object> { key };
12-
args.AddCapacity(capacity);
13-
args.AddError(error);
14-
args.AddExpansion(expansion);
15-
args.AddNoCreate(nocreate);
16-
args.AddNoScaling(nonscaling);
17-
args.AddItems(items);
11+
var args = new List<object> { key };
12+
args.AddCapacity(capacity);
13+
args.AddError(error);
14+
args.AddExpansion(expansion);
15+
args.AddNoCreate(nocreate);
16+
args.AddNoScaling(nonscaling);
17+
args.AddItems(items);
1818

19-
return args;
20-
}
19+
return args;
20+
}
2121

22-
private static void AddItems(this List<object> args, RedisValue[] items)
23-
{
24-
args.Add(BloomArgs.ITEMS);
25-
foreach (var item in items)
26-
{
27-
args.Add(item);
28-
}
29-
}
22+
private static void AddItems(this List<object> args, IEnumerable<RedisValue> items)
23+
{
24+
args.Add(BloomArgs.ITEMS);
25+
args.AddRange(items.Cast<object>());
26+
}
3027

31-
private static void AddNoScaling(this List<object> args, bool nonscaling)
28+
private static void AddNoScaling(this ICollection<object> args, bool nonScaling)
29+
{
30+
if (nonScaling)
3231
{
33-
if (nonscaling)
34-
{
35-
args.Add(BloomArgs.NONSCALING);
36-
}
32+
args.Add(BloomArgs.NONSCALING);
3733
}
34+
}
3835

39-
private static void AddNoCreate(this List<object> args, bool nocreate)
36+
private static void AddNoCreate(this ICollection<object> args, bool nocreate)
37+
{
38+
if (nocreate)
4039
{
41-
if (nocreate)
42-
{
43-
args.Add(BloomArgs.NOCREATE);
44-
}
40+
args.Add(BloomArgs.NOCREATE);
4541
}
42+
}
4643

47-
private static void AddExpansion(this List<object> args, int? expansion)
48-
{
49-
if (expansion != null)
50-
{
51-
args.Add(BloomArgs.EXPANSION);
52-
args.Add(expansion);
53-
}
54-
}
44+
private static void AddExpansion(this ICollection<object> args, int? expansion)
45+
{
46+
if (expansion == null) return;
47+
args.Add(BloomArgs.EXPANSION);
48+
args.Add(expansion);
49+
}
5550

56-
private static void AddError(this List<object> args, double? error)
57-
{
58-
if (error != null)
59-
{
60-
args.Add(BloomArgs.ERROR);
61-
args.Add(error);
62-
}
63-
}
51+
private static void AddError(this ICollection<object> args, double? error)
52+
{
53+
if (error == null) return;
54+
args.Add(BloomArgs.ERROR);
55+
args.Add(error);
56+
}
6457

65-
private static void AddCapacity(this List<object> args, int? capacity)
66-
{
67-
if (capacity != null)
68-
{
69-
args.Add(BloomArgs.CAPACITY);
70-
args.Add(capacity);
71-
}
72-
}
58+
private static void AddCapacity(this ICollection<object> args, int? capacity)
59+
{
60+
if (capacity == null) return;
61+
args.Add(BloomArgs.CAPACITY);
62+
args.Add(capacity);
7363
}
74-
}
64+
}

0 commit comments

Comments
 (0)