Sfoglia il codice sorgente

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.
Yicheng Qin 10 anni fa
parent
commit
158d6e0e03
2 ha cambiato i file con 28 aggiunte e 1 eliminazioni
  1. 5 1
      storage/key_index.go
  2. 23 0
      storage/key_index_test.go

+ 5 - 1
storage/key_index.go

@@ -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
 		}
 	}

+ 23 - 0
storage/key_index_test.go

@@ -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)