|
| 1 | +package io.zentity.model; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +public class Index { |
| 10 | + |
| 11 | + public static final Set<String> REQUIRED_FIELDS = new HashSet<>( |
| 12 | + Arrays.asList("fields") |
| 13 | + ); |
| 14 | + |
| 15 | + private final String name; |
| 16 | + private HashMap<String, IndexField> fields; |
| 17 | + private HashMap<String, HashMap<String, IndexField>> attributeIndexFieldsMap = new HashMap<>(); |
| 18 | + |
| 19 | + public Index(String name, JsonNode json) throws ValidationException { |
| 20 | + validateName(name); |
| 21 | + this.name = name; |
| 22 | + this.deserialize(json); |
| 23 | + } |
| 24 | + |
| 25 | + public Index(String name, String json) throws ValidationException, IOException { |
| 26 | + validateName(name); |
| 27 | + this.name = name; |
| 28 | + this.deserialize(json); |
| 29 | + } |
| 30 | + |
| 31 | + public String name() { |
| 32 | + return this.name; |
| 33 | + } |
| 34 | + |
| 35 | + public HashMap<String, HashMap<String, IndexField>> attributeIndexFieldsMap() { |
| 36 | + return this.attributeIndexFieldsMap; |
| 37 | + } |
| 38 | + |
| 39 | + public HashMap<String, IndexField> fields() { |
| 40 | + return this.fields; |
| 41 | + } |
| 42 | + |
| 43 | + public void fields(JsonNode value) throws ValidationException { |
| 44 | + validateFields(value); |
| 45 | + HashMap<String, IndexField> fields = new HashMap<>(); |
| 46 | + Iterator<Map.Entry<String, JsonNode>> children = value.fields(); |
| 47 | + while (children.hasNext()) { |
| 48 | + Map.Entry<String, JsonNode> child = children.next(); |
| 49 | + String fieldName = child.getKey(); |
| 50 | + JsonNode fieldObject = child.getValue(); |
| 51 | + validateField(fieldName, fieldObject); |
| 52 | + fields.put(fieldName, new IndexField(this.name, fieldName, fieldObject)); |
| 53 | + } |
| 54 | + this.fields = fields; |
| 55 | + this.rebuildAttributeIndexFieldsMap(); |
| 56 | + } |
| 57 | + |
| 58 | + private void validateName(String value) throws ValidationException { |
| 59 | + if (value.matches("^\\s*$")) |
| 60 | + throw new ValidationException("'indices' has an index with an empty name."); |
| 61 | + } |
| 62 | + |
| 63 | + private void validateField(String fieldName, JsonNode fieldObject) throws ValidationException { |
| 64 | + if (fieldName.equals("")) |
| 65 | + throw new ValidationException("'indices." + this.name + ".fields' has a field with an empty name."); |
| 66 | + if (!fieldObject.isObject()) |
| 67 | + throw new ValidationException("'indices." + this.name + ".fields." + fieldName + "' must be an object."); |
| 68 | + if (fieldObject.size() == 0) |
| 69 | + throw new ValidationException("'indices." + this.name + ".fields." + fieldName + "' must not be empty."); |
| 70 | + } |
| 71 | + |
| 72 | + private void validateFields(JsonNode value) throws ValidationException { |
| 73 | + if (!value.isObject()) |
| 74 | + throw new ValidationException("'indices." + this.name + ".fields' must be an object."); |
| 75 | + if (value.size() == 0) |
| 76 | + throw new ValidationException("'indices." + this.name + ".fields' must not be empty."); |
| 77 | + } |
| 78 | + |
| 79 | + private void validateObject(JsonNode object) throws ValidationException { |
| 80 | + if (!object.isObject()) |
| 81 | + throw new ValidationException("'indices." + this.name + "' must be an object."); |
| 82 | + if (object.size() == 0) |
| 83 | + throw new ValidationException("'indices." + this.name + "' is empty."); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Create a reverse index of attribute names to index fields for faster lookup of index fields by attributes |
| 88 | + * during a resolution job. |
| 89 | + */ |
| 90 | + private void rebuildAttributeIndexFieldsMap() { |
| 91 | + this.attributeIndexFieldsMap = new HashMap<>(); |
| 92 | + for (String indexFieldName : this.fields().keySet()) { |
| 93 | + String attributeName = this.fields().get(indexFieldName).attribute(); |
| 94 | + if (!this.attributeIndexFieldsMap.containsKey(attributeName)) |
| 95 | + this.attributeIndexFieldsMap.put(attributeName, new HashMap<>()); |
| 96 | + if (!this.attributeIndexFieldsMap.get(attributeName).containsKey(indexFieldName)) |
| 97 | + this.attributeIndexFieldsMap.get(attributeName).put(indexFieldName, this.fields.get(indexFieldName)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Deserialize, validate, and hold the state of an index object of an entity model. |
| 103 | + * Expected structure of the json variable: |
| 104 | + * <pre> |
| 105 | + * { |
| 106 | + * "fields": { |
| 107 | + * INDEX_FIELD_NAME: INDEX_FIELD_OBJECT |
| 108 | + * ... |
| 109 | + * } |
| 110 | + * } |
| 111 | + * </pre> |
| 112 | + * |
| 113 | + * @param json Index object of an entity model. |
| 114 | + * @throws ValidationException |
| 115 | + */ |
| 116 | + public void deserialize(JsonNode json) throws ValidationException { |
| 117 | + validateObject(json); |
| 118 | + |
| 119 | + // Validate the existence of required fields. |
| 120 | + for (String field : REQUIRED_FIELDS) |
| 121 | + if (!json.has(field)) |
| 122 | + throw new ValidationException("'indices." + this.name + "' is missing required field '" + field + "'."); |
| 123 | + |
| 124 | + // Validate and hold the state of fields. |
| 125 | + Iterator<Map.Entry<String, JsonNode>> fields = json.fields(); |
| 126 | + while (fields.hasNext()) { |
| 127 | + Map.Entry<String, JsonNode> field = fields.next(); |
| 128 | + String name = field.getKey(); |
| 129 | + JsonNode value = field.getValue(); |
| 130 | + switch (name) { |
| 131 | + case "fields": |
| 132 | + this.fields(value); |
| 133 | + break; |
| 134 | + default: |
| 135 | + throw new ValidationException("'indices." + this.name + "." + name + "' is not a recognized field."); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public void deserialize(String json) throws ValidationException, IOException { |
| 141 | + deserialize(new ObjectMapper().readTree(json)); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments