mirror of https://github.com/tidwall/buntdb.git
db close check in shrink
This commit is contained in:
parent
2a94eebbca
commit
48f8304351
13
buntdb.go
13
buntdb.go
|
@ -111,6 +111,9 @@ type Config struct {
|
||||||
// AutoShrinkMinSize defines the minimum size of the aof file before
|
// AutoShrinkMinSize defines the minimum size of the aof file before
|
||||||
// an automatic shrink can occur.
|
// an automatic shrink can occur.
|
||||||
AutoShrinkMinSize int
|
AutoShrinkMinSize int
|
||||||
|
|
||||||
|
// AutoShrinkDisabled turns off automatic background shrinking
|
||||||
|
AutoShrinkDisabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// exctx is a simple b-tree context for ordering by expiration.
|
// exctx is a simple b-tree context for ordering by expiration.
|
||||||
|
@ -442,7 +445,7 @@ func (db *DB) backgroundManager() {
|
||||||
// Open a standard view. This will take a full lock of the
|
// Open a standard view. This will take a full lock of the
|
||||||
// database thus allowing for access to anything we need.
|
// database thus allowing for access to anything we need.
|
||||||
err := db.Update(func(tx *Tx) error {
|
err := db.Update(func(tx *Tx) error {
|
||||||
if db.persist {
|
if db.persist && !db.config.AutoShrinkDisabled {
|
||||||
pos, err := db.file.Seek(0, 1)
|
pos, err := db.file.Seek(0, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -575,11 +578,14 @@ func (db *DB) Shrink() error {
|
||||||
// We reached this far so all of the items have been written to a new tmp
|
// We reached this far so all of the items have been written to a new tmp
|
||||||
// There's some more work to do by appending the new line from the aof
|
// There's some more work to do by appending the new line from the aof
|
||||||
// to the tmp file and finally swap the files out.
|
// to the tmp file and finally swap the files out.
|
||||||
err = func() error {
|
return func() error {
|
||||||
// We're wrapping this in a function to get the benefit of a defered
|
// We're wrapping this in a function to get the benefit of a defered
|
||||||
// lock/unlock.
|
// lock/unlock.
|
||||||
db.mu.Lock()
|
db.mu.Lock()
|
||||||
defer db.mu.Unlock()
|
defer db.mu.Unlock()
|
||||||
|
if db.closed {
|
||||||
|
return ErrDatabaseClosed
|
||||||
|
}
|
||||||
// We are going to open a new version of the aof file so that we do
|
// We are going to open a new version of the aof file so that we do
|
||||||
// not change the seek position of the previous. This may cause a
|
// not change the seek position of the previous. This may cause a
|
||||||
// problem in the future if we choose to use syscall file locking.
|
// problem in the future if we choose to use syscall file locking.
|
||||||
|
@ -606,7 +612,7 @@ func (db *DB) Shrink() error {
|
||||||
if err := db.file.Close(); err != nil {
|
if err := db.file.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Anything failures below here is really bad. So just panic.
|
// Any failures below here is really bad. So just panic.
|
||||||
if err := os.Rename(tmpname, fname); err != nil {
|
if err := os.Rename(tmpname, fname); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -623,7 +629,6 @@ func (db *DB) Shrink() error {
|
||||||
db.lastaofsz = int(pos)
|
db.lastaofsz = int(pos)
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
func loadReadLine(r *bufio.Reader) (string, error) {
|
func loadReadLine(r *bufio.Reader) (string, error) {
|
||||||
line, err := r.ReadBytes('\n')
|
line, err := r.ReadBytes('\n')
|
||||||
|
|
Loading…
Reference in New Issue