-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from tombatron/issue-25
Expanded support for "read only" queries. Closes #25
- Loading branch information
Showing
7 changed files
with
239 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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,85 @@ | ||
using System.Collections.Generic; | ||
using StackExchange.Redis; | ||
using Xunit; | ||
|
||
namespace NRedisGraph.Tests.Issues; | ||
|
||
public class Number25 : BaseTest | ||
{ | ||
private ConnectionMultiplexer _readWriteMultiplexer; | ||
private ConnectionMultiplexer _readonlyMultiplexer; | ||
|
||
private RedisGraph _readonlyApi; | ||
private RedisGraph _readWriteApi; | ||
|
||
protected override void BeforeTest() | ||
{ | ||
// Setup the multiplexers. | ||
_readonlyMultiplexer = ConnectionMultiplexer.Connect("tombaserver.local:10000,tombaserver.local:10001"); | ||
_readWriteMultiplexer = ConnectionMultiplexer.Connect("tombaserver.local:10000,tombaserver.local:10001"); | ||
|
||
// Setup RedisGraph API... | ||
_readonlyApi = new RedisGraph(_readonlyMultiplexer.GetDatabase(0)); | ||
_readWriteApi = new RedisGraph(_readWriteMultiplexer.GetDatabase(0)); | ||
|
||
// Setup the data. | ||
_readWriteApi.GraphQuery("issue25", "CREATE (:Person {username: 'John', age: 20})"); | ||
_readWriteApi.GraphQuery("issue25", "CREATE (:Person {username: 'Peter', age: 23})"); | ||
_readWriteApi.GraphQuery("issue25", "CREATE (:Person {username: 'Steven', age: 30})"); | ||
} | ||
|
||
protected override void AfterTest() | ||
{ | ||
_readWriteApi.DeleteGraph("issue25"); | ||
_readWriteMultiplexer.Dispose(); | ||
|
||
_readonlyMultiplexer.Dispose(); | ||
} | ||
|
||
[Fact(Skip="Need a read only node...")] | ||
public void IssueReproduction() | ||
{ | ||
// Read from the read-only replica | ||
string readQuery = "MATCH x=(p:Person) RETURN nodes(x) as nodes"; | ||
|
||
ResultSet records = _readonlyApi.GraphReadOnlyQuery("issue25", readQuery); | ||
|
||
var result = new List<Person>(); | ||
|
||
foreach (Record record in records) // Here it throws the exception (On the iteration) | ||
{ | ||
var nodes = record.GetValue<object[]>("nodes"); | ||
|
||
foreach (var node in nodes) | ||
{ | ||
if (node is Node castedNode) | ||
{ | ||
Person person = GetPersonInfo(castedNode); | ||
|
||
result.Add(new Person(person.Name, person.Age)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static Person GetPersonInfo(Node node) | ||
{ | ||
var name = node.PropertyMap["username"]; | ||
var age = node.PropertyMap["age"]; | ||
|
||
return new Person(name.Value.ToString(), (long)age.Value); | ||
} | ||
} | ||
|
||
public class Person | ||
{ | ||
public string Name { get; } | ||
|
||
public long Age { get; } | ||
|
||
public Person(string name, long age) | ||
{ | ||
Name = name; | ||
Age = age; | ||
} | ||
} |
This file contains 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,22 +1,42 @@ | ||
namespace NRedisGraph | ||
{ | ||
internal sealed class GraphCache | ||
internal interface IGraphCache | ||
{ | ||
private readonly GraphCacheList _labels; | ||
private readonly GraphCacheList _propertyNames; | ||
private readonly GraphCacheList _relationshipTypes; | ||
string GetLabel(int index); | ||
string GetRelationshipType(int index); | ||
string GetPropertyName(int index); | ||
} | ||
|
||
internal abstract class BaseGraphCache : IGraphCache | ||
{ | ||
protected GraphCacheList Labels { get; set; } | ||
protected GraphCacheList PropertyNames { get; set; } | ||
protected GraphCacheList RelationshipTypes { get; set; } | ||
|
||
internal GraphCache(string graphId, RedisGraph redisGraph) | ||
{ | ||
_labels = new GraphCacheList(graphId, "db.labels", redisGraph); | ||
_propertyNames = new GraphCacheList(graphId, "db.propertyKeys", redisGraph); | ||
_relationshipTypes = new GraphCacheList(graphId, "db.relationshipTypes", redisGraph); | ||
} | ||
public string GetLabel(int index) => Labels.GetCachedData(index); | ||
|
||
internal string GetLabel(int index) => _labels.GetCachedData(index); | ||
public string GetRelationshipType(int index) => RelationshipTypes.GetCachedData(index); | ||
|
||
internal string GetRelationshipType(int index) => _relationshipTypes.GetCachedData(index); | ||
public string GetPropertyName(int index) => PropertyNames.GetCachedData(index); | ||
} | ||
|
||
internal string GetPropertyName(int index) => _propertyNames.GetCachedData(index); | ||
internal sealed class GraphCache : BaseGraphCache | ||
{ | ||
public GraphCache(string graphId, RedisGraph redisGraph) | ||
{ | ||
Labels = new GraphCacheList(graphId, "db.labels", redisGraph); | ||
PropertyNames = new GraphCacheList(graphId, "db.propertyKeys", redisGraph); | ||
RelationshipTypes = new GraphCacheList(graphId, "db.relationshipTypes", redisGraph); | ||
} | ||
} | ||
|
||
internal sealed class ReadOnlyGraphCache : BaseGraphCache | ||
{ | ||
public ReadOnlyGraphCache(string graphId, RedisGraph redisGraph) | ||
{ | ||
Labels = new ReadOnlyGraphCacheList(graphId, "db.labels", redisGraph); | ||
PropertyNames = new ReadOnlyGraphCacheList(graphId, "db.propertyKeys", redisGraph); | ||
RelationshipTypes = new ReadOnlyGraphCacheList(graphId, "db.relationshipTypes", redisGraph); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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