Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Use substring comparison rather than set operation to remove key pref…
Browse files Browse the repository at this point in the history
…ixes in KVCache (#156) (#159)
  • Loading branch information
sgrimm-sg authored and rickfast committed Oct 4, 2016
1 parent 0a64a2a commit 3714976
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/java/com/orbitz/consul/cache/KVCache.java
Expand Up @@ -26,16 +26,25 @@ public static KVCache newCache(
final int watchSeconds,
final QueryOptions queryOptions) {

final Set<String> rootPathSegments =
new LinkedHashSet<>(Arrays.asList(rootPath.split("/")));
final String rootPathWithTrailingSlash = rootPath.endsWith("/") ? rootPath : rootPath + "/";
final int rootPathWithTrailingSlashLength = rootPathWithTrailingSlash.length();

final Function<Value, String> keyExtractor = new Function<Value, String>() {
@Override
public String apply(Value input) {
final Set<String> inputPathSegments =
new LinkedHashSet<>(Arrays.asList(input.getKey().split("/")));
if (input == null) {
throw new RuntimeException("Input to key extractor is null");
}
if (input.getKey() == null) {
throw new RuntimeException("Input to key extractor has no key");
}

return StringUtils.join(Sets.difference(inputPathSegments, rootPathSegments), "/");
if (input.getKey().startsWith(rootPathWithTrailingSlash)) {
return input.getKey().substring(rootPathWithTrailingSlashLength);
} else {
throw new RuntimeException(String.format("Got value for key %s but root is %s",
input.getKey(), rootPathWithTrailingSlash));
}
}
};

Expand All @@ -50,6 +59,16 @@ public void consume(BigInteger index, ConsulResponseCallback<List<Value>> callba
return new KVCache(keyExtractor, callbackConsumer);
}

/**
* Factory method to construct a String/{@link Value} map.
*
* @param kvClient the {@link KeyValueClient} to use
* @param rootPath the root path (will be stripped from keys in the cache)
* @param watchSeconds how long to tell the Consul server to wait for new values (note that
* if this is 60 seconds or more, the client's read timeout will need
* to be increased as well)
* @return the cache object
*/
public static KVCache newCache(
final KeyValueClient kvClient,
final String rootPath,
Expand Down

0 comments on commit 3714976

Please sign in to comment.