Skip to content
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

adding JSON commands #29

Merged
merged 6 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions src/NRedisStack/Auxiliary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@ public static List<object> MergeArgs(RedisKey key, params RedisValue[] items)
foreach (var item in items) args.Add(item);
return args;
}

public static object[] AssembleNonNullArguments(params object?[] arguments)
{
var args = new List<object>();
foreach (var arg in arguments)
{
if (arg != null)
{
args.Add(arg);
}
}

return args.ToArray();
}
}
}
6 changes: 3 additions & 3 deletions src/NRedisStack/Bloom/BloomCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public bool[] Insert(RedisKey key, RedisValue[] items, int? capacity = null,
throw new ArgumentOutOfRangeException(nameof(items));

var args = BloomAux.BuildInsertArgs(key, items, capacity, error, expansion, nocreate, nonscaling);

return _db.Execute(BF.INSERT, args).ToBooleanArray();
}

Expand Down Expand Up @@ -335,7 +335,7 @@ public async Task<bool> ReserveAsync(RedisKey key, double errorRate, long capaci
/// <param name="iterator">Iterator value; either 0 or the iterator from a previous invocation of this command.</param>
/// <returns>Tuple of iterator and data.</returns>
/// <remarks><seealso href="https://redis.io/commands/bf.scandump"/></remarks>
public Tuple<long,Byte[]> ScanDump(RedisKey key, long iterator)
public Tuple<long, Byte[]> ScanDump(RedisKey key, long iterator)
{
return _db.Execute(BF.SCANDUMP, key, iterator).ToScanDumpTuple();
}
Expand All @@ -347,7 +347,7 @@ public Tuple<long,Byte[]> ScanDump(RedisKey key, long iterator)
/// <param name="iterator">Iterator value; either 0 or the iterator from a previous invocation of this command.</param>
/// <returns>Tuple of iterator and data.</returns>
/// <remarks><seealso href="https://redis.io/commands/bf.scandump"/></remarks>
public async Task<Tuple<long,Byte[]>> ScanDumpAsync(RedisKey key, long iterator)
public async Task<Tuple<long, Byte[]>> ScanDumpAsync(RedisKey key, long iterator)
{
var result = await _db.ExecuteAsync(BF.SCANDUMP, key, iterator);
return result.ToScanDumpTuple();
Expand Down
233 changes: 233 additions & 0 deletions src/NRedisStack/Json/IJsonCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using StackExchange.Redis;

namespace NRedisStack;

public interface IJsonCommands
{
/// <summary>
/// Appends the provided items to the array at the provided path.
/// </summary>
/// <param name="key">The key to append to</param>
/// <param name="path">The path to append to</param>
/// <param name="values">the values to append</param>
/// <returns>The new array sizes for the appended paths</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrappend"/></remarks>
public long?[] ArrAppend(RedisKey key, string? path = null, params object[] values);

/// <summary>
/// Finds the index of the provided item within the provided range
/// </summary>
/// <param name="key">The key to look up.</param>
/// <param name="path">The json path.</param>
/// <param name="value">The value to find the index of.</param>
/// <param name="start">The starting index within the array. Inclusive.</param>
/// <param name="stop">The ending index within the array. Exclusive</param>
/// <returns>The index of the value for each array the path resolved to.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrindex"/></remarks>
public long?[] ArrIndex(RedisKey key, string path, object value, long? start = null, long? stop = null);

/// <summary>
/// Inserts the provided items at the provided index within a json array.
/// </summary>
/// <param name="key">The key to insert into.</param>
/// <param name="path">The path of the array(s) within the key to insert into.</param>
/// <param name="index">The index to insert at.</param>
/// <param name="values">The values to insert</param>
/// <returns>The new size of each array the item was inserted into.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrinsert"/></remarks>
public long?[] ArrInsert(RedisKey key, string path, long index, params object[] values);

/// <summary>
/// Gets the length of the arrays resolved by the provided path.
/// </summary>
/// <param name="key">The key of the json object.</param>
/// <param name="path">The path to the array(s)</param>
/// <returns>The length of each array resolved by the json path.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrlen"/></remarks>
public long?[] ArrLen(RedisKey key, string? path = null);

/// <summary>
/// Pops an item from the array(s) at the provided index. Or the last element if no index is provided.
/// </summary>
/// <param name="key">The json key to use.</param>
/// <param name="path">The path of the array(s).</param>
/// <param name="index">The index to pop from</param>
/// <returns>The items popped from the array</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrpop"/></remarks>
public RedisResult[] ArrPop(RedisKey key, string? path = null, long? index = null);

/// <summary>
/// Trims the array(s) at the provided path, leaving the range between the specified indexes (inclusive).
/// </summary>
/// <param name="key">The key to trim from.</param>
/// <param name="path">The path of the array(s) within the json object to trim.</param>
/// <param name="start">the starting index to retain.</param>
/// <param name="stop">The ending index to retain.</param>
/// <returns>The new length of the array(s) after they're trimmed.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.arrtrim"/></remarks>
public long?[] ArrTrim(RedisKey key, string path, long start, long stop);

/// <summary>
/// Clear's container values(arrays/objects), and sets numeric values to 0.
/// </summary>
/// <param name="key">The key to clear.</param>
/// <param name="path">The path to clear.</param>
/// <returns>number of values cleared</returns>
/// <remarks><seealso href="https://redis.io/commands/json.clear"/></remarks>
public long Clear(RedisKey key, string? path = null);

/// <summary>
/// Deletes a json value.
/// </summary>
/// <param name="key">The key to delete from.</param>
/// <param name="path">The path to delete.</param>
/// <returns>number of path's deleted</returns>
/// <remarks><seealso href="https://redis.io/commands/json.del"/></remarks>
public long Del(RedisKey key, string? path = null);

/// <summary>
/// Deletes a json value.
/// </summary>
/// <param name="key">The key to delete from.</param>
/// <param name="path">The path to delete.</param>
/// <returns>number of path's deleted</returns>
/// <remarks><seealso href="https://redis.io/commands/json.forget"/></remarks>
public long Forget(RedisKey key, string? path = null);

/// <summary>
/// Gets the value stored at the key and path in redis.
/// </summary>
/// <param name="key">The key to retrieve.</param>
/// <param name="indent">the indentation string for nested levels</param>
/// <param name="newLine">sets the string that's printed at the end of each line</param>
/// <param name="space">sets the string that's put between a key and a value</param>
/// <param name="path">the path to get.</param>
/// <returns>The requested Items</returns>
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
public RedisResult Get(RedisKey key, RedisValue? indent = null, RedisValue? newLine = null, RedisValue? space = null, RedisValue? path = null);

/// <summary>
/// Generically gets an Item stored in Redis.
/// </summary>
/// <param name="key">The key to retrieve</param>
/// <param name="path">The path to retrieve</param>
/// <typeparam name="T">The type retrieved</typeparam>
/// <returns>The object requested</returns>
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
public T? Get<T>(RedisKey key, string path = "$");

/// <summary>
/// retrieves a group of items stored in redis, appropriate if the path will resolve to multiple records.
/// </summary>
/// <param name="key">The key to pull from.</param>
/// <param name="path">The path to pull.</param>
/// <typeparam name="T">The type.</typeparam>
/// <returns>An enumerable of the requested tyep</returns>
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
public IEnumerable<T?> GetEnumerable<T>(RedisKey key, string path = "$");

/// <summary>
/// Gets the provided path from multiple keys
/// </summary>
/// <param name="keys">The keys to retrieve from.</param>
/// <param name="path">The path to retrieve</param>
/// <returns>An array of RedisResults with the requested data.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.mget"/></remarks>
public RedisResult[] MGet(RedisKey[] keys, string path);

/// <summary>
/// Increments the fields at the provided path by the provided number.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="path">The path to increment.</param>
/// <param name="value">The value to increment by.</param>
/// <returns>The new values after being incremented, or null if the path resolved a non-numeric.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.numincrby"/></remarks>
public double?[] NumIncrby(RedisKey key, string path, double value);

/// <summary>
/// Gets the keys of the object at the provided path.
/// </summary>
/// <param name="key">the key of the json object.</param>
/// <param name="path">The path of the object(s)</param>
/// <returns>the keys of the resolved object(s)</returns>
/// <remarks><seealso href="https://redis.io/commands/json.objkeys"/></remarks>
public IEnumerable<HashSet<string>> ObjKeys(RedisKey key, string? path = null);

/// <summary>
/// returns the number of keys in the object(s) at the provided path.
/// </summary>
/// <param name="key">The key of the json object.</param>
/// <param name="path">The path of the object(s) to resolve.</param>
/// <returns>The length of the object(s) keyspace.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.objlen"/></remarks>
public long?[] ObjLen(RedisKey key, string? path = null);

/// <summary>
/// Gets the key in RESP(Redis Serialization Protocol) form.
/// </summary>
/// <param name="key">The key to get.</param>
/// <param name="path">Path within the key to get.</param>
/// <returns>the resultant resp</returns>
/// <remarks><seealso href="https://redis.io/commands/json.resp"/></remarks>
public RedisResult[] Resp(RedisKey key, string? path = null);

/// <summary>
/// Set's the key/path to the provided value.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="path">The path to set within the key.</param>
/// <param name="obj">The value to set.</param>
/// <param name="when">When to set the value.</param>
/// <returns>The disposition of the command</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
public bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always);

/// <summary>
/// Set's the key/path to the provided value.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="path">The path to set within the key.</param>
/// <param name="json">The value to set.</param>
/// <param name="when">When to set the value.</param>
/// <returns>The disposition of the command</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always);

/// <summary>
/// Appends the provided string to the string(s) at the provided path.
/// </summary>
/// <param name="key">The key to append to.</param>
/// <param name="path">The path of the string(s) to append to.</param>
/// <param name="value">The value to append.</param>
/// <returns>The new length of the string(s) appended to, those lengths will be null if the path did not resolve ot a string.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.strappend"/></remarks>
public long?[] StrAppend(RedisKey key, string value, string? path = null);

/// <summary>
/// Check's the length of the string(s) at the provided path.
/// </summary>
/// <param name="key">The key of the json object.</param>
/// <param name="path">The path of the string(s) within the json object.</param>
/// <returns>The length of the string(s) appended to, those lengths will be null if the path did not resolve ot a string.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.strlen"/></remarks>
public long?[] StrLen(RedisKey key, string? path = null);

/// <summary>
/// Toggles the boolean value(s) at the provided path.
/// </summary>
/// <param name="key">The key of the json object.</param>
/// <param name="path">The path of the value(s) to toggle.</param>
/// <returns>the new value(s). Which will be null if the path did not resolve to a boolean.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.toggle"/></remarks>
public bool?[] Toggle(RedisKey key, string? path = null);

/// <summary>
/// Gets the type(s) of the item(s) at the provided json path.
/// </summary>
/// <param name="key">The key of the JSON object.</param>
/// <param name="path">The path to resolve.</param>
/// <returns>An array of types.</returns>
/// <remarks><seealso href="https://redis.io/commands/json.type"/></remarks>
public JsonType[] Type(RedisKey key, string? path = null);
}
Loading