storage: fix calculating generation in keyIndex.since

It should skip last empty generation when the key is just tombstoned.

The rev15 and rev16 in the test fails if it doesn't skip last empty generation
and find previous generations.
release-2.3
Yicheng Qin 2015-09-17 18:59:42 -07:00
parent be80d11948
commit 158d6e0e03
2 changed files with 28 additions and 1 deletions

View File

@ -157,7 +157,11 @@ func (ki *keyIndex) since(rev int64) []revision {
var gi int
// find the generations to start checking
for gi = len(ki.generations) - 1; gi > 0; gi-- {
if since.GreaterThan(ki.generations[gi].created) {
g := ki.generations[gi]
if g.isEmpty() {
continue
}
if since.GreaterThan(g.created) {
break
}
}

View File

@ -84,6 +84,29 @@ func TestKeyIndexGet(t *testing.T) {
}
}
func TestKeyIndexSince(t *testing.T) {
ki := newTestKeyIndex()
ki.compact(4, make(map[revision]struct{}))
allRevs := []revision{{4, 0}, {6, 0}, {8, 0}, {10, 0}, {12, 0}, {14, 1}, {16, 0}}
tests := []struct {
rev int64
wrevs []revision
}{
{17, nil},
{16, allRevs[6:]},
{15, allRevs[6:]},
}
for i, tt := range tests {
revs := ki.since(tt.rev)
if !reflect.DeepEqual(revs, tt.wrevs) {
t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
}
}
}
func TestKeyIndexPut(t *testing.T) {
ki := &keyIndex{key: []byte("foo")}
ki.put(5, 0)