Skip to content

Commit

Permalink
fix delete wrong entry pointer in segment.set
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood committed Feb 10, 2018
1 parent 0eab74f commit a955dc1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,47 @@ func TestIterator(t *testing.T) {
}
}

func TestSetLargerEntryDeletesWrongEntry(t *testing.T) {
cachesize := 512 * 1024
cache := NewCache(cachesize)

value1 := "aaa"
key1 := []byte("key1")
value := value1
cache.Set(key1, []byte(value), 0)

it := cache.NewIterator()
entry := it.Next()
if !bytes.Equal(entry.Key, key1) {
t.Fatalf("key %s not equal to %s", entry.Key, key1)
}
if !bytes.Equal(entry.Value, []byte(value)) {
t.Fatalf("value %s not equal to %s", entry.Value, value)
}
entry = it.Next()
if entry != nil {
t.Fatalf("expected nil entry but got %s %s", entry.Key, entry.Value)
}

value = value1 + "XXXXXX"
cache.Set(key1, []byte(value), 0)

value = value1 + "XXXXYYYYYYY"
cache.Set(key1, []byte(value), 0)
it = cache.NewIterator()
entry = it.Next()
if !bytes.Equal(entry.Key, key1) {
t.Fatalf("key %s not equal to %s", entry.Key, key1)
}
if !bytes.Equal(entry.Value, []byte(value)) {
t.Fatalf("value %s not equal to %s", entry.Value, value)
}
entry = it.Next()
if entry != nil {
t.Fatalf("expected nil entry but got %s %s", entry.Key, entry.Value)
}
}

func BenchmarkCacheSet(b *testing.B) {
cache := NewCache(256 * 1024 * 1024)
var key [8]byte
Expand Down
3 changes: 2 additions & 1 deletion segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (seg *segment) set(key, value []byte, hashVal uint64, expireSeconds int) (e
return
}
// avoid unnecessary memory copy.
seg.delEntryPtr(slotId, hash16, seg.slotsData[idx].offset)
seg.delEntryPtr(slotId, hash16, slot[idx].offset)
//seg.delEntryPtr(slotId, hash16, seg.slotsData[idx].offset)
match = false
// increase capacity and limit entry len.
for hdr.valCap < hdr.valLen {
Expand Down

0 comments on commit a955dc1

Please sign in to comment.