Skip to content

Instantly share code, notes, and snippets.

@cristaloleg
Created March 26, 2018 11:49
Show Gist options
  • Save cristaloleg/fad647add6226659876fc716796f2a3c to your computer and use it in GitHub Desktop.
Save cristaloleg/fad647add6226659876fc716796f2a3c to your computer and use it in GitHub Desktop.
@janisz I did a small investigation today at morning.
We can create `AliasedHash` which implements `Hash` interface:
```
type AliasedHash struct {
aliases *sync.Map
}
func NewAliasedHash() AliasedHash {
return AliasedHash{
aliases: &sync.Map{},
}
}
func (h AliasedHash) AddAlias(key, value string) {
h.aliases.Store(key, value)
}
func (h AliasedHash) Sum64(key string) uint64 {
value, ok := h.aliases.Load(key)
if ok {
key = value.(string)
}
var hash uint64 = offset64
for i := 0; i < len(key); i++ {
hash ^= uint64(key[i])
hash *= prime64
}
return hash
}
```
But it will fall with this test:
```
func TestAliasedHash(t *testing.T) {
h := NewAliasedHash()
cfg := DefaultConfig(5 * time.Second)
cfg.Hasher = h
cache, _ := NewBigCache(cfg)
h.AddAlias("key_1", "key_2")
cache.Set("key_1", []byte("value"))
cache.Set("key_2", []byte("value"))
if _, err := cache.Get("key_1"); err != nil {
t.Fatal(err) // fails here
}
if _, err := cache.Get("key_2"); err != nil {
t.Fatal(err)
}
}
```
The problem is here:`shard.go:44` 'cause we're comparing given key & stored key
```
if entryKey := readKeyFromEntry(wrappedEntry); key != entryKey {
```
There is no possibility(right now) to change a key after a hashing function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment