Skip to content

Commit a4e42de

Browse files
committed
Update RecordImpl to return none String types
1 parent 26058fe commit a4e42de

File tree

4 files changed

+135
-100
lines changed

4 files changed

+135
-100
lines changed

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

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,69 @@
88
* List records are returned from RedisGraph statement execution, contained within a ResultSet.
99
*/
1010
public interface Record {
11-
12-
/**
13-
* The value at the given field index (represented as String)
14-
*
15-
* @param index
16-
* @return string representation of the value
17-
*/
18-
String getString(int index);
19-
20-
/**
21-
* The value at the given field (represented as String)
22-
*
23-
* @param key header key
24-
*
25-
* @return string representation of the value
26-
*/
27-
String getString(String key);
28-
29-
/**
30-
* The keys of the record
31-
*
32-
* @return list of the record key
33-
*/
34-
List<String> keys();
35-
36-
/**
37-
* The values of the record
38-
*
39-
* @return list of the record values
40-
*/
41-
List<String> values();
42-
43-
/**
44-
* Check if the record header contains the given key
45-
*
46-
* @param key header key
47-
*
48-
* @return <code>true</code> if the the key exists
49-
*/
50-
boolean containsKey(String key);
51-
52-
/**
53-
* The number of fields in this record
54-
*
55-
* @return the number of fields
56-
*/
57-
int size();
11+
12+
/**
13+
* The value at the given field index
14+
*
15+
* @param index field index
16+
*
17+
* @return the value
18+
*/
19+
<T> T getValue(int index);
20+
21+
/**
22+
* The value at the given field
23+
*
24+
* @param key header key
25+
*
26+
* @return the value
27+
*/
28+
<T> T getValue(String key);
29+
30+
/**
31+
* The value at the given field index (represented as String)
32+
*
33+
* @param index
34+
* @return string representation of the value
35+
*/
36+
String getString(int index);
37+
38+
/**
39+
* The value at the given field (represented as String)
40+
*
41+
* @param key header key
42+
*
43+
* @return string representation of the value
44+
*/
45+
String getString(String key);
46+
47+
/**
48+
* The keys of the record
49+
*
50+
* @return list of the record key
51+
*/
52+
List<String> keys();
53+
54+
/**
55+
* The values of the record
56+
*
57+
* @return list of the record values
58+
*/
59+
List<Object> values();
60+
61+
/**
62+
* Check if the record header contains the given key
63+
*
64+
* @param key header key
65+
*
66+
* @return <code>true</code> if the the key exists
67+
*/
68+
boolean containsKey(String key);
69+
70+
/**
71+
* The number of fields in this record
72+
*
73+
* @return the number of fields
74+
*/
75+
int size();
5876
}

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

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,59 @@
55
import com.redislabs.redisgraph.Record;
66

