|
3 | 3 | import java.util.EnumMap; |
4 | 4 | import java.util.List; |
5 | 5 | import java.util.Map; |
| 6 | +import java.util.Objects; |
6 | 7 |
|
7 | 8 | import com.redislabs.redisgraph.Statistics; |
8 | 9 |
|
9 | 10 | import redis.clients.jedis.util.SafeEncoder; |
10 | 11 |
|
| 12 | +/** |
| 13 | + * Query result statistics interface implementation |
| 14 | + */ |
11 | 15 | public class StatisticsImpl implements Statistics { |
| 16 | + //members |
12 | 17 | private final List<byte[]> raw; |
13 | 18 | private final Map<Statistics.Label, String> statistics; |
14 | | - |
| 19 | + |
| 20 | + /** |
| 21 | + * A raw representation of query exection statistics is a list of strings |
| 22 | + * (byte arrays which need to be de-serialized). |
| 23 | + * Each string is built in the form of "K:V" where K is statistics label and V is its value. |
| 24 | + * @param raw a raw representation of the query execution statistics |
| 25 | + */ |
15 | 26 | StatisticsImpl(List<byte[]> raw){ |
16 | 27 | this.raw = raw; |
17 | 28 | this.statistics = new EnumMap<>(Statistics.Label.class); // lazy loaded |
18 | 29 | } |
19 | | - |
| 30 | + |
| 31 | + |
| 32 | + public List<byte[]> getRaw() { |
| 33 | + return raw; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * |
| 38 | + * @param label the requested statistic label as key |
| 39 | + * @return a string with the value, if key exists, null otherwise |
| 40 | + */ |
20 | 41 | @Override |
21 | 42 | public String getStringValue(Statistics.Label label) { |
22 | 43 | return getStatistics().get(label); |
23 | 44 | } |
24 | 45 |
|
25 | 46 | private Map<Statistics.Label, String> getStatistics(){ |
26 | 47 | if(statistics.size() == 0) { |
27 | | - for(byte[] touple : this.raw) { |
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]); |
| 48 | + for(byte[] tuple : this.raw) { |
| 49 | + String text = SafeEncoder.encode(tuple); |
| 50 | + String[] rowTuple = text.split(":"); |
| 51 | + if(rowTuple.length == 2) { |
| 52 | + Statistics.Label label = Statistics.Label.getEnum(rowTuple[0]); |
32 | 53 | if(label != null) { |
33 | | - this.statistics.put( label, rowTouple[1].trim()); |
| 54 | + this.statistics.put( label, rowTuple[1].trim()); |
34 | 55 | } |
35 | 56 | } |
36 | 57 | } |
37 | 58 | } |
38 | 59 | return statistics; |
39 | 60 | } |
40 | | - |
| 61 | + |
| 62 | + /** |
| 63 | + * |
| 64 | + * @param label the requested statistic label as key |
| 65 | + * @return a string with the value, if key exists, 0 otherwise |
| 66 | + */ |
41 | 67 | public int getIntValue(Statistics.Label label) { |
42 | 68 | String value = getStringValue(label); |
43 | 69 | return value==null ? 0 : Integer.parseInt(value); |
44 | 70 | } |
45 | 71 |
|
| 72 | + /** |
| 73 | + * |
| 74 | + * @return number of nodes created after query execution |
| 75 | + */ |
46 | 76 | @Override |
47 | 77 | public int nodesCreated() { |
48 | 78 | return getIntValue(Label.NODES_CREATED); |
49 | 79 | } |
50 | 80 |
|
| 81 | + /** |
| 82 | + * |
| 83 | + * @return number of nodes deleted after query execution |
| 84 | + */ |
51 | 85 | @Override |
52 | 86 | public int nodesDeleted() { |
53 | 87 | return getIntValue(Label.NODES_DELETED); |
54 | 88 | } |
55 | 89 |
|
| 90 | + /** |
| 91 | + * |
| 92 | + * @return number of indices added after query execution |
| 93 | + */ |
56 | 94 | @Override |
57 | 95 | public int indicesAdded() { |
58 | 96 | return getIntValue(Label.INDICES_ADDED); |
59 | 97 | } |
60 | | - |
61 | | - |
| 98 | + |
| 99 | + |
| 100 | + /** |
| 101 | + * |
| 102 | + * @return number of lables added after query execution |
| 103 | + */ |
62 | 104 | @Override |
63 | 105 | public int labelsAdded() { |
64 | 106 | return getIntValue(Label.LABELS_ADDED); |
65 | 107 | } |
66 | 108 |
|
| 109 | + /** |
| 110 | + * |
| 111 | + * @return number of relationship deleted after query execution |
| 112 | + */ |
67 | 113 | @Override |
68 | 114 | public int relationshipsDeleted() { |
69 | 115 | return getIntValue(Label.RELATIONSHIPS_DELETED); |
70 | 116 | } |
71 | 117 |
|
| 118 | + /** |
| 119 | + * |
| 120 | + * @return number of relationship created after query execution |
| 121 | + */ |
72 | 122 | @Override |
73 | 123 | public int relationshipsCreated() { |
74 | 124 | return getIntValue(Label.RELATIONSHIPS_CREATED); |
75 | 125 | } |
76 | 126 |
|
| 127 | + /** |
| 128 | + * |
| 129 | + * @return number of properties set after query execution |
| 130 | + */ |
77 | 131 | @Override |
78 | 132 | public int propertiesSet() { |
79 | 133 | return getIntValue(Label.PROPERTIES_SET); |
80 | 134 | } |
81 | | - |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean equals(Object o) { |
| 138 | + if (this == o) return true; |
| 139 | + if (!(o instanceof StatisticsImpl)) return false; |
| 140 | + StatisticsImpl that = (StatisticsImpl) o; |
| 141 | + return Objects.equals(getRaw(), that.getRaw()) && |
| 142 | + Objects.equals(getStatistics(), that.getStatistics()); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int hashCode() { |
| 147 | + return Objects.hash(getRaw(), getStatistics()); |
| 148 | + } |
| 149 | + |
82 | 150 | @Override |
83 | 151 | public String toString() { |
84 | | - return getStatistics().toString(); |
| 152 | + final StringBuilder sb = new StringBuilder("StatisticsImpl{"); |
| 153 | + sb.append("statistics=").append(getStatistics()); |
| 154 | + sb.append('}'); |
| 155 | + return sb.toString(); |
85 | 156 | } |
86 | 157 | } |
0 commit comments