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

Add SetFromFile and SetFromDirectory Commands for Json Files #40

Merged
merged 9 commits into from
Nov 17, 2022
Merged
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
42 changes: 42 additions & 0 deletions src/NRedisStack/Json/IJsonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ public interface IJsonCommands
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always);

/// <summary>
/// Set json file from the provided file Path.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="path">The path to set within the key.</param>
/// <param name="filePath">The path of the file 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>
bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always);

/// <summary>
/// Set all json files in the provided file Path.
/// </summary>
/// <param name="path">The path to set within the file name as key.</param>
/// <param name="filesPath">The path of the file to set.</param>
/// <param name="when">When to set the value.</param>
/// <returns>The number of files that have been set</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always);

/// <summary>
/// Appends the provided string to the string(s) at the provided path.
/// </summary>
Expand Down Expand Up @@ -451,6 +472,27 @@ public interface IJsonCommands
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always);

/// <summary>
/// Set json file from the provided file Path.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="path">The path to set within the key.</param>
/// <param name="filePath">The path of the file 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>
Task<bool> SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always);

/// <summary>
/// Set all json files in the provided file Path.
/// </summary>
/// <param name="path">The path to set within the file name as key.</param>
/// <param name="filesPath">The path of the file to set.</param>
/// <param name="when">When to set the value.</param>
/// <returns>The number of files that have been set</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
Task<int> SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always);

/// <summary>
/// Appends the provided string to the string(s) at the provided path.
/// </summary>
Expand Down
71 changes: 71 additions & 0 deletions src/NRedisStack/Json/JsonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When
return true;
}

/// <inheritdoc/>
public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always)
{
if(!File.Exists(filePath))
{
throw new FileNotFoundException($"File {filePath} not found.");
}

string fileContent = File.ReadAllText(filePath);
return Set(key, path, fileContent, when);
}

/// <inheritdoc/>
public int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always)
{
int inserted = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks too specific to me.. I don't think we should implement this function

BTW, I think that the users will want to warp all the SetFromFile calls with a MULTI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can agree on a multi. We have this in python - hence wanted to get it into other clients.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will implement support for Transactions and pipelines in the future and then I will change this command

string key;
var files = Directory.EnumerateFiles(filesPath, "*.json");
foreach (var filePath in files)
{
key = filePath.Substring(0, filePath.IndexOf("."));
if(SetFromFile(key, path, filePath, when))
{
inserted++;
}
}

foreach (var dirPath in Directory.EnumerateDirectories(filesPath))
{
inserted += SetFromDirectory(path, dirPath, when);
}

return inserted;
}

/// <inheritdoc/>
public long?[] StrAppend(RedisKey key, string value, string? path = null)
{
Expand Down Expand Up @@ -402,6 +437,42 @@ public async Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json,
return true;
}

/// <inheritdoc/> // TODO: check way asnyc methods dont have documenation
public async Task<bool> SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"File {filePath} not found.");
}

string fileContent = File.ReadAllText(filePath);
return await SetAsync(key, path, fileContent, when);
}

/// <inheritdoc/>
public async Task<int> SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always)
{
int inserted = 0;
string key;
var files = Directory.EnumerateFiles(filesPath, "*.json");
foreach (var filePath in files)
{
key = filePath.Substring(0, filePath.IndexOf("."));
if(await SetFromFileAsync(key, path, filePath, when))
{
inserted++;
}
}

foreach (var dirPath in Directory.EnumerateDirectories(filesPath))
{
inserted += await SetFromDirectoryAsync(path, dirPath, when);
}

return inserted;
}


public async Task<long?[]> StrAppendAsync(RedisKey key, string value, string? path = null)
{
RedisResult result;
Expand Down
Loading