allow only one shrink at a time.

This commit is contained in:
Josh Baker 2016-07-26 10:25:29 -07:00
parent 70dec49583
commit d6a4f5c635
1 changed files with 31 additions and 14 deletions

View File

@ -50,6 +50,9 @@ var (
// ErrInvalidSyncPolicy is returned for an invalid SyncPolicy value.
ErrInvalidSyncPolicy = errors.New("invalid sync policy")
// ErrShrinkInProcess is returned when a shrink operation is in-process.
ErrShrinkInProcess = errors.New("shrink is in-process")
)
// Iterator allows callers of Ascend* or Descend* to iterate in-order
@ -73,6 +76,7 @@ type DB struct {
aoflen int // the number of lines in the aof file
config Config // the database configuration
persist bool // do we write to disk
shrinking bool // when an aof shrink is in-process.
}
// SyncPolicy represents how often data is synced to disk.
@ -107,10 +111,12 @@ type Config struct {
// disk may be up to 2x the number of items on disk.
// A zero value will disable auto shrinking.
// A negative value or 1 are invalid.
// The default is value is 5.
// The default is value is 50, but may change in the future.
AutoShrink int
}
const defaultAutoShrinkMultiplier = 50
// exctx is a simple b-tree context for ordering by expiration.
type exctx struct {
db *DB
@ -125,7 +131,7 @@ func Open(path string) (*DB, error) {
db.idxs = make(map[string]*index)
db.config = Config{
SyncPolicy: EverySecond,
AutoShrink: 5,
AutoShrink: defaultAutoShrinkMultiplier,
}
db.persist = path != ":memory:"
if db.persist {
@ -501,6 +507,17 @@ func (db *DB) Shrink() error {
db.mu.Unlock()
return nil
}
if db.shrinking {
// The database is already in the process of shrinking.
db.mu.Unlock()
return ErrShrinkInProcess
}
db.shrinking = true
defer func() {
db.mu.Lock()
db.shrinking = false
db.mu.Unlock()
}()
fname := db.file.Name()
tmpname := fname + ".tmp"
// the endpos is used to return to the end of the file when we are