Save metadata on Sync (#197)

* Save metadata on Sync

* Add test
This commit is contained in:
Haleem Assal 2020-12-14 16:32:48 -04:00 committed by GitHub
parent 3a6235ea03
commit 29e1cf648b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

@ -125,6 +125,11 @@ func (b *Bitcask) close() error {
func (b *Bitcask) Sync() error {
b.mu.RLock()
defer b.mu.RUnlock()
if err := b.saveMetadata(); err != nil {
return err
}
return b.curr.Sync()
}
@ -201,12 +206,7 @@ func (b *Bitcask) Put(key, value []byte) error {
}
// in case of successful `put`, IndexUpToDate will be always be false
if b.metadata.IndexUpToDate {
b.metadata.IndexUpToDate = false
if err := b.saveMetadata(); err != nil {
return err
}
}
b.metadata.IndexUpToDate = false
if oldItem, found := b.trie.Search(key); found {
b.metadata.ReclaimableSpace += oldItem.(internal.Item).Size

@ -633,6 +633,11 @@ func TestSync(t *testing.T) {
value := []byte("foobar")
err = db.Put(key, value)
})
t.Run("Put", func(t *testing.T) {
err = db.Put([]byte("hello"), []byte("world"))
assert.NoError(err)
})
}
func TestMaxKeySize(t *testing.T) {