Skip to content

Commit

Permalink
Fix seek bug (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam authored Jan 28, 2017
1 parent 346ee5e commit 9d9ca95
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *Cursor) Seek(key string) {
c.first = true
for rec != nil {
rec.lock.RLock()
if rec.Key > key {
if rec.Key >= key {
if (rec.Deleted > 0 && rec.Deleted <= c.snapshot) || (rec.Offset >= c.snapshot) {
oldRec := rec
rec = c.collection.nextRecord(rec)
Expand All @@ -159,7 +159,7 @@ func (c *Cursor) Seek(key string) {
oldRec.lock.RUnlock()
continue
}
if rec.Key <= key {
if rec.Key < key {
c.current = rec
}
oldRec := rec
Expand Down
46 changes: 46 additions & 0 deletions lm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,49 @@ func TestDeleteInFirstUpdate(t *testing.T) {
t.Fatal(err)
}
}

func TestSeekOverwrittenKey(t *testing.T) {
c, err := NewCollection("/tmp/test_seekoverwrittenkey.lm2", 100)
if err != nil {
t.Fatal(err)
}
defer c.Destroy()

wb := NewWriteBatch()
wb.Set("committed", "0")
wb.Delete("pending")
_, err = c.Update(wb)
if err != nil {
t.Fatal(err)
}

wb = NewWriteBatch()
wb.Set("1", "a")
wb.Set("pending", "1")
_, err = c.Update(wb)
if err != nil {
t.Fatal(err)
}

wb = NewWriteBatch()
wb.Set("committed", "1")
wb.Delete("pending")
_, err = c.Update(wb)
if err != nil {
t.Fatal(err)
}

cur, err := c.NewCursor()
if err != nil {
t.Fatal(err)
}

cur.Seek("committed")
if !cur.Next() {
t.Fatal("expected cur.Next() to return true")
}

if cur.Key() != "committed" {
t.Fatalf("expected cur.Key() to be %s, got %s", "committed", cur.Key())
}
}

0 comments on commit 9d9ca95

Please sign in to comment.