Skip to content

Commit a1e5d2b

Browse files
committed
Add support for create index
1 parent ea729bd commit a1e5d2b

File tree

4 files changed

+97
-63
lines changed

4 files changed

+97
-63
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface Statistics {
88
*/
99
enum Label{
1010
LABELS_ADDED("Labels added"),
11+
INDICES_ADDED("Indices added"),
1112
NODES_CREATED("Nodes created"),
1213
NODES_DELETED("Nodes deleted"),
1314
RELATIONSHIPS_DELETED("Relationships deleted"),
@@ -36,7 +37,7 @@ public static Label getEnum(String value) {
3637
for(Label v : values()) {
3738
if(v.toString().equalsIgnoreCase(value)) return v;
3839
}
39-
throw new IllegalArgumentException();
40+
return null;
4041
}
4142
}
4243

@@ -47,11 +48,13 @@ public static Label getEnum(String value) {
4748
* @return a String representation of the specific statistic or null
4849
*/
4950
String getStringValue(Statistics.Label label);
50-
51+
5152
int nodesCreated();
5253

5354
int nodesDeleted();
5455

56+
int indicesAdded();
57+
5558
int labelsAdded();
5659

5760
int relationshipsDeleted();

src/main/java/com/redislabs/redisgraph/impl/ResultSetImpl.java

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,63 @@
1212
import redis.clients.jedis.util.SafeEncoder;
1313

1414
public class ResultSetImpl implements ResultSet{
15-
16-
private final int totalResults;
17-
private final List<String> header;
18-
private final List<Record> results;
19-
private final Statistics statistics;
20-
private int position = 0;
15+
16+
private final int totalResults;
17+
private final List<String> header;
18+
private final List<Record> results;
19+
private final Statistics statistics;
20+
private int position = 0;
2121

22-
public ResultSetImpl(List<Object> resp) {
23-
24-
this.statistics = new StatisticsImpl((List<byte[]>)resp.get(1));
25-
26-
ArrayList<ArrayList<byte[]>> result = (ArrayList<ArrayList<byte[]>>) resp.get(0);
27-
28-
// Empty result set
29-
if(result == null || result.isEmpty()) {
30-
header = new ArrayList<>(0);
31-
totalResults = 0;
32-
results = new ArrayList<>(0);
33-
} else {
34-
ArrayList<byte[]> headers = result.get(0);
35-
header = headers.stream().map( String::new).collect(Collectors.toList());
36-
37-
// First row is a header row
38-
totalResults = result.size()-1;
39-
results = new ArrayList<>(totalResults);
40-
// Skips last row (runtime info)
41-
for (int i = 1; i <= totalResults; i++) {
42-
ArrayList<byte[]> row = result.get(i);
43-
Record record = new RecordImpl(header, row.stream().map( SafeEncoder::encode).collect(Collectors.toList()));
44-
results.add(record);
45-
}
46-
}
47-
}
22+
public ResultSetImpl(List<Object> resp) {
4823

49-
public List<String> getHeader(){
50-
return header;
51-
}
24+
this.statistics = new StatisticsImpl((List<byte[]>)resp.get(1));
25+
26+
ArrayList<ArrayList<byte[]>> result = (ArrayList<ArrayList<byte[]>>) resp.get(0);
5227

53-
@Override
54-
public boolean hasNext() {
55-
return position < results.size();
56-
}
28+
// Empty result set
29+
if(result == null || result.isEmpty()) {
30+
header = new ArrayList<>(0);
31+
totalResults = 0;
32+
results = new ArrayList<>(0);
33+
} else {
34+
ArrayList<byte[]> headers = result.get(0);
35+
header = headers.stream().map( String::new).collect(Collectors.toList());
36+
37+
// First row is a header row
38+
totalResults = result.size()-1;
39+
results = new ArrayList<>(totalResults);
40+
// Skips last row (runtime info)
41+
for (int i = 1; i <= totalResults; i++) {
42+
ArrayList<byte[]> row = result.get(i);
43+
Record record = new RecordImpl(header, row.stream().map( SafeEncoder::encode).collect(Collectors.toList()));
44+
results.add(record);
45+
}
46+
}
47+
}
48+
49+
public List<String> getHeader(){
50+
return header;
51+
}
52+
53+
@Override
54+
public boolean hasNext() {
55+
return position < results.size();
56+
}
57+
58+
@Override
59+
public Record next() {
60+
if (!hasNext())
61+
throw new NoSuchElementException();
62+
return results.get(position++);
63+
}
5764

58-
@Override
59-
public Record next() {
60-
if (!hasNext())
61-
throw new NoSuchElementException();
62-
return results.get(position++);
63-
}
65+
@Override
66+
public Statistics getStatistics() {
67+
return statistics;
68+
}
6469

65-
@Override
66-
public Statistics getStatistics() {
67-
return statistics;
68-
}
69-
70-
@Override
71-
public String toString() {
72-
return this.header + "\n" + this.results + "\n" + this.statistics;
73-
}
70+
@Override
71+
public String toString() {
72+
return this.header + "\n" + this.results + "\n" + this.statistics;
73+
}
7474
}

src/main/java/com/redislabs/redisgraph/impl/StatisticsImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ public String getStringValue(Statistics.Label label) {
2525
private Map<Statistics.Label, String> getStatistics(){
2626
if(statistics.size() == 0) {
2727
for(byte[] touple : this.raw) {
28-
String[] rowTouple = SafeEncoder.encode(touple).split(":");
29-
this.statistics.put( Statistics.Label.getEnum(rowTouple[0]), rowTouple[1].trim());
28+
String text = SafeEncoder.encode(touple);
29+
String[] rowTouple = text.split(":");
30+
if(rowTouple.length == 2) {
31+
Statistics.Label label = Statistics.Label.getEnum(rowTouple[0]);
32+
if(label != null) {
33+
this.statistics.put( label, rowTouple[1].trim());
34+
}
35+
}
3036
}
3137
}
3238
return statistics;
@@ -47,6 +53,12 @@ public int nodesDeleted() {
4753
return getIntValue(Label.NODES_DELETED);
4854
}
4955

56+
@Override
57+
public int indicesAdded() {
58+
return getIntValue(Label.INDICES_ADDED);
59+
}
60+
61+
5062
@Override
5163
public int labelsAdded() {
5264
return getIntValue(Label.LABELS_ADDED);

src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class RedisGraphAPITest {
1111

1212
public RedisGraphAPITest() {
1313
api = new RedisGraphAPI("social");
14+
15+
/* Dummy call to generate graph so the first deleteGraph() won't fail */
16+
api.query("CREATE ({name:'roi',age:32})");
1417
}
1518

1619
@Before
@@ -46,8 +49,8 @@ public void testCreateLabeledNode() throws Exception {
4649
@Test
4750
public void testConnectNodes() throws Exception {
4851
// Create both source and destination nodes
49-
ResultSet createResult1 = api.query("CREATE (:person{name:'roi',age:32})");
50-
ResultSet createResult2 = api.query("CREATE (:person{name:'amit',age:30})");
52+
Assert.assertNotNull(api.query("CREATE (:person{name:'roi',age:32})"));
53+
Assert.assertNotNull(api.query("CREATE (:person{name:'amit',age:30})"));
5154

5255
// Connect source and destination nodes.
5356
ResultSet matchResult = api.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)");
@@ -59,16 +62,32 @@ public void testConnectNodes() throws Exception {
5962
Assert.assertEquals(0, matchResult.getStatistics().relationshipsDeleted());
6063
Assert.assertNotNull(matchResult.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));
6164
}
65+
66+
@Test
67+
public void testIndex() throws Exception {
68+
// Create both source and destination nodes
69+
Assert.assertNotNull(api.query("CREATE (:person{name:'roi',age:32})"));
70+
71+
ResultSet createIndexResult = api.query("CREATE INDEX ON :person(age)");
72+
Assert.assertFalse(createIndexResult.hasNext());
73+
Assert.assertEquals(1, createIndexResult.getStatistics().indicesAdded());
74+
75+
ResultSet failCreateIndexResult = api.query("CREATE INDEX ON :person(age1)");
76+
Assert.assertFalse(failCreateIndexResult.hasNext());
77+
Assert.assertNull(failCreateIndexResult.getStatistics().getStringValue(Label.INDICES_ADDED));
78+
Assert.assertEquals(0, failCreateIndexResult.getStatistics().indicesAdded());
79+
}
80+
6281

6382
@Test
6483
public void testQuery() throws Exception {
6584

6685
// Create both source and destination nodes
67-
ResultSet create1Result = api.query("CREATE (:qhuman{name:'roi',age:32})");
68-
ResultSet create2Result = api.query("CREATE (:qhuman{name:'amit',age:30})");
86+
Assert.assertNotNull(api.query("CREATE (:qhuman{name:'roi',age:32})"));
87+
Assert.assertNotNull(api.query("CREATE (:qhuman{name:'amit',age:30})"));
6988

7089
// Connect source and destination nodes.
71-
ResultSet connectResult= api.query("MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
90+
Assert.assertNotNull(api.query("MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"));
7291

7392
// Query
7493
ResultSet resultSet = api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a");

0 commit comments

Comments
 (0)