Compare commits

...

2 Commits

Author SHA1 Message Date
James Mills
7149cb9afe Fixed concurrency bug with reopening datafiles when maxDatafileSize is exceeded 2019-03-22 17:59:07 +10:00
James Mills
c593bc966f Refactor Datafile.Size() 2019-03-22 17:33:24 +10:00
2 changed files with 8 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"github.com/gofrs/flock" "github.com/gofrs/flock"
"github.com/prologic/trie" "github.com/prologic/trie"
@@ -40,6 +41,8 @@ var (
// and in-memory hash of key/value pairs as per the Bitcask paper and seen // and in-memory hash of key/value pairs as per the Bitcask paper and seen
// in the Riak database. // in the Riak database.
type Bitcask struct { type Bitcask struct {
mu sync.RWMutex
*flock.Flock *flock.Flock
config *config config *config
@@ -175,11 +178,10 @@ func (b *Bitcask) Fold(f func(key string) error) error {
} }
func (b *Bitcask) put(key string, value []byte) (int64, error) { func (b *Bitcask) put(key string, value []byte) (int64, error) {
size, err := b.curr.Size() b.mu.Lock()
if err != nil { defer b.mu.Unlock()
return -1, err
}
size := b.curr.Size()
if size >= int64(b.config.maxDatafileSize) { if size >= int64(b.config.maxDatafileSize) {
err := b.curr.Close() err := b.curr.Close()
if err != nil { if err != nil {

View File

@@ -98,10 +98,10 @@ func (df *Datafile) Sync() error {
return df.w.Sync() return df.w.Sync()
} }
func (df *Datafile) Size() (int64, error) { func (df *Datafile) Size() int64 {
df.RLock() df.RLock()
defer df.RUnlock() defer df.RUnlock()
return df.offset, nil return df.offset
} }
func (df *Datafile) Read() (e pb.Entry, err error) { func (df *Datafile) Read() (e pb.Entry, err error) {