Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 4fe64b9

Browse files
committed
shed: Index.Offset - correct handling of nil start Item
1 parent 44b6459 commit 4fe64b9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

shed/index.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,15 @@ func (f Index) Offset(start *Item, shift int64) (i Item, err error) {
458458
if err != nil {
459459
return i, err
460460
}
461+
} else {
462+
startKey = f.prefix
461463
}
464+
462465
it := f.db.NewIterator()
463466
defer it.Release()
464467

465468
ok := it.Seek(startKey)
466-
if !ok || bytes.Compare(it.Key(), startKey) != 0 {
469+
if !ok || !bytes.HasPrefix(it.Key(), startKey) {
467470
return i, errors.New("start Item not found in index")
468471
}
469472

@@ -473,7 +476,7 @@ func (f Index) Offset(start *Item, shift int64) (i Item, err error) {
473476
shift *= -1
474477
}
475478

476-
key := startKey
479+
key := it.Key()
477480
for shift != 0 && next() {
478481
key = it.Key()
479482
if key[0] != f.prefix[0] {

shed/index_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,4 +1247,18 @@ func TestIndexOffset(t *testing.T) {
12471247
tt.Error("expected error")
12481248
}
12491249
})
1250+
1251+
t.Run("nil start Item", func(tt *testing.T) {
1252+
item, err := index1.Offset(nil, 0)
1253+
if err != nil {
1254+
t.Error(err)
1255+
}
1256+
checkItem(t, item, items[0])
1257+
1258+
item, err = index2.Offset(nil, 10)
1259+
if err != nil {
1260+
t.Error(err)
1261+
}
1262+
checkItem(t, item, items[10])
1263+
})
12501264
}

0 commit comments

Comments
 (0)