Skip to content

Commit c241df0

Browse files
xiehui3651laynexie
and
laynexie
authored
Add ExpireAt to the iterator return result (#128)
Co-authored-by: laynexie <[email protected]>
1 parent a3ea333 commit c241df0

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

cache_test.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,43 @@ func TestIterator(t *testing.T) {
607607
}
608608
}
609609

610+
func TestIteratorExpireAt(t *testing.T) {
611+
cache := NewCache(1024)
612+
expireSecond := uint32(5)
613+
// Set some value that expires to make sure expired entry is not returned.
614+
cache.Set([]byte("no_expire"), []byte("def"), 0)
615+
cache.Set([]byte("has_expire"), []byte("exp"), int(expireSecond))
616+
617+
it := cache.NewIterator()
618+
for {
619+
next := it.Next()
620+
if next == nil {
621+
break
622+
}
623+
if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
624+
t.Fatalf("no_expire's ExpireAt should be 0")
625+
}
626+
expectExpireAt := uint32(time.Now().Unix()) + expireSecond
627+
if string(next.Key) == "has_expire" && next.ExpireAt != expectExpireAt {
628+
t.Fatalf("has_expire's ExpireAt should be 10,actually is %d", next.ExpireAt)
629+
}
630+
}
631+
time.Sleep(time.Duration(expireSecond) * time.Second)
632+
it2 := cache.NewIterator()
633+
for {
634+
next := it2.Next()
635+
if next == nil {
636+
return
637+
}
638+
if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
639+
t.Fatalf("no_expire's ExpireAt should be 0")
640+
}
641+
if string(next.Key) == "has_expire" {
642+
t.Fatalf("has_expire should expired")
643+
}
644+
}
645+
}
646+
610647
func TestSetLargerEntryDeletesWrongEntry(t *testing.T) {
611648
cachesize := 512 * 1024
612649
cache := NewCache(cachesize)
@@ -1041,7 +1078,8 @@ func TestUpdate(t *testing.T) {
10411078
updaterReplace = replace
10421079
}
10431080

1044-
assertExpectations := func(testCase int, expectedFound, expectedReplaced bool, expectedPrevVal []byte, expectedVal []byte) {
1081+
assertExpectations := func(testCase int, expectedFound, expectedReplaced bool, expectedPrevVal []byte,
1082+
expectedVal []byte) {
10451083
failPrefix := fmt.Sprintf("%s(%d)", testName, testCase)
10461084

10471085
if expectedFound != found {
@@ -1054,7 +1092,8 @@ func TestUpdate(t *testing.T) {
10541092
t.Fatalf("%s unexpected err %v", failPrefix, err)
10551093
}
10561094
if string(prevVal) != string(expectedPrevVal) {
1057-
t.Fatalf("%s previous value expected %s instead of %s", failPrefix, string(expectedPrevVal), string(prevVal))
1095+
t.Fatalf("%s previous value expected %s instead of %s", failPrefix, string(expectedPrevVal),
1096+
string(prevVal))
10581097
}
10591098

10601099
// Check value

iterator.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type Iterator struct {
1414

1515
// Entry represents a key/value pair.
1616
type Entry struct {
17-
Key []byte
18-
Value []byte
17+
Key []byte
18+
Value []byte
19+
ExpireAt uint32
1920
}
2021

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

0 commit comments

Comments
 (0)