77
public class RecordImpl implements Record {
8-
9-
private final List<String> header;
10-
private final List<String> values;
11-
12-
RecordImpl(List<String> header, List<String> values){
13-
this.header=header;
14-
this.values = values;
15-
}
16-
17-
@Override
18-
public String getString(int index) {
19-
return this.values.get(index);
20-
}
21-
22-
@Override
23-
public String getString(String key) {
24-
return getString(this.header.indexOf(key));
25-
}
26-
27-
@Override
28-
public List<String> keys() {
29-
return header;
30-
}
31-
32-
@Override
33-
public List<String> values() {
34-
return this.values;
35-
}
36-
37-
@Override
38-
public boolean containsKey(String key) {
39-
return this.header.contains(key);
40-
}
41-
42-
@Override
43-
public int size() {
44-
return this.header.size();
45-
}
46-
47-
@Override
48-
public String toString() {
49-
return this.values.toString();
50-
}
8+
9+
private final List<String> header;
10+
private final List<Object> values;
11+
12+
RecordImpl(List<String> header, List<Object> values){
13+
this.header=header;
14+
this.values = values;
15+
}
16+
17+
@Override
18+
public <T> T getValue(int index) {
19+
return (T)this.values.get(index);
20+
}
21+
22+
@Override
23+
public <T> T getValue(String key) {
24+
return getValue(this.header.indexOf(key));
25+
}
26+
27+
@Override
28+
public String getString(int index) {
29+
return this.values.get(index).toString();
30+
}
31+
32+
@Override
33+
public String getString(String key) {
34+
return getString(this.header.indexOf(key));
35+
}
36+
37+
@Override
38+
public List<String> keys() {
39+
return header;
40+
}
41+
42+
@Override
43+
public List<Object> values() {
44+
return this.values;
45+
}
46+
47+
@Override
48+
public boolean containsKey(String key) {
49+
return this.header.contains(key);
50+
}
51+
52+
@Override
53+
public int size() {
54+
return this.header.size();
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return this.values.toString();
60+
}
61+
5162

5263
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,29 @@ public ResultSetImpl(List<Object> resp) {
2323

2424
this.statistics = new StatisticsImpl((List<byte[]>)resp.get(1));
2525

26-
ArrayList<ArrayList<byte[]>> result = (ArrayList<ArrayList<byte[]>>) resp.get(0);
26+
ArrayList<ArrayList<?>> result = (ArrayList<ArrayList<?>>) resp.get(0);
2727

2828
// Empty result set
2929
if(result == null || result.isEmpty()) {
3030
header = new ArrayList<>(0);
3131
totalResults = 0;
3232
results = new ArrayList<>(0);
3333
} else {
34-
ArrayList<byte[]> headers = result.get(0);
34+
ArrayList<byte[]> headers = (ArrayList<byte[]>)result.get(0);
3535
header = headers.stream().map( String::new).collect(Collectors.toList());
3636

3737
// First row is a header row
3838
totalResults = result.size()-1;
3939
results = new ArrayList<>(totalResults);
4040
// Skips last row (runtime info)
4141
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()));
42+
ArrayList<?> row = result.get(i);
43+
Record record = new RecordImpl(header, row.stream().map( obj -> {
44+
if(obj instanceof byte[]) {
45+
return SafeEncoder.encode((byte[])obj);
46+
}
47+
return obj;
48+
}).collect(Collectors.toList()));
4449
results.add(record);
4550
}
4651
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void testQuery(){
101101
ResultSet resultSet = api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a");
102102

103103
Assert.assertEquals(Arrays.asList("a.age", "a.name"), resultSet.getHeader());
104-
Assert.assertEquals("[a.age, a.name]\n[[32.000000, roi]]\n" + resultSet.getStatistics(), resultSet.toString());
104+
Assert.assertEquals("[a.age, a.name]\n[[32, roi]]\n" + resultSet.getStatistics(), resultSet.toString());
105105

106106
Assert.assertTrue(resultSet.hasNext());
107107
Assert.assertEquals(0, resultSet.getStatistics().nodesCreated());
@@ -114,15 +114,16 @@ public void testQuery(){
114114

115115
Record record = resultSet.next();
116116
Assert.assertEquals( Arrays.asList("a.age", "a.name"), record.keys());
117-
Assert.assertEquals( Arrays.asList("32.000000", "roi"), record.values());
117+
Assert.assertEquals( Arrays.asList(32L, "roi"), record.values());
118118
Assert.assertTrue(record.containsKey("a.name"));
119119
Assert.assertEquals( 2, record.size());
120-
Assert.assertEquals( "[32.000000, roi]", record.toString());
120+
Assert.assertEquals( "[32, roi]", record.toString());
121121

122122
Assert.assertEquals( "roi", record.getString(1));
123-
Assert.assertEquals( "32.000000", record.getString(0));
123+
Assert.assertEquals( "32", record.getString(0));
124+
Assert.assertEquals( 32L, ((Long)record.getValue(0)).longValue());
125+
Assert.assertEquals( 32L, ((Long)record.getValue("a.age")).longValue());
124126
Assert.assertEquals( "roi", record.getString("a.name"));
125-
Assert.assertEquals( "32.000000", record.getString("a.age"));
126-
127+
Assert.assertEquals( "32", record.getString("a.age"));
127128
}
128129
}

0 commit comments

Comments
 (0)