|
| 1 | +package com.microsoft.azure.documentdb; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | + |
| 6 | +import org.apache.commons.lang3.text.WordUtils; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +public class IncludedPath extends JsonSerializable { |
| 11 | + |
| 12 | + // default number precisions |
| 13 | + private static final short DEFAULT_NUMBER_HASH_PRECISION = 3; |
| 14 | + private static final short DEFAULT_NUMBER_RANGE_PRECISION = -1; |
| 15 | + |
| 16 | + // default string precision |
| 17 | + private static final short DEFAULT_STRING_HASH_PRECISION = 3; |
| 18 | + private static final short DEFAULT_STRING_RANGE_PRECISION = -1; |
| 19 | + |
| 20 | + private Collection<Index> indexes; |
| 21 | + |
| 22 | + /** |
| 23 | + * Constructor. |
| 24 | + */ |
| 25 | + public IncludedPath() { |
| 26 | + super(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor. |
| 31 | + * |
| 32 | + * @param jsonString the json string that represents the included path. |
| 33 | + */ |
| 34 | + public IncludedPath(String jsonString) { |
| 35 | + super(jsonString); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor. |
| 40 | + * |
| 41 | + * @param jsonObject the json object that represents the included path. |
| 42 | + */ |
| 43 | + public IncludedPath(JSONObject jsonObject) { |
| 44 | + super(jsonObject); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets path. |
| 49 | + * |
| 50 | + * @return the path. |
| 51 | + */ |
| 52 | + public String getPath() { |
| 53 | + return super.getString(Constants.Properties.PATH); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets path. |
| 58 | + * |
| 59 | + * @param path the path. |
| 60 | + */ |
| 61 | + public void setPath(String path) { |
| 62 | + super.set(Constants.Properties.PATH, path); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Gets the paths that are chosen to be indexed by the user. |
| 67 | + * |
| 68 | + * @return the included paths. |
| 69 | + */ |
| 70 | + public Collection<Index> getIndexes() { |
| 71 | + if (this.indexes == null) { |
| 72 | + this.indexes = this.getIndexCollection(); |
| 73 | + |
| 74 | + if (this.indexes == null) { |
| 75 | + this.indexes = new ArrayList<Index>(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return this.indexes; |
| 80 | + } |
| 81 | + |
| 82 | + private Collection<Index> getIndexCollection() { |
| 83 | + if (this.propertyBag != null && this.propertyBag.has(Constants.Properties.INDEXES)) { |
| 84 | + JSONArray jsonArray = this.propertyBag.getJSONArray(Constants.Properties.INDEXES); |
| 85 | + Collection<Index> result = new ArrayList<Index>(); |
| 86 | + |
| 87 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 88 | + JSONObject jsonObject = jsonArray.getJSONObject(i); |
| 89 | + |
| 90 | + IndexKind indexKind = IndexKind.valueOf(WordUtils.capitalize( |
| 91 | + jsonObject.getString(Constants.Properties.INDEX_KIND))); |
| 92 | + switch (indexKind) { |
| 93 | + case Hash: |
| 94 | + result.add(new HashIndex(jsonObject.toString())); |
| 95 | + break; |
| 96 | + case Range: |
| 97 | + result.add(new RangeIndex(jsonObject.toString())); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + public void setIndexes(Collection<Index> indexes) { |
| 109 | + this.indexes = indexes; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + void onSave() { |
| 114 | + if (this.getIndexes().size() == 0) { |
| 115 | + HashIndex hashIndex = new HashIndex(DataType.String); |
| 116 | + hashIndex.setPrecision(IncludedPath.DEFAULT_STRING_HASH_PRECISION); |
| 117 | + this.indexes.add(hashIndex); |
| 118 | + |
| 119 | + RangeIndex rangeIndex = new RangeIndex(DataType.Number); |
| 120 | + rangeIndex.setPrecision(IncludedPath.DEFAULT_NUMBER_RANGE_PRECISION); |
| 121 | + this.indexes.add(rangeIndex); |
| 122 | + } |
| 123 | + |
| 124 | + for (Index index : this.indexes) { |
| 125 | + if (index.getKind() == IndexKind.Hash) { |
| 126 | + HashIndex hashIndex = (HashIndex)index; |
| 127 | + if (!hashIndex.hasPrecision()) { |
| 128 | + if(hashIndex.getDataType() == DataType.Number) { |
| 129 | + hashIndex.setPrecision(IncludedPath.DEFAULT_NUMBER_HASH_PRECISION); |
| 130 | + } else if(hashIndex.getDataType() == DataType.String) { |
| 131 | + hashIndex.setPrecision(IncludedPath.DEFAULT_STRING_HASH_PRECISION); |
| 132 | + } |
| 133 | + } |
| 134 | + } else if(index.getKind() == IndexKind.Range) { |
| 135 | + RangeIndex rangeIndex = (RangeIndex)index; |
| 136 | + if (!rangeIndex.hasPrecision()) { |
| 137 | + if (rangeIndex.getDataType() == DataType.Number) { |
| 138 | + rangeIndex.setPrecision(IncludedPath.DEFAULT_NUMBER_RANGE_PRECISION); |
| 139 | + } else if (rangeIndex.getDataType() == DataType.String) { |
| 140 | + rangeIndex.setPrecision(IncludedPath.DEFAULT_STRING_RANGE_PRECISION); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + super.set(Constants.Properties.INDEXES, this.indexes); |
| 147 | + } |
| 148 | +} |
0 commit comments