-
Notifications
You must be signed in to change notification settings - Fork 46
Introduce procedure mode selection for RedisGraph. #162
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b8341e
Introduce procedure mode selection for RedisGraph.
haysch 926f578
Document constructor and initialize data array to avoid nulls.
haysch f9b8acb
Minor formatting changes.
haysch 89c039f
Add tests for CallProcedure and CallProcedureAsync
haysch 34857ae
Replace lambda with method call
haysch 08238a8
Trim trailing whitespace
haysch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,74 @@ | ||
namespace NRedisStack.Graph | ||
{ | ||
internal class GraphCacheList | ||
internal sealed class GraphCacheList | ||
{ | ||
protected readonly string GraphName; | ||
protected readonly string Procedure; | ||
private string[] _data; | ||
private readonly string _graphName; | ||
private readonly string _procedure; | ||
|
||
protected readonly GraphCommands graph; | ||
protected readonly GraphCommandsAsync asyncGraph; | ||
private bool asyncGraphUsed; | ||
private string[] _data = Array.Empty<string>(); | ||
|
||
private readonly GraphCommandsAsync _redisGraph; | ||
|
||
private readonly object _locker = new object(); | ||
|
||
internal GraphCacheList(string graphName, string procedure, GraphCommands redisGraph) : this(graphName, procedure) | ||
/// <summary> | ||
/// Constructs a <see cref="GraphCacheList"/> for providing cached information about the graph. | ||
/// </summary> | ||
/// <param name="graphName">The name of the graph to cache information for.</param> | ||
/// <param name="procedure">The saved procedure to call to populate cache. Must be a `read` procedure.</param> | ||
/// <param name="redisGraph">The graph used for the calling the <paramref name="procedure"/>.</param> | ||
internal GraphCacheList(string graphName, string procedure, GraphCommands redisGraph) | ||
{ | ||
graph = redisGraph; | ||
asyncGraphUsed = false; | ||
} | ||
_graphName = graphName; | ||
_procedure = procedure; | ||
|
||
internal GraphCacheList(string graphName, string procedure, GraphCommandsAsync redisGraph) : this(graphName, procedure) | ||
{ | ||
asyncGraph = redisGraph; | ||
asyncGraphUsed = true; | ||
_redisGraph = redisGraph; | ||
} | ||
|
||
private GraphCacheList(string graphName, string procedure) | ||
/// <summary> | ||
/// Constructs a <see cref="GraphCacheList"/> for providing cached information about the graph. | ||
/// </summary> | ||
/// <param name="graphName">The name of the graph to cache information for.</param> | ||
/// <param name="procedure">The saved procedure to call to populate cache. Must be a `read` procedure.</param> | ||
/// <param name="redisGraph">The graph used for the calling the <paramref name="procedure"/>.</param> | ||
internal GraphCacheList(string graphName, string procedure, GraphCommandsAsync redisGraphAsync) | ||
{ | ||
GraphName = graphName; | ||
Procedure = procedure; | ||
_graphName = graphName; | ||
_procedure = procedure; | ||
|
||
_redisGraph = redisGraphAsync; | ||
} | ||
|
||
// TODO: Change this to use Lazy<T>? | ||
internal string GetCachedData(int index) | ||
{ | ||
if (_data == null || index >= _data.Length) | ||
if (index >= _data.Length) | ||
{ | ||
lock (_locker) | ||
{ | ||
if (_data == null || index >= _data.Length) | ||
if (index >= _data.Length) | ||
{ | ||
GetProcedureInfo(); | ||
_data = GetProcedureInfo(); | ||
} | ||
} | ||
} | ||
|
||
return _data.ElementAtOrDefault(index); | ||
} | ||
|
||
private void GetProcedureInfo() | ||
private string[] GetProcedureInfo() | ||
{ | ||
var resultSet = CallProcedure(asyncGraphUsed); | ||
var newData = new string[resultSet.Count]; | ||
var i = 0; | ||
|
||
foreach (var record in resultSet) | ||
{ | ||
newData[i++] = record.GetString(0); | ||
} | ||
|
||
_data = newData; | ||
var resultSet = CallProcedure(); | ||
return resultSet | ||
.Select(r => r.GetString(0)) | ||
.ToArray(); | ||
} | ||
|
||
protected virtual ResultSet CallProcedure(bool asyncGraphUsed = false) | ||
private ResultSet CallProcedure() | ||
{ | ||
return asyncGraphUsed | ||
? asyncGraph.CallProcedureAsync(GraphName, Procedure).Result | ||
: graph.CallProcedure(GraphName, Procedure); | ||
return _redisGraph is GraphCommands graphSync | ||
? graphSync.CallProcedure(_graphName, _procedure, ProcedureMode.Read) | ||
: _redisGraph.CallProcedureAsync(_graphName, _procedure, ProcedureMode.Read).Result; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace NRedisStack.Graph | ||
{ | ||
/// <summary> | ||
/// Defines the mode of a saved procedure. | ||
/// </summary> | ||
public enum ProcedureMode | ||
{ | ||
Read, | ||
Write | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.