/** * Creates a new Aliases instance with the same data as the current one but with a modification based on the * parameters. * <p> * Note that the state in zookeeper is unaffected by this method and the change must still be persisted via * {@link ZkStateReader.AliasesManager#applyModificationAndExportToZk(UnaryOperator)} * * @param alias the alias to update, must not be null * @param collections the comma separated list of collections for the alias, null to remove the alias */ public Aliases cloneWithCollectionAlias(String alias, String collections) { if (alias == null) { throw new NullPointerException("Alias name cannot be null"); } Map<String, Map<String, String>> newColProperties; Map<String, List<String>> newColAliases = new LinkedHashMap<>(this.collectionAliases);//clone to modify if (collections == null) { // REMOVE: newColProperties = new LinkedHashMap<>(this.collectionAliasProperties);//clone to modify newColProperties.remove(alias); newColAliases.remove(alias); } else { newColProperties = this.collectionAliasProperties;// no changes // java representation is a list, so split before adding to maintain consistency newColAliases.put(alias, splitCollections(collections)); // note: unmodifiableList } return new Aliases(newColAliases, newColProperties, zNodeVersion); }
/** * Set the values for some properties keys on a collection alias. This is done by creating a new Aliases instance * with the same data as the current one but with a modification based on the parameters. * <p> * Note that the state in zookeeper is unaffected by this method and the change must still be persisted via * {@link ZkStateReader.AliasesManager#applyModificationAndExportToZk(UnaryOperator)} * * @param alias the alias to update * @param properties the properties to add/replace, null values in the map will remove the key. * @return An immutable copy of the aliases with the new properties. */ public Aliases cloneWithCollectionAliasProperties(String alias, Map<String,String> properties) { if (!collectionAliases.containsKey(alias)) { throw new IllegalArgumentException(alias + " is not a valid alias"); } if (properties == null) { throw new IllegalArgumentException("Null is not a valid properties map"); } Map<String,Map<String,String>> newColProperties = new LinkedHashMap<>(this.collectionAliasProperties);//clone to modify Map<String, String> newMetaMap = new LinkedHashMap<>(newColProperties.getOrDefault(alias, Collections.emptyMap())); for (Map.Entry<String, String> metaEntry : properties.entrySet()) { if (metaEntry.getValue() != null) { newMetaMap.put(metaEntry.getKey(), metaEntry.getValue()); } else { newMetaMap.remove(metaEntry.getKey()); } } newColProperties.put(alias, Collections.unmodifiableMap(newMetaMap)); return new Aliases(collectionAliases, newColProperties, zNodeVersion); }
/** * Create an instance from the JSON bytes read from zookeeper. Generally this should * only be done by a ZkStateReader. * * @param bytes The bytes read via a getData request to zookeeper (possibly null) * @param zNodeVersion the version of the data in zookeeper that this instance corresponds to * @return A new immutable Aliases object */ @SuppressWarnings("unchecked") public static Aliases fromJSON(byte[] bytes, int zNodeVersion) { Map<String, Map> aliasMap; if (bytes == null || bytes.length == 0) { aliasMap = Collections.emptyMap(); } else { aliasMap = (Map<String, Map>) Utils.fromJSON(bytes); } Map colAliases = aliasMap.getOrDefault(COLLECTION, Collections.emptyMap()); colAliases = convertMapOfCommaDelimitedToMapOfList(colAliases); // also unmodifiable Map<String, Map<String, String>> colMeta = aliasMap.getOrDefault(COLLECTION_METADATA, Collections.emptyMap()); colMeta.replaceAll((k, metaMap) -> Collections.unmodifiableMap(metaMap)); return new Aliases(colAliases, colMeta, zNodeVersion); }