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:
parent
4dfe42cb3b
commit
be3fd71ebe
1
AUTHORS
1
AUTHORS
@ -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>
|
||||
|
17
bitcask.go
17
bitcask.go
@ -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()
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user