|
| 1 | +package com.redislabs.redisgraph.impl; |
| 2 | + |
| 3 | +import com.redislabs.redisgraph.Header; |
| 4 | +import redis.clients.jedis.util.SafeEncoder; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +/** |
| 11 | + * Query result header interface implementation |
| 12 | + */ |
| 13 | +public class HeaderImpl implements Header { |
| 14 | + |
| 15 | + //members |
| 16 | + private final List<List<Object>> raw; |
| 17 | + private final List<ResultSetColumnTypes> schemaTypes = new ArrayList<>(); |
| 18 | + private final List<String> schemaNames = new ArrayList<>(); |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * Parameterized constructor |
| 23 | + * A raw representation of a header (query response schema) is a list. |
| 24 | + * Each entry in the list is a tuple (list of size 2). |
| 25 | + * tuple[0] represents the type of the column, and tuple[1] represents the name of the column. |
| 26 | + * |
| 27 | + * @param raw - raw representation of a header |
| 28 | + */ |
| 29 | + public HeaderImpl(List<List<Object>> raw) { |
| 30 | + this.raw = raw; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * @return a list of column names, ordered by they appearance in the query |
| 36 | + */ |
| 37 | + @Override |
| 38 | + public List<String> getSchemaNames() { |
| 39 | + if (schemaNames.size() == 0) { |
| 40 | + buildSchema(); |
| 41 | + } |
| 42 | + return schemaNames; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return a list of column types, ordered by they appearance in the query |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public List<ResultSetColumnTypes> getSchemaTypes() { |
| 50 | + if (schemaTypes.size() == 0) { |
| 51 | + buildSchema(); |
| 52 | + } |
| 53 | + return schemaTypes; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Extracts schema names and types from the raw representation |
| 58 | + */ |
| 59 | + private void buildSchema() { |
| 60 | + for (List<Object> tuple : this.raw) { |
| 61 | + |
| 62 | + //get type |
| 63 | + ResultSetColumnTypes type = ResultSetColumnTypes.values()[(int) (long) tuple.get(0)]; |
| 64 | + //get text |
| 65 | + String text = SafeEncoder.encode((byte[]) tuple.get(1)); |
| 66 | + if (type != null) { |
| 67 | + schemaTypes.add(type); |
| 68 | + schemaNames.add(text); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean equals(Object o) { |
| 75 | + if (this == o) return true; |
| 76 | + if (!(o instanceof HeaderImpl)) return false; |
| 77 | + HeaderImpl header = (HeaderImpl) o; |
| 78 | + return Objects.equals(getSchemaTypes(), header.getSchemaTypes()) && |
| 79 | + Objects.equals(getSchemaNames(), header.getSchemaNames()); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + return Objects.hash(getSchemaTypes(), getSchemaNames()); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toString() { |
| 89 | + final StringBuilder sb = new StringBuilder("HeaderImpl{"); |
| 90 | + sb.append("schemaTypes=").append(schemaTypes); |
| 91 | + sb.append(", schemaNames=").append(schemaNames); |
| 92 | + sb.append('}'); |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | +} |
0 commit comments