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

Commit 39406f9

Browse files
committed
shed: fixed implementation of Index.Offset
+ more test cases
1 parent d4216f0 commit 39406f9

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

shed/index.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,21 +462,26 @@ func (f Index) Offset(start *Item, shift int64) (i Item, err error) {
462462
it := f.db.NewIterator()
463463
defer it.Release()
464464

465+
ok := it.Seek(startKey)
466+
if !ok || bytes.Compare(it.Key(), startKey) != 0 {
467+
return i, errors.New("start Item not found in index")
468+
}
469+
465470
next := it.Next
466471
if shift < 0 {
467472
next = it.Prev
468473
shift *= -1
469474
}
470475

471-
var key []byte
472-
for ok := it.Seek(startKey); ok && shift > 0; ok = next() {
476+
key := startKey
477+
for shift != 0 && next() {
473478
key = it.Key()
474479
if key[0] != f.prefix[0] {
475480
break
476481
}
477482
shift--
478483
}
479-
if key == nil {
484+
if shift != 0 {
480485
return i, errors.New("key not found")
481486
}
482487

shed/index_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,17 @@ func TestIndexOffset(t *testing.T) {
11381138
t.Fatal(err)
11391139
}
11401140

1141+
index4, err := db.NewIndex("test4", retrievalIndexFuncs)
1142+
if err != nil {
1143+
t.Fatal(err)
1144+
}
1145+
11411146
items := generateItems(100)
11421147
for _, item := range items {
11431148
index1.Put(item)
11441149
index2.Put(item)
1145-
index3.Put(item)
1150+
// index3 is intentionally empty
1151+
index4.Put(item)
11461152
}
11471153

11481154
tests := []struct {
@@ -1175,6 +1181,11 @@ func TestIndexOffset(t *testing.T) {
11751181
checkItem(tt, item, items[tc.start+tc.offset])
11761182

11771183
item, err = index3.Offset(&items[tc.start], int64(tc.offset))
1184+
if err == nil {
1185+
tt.Error("expected error")
1186+
}
1187+
1188+
item, err = index4.Offset(&items[tc.start], int64(tc.offset))
11781189
if err != nil {
11791190
tt.Error(err)
11801191
}
@@ -1206,6 +1217,34 @@ func TestIndexOffset(t *testing.T) {
12061217
if err == nil {
12071218
tt.Error("expected error")
12081219
}
1220+
_, err = index4.Offset(&items[tc.start], int64(tc.offset))
1221+
if err == nil {
1222+
tt.Error("expected error")
1223+
}
12091224
})
12101225
}
1226+
1227+
t.Run("Invalid start Item", func(tt *testing.T) {
1228+
invalid := Item{
1229+
Address: []byte("out-of-index"),
1230+
Data: []byte("not so random data"),
1231+
AccessTimestamp: time.Now().UnixNano(),
1232+
}
1233+
_, err := index1.Offset(&invalid, 0)
1234+
if err == nil {
1235+
tt.Error("expected error")
1236+
}
1237+
_, err = index2.Offset(&invalid, 0)
1238+
if err == nil {
1239+
tt.Error("expected error")
1240+
}
1241+
_, err = index3.Offset(&invalid, 0)
1242+
if err == nil {
1243+
tt.Error("expected error")
1244+
}
1245+
_, err = index4.Offset(&invalid, 0)
1246+
if err == nil {
1247+
tt.Error("expected error")
1248+
}
1249+
})
12111250
}

0 commit comments

Comments
 (0)