Skip to content

Commit 973da10

Browse files
author
DvirDukhan
authored
Merge pull request #43 from RedisGraph/multi-exec
Multi exec
2 parents 1905350 + a2b6506 commit 973da10

File tree

7 files changed

+211
-179
lines changed

7 files changed

+211
-179
lines changed

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ and
5656
```
5757

5858
# Example: Using the Java Client
59-
59+
## Up to 2.0.0
6060
```java
6161
package com.redislabs.redisgraph;
6262

@@ -80,3 +80,60 @@ public class RedisGraphExample {
8080
}
8181

8282
```
83+
## From 2.0.0
84+
85+
```java
86+
package com.redislabs.redisgraph;
87+
88+
import com.redislabs.redisgraph.graph_entities.Edge;
89+
import com.redislabs.redisgraph.graph_entities.Node;
90+
import com.redislabs.redisgraph.impl.api.RedisGraph;
91+
92+
import java.util.List;
93+
94+
public class RedisGraphExample {
95+
public static void main(String[] args) {
96+
// general context api. Not bound to graph key or connection
97+
RedisGraph graph = new RedisGraph();
98+
99+
// send queries to a specific graph called "social"
100+
graph.query("social","CREATE (:person{name:'roi',age:32})");
101+
graph.query("social","CREATE (:person{name:%s,age:%d})", "amit", 30);
102+
graph.query("social","MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
103+
104+
ResultSet resultSet = graph.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b");
105+
while(resultSet.hasNext()) {
106+
Record record = resultSet.next();
107+
// get values
108+
Node a = record.getValue("a");
109+
Edge r = record.getValue("r");
110+
111+
//print record
112+
System.out.println(record.toString());
113+
}
114+
115+
// delete graph
116+
graph.deleteGraph("social");
117+
118+
// get connection context - closable object
119+
try(RedisGraphContext context = graph.getContext()) {
120+
context.query("contextSocial","CREATE (:person{name:'roi',age:32})");
121+
context.query("contextSocial","CREATE (:person{name:%s,age:%d})", "amit", 30);
122+
context.query("contextSocial", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
123+
// WATCH/MULTI/EXEC
124+
context.watch("contextSocial");
125+
RedisGraphTransaction t = context.multi();
126+
t.query("contextSocial", "MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b");
127+
// support for Redis/Jedis native commands in transaction
128+
t.set("x", "1");
129+
t.get("x");
130+
// get multi/exec results
131+
List<Object> execResults = t.exec();
132+
System.out.println(execResults.toString());
133+
134+
context.deleteGraph("contextSocial");
135+
}
136+
}
137+
}
138+
139+
```

src/main/java/com/redislabs/redisgraph/RedisGraph.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ public interface RedisGraph extends Closeable {
4949
*/
5050
String deleteGraph(String graphId);
5151

52+
@Override
53+
void close();
5254
}

src/main/java/com/redislabs/redisgraph/RedisGraphContexted.java renamed to src/main/java/com/redislabs/redisgraph/RedisGraphContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import redis.clients.jedis.Jedis;
44

5-
public interface RedisGraphContexted extends RedisGraph {
5+
public interface RedisGraphContext extends RedisGraph {
66

77

88
/**
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.redislabs.redisgraph;
22

3-
public interface RedisGraphGeneralContext extends RedisGraph {
3+
public interface RedisGraphContextGenerator extends RedisGraph {
44

55
/**
66
* Generate a connection bounded api
77
* @return a connection bounded api
88
*/
9-
RedisGraphContexted getContextedAPI();
9+
RedisGraphContext getContext();
1010

1111
}

src/main/java/com/redislabs/redisgraph/impl/api/ContextedRedisGraph.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.redislabs.redisgraph.impl.api;
22

3-
import com.redislabs.redisgraph.RedisGraphContexted;
3+
import com.redislabs.redisgraph.RedisGraphContext;
44
import com.redislabs.redisgraph.ResultSet;
55
import com.redislabs.redisgraph.impl.Utils;
66
import com.redislabs.redisgraph.impl.graph_cache.RedisGraphCaches;
@@ -12,10 +12,10 @@
1212
import java.util.List;
1313

1414
/**
15-
* An implementaion of RedisGraphContexted. Allows sending RedisGraph and some Redis commands,
15+
* An implementaion of RedisGraphContext. Allows sending RedisGraph and some Redis commands,
1616
* within a specific connection context
1717
*/
18-
public class ContextedRedisGraph extends AbstractRedisGraph implements RedisGraphContexted, RedisGraphCacheHolder {
18+
public class ContextedRedisGraph extends AbstractRedisGraph implements RedisGraphContext, RedisGraphCacheHolder {
1919

2020
private Jedis connectionContext;
2121
private RedisGraphCaches caches;

src/main/java/com/redislabs/redisgraph/impl/api/RedisGraph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.redislabs.redisgraph.impl.api;
22

3-
import com.redislabs.redisgraph.RedisGraphContexted;
4-
import com.redislabs.redisgraph.RedisGraphGeneralContext;
3+
import com.redislabs.redisgraph.RedisGraphContext;
4+
import com.redislabs.redisgraph.RedisGraphContextGenerator;
55
import com.redislabs.redisgraph.ResultSet;
66
import com.redislabs.redisgraph.impl.graph_cache.RedisGraphCaches;
77
import redis.clients.jedis.Jedis;
@@ -12,7 +12,7 @@
1212
/**
1313
*
1414
*/
15-
public class RedisGraph extends AbstractRedisGraph implements RedisGraphGeneralContext {
15+
public class RedisGraph extends AbstractRedisGraph implements RedisGraphContextGenerator {
1616

1717
private final Pool<Jedis> client;
1818
private RedisGraphCaches caches = new RedisGraphCaches();
@@ -99,7 +99,7 @@ public String deleteGraph(String graphId) {
9999
* @return ContextedRedisGraph
100100
*/
101101
@Override
102-
public RedisGraphContexted getContextedAPI() {
102+
public RedisGraphContext getContext() {
103103
ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection());
104104
contextedRedisGraph.setRedisGraphCaches(this.caches);
105105
return contextedRedisGraph;

0 commit comments

Comments
 (0)