Skip to content

Commit

Permalink
memdb: fix memdb snapshot get/iter is not actually snapshot (#1393) (#…
Browse files Browse the repository at this point in the history
…1433)

ref #1394

Signed-off-by: you06 <[email protected]>
  • Loading branch information
you06 authored Sep 6, 2024
1 parent a8fc3fd commit 1f18db9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/unionstore/memdb_arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (l *memdbVlog) getSnapshotValue(addr memdbArenaAddr, snap *MemDBCheckpoint)
if result.isNull() {
return nil, false
}
return l.getValue(addr), true
return l.getValue(result), true
}

func (l *memdbVlog) selectValueHistory(addr memdbArenaAddr, predicate func(memdbArenaAddr) bool) memdbArenaAddr {
Expand Down
36 changes: 36 additions & 0 deletions internal/unionstore/memdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package unionstore

import (
"context"
"encoding/binary"
"fmt"
"testing"
Expand Down Expand Up @@ -860,3 +861,38 @@ func TestUnsetTemporaryFlag(t *testing.T) {
require.Nil(err)
require.False(flags.HasNeedConstraintCheckInPrewrite())
}

func TestSnapshotGetIter(t *testing.T) {
assert := assert.New(t)
buffer := newMemDB()
var getters []Getter
var iters []Iterator
for i := 0; i < 100; i++ {
assert.Nil(buffer.Set([]byte{byte(0)}, []byte{byte(i)}))
// getter
getter := buffer.SnapshotGetter()
val, err := getter.Get(context.Background(), []byte{byte(0)})
assert.Nil(err)
assert.Equal(val, []byte{byte(min(i, 50))})
getters = append(getters, getter)
// iter
iter := buffer.SnapshotIter(nil, nil)
assert.Nil(err)
assert.Equal(iter.Key(), []byte{byte(0)})
assert.Equal(iter.Value(), []byte{byte(min(i, 50))})
iter.Close()
iters = append(iters, buffer.SnapshotIter(nil, nil))
if i == 50 {
_ = buffer.Staging()
}
}
for _, getter := range getters {
val, err := getter.Get(context.Background(), []byte{byte(0)})
assert.Nil(err)
assert.Equal(val, []byte{byte(50)})
}
for _, iter := range iters {
assert.Equal(iter.Key(), []byte{byte(0)})
assert.Equal(iter.Value(), []byte{byte(50)})
}
}

0 comments on commit 1f18db9

Please sign in to comment.