aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
authorGyu-Ho Lee <gyuhox@gmail.com>2015-12-21 15:28:44 -0800
committerGyu-Ho Lee <gyuhox@gmail.com>2015-12-21 15:36:00 -0800
commit082efcc23e41f42d15f9e90f4e079fdb793e997f (patch)
treea88f0fb9f747a01a256e9dc33b8f7a99bd78a789 /db.go
parentMerge pull request #470 from ReadmeCritic/master (diff)
downloaddedo-082efcc23e41f42d15f9e90f4e079fdb793e997f.tar.gz
dedo-082efcc23e41f42d15f9e90f4e079fdb793e997f.tar.xz
Introduce InitialMmapSize to prevent deadlock
InitialMmapSize is the initial mmap size of the database in bytes. Read transaction won't block write transaction if InitialMmapSize is large enough to handle mmap size. Copied from https://github.com/boltdb/bolt/pull/432.
Diffstat (limited to 'db.go')
-rw-r--r--db.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/db.go b/db.go
index d5ad715..7924056 100644
--- a/db.go
+++ b/db.go
@@ -196,7 +196,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
}
// Memory map the data file.
- if err := db.mmap(0); err != nil {
+ if err := db.mmap(options.InitialMmapSize); err != nil {
_ = db.close()
return nil, err
}
@@ -411,6 +411,10 @@ func (db *DB) close() error {
// writer to deadlock because the database periodically needs to re-mmap itself
// as it grows and it cannot do that while a read transaction is open.
//
+// If a long running read transaction (for example, a snapshot transaction) is
+// needed, you might want to set DB.InitialMmapSize to a large enough value
+// to avoid potential blocking of write transaction.
+//
// IMPORTANT: You must close read-only transactions after you are finished or
// else the database will not reclaim old pages.
func (db *DB) Begin(writable bool) (*Tx, error) {
@@ -680,6 +684,16 @@ type Options struct {
// Sets the DB.MmapFlags flag before memory mapping the file.
MmapFlags int
+
+ // InitialMmapSize is the initial mmap size of the database
+ // in bytes. Read transactions won't block write transaction
+ // if the InitialMmapSize is large enough to hold database mmap
+ // size. (See DB.Begin for more information)
+ //
+ // If <=0, the initial map size is 0.
+ // If initialMmapSize is smaller than the previous database size,
+ // it takes no effect.
+ InitialMmapSize int
}
// DefaultOptions represent the options used if nil options are passed into Open().