@Override public void setKeyValueStore(KeyValueStore keyValueStore) { this.keyValueStore = keyValueStore; // Check initialization for the cardinality in a keyValueStore if (keyValueStore.get(DimensionStoreKeyUtils.getCardinalityKey()) == null) { keyValueStore.put(DimensionStoreKeyUtils.getCardinalityKey(), "0"); } //Check Dimension value keys availability String allValuesKey = DimensionStoreKeyUtils.getAllValuesKey(); if (keyValueStore.get(allValuesKey) == null) { keyValueStore.put(allValuesKey, "[]"); } }
@Override public int getDimensionCardinality() { return Integer.parseInt( keyValueStore.getOrDefault(DimensionStoreKeyUtils.getCardinalityKey(), "0") ); }
/** * Internal method for cleaning the dimension rows. */ public void deleteAllDimensionRows() { try { String dimRowIndexes = keyValueStore.get(DimensionStoreKeyUtils.getAllValuesKey()); if (dimRowIndexes == null) { return; } String[] keys = objectMapper.readValue(dimRowIndexes, String[].class); LinkedHashSet<String> dimRowKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (String dimRowKey : dimRowKeys) { keyValueStore.remove(dimRowKey); } searchProvider.setKeyValueStore(keyValueStore); // Reset cardinality to 0 keyValueStore.put(DimensionStoreKeyUtils.getCardinalityKey(), "0"); // Reset list to empty String allValuesKey = DimensionStoreKeyUtils.getAllValuesKey(); keyValueStore.put(allValuesKey, "[]"); } catch (IOException e) { LOG.error("Exception while reading dimension rows {}", e); throw new RuntimeException(e); } }
/** * Get the value for a key from store or provide a default. * * @param key Key to get the value for * @param defaultValue A default value in case key is null * * @return the value for corresponding key */ default String getOrDefault(@NotNull String key, String defaultValue) { return get(key) == null ? defaultValue : get(key); }
/** * Update the cardinality count. */ private void refreshCardinality() { keyValueStore.put(DimensionStoreKeyUtils.getCardinalityKey(), Integer.toString(findAllDimensionRows().size())); }
@Override public Result check() { if (keyValueStore.isHealthy()) { return Result.healthy("KeyValueStore is healthy."); } else { return Result.unhealthy("KeyValueStore has been corrupted."); } } }
String oldRowValuesKeysJson = keyValueStore.get(oldRowValueKey); String[] oldRowValueSKeys = objectMapper.readValue(oldRowValuesKeysJson, String[].class); Set<String> oldRowValuesKeySet = new LinkedHashSet<>(Arrays.asList(oldRowValueSKeys)); oldRowValuesKeySet.remove(rowId); if (oldRowValuesKeySet.isEmpty()) { keyValueStore.remove(oldRowValueKey); } else { String updatedOldRowValueSKeys = objectMapper.writeValueAsString(oldRowValuesKeySet); keyValueStore.put(oldRowValueKey, updatedOldRowValueSKeys); String rowValueSKeys = keyValueStore.get(rowValueKey); Set<String> rowValueSKeySet = new LinkedHashSet<>(); if (rowValueSKeys != null) { keyValueStore.put(rowValueKey, updatedRowValueSKeys);
@Override public DateTime getLastUpdated() { String lastUpdatedString = keyValueStore.get(lastUpdatedKey); if (lastUpdatedString == null) { return null; } else { return DateTime.parse(lastUpdatedString); } }
/** * Update the cardinality count. * <p> * Note that this method acquires a read lock to query the lucene index for the number of documents. */ private void refreshCardinality() { int numDocs; initializeIndexSearcher(); lock.readLock().lock(); try { numDocs = luceneIndexSearcher.getIndexReader().numDocs(); } finally { lock.readLock().unlock(); } keyValueStore.put( DimensionStoreKeyUtils.getCardinalityKey(), Integer.toString(numDocs) ); }
@Override public void clearDimension() { //Remove all dimension data from the store. findAllDimensionRows().stream() .flatMap(dimensionRow -> dimensionRow.entrySet().stream()) .map(entry -> DimensionStoreKeyUtils.getRowKey(entry.getKey().getName(), entry.getValue())) .forEach(keyValueStore::remove); //Since the indices are being dropped, the dimension field stored via the columnKey is becoming stale. keyValueStore.remove(DimensionStoreKeyUtils.getColumnKey(dimension.getKey().getName())); // The allValues key mapping needs to reflect the fact that we are dropping all dimension data. keyValueStore.put(DimensionStoreKeyUtils.getAllValuesKey(), "[]"); //We're resetting the keyValueStore, so we don't want any stale last updated date floating around. keyValueStore.remove(DimensionStoreKeyUtils.getLastUpdatedKey()); refreshCardinality(); }
/** * This method updates the set of values corresponding to a dimension. * <p> * Eg: For dimension dim1 there were 3 rows with ID's v1, v2, v3 and DESC's value1, value2, value3 respectively * the keyValueStore would look like * all_values_key -> [id_v1_row_key, id_v2_row_key, id_v3_row_key] * <p> * After this refreshIndexForDimensionKey call, given a new row id: id_v4_row_key, it would look like * all_values_key -> [id_v1_row_key, id_v2_row_key, id_v3_row_key, id_v4_row_key] * <p> * This is useful to list all values for <blah>/dim1/values endpoint * * @param rowId The row id to be added */ private void refreshIndexForDimensionKey(String rowId) { try { String allValuesKey = DimensionStoreKeyUtils.getAllValuesKey(); String dimensionRows = keyValueStore.get(allValuesKey); Set<String> dimensionRowsSet = new LinkedHashSet<>(); if (dimensionRows != null) { dimensionRowsSet.addAll(Arrays.asList(objectMapper.readValue(dimensionRows, String[].class))); } dimensionRowsSet.add(rowId); String dimRows = objectMapper.writeValueAsString(dimensionRowsSet); keyValueStore.put(allValuesKey, dimRows); } catch (IOException e) { LOG.error("Exception while adding dimension entry in KeyValueStore : {}", e); throw new RuntimeException(e); } }
@Override public int getDimensionCardinality() { return Integer.parseInt(keyValueStore.get(DimensionStoreKeyUtils.getCardinalityKey())); }
/** * Get index of rows. * * @return The index of rows */ private List<String> getDimRowIndexes() { return readValue(new TypeReference<List>() { }, keyValueStore.get(DimensionStoreKeyUtils.getAllValuesKey())); }
@Override public void setKeyValueStore(KeyValueStore keyValueStore) { this.keyValueStore = keyValueStore; // Check initialization for the cardinality in a keyValueStore if (keyValueStore.get(DimensionStoreKeyUtils.getCardinalityKey()) == null) { refreshCardinality(); } }
@Override public DimensionRow findDimensionRowByKeyValue(String value) { /* * Key to fetch row from keyValueStore * eg: if key column is ID and value is 12345 * rowKey would be id_12345_row_key */ String rowKey = DimensionStoreKeyUtils.getRowKey(getKey().getName(), value); DimensionRow drByKey = null; try { String dimRowJson = keyValueStore.get(rowKey); if (dimRowJson != null) { Map<String, String> dimensionRowMap = objectMapper.readValue( dimRowJson, new TypeReference<LinkedHashMap<String, String>>() { } ); drByKey = parseDimensionRow(dimensionRowMap); } } catch (IOException e) { LOG.error("Cannot map string to DimensionRow object. {}", e); throw new RuntimeException(e); } return drByKey; }