Skip to content

Commit

Permalink
Merge pull request #97 from Netflix/feature/base85
Browse files Browse the repository at this point in the history
Fixed a bug with hashedkey where we were dueting it by default.
  • Loading branch information
smadappa authored Oct 19, 2020
2 parents 26109f3 + b6f86e1 commit 2f689e3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.netflix.evcache;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.archaius.api.Property;
import com.netflix.evcache.util.KeyHasher;
import com.netflix.evcache.util.KeyHasher.HashingAlgorithm;
import java.util.HashMap;
import java.util.Map;

public class EVCacheKey {
private static final Logger log = LoggerFactory.getLogger(EVCacheKey.class);
private final String appName;
private final HashingAlgorithm hashingAlgorithmAtAppLevel;
private final Property<Boolean> shouldEncodeHashKeyAtAppLevel;
Expand Down Expand Up @@ -58,6 +63,7 @@ private String getCanonicalKeyForDuet() {
if (null == canonicalKeyForDuet) {
final int duetKeyLength = appName.length() + 1 + canonicalKey.length();
canonicalKeyForDuet = new StringBuilder(duetKeyLength).append(appName).append(':').append(canonicalKey).toString();
if (log.isDebugEnabled()) log.debug("canonicalKeyForDuet : " + canonicalKeyForDuet);
}

return canonicalKeyForDuet;
Expand Down Expand Up @@ -94,7 +100,9 @@ public String getHashKey(boolean isDuet, HashingAlgorithm hashingAlgorithm, Bool
baseEnoder = encoder;
}

return isDuet ? getHashKeyForDuet(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder) : getHashKey(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder);
final String rKey = isDuet ? getHashKeyForDuet(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder) : getHashKey(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder);
if (log.isDebugEnabled()) log.debug("Key : " + rKey);
return rKey;
}

// overlays app level hashing algorithm and client level hashing algorithm
Expand All @@ -104,7 +112,9 @@ public String getDerivedKey(boolean isDuet, HashingAlgorithm hashingAlgorithm, B
hashingAlgorithm = hashingAlgorithmAtAppLevel;
}

return null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm.NO_HASHING ? getCanonicalKey(isDuet) : getHashKey(isDuet, hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder);
final String derivedKey = null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm.NO_HASHING ? getCanonicalKey(isDuet) : getHashKey(isDuet, hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder);
if (log.isDebugEnabled()) log.debug("derivedKey : " + derivedKey);
return derivedKey;
}

private String getHashKey(HashingAlgorithm hashingAlgorithm, Boolean shouldEncodeHashKey, Integer maxDigestBytes, Integer maxHashLength, String encoder) {
Expand All @@ -115,9 +125,10 @@ private String getHashKey(HashingAlgorithm hashingAlgorithm, Boolean shouldEncod
final String key = hashingAlgorithm.toString()+ maxDigestBytes != null ? maxDigestBytes.toString() : "-" + maxHashLength != null ? maxHashLength.toString() : "-" + encoder != null ? encoder : "-";
String val = hashedKeysByAlgorithm.get(key);
if(val == null) {
val = KeyHasher.getHashedKeyEncoded(getCanonicalKeyForDuet(), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder);
val = KeyHasher.getHashedKeyEncoded(getCanonicalKey(false), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder);
hashedKeysByAlgorithm.put(key , val);
}
if (log.isDebugEnabled()) log.debug("getHashKey : " + val);
// TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly
return val;
}
Expand All @@ -133,6 +144,7 @@ private String getHashKeyForDuet(HashingAlgorithm hashingAlgorithm, Boolean shou
val = KeyHasher.getHashedKeyEncoded(getCanonicalKeyForDuet(), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder);
hashedKeysByAlgorithmForDuet.put(key , val);
}
if (log.isDebugEnabled()) log.debug("getHashKeyForDuet : " + val);
// TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly
return val;
}
Expand Down

0 comments on commit 2f689e3

Please sign in to comment.