1
0
mirror of https://github.com/taigrr/bitcask synced 2025-01-18 04:03:17 -08:00

Fix loadIndex to be deterministic (#115)

This commit is contained in:
Alain Gilbert 2019-12-19 18:45:10 -10:00 committed by James Mills
parent 4dfe42cb3b
commit be3fd71ebe
3 changed files with 34 additions and 1 deletions

View File

@ -2,6 +2,7 @@
# Name or Organization <email address>
# The email address is not required for organizations.
Alain Gilbert <alain.gilbert.15@gmail.com>
Awn Umar <awn@spacetime.dev>
Christian Muehlhaeuser <muesli@gmail.com>
Ignacio Hagopian <jsign.uy@gmail.com>

View File

@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"sort"
"sync"
"github.com/gofrs/flock"
@ -468,13 +469,27 @@ func loadDatafiles(path string, maxKeySize uint32, maxValueSize uint64) (datafil
return
}
func getSortedDatafiles(datafiles map[int]data.Datafile) []data.Datafile {
out := make([]data.Datafile, len(datafiles))
idx := 0
for _, df := range datafiles {
out[idx] = df
idx++
}
sort.Slice(out, func(i, j int) bool {
return out[i].FileID() < out[j].FileID()
})
return out
}
func loadIndex(path string, indexer index.Indexer, maxKeySize uint32, datafiles map[int]data.Datafile) (art.Tree, error) {
t, found, err := indexer.Load(filepath.Join(path, "index"), maxKeySize)
if err != nil {
return nil, err
}
if !found {
for _, df := range datafiles {
sortedDatafiles := getSortedDatafiles(datafiles)
for _, df := range sortedDatafiles {
var offset int64
for {
e, n, err := df.Read()

View File

@ -133,6 +133,23 @@ func TestAll(t *testing.T) {
})
}
func TestReopen1(t *testing.T) {
assert := assert.New(t)
for i := 0; i < 10; i ++ {
testdir, _ := ioutil.TempDir("", "bitcask")
db, _ := Open(testdir, WithMaxDatafileSize(1))
_ = db.Put([]byte("foo"), []byte("bar"))
_ = db.Put([]byte("foo"), []byte("bar1"))
_ = db.Put([]byte("foo"), []byte("bar2"))
_ = db.Put([]byte("foo"), []byte("bar3"))
_ = db.Put([]byte("foo"), []byte("bar4"))
_ = db.Put([]byte("foo"), []byte("bar5"))
_ = db.Reopen()
val, _ := db.Get([]byte("foo"))
assert.Equal("bar5", string(val))
}
}
func TestReopen(t *testing.T) {
assert := assert.New(t)