-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathStatisticsImpl.java
More file actions
192 lines (163 loc) · 4.28 KB
/
StatisticsImpl.java
File metadata and controls
192 lines (163 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.redislabs.redisgraph.impl.resultset;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.redislabs.redisgraph.Statistics;
import redis.clients.jedis.util.SafeEncoder;
/**
* Query result statistics interface implementation
*/
public class StatisticsImpl implements Statistics {
private List<byte[]> raw;
private final Map<Statistics.Label, String> statistics = new EnumMap<>(Statistics.Label.class); // lazy loaded
public StatisticsImpl(){}
/**
* A raw representation of query execution statistics is a list of strings
* (byte arrays which need to be de-serialized).
* Each string is built in the form of "K:V" where K is statistics label and V is its value.
* @param raw a raw representation of the query execution statistics
*/
public StatisticsImpl(List<byte[]> raw){
this.raw = raw;
}
/**
*
* @param label the requested statistic label as key
* @return a string with the value, if key exists, null otherwise
*/
@Override
public String getStringValue(Statistics.Label label) {
return getStatistics().get(label);
}
/**
* Lazy parse statistics on first call
*/
public Map<Statistics.Label, String> getStatistics(){
if(statistics.size() == 0 && this.raw != null) {
for(byte[] tuple : this.raw) {
String text = SafeEncoder.encode(tuple);
String[] rowTuple = text.split(":");
if(rowTuple.length == 2) {
Statistics.Label label = Statistics.Label.getEnum(rowTuple[0]);
if(label != null) {
this.statistics.put( label, rowTuple[1].trim());
}
}
}
}
return statistics;
}
/**
*
* @param label the requested statistic label as key
* @return a string with the value, if key exists, 0 otherwise
*/
public int getIntValue(Statistics.Label label) {
String value = getStringValue(label);
return value==null ? 0 : Integer.parseInt(value);
}
/**
*
* @return number of nodes created after query execution
*/
@Override
public int nodesCreated() {
return getIntValue(Label.NODES_CREATED);
}
/**
*
* @return number of nodes deleted after query execution
*/
@Override
public int nodesDeleted() {
return getIntValue(Label.NODES_DELETED);
}
/**
*
* @return number of indices added after query execution
*/
@Override
public int indicesAdded() {
return getIntValue(Label.INDICES_ADDED);
}
@Override
public int indicesDeleted() {return getIntValue(Label.INDICES_DELETED);}
/**
*
* @return number of labels added after query execution
*/
@Override
public int labelsAdded() {
return getIntValue(Label.LABELS_ADDED);
}
/**
*
* @return number of relationship deleted after query execution
*/
@Override
public int relationshipsDeleted() {
return getIntValue(Label.RELATIONSHIPS_DELETED);
}
/**
*
* @return number of relationship created after query execution
*/
@Override
public int relationshipsCreated() {
return getIntValue(Label.RELATIONSHIPS_CREATED);
}
/**
*
* @return number of properties set after query execution
*/
@Override
public int propertiesSet() {
return getIntValue(Label.PROPERTIES_SET);
}
/**
*
* @return The execution plan was cached on RedisGraph.
*/
@Override
public boolean cachedExecution() {
return getIntValue(Label.CACHED_EXECUTION) == 1;
}
/**
*
* @return The execution time for the Query.
*/
@Override
public String queryExecutionTime() {
return getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StatisticsImpl)) return false;
StatisticsImpl that = (StatisticsImpl) o;
return Objects.equals(this.raw, that.raw) &&
Objects.equals(getStatistics(), that.getStatistics());
}
@Override
public int hashCode() {
return Objects.hash(this.raw, getStatistics());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("StatisticsImpl{");
sb.append("statistics=").append(getStatistics());
sb.append('}');
return sb.toString();
}
public List<byte[]> getRaw() {
return raw;
}
public void setRaw(List<byte[]> raw) {
this.raw = raw;
// if statistics already holds parsed data from raw, it needs to be cleared
if(this.statistics.size() > 0) {
this.statistics.clear();
}
}
}