diff options
author | Tommi Virtanen <tv@eagain.net> | 2014-08-24 15:42:55 -0700 |
---|---|---|
committer | Tommi Virtanen <tv@eagain.net> | 2015-02-18 12:26:45 -0800 |
commit | adbb1a19c1b93a95f17a4eb3c5524e5be8b0d10f (patch) | |
tree | 03bf30a873b68f7820dcb910b87eb639f22786ae /db.go | |
parent | Merge pull request #309 from everdev/operation-go (diff) | |
download | dedo-adbb1a19c1b93a95f17a4eb3c5524e5be8b0d10f.tar.gz dedo-adbb1a19c1b93a95f17a4eb3c5524e5be8b0d10f.tar.xz |
Add transaction batching
DB.Batch makes it easy to make lots of small transactions with
significantly better performance. Batch combines multiple concurrent
Update calls into a single disk transaction, managing errors smartly.
Diffstat (limited to 'db.go')
-rw-r--r-- | db.go | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -27,6 +27,12 @@ const magic uint32 = 0xED0CDAED // must be synchronzied using the msync(2) syscall. const IgnoreNoSync = runtime.GOOS == "openbsd" +// Default values if not set in a DB instance. +const ( + DefaultMaxBatchSize int = 1000 + DefaultMaxBatchDelay = 10 * time.Millisecond +) + // DB represents a collection of buckets persisted to a file on disk. // All data access is performed through transactions which can be obtained through the DB. // All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called. @@ -49,6 +55,22 @@ type DB struct { // THIS IS UNSAFE. PLEASE USE WITH CAUTION. NoSync bool + // MaxBatchSize is the maximum size of a batch. Default value is + // copied from DefaultMaxBatchSize in Open. + // + // If <=0, disables batching. + // + // Do not change concurrently with calls to Batch. + MaxBatchSize int + + // MaxBatchDelay is the maximum delay before a batch starts. + // Default value is copied from DefaultMaxBatchDelay in Open. + // + // If <=0, effectively disables batching. + // + // Do not change concurrently with calls to Batch. + MaxBatchDelay time.Duration + path string file *os.File dataref []byte @@ -63,6 +85,9 @@ type DB struct { freelist *freelist stats Stats + batchMu sync.Mutex + batch *batch + rwlock sync.Mutex // Allows only one writer at a time. metalock sync.Mutex // Protects meta page access. mmaplock sync.RWMutex // Protects mmap access during remapping. @@ -99,6 +124,10 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { options = DefaultOptions } + // Set default values for later DB operations. + db.MaxBatchSize = DefaultMaxBatchSize + db.MaxBatchDelay = DefaultMaxBatchDelay + // Open data file and separate sync handler for metadata writes. db.path = path |