Skip to content

Prefer using exception-throwing methods #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
namespace StackExchange.Redis.Extensions.Core.Helpers;
internal static class ExceptionThrowHelper
{
public static void ThrowIfSpanEmpty<T>(ReadOnlySpan<T> argument, string paramName)
{
if (argument.IsEmpty)
throw new ArgumentException("The argument cannot be empty.", paramName);
}

public static void ThrowIfExistsNullElement<T>(ReadOnlySpan<T> argument, string paramName)
{
if (argument.Any(x => x is null))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Ugo Lattanzi. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -49,7 +48,7 @@ public Task<bool> HashExistsAsync(string hashKey, string key, CommandFlags flag
public async Task<IDictionary<string, T?>> HashGetAsync<T>(string hashKey, string[] keys, CommandFlags flag = CommandFlags.None)
{
#if NET6_0_OR_GREATER
var concurrent = new ConcurrentDictionary<string, T?>();
var concurrent = new System.Collections.Concurrent.ConcurrentDictionary<string, T?>();

await Parallel.ForEachAsync(keys, async (key, _) =>
{
Expand Down Expand Up @@ -92,7 +91,7 @@ await Parallel.ForEachAsync(keys, async (key, _) =>

var dictionary = new Dictionary<string, T?>();

var redisValues = ((RedisValue[]?)data);
var redisValues = (RedisValue[]?)data;

ref var searchSpaceRedisValue = ref MemoryMarshal.GetReference(redisValues.AsSpan());

Expand All @@ -103,12 +102,12 @@ await Parallel.ForEachAsync(keys, async (key, _) =>
{
ref var key = ref Unsafe.Add(ref searchSpaceRedisValue, i);

if (key.HasValue == false)
if (!key.HasValue)
continue;

var redisValue = redisValues[i + 1];

if (redisValue.HasValue == false)
if (!redisValue.HasValue)
continue;

#pragma warning disable CS8604 // Possible null reference argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ public partial class RedisDatabase
/// <inheritdoc/>
public Task<long> ListAddToLeftAsync<T>(string key, T item, When when = When.Always, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(item);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (item == null)
throw new ArgumentNullException(nameof(item), "item cannot be null.");
#endif

var serializedItem = Serializer.Serialize(item);

Expand All @@ -26,11 +31,16 @@ public Task<long> ListAddToLeftAsync<T>(string key, T item, When when = When.Alw
/// <inheritdoc/>
public Task<long> ListAddToLeftAsync<T>(string key, T[] items, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(items);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (items == null)
throw new ArgumentNullException(nameof(items), "item cannot be null.");
#endif

var serializedItems = items.ToFastArray(item => (RedisValue)Serializer.Serialize(item));

Expand All @@ -40,8 +50,12 @@ public Task<long> ListAddToLeftAsync<T>(string key, T[] items, CommandFlags flag
/// <inheritdoc/>
public async Task<T?> ListGetFromRightAsync<T>(string key, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));
#endif

var item = await Database.ListRightPopAsync(key, flag).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public Task<long> PublishAsync<T>(RedisChannel channel, T message, CommandFlags
/// <inheritdoc/>
public Task SubscribeAsync<T>(RedisChannel channel, Func<T?, Task> handler, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(handler);
#else
if (handler == null)
throw new ArgumentNullException(nameof(handler));
#endif

var sub = connectionPoolManager.GetConnection().GetSubscriber();

Expand All @@ -34,8 +38,12 @@ void Handler(RedisChannel redisChannel, RedisValue value) =>
/// <inheritdoc/>
public Task UnsubscribeAsync<T>(RedisChannel channel, Func<T?, Task> handler, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(handler);
#else
if (handler == null)
throw new ArgumentNullException(nameof(handler));
#endif

var sub = connectionPoolManager.GetConnection().GetSubscriber();
return sub.UnsubscribeAsync(channel, (_, value) => handler(Serializer.Deserialize<T>(value)), flag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,16 @@ public async Task<bool> AddAllAsync<T>(Tuple<string, T>[] items, TimeSpan expire
/// <inheritdoc/>
public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(item);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (item == null)
throw new ArgumentNullException(nameof(item), "item cannot be null.");
#endif

var serializedObject = Serializer.Serialize(item);

Expand All @@ -279,8 +284,12 @@ public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = Command
/// <inheritdoc/>
public async Task<T?> SetPopAsync<T>(string key, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));
#endif

var item = await Database.SetPopAsync(key, flag).ConfigureAwait(false);

Expand All @@ -292,8 +301,12 @@ public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = Command
/// <inheritdoc/>
public async Task<IEnumerable<T?>> SetPopAsync<T>(string key, long count, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));
#endif

var items = await Database.SetPopAsync(key, count, flag).ConfigureAwait(false);

Expand All @@ -303,11 +316,16 @@ public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = Command
/// <inheritdoc/>
public Task<bool> SetContainsAsync<T>(string key, T item, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(item);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (item == null)
throw new ArgumentNullException(nameof(item), "item cannot be null.");
#endif

var serializedObject = Serializer.Serialize(item);

Expand All @@ -317,11 +335,16 @@ public Task<bool> SetContainsAsync<T>(string key, T item, CommandFlags flag = Co
/// <inheritdoc/>
public Task<long> SetAddAllAsync<T>(string key, CommandFlags flag = CommandFlags.None, params T[]? items)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(items);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (items == null)
throw new ArgumentNullException(nameof(items), "items cannot be null.");
#endif

ExceptionThrowHelper.ThrowIfExistsNullElement(items, nameof(items));

Expand Down Expand Up @@ -357,11 +380,16 @@ public Task<long> SetAddAllAsync<T>(string key, CommandFlags flag = CommandFlags
/// <inheritdoc/>
public Task<bool> SetRemoveAsync<T>(string key, T item, CommandFlags flag = CommandFlags.None)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(item);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (item == null)
throw new ArgumentNullException(nameof(item), "item cannot be null.");
#endif

var serializedObject = Serializer.Serialize(item);

Expand All @@ -371,11 +399,16 @@ public Task<bool> SetRemoveAsync<T>(string key, T item, CommandFlags flag = Comm
/// <inheritdoc/>
public Task<long> SetRemoveAllAsync<T>(string key, CommandFlags flag = CommandFlags.None, params T[] items)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(key);
ArgumentNullException.ThrowIfNull(items);
#else
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (items == null)
throw new ArgumentNullException(nameof(items), "items cannot be null.");
#endif

ExceptionThrowHelper.ThrowIfExistsNullElement(items, nameof(items));

Expand Down Expand Up @@ -526,6 +559,6 @@ private static InfoDetail[] ParseCategorizedInfo(string info)

info.AsSpan().EnumerateLines(ref data, ref category);

return data.ToArray();
return [.. data];
}
}