Skip to content

Commit

Permalink
Add ExpireAt to the iterator return result (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: laynexie <laynexie@tencent.com>
  • Loading branch information
xiehui3651 and laynexie authored Sep 22, 2023
1 parent a3ea333 commit c241df0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
43 changes: 41 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,43 @@ func TestIterator(t *testing.T) {
}
}

func TestIteratorExpireAt(t *testing.T) {
cache := NewCache(1024)
expireSecond := uint32(5)
// Set some value that expires to make sure expired entry is not returned.
cache.Set([]byte("no_expire"), []byte("def"), 0)
cache.Set([]byte("has_expire"), []byte("exp"), int(expireSecond))

it := cache.NewIterator()
for {
next := it.Next()
if next == nil {
break
}
if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
t.Fatalf("no_expire's ExpireAt should be 0")
}
expectExpireAt := uint32(time.Now().Unix()) + expireSecond
if string(next.Key) == "has_expire" && next.ExpireAt != expectExpireAt {
t.Fatalf("has_expire's ExpireAt should be 10,actually is %d", next.ExpireAt)
}
}
time.Sleep(time.Duration(expireSecond) * time.Second)
it2 := cache.NewIterator()
for {
next := it2.Next()
if next == nil {
return
}
if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
t.Fatalf("no_expire's ExpireAt should be 0")
}
if string(next.Key) == "has_expire" {
t.Fatalf("has_expire should expired")
}
}
}

func TestSetLargerEntryDeletesWrongEntry(t *testing.T) {
cachesize := 512 * 1024
cache := NewCache(cachesize)
Expand Down Expand Up @@ -1041,7 +1078,8 @@ func TestUpdate(t *testing.T) {
updaterReplace = replace
}

assertExpectations := func(testCase int, expectedFound, expectedReplaced bool, expectedPrevVal []byte, expectedVal []byte) {
assertExpectations := func(testCase int, expectedFound, expectedReplaced bool, expectedPrevVal []byte,
expectedVal []byte) {
failPrefix := fmt.Sprintf("%s(%d)", testName, testCase)

if expectedFound != found {
Expand All @@ -1054,7 +1092,8 @@ func TestUpdate(t *testing.T) {
t.Fatalf("%s unexpected err %v", failPrefix, err)
}
if string(prevVal) != string(expectedPrevVal) {
t.Fatalf("%s previous value expected %s instead of %s", failPrefix, string(expectedPrevVal), string(prevVal))
t.Fatalf("%s previous value expected %s instead of %s", failPrefix, string(expectedPrevVal),
string(prevVal))
}

// Check value
Expand Down
6 changes: 4 additions & 2 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type Iterator struct {

// Entry represents a key/value pair.
type Entry struct {
Key []byte
Value []byte
Key []byte
Value []byte
ExpireAt uint32
}

// Next returns the next entry for the iterator.
Expand Down Expand Up @@ -63,6 +64,7 @@ func (it *Iterator) nextForSlot(seg *segment, slotId int) *Entry {
entry := new(Entry)
entry.Key = make([]byte, hdr.keyLen)
entry.Value = make([]byte, hdr.valLen)
entry.ExpireAt = hdr.expireAt
seg.rb.ReadAt(entry.Key, ptr.offset+ENTRY_HDR_SIZE)
seg.rb.ReadAt(entry.Value, ptr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
return entry
Expand Down

0 comments on commit c241df0

Please sign in to comment.