|
|
bfbe87 |
From a41917c2c88bd7f694d141ac67f4a194aaa16fa1 Mon Sep 17 00:00:00 2001
|
|
|
bfbe87 |
From: Qiang Huang <h.huangqiang@huawei.com>
|
|
|
bfbe87 |
Date: Wed, 28 Oct 2015 08:49:45 +0800
|
|
|
bfbe87 |
Subject: [PATCH] Bump bolt to v1.1.0
|
|
|
bfbe87 |
|
|
|
bfbe87 |
It adds ARM64, ppc64le, s390x, solaris support, and a bunch of
|
|
|
bfbe87 |
bugfixs.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
|
|
|
bfbe87 |
---
|
|
|
bfbe87 |
hack/vendor.sh | 2 +-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/.gitignore | 1 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/README.md | 250 +++++++++++++++++++--
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/batch.go | 138 ++++++++++++
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_386.go | 5 +-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_amd64.go | 3 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_arm.go | 5 +-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_arm64.go | 9 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_ppc64le.go | 9 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_s390x.go | 9 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_unix.go | 37 ++-
|
|
|
bfbe87 |
.../github.com/boltdb/bolt/bolt_unix_solaris.go | 101 +++++++++
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bolt_windows.go | 10 +-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/bucket.go | 29 ++-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/cursor.go | 12 +-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/db.go | 195 ++++++++++++----
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/errors.go | 4 +
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/freelist.go | 28 ++-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/node.go | 36 ++-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/page.go | 45 +++-
|
|
|
bfbe87 |
vendor/src/github.com/boltdb/bolt/tx.go | 80 +++++--
|
|
|
bfbe87 |
21 files changed, 886 insertions(+), 122 deletions(-)
|
|
|
bfbe87 |
create mode 100644 vendor/src/github.com/boltdb/bolt/batch.go
|
|
|
bfbe87 |
create mode 100644 vendor/src/github.com/boltdb/bolt/bolt_arm64.go
|
|
|
bfbe87 |
create mode 100644 vendor/src/github.com/boltdb/bolt/bolt_ppc64le.go
|
|
|
bfbe87 |
create mode 100644 vendor/src/github.com/boltdb/bolt/bolt_s390x.go
|
|
|
bfbe87 |
create mode 100644 vendor/src/github.com/boltdb/bolt/bolt_unix_solaris.go
|
|
|
bfbe87 |
|
|
|
bfbe87 |
diff --git a/hack/vendor.sh b/hack/vendor.sh
|
|
|
bfbe87 |
index d872d4a..c28e677 100755
|
|
|
bfbe87 |
--- a/hack/vendor.sh
|
|
|
bfbe87 |
+++ b/hack/vendor.sh
|
|
|
bfbe87 |
@@ -36,7 +36,7 @@ clone git github.com/coreos/etcd v2.2.0
|
|
|
bfbe87 |
fix_rewritten_imports github.com/coreos/etcd
|
|
|
bfbe87 |
clone git github.com/ugorji/go 5abd4e96a45c386928ed2ca2a7ef63e2533e18ec
|
|
|
bfbe87 |
clone git github.com/hashicorp/consul v0.5.2
|
|
|
bfbe87 |
-clone git github.com/boltdb/bolt v1.0
|
|
|
bfbe87 |
+clone git github.com/boltdb/bolt v1.1.0
|
|
|
bfbe87 |
|
|
|
bfbe87 |
# get graph and distribution packages
|
|
|
bfbe87 |
clone git github.com/docker/distribution 20c4b7a1805a52753dfd593ee1cc35558722a0ce # docker/1.9 branch
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/.gitignore b/vendor/src/github.com/boltdb/bolt/.gitignore
|
|
|
bfbe87 |
index b2bb382..c7bd2b7 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/.gitignore
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/.gitignore
|
|
|
bfbe87 |
@@ -1,3 +1,4 @@
|
|
|
bfbe87 |
*.prof
|
|
|
bfbe87 |
*.test
|
|
|
bfbe87 |
+*.swp
|
|
|
bfbe87 |
/bin/
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/README.md b/vendor/src/github.com/boltdb/bolt/README.md
|
|
|
bfbe87 |
index 727e977..0a33ebc 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/README.md
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/README.md
|
|
|
bfbe87 |
@@ -16,7 +16,7 @@ and setting values. That's it.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
## Project Status
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-Bolt is stable and the API is fixed. Full unit test coverage and randomized
|
|
|
bfbe87 |
+Bolt is stable and the API is fixed. Full unit test coverage and randomized
|
|
|
bfbe87 |
black box testing are used to ensure database consistency and thread safety.
|
|
|
bfbe87 |
Bolt is currently in high-load production environments serving databases as
|
|
|
bfbe87 |
large as 1TB. Many companies such as Shopify and Heroku use Bolt-backed
|
|
|
bfbe87 |
@@ -87,6 +87,11 @@ are not thread safe. To work with data in multiple goroutines you must start
|
|
|
bfbe87 |
a transaction for each one or use locking to ensure only one goroutine accesses
|
|
|
bfbe87 |
a transaction at a time. Creating transaction from the `DB` is thread safe.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+Read-only transactions and read-write transactions should not depend on one
|
|
|
bfbe87 |
+another and generally shouldn't be opened simultaneously in the same goroutine.
|
|
|
bfbe87 |
+This can cause a deadlock as the read-write transaction needs to periodically
|
|
|
bfbe87 |
+re-map the data file but it cannot do so while a read-only transaction is open.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
|
|
|
bfbe87 |
#### Read-write transactions
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -120,12 +125,88 @@ err := db.View(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
})
|
|
|
bfbe87 |
```
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-You also get a consistent view of the database within this closure, however,
|
|
|
bfbe87 |
+You also get a consistent view of the database within this closure, however,
|
|
|
bfbe87 |
no mutating operations are allowed within a read-only transaction. You can only
|
|
|
bfbe87 |
retrieve buckets, retrieve values, and copy the database within a read-only
|
|
|
bfbe87 |
transaction.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+#### Batch read-write transactions
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+Each `DB.Update()` waits for disk to commit the writes. This overhead
|
|
|
bfbe87 |
+can be minimized by combining multiple updates with the `DB.Batch()`
|
|
|
bfbe87 |
+function:
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```go
|
|
|
bfbe87 |
+err := db.Batch(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
+ ...
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+})
|
|
|
bfbe87 |
+```
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+Concurrent Batch calls are opportunistically combined into larger
|
|
|
bfbe87 |
+transactions. Batch is only useful when there are multiple goroutines
|
|
|
bfbe87 |
+calling it.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+The trade-off is that `Batch` can call the given
|
|
|
bfbe87 |
+function multiple times, if parts of the transaction fail. The
|
|
|
bfbe87 |
+function must be idempotent and side effects must take effect only
|
|
|
bfbe87 |
+after a successful return from `DB.Batch()`.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+For example: don't display messages from inside the function, instead
|
|
|
bfbe87 |
+set variables in the enclosing scope:
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```go
|
|
|
bfbe87 |
+var id uint64
|
|
|
bfbe87 |
+err := db.Batch(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
+ // Find last key in bucket, decode as bigendian uint64, increment
|
|
|
bfbe87 |
+ // by one, encode back to []byte, and add new key.
|
|
|
bfbe87 |
+ ...
|
|
|
bfbe87 |
+ id = newValue
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+})
|
|
|
bfbe87 |
+if err != nil {
|
|
|
bfbe87 |
+ return ...
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+fmt.Println("Allocated ID %d", id)
|
|
|
bfbe87 |
+```
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+#### Managing transactions manually
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+The `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`
|
|
|
bfbe87 |
+function. These helper functions will start the transaction, execute a function,
|
|
|
bfbe87 |
+and then safely close your transaction if an error is returned. This is the
|
|
|
bfbe87 |
+recommended way to use Bolt transactions.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+However, sometimes you may want to manually start and end your transactions.
|
|
|
bfbe87 |
+You can use the `Tx.Begin()` function directly but _please_ be sure to close the
|
|
|
bfbe87 |
+transaction.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```go
|
|
|
bfbe87 |
+// Start a writable transaction.
|
|
|
bfbe87 |
+tx, err := db.Begin(true)
|
|
|
bfbe87 |
+if err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+defer tx.Rollback()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// Use the transaction...
|
|
|
bfbe87 |
+_, err := tx.CreateBucket([]byte("MyBucket"))
|
|
|
bfbe87 |
+if err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// Commit the transaction and check for error.
|
|
|
bfbe87 |
+if err := tx.Commit(); err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+```
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+The first argument to `DB.Begin()` is a boolean stating if the transaction
|
|
|
bfbe87 |
+should be writable.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
### Using buckets
|
|
|
bfbe87 |
|
|
|
bfbe87 |
Buckets are collections of key/value pairs within the database. All keys in a
|
|
|
bfbe87 |
@@ -175,13 +256,61 @@ db.View(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
```
|
|
|
bfbe87 |
|
|
|
bfbe87 |
The `Get()` function does not return an error because its operation is
|
|
|
bfbe87 |
-guarenteed to work (unless there is some kind of system failure). If the key
|
|
|
bfbe87 |
+guaranteed to work (unless there is some kind of system failure). If the key
|
|
|
bfbe87 |
exists then it will return its byte slice value. If it doesn't exist then it
|
|
|
bfbe87 |
will return `nil`. It's important to note that you can have a zero-length value
|
|
|
bfbe87 |
set to a key which is different than the key not existing.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
Use the `Bucket.Delete()` function to delete a key from the bucket.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+Please note that values returned from `Get()` are only valid while the
|
|
|
bfbe87 |
+transaction is open. If you need to use a value outside of the transaction
|
|
|
bfbe87 |
+then you must use `copy()` to copy it to another byte slice.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+### Autoincrementing integer for the bucket
|
|
|
bfbe87 |
+By using the NextSequence() function, you can let Bolt determine a sequence
|
|
|
bfbe87 |
+which can be used as the unique identifier for your key/value pairs. See the
|
|
|
bfbe87 |
+example below.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```go
|
|
|
bfbe87 |
+// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.
|
|
|
bfbe87 |
+func (s *Store) CreateUser(u *User) error {
|
|
|
bfbe87 |
+ return s.db.Update(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
+ // Retrieve the users bucket.
|
|
|
bfbe87 |
+ // This should be created when the DB is first opened.
|
|
|
bfbe87 |
+ b := tx.Bucket([]byte("users"))
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Generate ID for the user.
|
|
|
bfbe87 |
+ // This returns an error only if the Tx is closed or not writeable.
|
|
|
bfbe87 |
+ // That can't happen in an Update() call so I ignore the error check.
|
|
|
bfbe87 |
+ id, _ = b.NextSequence()
|
|
|
bfbe87 |
+ u.ID = int(id)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Marshal user data into bytes.
|
|
|
bfbe87 |
+ buf, err := json.Marshal(u)
|
|
|
bfbe87 |
+ if err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Persist bytes to users bucket.
|
|
|
bfbe87 |
+ return b.Put(itob(u.ID), buf)
|
|
|
bfbe87 |
+ })
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// itob returns an 8-byte big endian representation of v.
|
|
|
bfbe87 |
+func itob(v int) []byte {
|
|
|
bfbe87 |
+ b := make([]byte, 8)
|
|
|
bfbe87 |
+ binary.BigEndian.PutUint64(b, uint64(v))
|
|
|
bfbe87 |
+ return b
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+type User struct {
|
|
|
bfbe87 |
+ ID int
|
|
|
bfbe87 |
+ ...
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```
|
|
|
bfbe87 |
|
|
|
bfbe87 |
### Iterating over keys
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -254,7 +383,7 @@ db.View(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
max := []byte("2000-01-01T00:00:00Z")
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Iterate over the 90's.
|
|
|
bfbe87 |
- for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) != -1; k, v = c.Next() {
|
|
|
bfbe87 |
+ for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
|
|
|
bfbe87 |
fmt.Printf("%s: %s\n", k, v)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -294,7 +423,7 @@ func (*Bucket) DeleteBucket(key []byte) error
|
|
|
bfbe87 |
|
|
|
bfbe87 |
### Database backups
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-Bolt is a single file so it's easy to backup. You can use the `Tx.Copy()`
|
|
|
bfbe87 |
+Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`
|
|
|
bfbe87 |
function to write a consistent view of the database to a writer. If you call
|
|
|
bfbe87 |
this from a read-only transaction, it will perform a hot backup and not block
|
|
|
bfbe87 |
your other database reads and writes. It will also use `O_DIRECT` when available
|
|
|
bfbe87 |
@@ -305,11 +434,12 @@ do database backups:
|
|
|
bfbe87 |
|
|
|
bfbe87 |
```go
|
|
|
bfbe87 |
func BackupHandleFunc(w http.ResponseWriter, req *http.Request) {
|
|
|
bfbe87 |
- err := db.View(func(tx bolt.Tx) error {
|
|
|
bfbe87 |
+ err := db.View(func(tx *bolt.Tx) error {
|
|
|
bfbe87 |
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
bfbe87 |
w.Header().Set("Content-Disposition", `attachment; filename="my.db"`)
|
|
|
bfbe87 |
w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
|
|
|
bfbe87 |
- return tx.Copy(w)
|
|
|
bfbe87 |
+ _, err := tx.WriteTo(w)
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
})
|
|
|
bfbe87 |
if err != nil {
|
|
|
bfbe87 |
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
bfbe87 |
@@ -351,14 +481,13 @@ go func() {
|
|
|
bfbe87 |
// Grab the current stats and diff them.
|
|
|
bfbe87 |
stats := db.Stats()
|
|
|
bfbe87 |
diff := stats.Sub(&prev)
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Encode stats to JSON and print to STDERR.
|
|
|
bfbe87 |
json.NewEncoder(os.Stderr).Encode(diff)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Save stats for the next loop.
|
|
|
bfbe87 |
prev = stats
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
-}
|
|
|
bfbe87 |
}()
|
|
|
bfbe87 |
```
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -366,25 +495,83 @@ It's also useful to pipe these stats to a service such as statsd for monitoring
|
|
|
bfbe87 |
or to provide an HTTP endpoint that will perform a fixed-length sample.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+### Read-Only Mode
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+Sometimes it is useful to create a shared, read-only Bolt database. To this,
|
|
|
bfbe87 |
+set the `Options.ReadOnly` flag when opening your database. Read-only mode
|
|
|
bfbe87 |
+uses a shared lock to allow multiple processes to read from the database but
|
|
|
bfbe87 |
+it will block any processes from opening the database in read-write mode.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+```go
|
|
|
bfbe87 |
+db, err := bolt.Open("my.db", 0666, &bolt.Options{ReadOnly: true})
|
|
|
bfbe87 |
+if err != nil {
|
|
|
bfbe87 |
+ log.Fatal(err)
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+```
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
## Resources
|
|
|
bfbe87 |
|
|
|
bfbe87 |
For more information on getting started with Bolt, check out the following articles:
|
|
|
bfbe87 |
|
|
|
bfbe87 |
* [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch).
|
|
|
bfbe87 |
+* [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+## Comparison with other databases
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+### Postgres, MySQL, & other relational databases
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+Relational databases structure data into rows and are only accessible through
|
|
|
bfbe87 |
+the use of SQL. This approach provides flexibility in how you store and query
|
|
|
bfbe87 |
+your data but also incurs overhead in parsing and planning SQL statements. Bolt
|
|
|
bfbe87 |
+accesses all data by a byte slice key. This makes Bolt fast to read and write
|
|
|
bfbe87 |
+data by key but provides no built-in support for joining values together.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+Most relational databases (with the exception of SQLite) are standalone servers
|
|
|
bfbe87 |
+that run separately from your application. This gives your systems
|
|
|
bfbe87 |
+flexibility to connect multiple application servers to a single database
|
|
|
bfbe87 |
+server but also adds overhead in serializing and transporting data over the
|
|
|
bfbe87 |
+network. Bolt runs as a library included in your application so all data access
|
|
|
bfbe87 |
+has to go through your application's process. This brings data closer to your
|
|
|
bfbe87 |
+application but limits multi-process access to the data.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+### LevelDB, RocksDB
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that
|
|
|
bfbe87 |
+they are libraries bundled into the application, however, their underlying
|
|
|
bfbe87 |
+structure is a log-structured merge-tree (LSM tree). An LSM tree optimizes
|
|
|
bfbe87 |
+random writes by using a write ahead log and multi-tiered, sorted files called
|
|
|
bfbe87 |
+SSTables. Bolt uses a B+tree internally and only a single file. Both approaches
|
|
|
bfbe87 |
+have trade offs.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+If you require a high random write throughput (>10,000 w/sec) or you need to use
|
|
|
bfbe87 |
+spinning disks then LevelDB could be a good choice. If your application is
|
|
|
bfbe87 |
+read-heavy or does a lot of range scans then Bolt could be a good choice.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-## Comparing Bolt to LMDB
|
|
|
bfbe87 |
+One other important consideration is that LevelDB does not have transactions.
|
|
|
bfbe87 |
+It supports batch writing of key/values pairs and it supports read snapshots
|
|
|
bfbe87 |
+but it will not give you the ability to do a compare-and-swap operation safely.
|
|
|
bfbe87 |
+Bolt supports fully serializable ACID transactions.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+### LMDB
|
|
|
bfbe87 |
|
|
|
bfbe87 |
Bolt was originally a port of LMDB so it is architecturally similar. Both use
|
|
|
bfbe87 |
-a B+tree, have ACID semanetics with fully serializable transactions, and support
|
|
|
bfbe87 |
+a B+tree, have ACID semantics with fully serializable transactions, and support
|
|
|
bfbe87 |
lock-free MVCC using a single writer and multiple readers.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
The two projects have somewhat diverged. LMDB heavily focuses on raw performance
|
|
|
bfbe87 |
while Bolt has focused on simplicity and ease of use. For example, LMDB allows
|
|
|
bfbe87 |
-several unsafe actions such as direct writes and append writes for the sake of
|
|
|
bfbe87 |
-performance. Bolt opts to disallow actions which can leave the database in a
|
|
|
bfbe87 |
-corrupted state. The only exception to this in Bolt is `DB.NoSync`.
|
|
|
bfbe87 |
+several unsafe actions such as direct writes for the sake of performance. Bolt
|
|
|
bfbe87 |
+opts to disallow actions which can leave the database in a corrupted state. The
|
|
|
bfbe87 |
+only exception to this in Bolt is `DB.NoSync`.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+There are also a few differences in API. LMDB requires a maximum mmap size when
|
|
|
bfbe87 |
+opening an `mdb_env` whereas Bolt will handle incremental mmap resizing
|
|
|
bfbe87 |
+automatically. LMDB overloads the getter and setter functions with multiple
|
|
|
bfbe87 |
+flags whereas Bolt splits these specialized cases into their own functions.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
|
|
|
bfbe87 |
## Caveats & Limitations
|
|
|
bfbe87 |
@@ -425,14 +612,33 @@ Here are a few things to note when evaluating and using Bolt:
|
|
|
bfbe87 |
can in memory and will release memory as needed to other processes. This means
|
|
|
bfbe87 |
that Bolt can show very high memory usage when working with large databases.
|
|
|
bfbe87 |
However, this is expected and the OS will release memory as needed. Bolt can
|
|
|
bfbe87 |
- handle databases much larger than the available physical RAM.
|
|
|
bfbe87 |
+ handle databases much larger than the available physical RAM, provided its
|
|
|
bfbe87 |
+ memory-map fits in the process virtual address space. It may be problematic
|
|
|
bfbe87 |
+ on 32-bits systems.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+* The data structures in the Bolt database are memory mapped so the data file
|
|
|
bfbe87 |
+ will be endian specific. This means that you cannot copy a Bolt file from a
|
|
|
bfbe87 |
+ little endian machine to a big endian machine and have it work. For most
|
|
|
bfbe87 |
+ users this is not a concern since most modern CPUs are little endian.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+* Because of the way pages are laid out on disk, Bolt cannot truncate data files
|
|
|
bfbe87 |
+ and return free pages back to the disk. Instead, Bolt maintains a free list
|
|
|
bfbe87 |
+ of unused pages within its data file. These free pages can be reused by later
|
|
|
bfbe87 |
+ transactions. This works well for many use cases as databases generally tend
|
|
|
bfbe87 |
+ to grow. However, it's important to note that deleting large chunks of data
|
|
|
bfbe87 |
+ will not allow you to reclaim that space on disk.
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ For more information on page allocation, [see this comment][page-allocation].
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+[page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638
|
|
|
bfbe87 |
|
|
|
bfbe87 |
|
|
|
bfbe87 |
## Other Projects Using Bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
Below is a list of public, open source projects that use Bolt:
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-* [Bazil](https://github.com/bazillion/bazil) - A file system that lets your data reside where it is most convenient for it to reside.
|
|
|
bfbe87 |
+* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.
|
|
|
bfbe87 |
+* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.
|
|
|
bfbe87 |
* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
|
|
|
bfbe87 |
* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.
|
|
|
bfbe87 |
* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.
|
|
|
bfbe87 |
@@ -450,6 +656,16 @@ Below is a list of public, open source projects that use Bolt:
|
|
|
bfbe87 |
* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
|
|
|
bfbe87 |
* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
|
|
|
bfbe87 |
* [SkyDB](https://github.com/skydb/sky) - Behavioral analytics database.
|
|
|
bfbe87 |
+* [Seaweed File System](https://github.com/chrislusf/weed-fs) - Highly scalable distributed key~file system with O(1) disk read.
|
|
|
bfbe87 |
+* [InfluxDB](http://influxdb.com) - Scalable datastore for metrics, events, and real-time analytics.
|
|
|
bfbe87 |
+* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.
|
|
|
bfbe87 |
+* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.
|
|
|
bfbe87 |
+* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
|
|
|
bfbe87 |
+* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
|
|
|
bfbe87 |
+* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems.
|
|
|
bfbe87 |
+* [stow](https://github.com/djherbis/stow) - a persistence manager for objects
|
|
|
bfbe87 |
+ backed by boltdb.
|
|
|
bfbe87 |
+* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining
|
|
|
bfbe87 |
+ simple tx and key scans.
|
|
|
bfbe87 |
|
|
|
bfbe87 |
If you are using Bolt in a project please send a pull request to add it to the list.
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/batch.go b/vendor/src/github.com/boltdb/bolt/batch.go
|
|
|
bfbe87 |
new file mode 100644
|
|
|
bfbe87 |
index 0000000..84acae6
|
|
|
bfbe87 |
--- /dev/null
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/batch.go
|
|
|
bfbe87 |
@@ -0,0 +1,138 @@
|
|
|
bfbe87 |
+package bolt
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+import (
|
|
|
bfbe87 |
+ "errors"
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
+ "sync"
|
|
|
bfbe87 |
+ "time"
|
|
|
bfbe87 |
+)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// Batch calls fn as part of a batch. It behaves similar to Update,
|
|
|
bfbe87 |
+// except:
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// 1. concurrent Batch calls can be combined into a single Bolt
|
|
|
bfbe87 |
+// transaction.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// 2. the function passed to Batch may be called multiple times,
|
|
|
bfbe87 |
+// regardless of whether it returns error or not.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// This means that Batch function side effects must be idempotent and
|
|
|
bfbe87 |
+// take permanent effect only after a successful return is seen in
|
|
|
bfbe87 |
+// caller.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// The maximum batch size and delay can be adjusted with DB.MaxBatchSize
|
|
|
bfbe87 |
+// and DB.MaxBatchDelay, respectively.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// Batch is only useful when there are multiple goroutines calling it.
|
|
|
bfbe87 |
+func (db *DB) Batch(fn func(*Tx) error) error {
|
|
|
bfbe87 |
+ errCh := make(chan error, 1)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ db.batchMu.Lock()
|
|
|
bfbe87 |
+ if (db.batch == nil) || (db.batch != nil && len(db.batch.calls) >= db.MaxBatchSize) {
|
|
|
bfbe87 |
+ // There is no existing batch, or the existing batch is full; start a new one.
|
|
|
bfbe87 |
+ db.batch = &batch{
|
|
|
bfbe87 |
+ db: db,
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ db.batch.timer = time.AfterFunc(db.MaxBatchDelay, db.batch.trigger)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ db.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh})
|
|
|
bfbe87 |
+ if len(db.batch.calls) >= db.MaxBatchSize {
|
|
|
bfbe87 |
+ // wake up batch, it's ready to run
|
|
|
bfbe87 |
+ go db.batch.trigger()
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ db.batchMu.Unlock()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ err := <-errCh
|
|
|
bfbe87 |
+ if err == trySolo {
|
|
|
bfbe87 |
+ err = db.Update(fn)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+type call struct {
|
|
|
bfbe87 |
+ fn func(*Tx) error
|
|
|
bfbe87 |
+ err chan<- error
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+type batch struct {
|
|
|
bfbe87 |
+ db *DB
|
|
|
bfbe87 |
+ timer *time.Timer
|
|
|
bfbe87 |
+ start sync.Once
|
|
|
bfbe87 |
+ calls []call
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// trigger runs the batch if it hasn't already been run.
|
|
|
bfbe87 |
+func (b *batch) trigger() {
|
|
|
bfbe87 |
+ b.start.Do(b.run)
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// run performs the transactions in the batch and communicates results
|
|
|
bfbe87 |
+// back to DB.Batch.
|
|
|
bfbe87 |
+func (b *batch) run() {
|
|
|
bfbe87 |
+ b.db.batchMu.Lock()
|
|
|
bfbe87 |
+ b.timer.Stop()
|
|
|
bfbe87 |
+ // Make sure no new work is added to this batch, but don't break
|
|
|
bfbe87 |
+ // other batches.
|
|
|
bfbe87 |
+ if b.db.batch == b {
|
|
|
bfbe87 |
+ b.db.batch = nil
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ b.db.batchMu.Unlock()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+retry:
|
|
|
bfbe87 |
+ for len(b.calls) > 0 {
|
|
|
bfbe87 |
+ var failIdx = -1
|
|
|
bfbe87 |
+ err := b.db.Update(func(tx *Tx) error {
|
|
|
bfbe87 |
+ for i, c := range b.calls {
|
|
|
bfbe87 |
+ if err := safelyCall(c.fn, tx); err != nil {
|
|
|
bfbe87 |
+ failIdx = i
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+ })
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ if failIdx >= 0 {
|
|
|
bfbe87 |
+ // take the failing transaction out of the batch. it's
|
|
|
bfbe87 |
+ // safe to shorten b.calls here because db.batch no longer
|
|
|
bfbe87 |
+ // points to us, and we hold the mutex anyway.
|
|
|
bfbe87 |
+ c := b.calls[failIdx]
|
|
|
bfbe87 |
+ b.calls[failIdx], b.calls = b.calls[len(b.calls)-1], b.calls[:len(b.calls)-1]
|
|
|
bfbe87 |
+ // tell the submitter re-run it solo, continue with the rest of the batch
|
|
|
bfbe87 |
+ c.err <- trySolo
|
|
|
bfbe87 |
+ continue retry
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // pass success, or bolt internal errors, to all callers
|
|
|
bfbe87 |
+ for _, c := range b.calls {
|
|
|
bfbe87 |
+ if c.err != nil {
|
|
|
bfbe87 |
+ c.err <- err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ break retry
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// trySolo is a special sentinel error value used for signaling that a
|
|
|
bfbe87 |
+// transaction function should be re-run. It should never be seen by
|
|
|
bfbe87 |
+// callers.
|
|
|
bfbe87 |
+var trySolo = errors.New("batch function returned an error and should be re-run solo")
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+type panicked struct {
|
|
|
bfbe87 |
+ reason interface{}
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+func (p panicked) Error() string {
|
|
|
bfbe87 |
+ if err, ok := p.reason.(error); ok {
|
|
|
bfbe87 |
+ return err.Error()
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ return fmt.Sprintf("panic: %v", p.reason)
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+func safelyCall(fn func(*Tx) error, tx *Tx) (err error) {
|
|
|
bfbe87 |
+ defer func() {
|
|
|
bfbe87 |
+ if p := recover(); p != nil {
|
|
|
bfbe87 |
+ err = panicked{p}
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }()
|
|
|
bfbe87 |
+ return fn(tx)
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_386.go b/vendor/src/github.com/boltdb/bolt/bolt_386.go
|
|
|
bfbe87 |
index 856f401..e659bfb 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bolt_386.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_386.go
|
|
|
bfbe87 |
@@ -1,4 +1,7 @@
|
|
|
bfbe87 |
package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
-const maxMapSize = 0xFFFFFFF // 256MB
|
|
|
bfbe87 |
+const maxMapSize = 0x7FFFFFFF // 2GB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0xFFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_amd64.go b/vendor/src/github.com/boltdb/bolt/bolt_amd64.go
|
|
|
bfbe87 |
index 4262932..cca6b7e 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bolt_amd64.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_amd64.go
|
|
|
bfbe87 |
@@ -2,3 +2,6 @@ package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0x7FFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_arm.go b/vendor/src/github.com/boltdb/bolt/bolt_arm.go
|
|
|
bfbe87 |
index 856f401..e659bfb 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bolt_arm.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_arm.go
|
|
|
bfbe87 |
@@ -1,4 +1,7 @@
|
|
|
bfbe87 |
package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
-const maxMapSize = 0xFFFFFFF // 256MB
|
|
|
bfbe87 |
+const maxMapSize = 0x7FFFFFFF // 2GB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0xFFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_arm64.go b/vendor/src/github.com/boltdb/bolt/bolt_arm64.go
|
|
|
bfbe87 |
new file mode 100644
|
|
|
bfbe87 |
index 0000000..6d23093
|
|
|
bfbe87 |
--- /dev/null
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_arm64.go
|
|
|
bfbe87 |
@@ -0,0 +1,9 @@
|
|
|
bfbe87 |
+// +build arm64
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+package bolt
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
+const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0x7FFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_ppc64le.go b/vendor/src/github.com/boltdb/bolt/bolt_ppc64le.go
|
|
|
bfbe87 |
new file mode 100644
|
|
|
bfbe87 |
index 0000000..8351e12
|
|
|
bfbe87 |
--- /dev/null
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_ppc64le.go
|
|
|
bfbe87 |
@@ -0,0 +1,9 @@
|
|
|
bfbe87 |
+// +build ppc64le
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+package bolt
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
+const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0x7FFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_s390x.go b/vendor/src/github.com/boltdb/bolt/bolt_s390x.go
|
|
|
bfbe87 |
new file mode 100644
|
|
|
bfbe87 |
index 0000000..f4dd26b
|
|
|
bfbe87 |
--- /dev/null
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_s390x.go
|
|
|
bfbe87 |
@@ -0,0 +1,9 @@
|
|
|
bfbe87 |
+// +build s390x
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+package bolt
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxMapSize represents the largest mmap size supported by Bolt.
|
|
|
bfbe87 |
+const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// maxAllocSize is the size used when creating array pointers.
|
|
|
bfbe87 |
+const maxAllocSize = 0x7FFFFFFF
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_unix.go b/vendor/src/github.com/boltdb/bolt/bolt_unix.go
|
|
|
bfbe87 |
index 95647a7..6eef6b2 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bolt_unix.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_unix.go
|
|
|
bfbe87 |
@@ -1,8 +1,9 @@
|
|
|
bfbe87 |
-// +build !windows,!plan9
|
|
|
bfbe87 |
+// +build !windows,!plan9,!solaris
|
|
|
bfbe87 |
|
|
|
bfbe87 |
package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
import (
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
"os"
|
|
|
bfbe87 |
"syscall"
|
|
|
bfbe87 |
"time"
|
|
|
bfbe87 |
@@ -10,7 +11,7 @@ import (
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// flock acquires an advisory lock on a file descriptor.
|
|
|
bfbe87 |
-func flock(f *os.File, timeout time.Duration) error {
|
|
|
bfbe87 |
+func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
|
|
bfbe87 |
var t time.Time
|
|
|
bfbe87 |
for {
|
|
|
bfbe87 |
// If we're beyond our timeout then return an error.
|
|
|
bfbe87 |
@@ -20,9 +21,13 @@ func flock(f *os.File, timeout time.Duration) error {
|
|
|
bfbe87 |
} else if timeout > 0 && time.Since(t) > timeout {
|
|
|
bfbe87 |
return ErrTimeout
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+ flag := syscall.LOCK_SH
|
|
|
bfbe87 |
+ if exclusive {
|
|
|
bfbe87 |
+ flag = syscall.LOCK_EX
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Otherwise attempt to obtain an exclusive lock.
|
|
|
bfbe87 |
- err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
|
|
bfbe87 |
+ err := syscall.Flock(int(f.Fd()), flag|syscall.LOCK_NB)
|
|
|
bfbe87 |
if err == nil {
|
|
|
bfbe87 |
return nil
|
|
|
bfbe87 |
} else if err != syscall.EWOULDBLOCK {
|
|
|
bfbe87 |
@@ -41,11 +46,28 @@ func funlock(f *os.File) error {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// mmap memory maps a DB's data file.
|
|
|
bfbe87 |
func mmap(db *DB, sz int) error {
|
|
|
bfbe87 |
+ // Truncate and fsync to ensure file size metadata is flushed.
|
|
|
bfbe87 |
+ // https://github.com/boltdb/bolt/issues/284
|
|
|
bfbe87 |
+ if !db.NoGrowSync && !db.readOnly {
|
|
|
bfbe87 |
+ if err := db.file.Truncate(int64(sz)); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("file resize error: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ if err := db.file.Sync(); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("file sync error: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Map the data file to memory.
|
|
|
bfbe87 |
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED)
|
|
|
bfbe87 |
if err != nil {
|
|
|
bfbe87 |
return err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+ // Advise the kernel that the mmap is accessed randomly.
|
|
|
bfbe87 |
+ if err := madvise(b, syscall.MADV_RANDOM); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("madvise: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Save the original byte slice and convert to a byte array pointer.
|
|
|
bfbe87 |
db.dataref = b
|
|
|
bfbe87 |
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
|
|
bfbe87 |
@@ -67,3 +89,12 @@ func munmap(db *DB) error {
|
|
|
bfbe87 |
db.datasz = 0
|
|
|
bfbe87 |
return err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// NOTE: This function is copied from stdlib because it is not available on darwin.
|
|
|
bfbe87 |
+func madvise(b []byte, advice int) (err error) {
|
|
|
bfbe87 |
+ _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
|
|
|
bfbe87 |
+ if e1 != 0 {
|
|
|
bfbe87 |
+ err = e1
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ return
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_unix_solaris.go b/vendor/src/github.com/boltdb/bolt/bolt_unix_solaris.go
|
|
|
bfbe87 |
new file mode 100644
|
|
|
bfbe87 |
index 0000000..f480ee7
|
|
|
bfbe87 |
--- /dev/null
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_unix_solaris.go
|
|
|
bfbe87 |
@@ -0,0 +1,101 @@
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+package bolt
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+import (
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
+ "os"
|
|
|
bfbe87 |
+ "syscall"
|
|
|
bfbe87 |
+ "time"
|
|
|
bfbe87 |
+ "unsafe"
|
|
|
bfbe87 |
+ "golang.org/x/sys/unix"
|
|
|
bfbe87 |
+)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// flock acquires an advisory lock on a file descriptor.
|
|
|
bfbe87 |
+func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
|
|
bfbe87 |
+ var t time.Time
|
|
|
bfbe87 |
+ for {
|
|
|
bfbe87 |
+ // If we're beyond our timeout then return an error.
|
|
|
bfbe87 |
+ // This can only occur after we've attempted a flock once.
|
|
|
bfbe87 |
+ if t.IsZero() {
|
|
|
bfbe87 |
+ t = time.Now()
|
|
|
bfbe87 |
+ } else if timeout > 0 && time.Since(t) > timeout {
|
|
|
bfbe87 |
+ return ErrTimeout
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ var lock syscall.Flock_t
|
|
|
bfbe87 |
+ lock.Start = 0
|
|
|
bfbe87 |
+ lock.Len = 0
|
|
|
bfbe87 |
+ lock.Pid = 0
|
|
|
bfbe87 |
+ lock.Whence = 0
|
|
|
bfbe87 |
+ lock.Pid = 0
|
|
|
bfbe87 |
+ if exclusive {
|
|
|
bfbe87 |
+ lock.Type = syscall.F_WRLCK
|
|
|
bfbe87 |
+ } else {
|
|
|
bfbe87 |
+ lock.Type = syscall.F_RDLCK
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock)
|
|
|
bfbe87 |
+ if err == nil {
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+ } else if err != syscall.EAGAIN {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Wait for a bit and try again.
|
|
|
bfbe87 |
+ time.Sleep(50 * time.Millisecond)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// funlock releases an advisory lock on a file descriptor.
|
|
|
bfbe87 |
+func funlock(f *os.File) error {
|
|
|
bfbe87 |
+ var lock syscall.Flock_t
|
|
|
bfbe87 |
+ lock.Start = 0
|
|
|
bfbe87 |
+ lock.Len = 0
|
|
|
bfbe87 |
+ lock.Type = syscall.F_UNLCK
|
|
|
bfbe87 |
+ lock.Whence = 0
|
|
|
bfbe87 |
+ return syscall.FcntlFlock(uintptr(f.Fd()), syscall.F_SETLK, &lock)
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// mmap memory maps a DB's data file.
|
|
|
bfbe87 |
+func mmap(db *DB, sz int) error {
|
|
|
bfbe87 |
+ // Truncate and fsync to ensure file size metadata is flushed.
|
|
|
bfbe87 |
+ // https://github.com/boltdb/bolt/issues/284
|
|
|
bfbe87 |
+ if !db.NoGrowSync && !db.readOnly {
|
|
|
bfbe87 |
+ if err := db.file.Truncate(int64(sz)); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("file resize error: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ if err := db.file.Sync(); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("file sync error: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Map the data file to memory.
|
|
|
bfbe87 |
+ b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED)
|
|
|
bfbe87 |
+ if err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Advise the kernel that the mmap is accessed randomly.
|
|
|
bfbe87 |
+ if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("madvise: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Save the original byte slice and convert to a byte array pointer.
|
|
|
bfbe87 |
+ db.dataref = b
|
|
|
bfbe87 |
+ db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
|
|
bfbe87 |
+ db.datasz = sz
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// munmap unmaps a DB's data file from memory.
|
|
|
bfbe87 |
+func munmap(db *DB) error {
|
|
|
bfbe87 |
+ // Ignore the unmap if we have no mapped data.
|
|
|
bfbe87 |
+ if db.dataref == nil {
|
|
|
bfbe87 |
+ return nil
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Unmap using the original byte slice.
|
|
|
bfbe87 |
+ err := unix.Munmap(db.dataref)
|
|
|
bfbe87 |
+ db.dataref = nil
|
|
|
bfbe87 |
+ db.data = nil
|
|
|
bfbe87 |
+ db.datasz = 0
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bolt_windows.go b/vendor/src/github.com/boltdb/bolt/bolt_windows.go
|
|
|
bfbe87 |
index c8539d4..8b782be 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bolt_windows.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bolt_windows.go
|
|
|
bfbe87 |
@@ -16,7 +16,7 @@ func fdatasync(db *DB) error {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// flock acquires an advisory lock on a file descriptor.
|
|
|
bfbe87 |
-func flock(f *os.File, _ time.Duration) error {
|
|
|
bfbe87 |
+func flock(f *os.File, _ bool, _ time.Duration) error {
|
|
|
bfbe87 |
return nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -28,9 +28,11 @@ func funlock(f *os.File) error {
|
|
|
bfbe87 |
// mmap memory maps a DB's data file.
|
|
|
bfbe87 |
// Based on: https://github.com/edsrzf/mmap-go
|
|
|
bfbe87 |
func mmap(db *DB, sz int) error {
|
|
|
bfbe87 |
- // Truncate the database to the size of the mmap.
|
|
|
bfbe87 |
- if err := db.file.Truncate(int64(sz)); err != nil {
|
|
|
bfbe87 |
- return fmt.Errorf("truncate: %s", err)
|
|
|
bfbe87 |
+ if !db.readOnly {
|
|
|
bfbe87 |
+ // Truncate the database to the size of the mmap.
|
|
|
bfbe87 |
+ if err := db.file.Truncate(int64(sz)); err != nil {
|
|
|
bfbe87 |
+ return fmt.Errorf("truncate: %s", err)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Open a file mapping handle.
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/bucket.go b/vendor/src/github.com/boltdb/bolt/bucket.go
|
|
|
bfbe87 |
index 2630800..2925288 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/bucket.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/bucket.go
|
|
|
bfbe87 |
@@ -99,6 +99,7 @@ func (b *Bucket) Cursor() *Cursor {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Bucket retrieves a nested bucket by name.
|
|
|
bfbe87 |
// Returns nil if the bucket does not exist.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (b *Bucket) Bucket(name []byte) *Bucket {
|
|
|
bfbe87 |
if b.buckets != nil {
|
|
|
bfbe87 |
if child := b.buckets[string(name)]; child != nil {
|
|
|
bfbe87 |
@@ -148,6 +149,7 @@ func (b *Bucket) openBucket(value []byte) *Bucket {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// CreateBucket creates a new bucket at the given key and returns the new bucket.
|
|
|
bfbe87 |
// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
|
|
|
bfbe87 |
if b.tx.db == nil {
|
|
|
bfbe87 |
return nil, ErrTxClosed
|
|
|
bfbe87 |
@@ -192,6 +194,7 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
|
|
|
bfbe87 |
// Returns an error if the bucket name is blank, or if the bucket name is too long.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
|
|
|
bfbe87 |
child, err := b.CreateBucket(key)
|
|
|
bfbe87 |
if err == ErrBucketExists {
|
|
|
bfbe87 |
@@ -252,6 +255,7 @@ func (b *Bucket) DeleteBucket(key []byte) error {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Get retrieves the value for a key in the bucket.
|
|
|
bfbe87 |
// Returns a nil value if the key does not exist or if the key is a nested bucket.
|
|
|
bfbe87 |
+// The returned value is only valid for the life of the transaction.
|
|
|
bfbe87 |
func (b *Bucket) Get(key []byte) []byte {
|
|
|
bfbe87 |
k, v, flags := b.Cursor().seek(key)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -332,6 +336,12 @@ func (b *Bucket) NextSequence() (uint64, error) {
|
|
|
bfbe87 |
return 0, ErrTxNotWritable
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+ // Materialize the root node if it hasn't been already so that the
|
|
|
bfbe87 |
+ // bucket will be saved during commit.
|
|
|
bfbe87 |
+ if b.rootNode == nil {
|
|
|
bfbe87 |
+ _ = b.node(b.root, nil)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Increment and return the sequence.
|
|
|
bfbe87 |
b.bucket.sequence++
|
|
|
bfbe87 |
return b.bucket.sequence, nil
|
|
|
bfbe87 |
@@ -339,7 +349,8 @@ func (b *Bucket) NextSequence() (uint64, error) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// ForEach executes a function for each key/value pair in a bucket.
|
|
|
bfbe87 |
// If the provided function returns an error then the iteration is stopped and
|
|
|
bfbe87 |
-// the error is returned to the caller.
|
|
|
bfbe87 |
+// the error is returned to the caller. The provided function must not modify
|
|
|
bfbe87 |
+// the bucket; this will result in undefined behavior.
|
|
|
bfbe87 |
func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
|
|
|
bfbe87 |
if b.tx.db == nil {
|
|
|
bfbe87 |
return ErrTxClosed
|
|
|
bfbe87 |
@@ -511,8 +522,12 @@ func (b *Bucket) spill() error {
|
|
|
bfbe87 |
// Update parent node.
|
|
|
bfbe87 |
var c = b.Cursor()
|
|
|
bfbe87 |
k, _, flags := c.seek([]byte(name))
|
|
|
bfbe87 |
- _assert(bytes.Equal([]byte(name), k), "misplaced bucket header: %x -> %x", []byte(name), k)
|
|
|
bfbe87 |
- _assert(flags&bucketLeafFlag != 0, "unexpected bucket header flag: %x", flags)
|
|
|
bfbe87 |
+ if !bytes.Equal([]byte(name), k) {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("misplaced bucket header: %x -> %x", []byte(name), k))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ if flags&bucketLeafFlag == 0 {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("unexpected bucket header flag: %x", flags))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
c.node().put([]byte(name), []byte(name), value, 0, bucketLeafFlag)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -528,7 +543,9 @@ func (b *Bucket) spill() error {
|
|
|
bfbe87 |
b.rootNode = b.rootNode.root()
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Update the root node for this bucket.
|
|
|
bfbe87 |
- _assert(b.rootNode.pgid < b.tx.meta.pgid, "pgid (%d) above high water mark (%d)", b.rootNode.pgid, b.tx.meta.pgid)
|
|
|
bfbe87 |
+ if b.rootNode.pgid >= b.tx.meta.pgid {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", b.rootNode.pgid, b.tx.meta.pgid))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
b.root = b.rootNode.pgid
|
|
|
bfbe87 |
|
|
|
bfbe87 |
return nil
|
|
|
bfbe87 |
@@ -659,7 +676,9 @@ func (b *Bucket) pageNode(id pgid) (*page, *node) {
|
|
|
bfbe87 |
// Inline buckets have a fake page embedded in their value so treat them
|
|
|
bfbe87 |
// differently. We'll return the rootNode (if available) or the fake page.
|
|
|
bfbe87 |
if b.root == 0 {
|
|
|
bfbe87 |
- _assert(id == 0, "inline bucket non-zero page access(2): %d != 0", id)
|
|
|
bfbe87 |
+ if id != 0 {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("inline bucket non-zero page access(2): %d != 0", id))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
if b.rootNode != nil {
|
|
|
bfbe87 |
return nil, b.rootNode
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/cursor.go b/vendor/src/github.com/boltdb/bolt/cursor.go
|
|
|
bfbe87 |
index 3bfc2f1..006c548 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/cursor.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/cursor.go
|
|
|
bfbe87 |
@@ -2,6 +2,7 @@ package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
import (
|
|
|
bfbe87 |
"bytes"
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
"sort"
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -9,6 +10,8 @@ import (
|
|
|
bfbe87 |
// Cursors see nested buckets with value == nil.
|
|
|
bfbe87 |
// Cursors can be obtained from a transaction and are valid as long as the transaction is open.
|
|
|
bfbe87 |
//
|
|
|
bfbe87 |
+// Keys and values returned from the cursor are only valid for the life of the transaction.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
// Changing data while traversing with a cursor may cause it to be invalidated
|
|
|
bfbe87 |
// and return unexpected keys and/or values. You must reposition your cursor
|
|
|
bfbe87 |
// after mutating data.
|
|
|
bfbe87 |
@@ -24,6 +27,7 @@ func (c *Cursor) Bucket() *Bucket {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// First moves the cursor to the first item in the bucket and returns its key and value.
|
|
|
bfbe87 |
// If the bucket is empty then a nil key and value are returned.
|
|
|
bfbe87 |
+// The returned key and value are only valid for the life of the transaction.
|
|
|
bfbe87 |
func (c *Cursor) First() (key []byte, value []byte) {
|
|
|
bfbe87 |
_assert(c.bucket.tx.db != nil, "tx closed")
|
|
|
bfbe87 |
c.stack = c.stack[:0]
|
|
|
bfbe87 |
@@ -40,6 +44,7 @@ func (c *Cursor) First() (key []byte, value []byte) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Last moves the cursor to the last item in the bucket and returns its key and value.
|
|
|
bfbe87 |
// If the bucket is empty then a nil key and value are returned.
|
|
|
bfbe87 |
+// The returned key and value are only valid for the life of the transaction.
|
|
|
bfbe87 |
func (c *Cursor) Last() (key []byte, value []byte) {
|
|
|
bfbe87 |
_assert(c.bucket.tx.db != nil, "tx closed")
|
|
|
bfbe87 |
c.stack = c.stack[:0]
|
|
|
bfbe87 |
@@ -57,6 +62,7 @@ func (c *Cursor) Last() (key []byte, value []byte) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Next moves the cursor to the next item in the bucket and returns its key and value.
|
|
|
bfbe87 |
// If the cursor is at the end of the bucket then a nil key and value are returned.
|
|
|
bfbe87 |
+// The returned key and value are only valid for the life of the transaction.
|
|
|
bfbe87 |
func (c *Cursor) Next() (key []byte, value []byte) {
|
|
|
bfbe87 |
_assert(c.bucket.tx.db != nil, "tx closed")
|
|
|
bfbe87 |
k, v, flags := c.next()
|
|
|
bfbe87 |
@@ -68,6 +74,7 @@ func (c *Cursor) Next() (key []byte, value []byte) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Prev moves the cursor to the previous item in the bucket and returns its key and value.
|
|
|
bfbe87 |
// If the cursor is at the beginning of the bucket then a nil key and value are returned.
|
|
|
bfbe87 |
+// The returned key and value are only valid for the life of the transaction.
|
|
|
bfbe87 |
func (c *Cursor) Prev() (key []byte, value []byte) {
|
|
|
bfbe87 |
_assert(c.bucket.tx.db != nil, "tx closed")
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -99,6 +106,7 @@ func (c *Cursor) Prev() (key []byte, value []byte) {
|
|
|
bfbe87 |
// Seek moves the cursor to a given key and returns it.
|
|
|
bfbe87 |
// If the key does not exist then the next key is used. If no keys
|
|
|
bfbe87 |
// follow, a nil key is returned.
|
|
|
bfbe87 |
+// The returned key and value are only valid for the life of the transaction.
|
|
|
bfbe87 |
func (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {
|
|
|
bfbe87 |
k, v, flags := c.seek(seek)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -228,8 +236,8 @@ func (c *Cursor) next() (key []byte, value []byte, flags uint32) {
|
|
|
bfbe87 |
// search recursively performs a binary search against a given page/node until it finds a given key.
|
|
|
bfbe87 |
func (c *Cursor) search(key []byte, pgid pgid) {
|
|
|
bfbe87 |
p, n := c.bucket.pageNode(pgid)
|
|
|
bfbe87 |
- if p != nil {
|
|
|
bfbe87 |
- _assert((p.flags&(branchPageFlag|leafPageFlag)) != 0, "invalid page type: %d: %x", p.id, p.flags)
|
|
|
bfbe87 |
+ if p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("invalid page type: %d: %x", p.id, p.flags))
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
e := elemRef{page: p, node: n}
|
|
|
bfbe87 |
c.stack = append(c.stack, e)
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/db.go b/vendor/src/github.com/boltdb/bolt/db.go
|
|
|
bfbe87 |
index 6c45736..d39c4aa 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/db.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/db.go
|
|
|
bfbe87 |
@@ -12,9 +12,6 @@ import (
|
|
|
bfbe87 |
"unsafe"
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-// The smallest size that the mmap can be.
|
|
|
bfbe87 |
-const minMmapSize = 1 << 22 // 4MB
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
// The largest step that can be taken when remapping the mmap.
|
|
|
bfbe87 |
const maxMmapStep = 1 << 30 // 1GB
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -30,6 +27,12 @@ const magic uint32 = 0xED0CDAED
|
|
|
bfbe87 |
// must be synchronzied using the msync(2) syscall.
|
|
|
bfbe87 |
const IgnoreNoSync = runtime.GOOS == "openbsd"
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+// Default values if not set in a DB instance.
|
|
|
bfbe87 |
+const (
|
|
|
bfbe87 |
+ DefaultMaxBatchSize int = 1000
|
|
|
bfbe87 |
+ DefaultMaxBatchDelay = 10 * time.Millisecond
|
|
|
bfbe87 |
+)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// DB represents a collection of buckets persisted to a file on disk.
|
|
|
bfbe87 |
// All data access is performed through transactions which can be obtained through the DB.
|
|
|
bfbe87 |
// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
|
|
|
bfbe87 |
@@ -52,9 +55,33 @@ type DB struct {
|
|
|
bfbe87 |
// THIS IS UNSAFE. PLEASE USE WITH CAUTION.
|
|
|
bfbe87 |
NoSync bool
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+ // When true, skips the truncate call when growing the database.
|
|
|
bfbe87 |
+ // Setting this to true is only safe on non-ext3/ext4 systems.
|
|
|
bfbe87 |
+ // Skipping truncation avoids preallocation of hard drive space and
|
|
|
bfbe87 |
+ // bypasses a truncate() and fsync() syscall on remapping.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // https://github.com/boltdb/bolt/issues/284
|
|
|
bfbe87 |
+ NoGrowSync bool
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // MaxBatchSize is the maximum size of a batch. Default value is
|
|
|
bfbe87 |
+ // copied from DefaultMaxBatchSize in Open.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // If <=0, disables batching.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // Do not change concurrently with calls to Batch.
|
|
|
bfbe87 |
+ MaxBatchSize int
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // MaxBatchDelay is the maximum delay before a batch starts.
|
|
|
bfbe87 |
+ // Default value is copied from DefaultMaxBatchDelay in Open.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // If <=0, effectively disables batching.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // Do not change concurrently with calls to Batch.
|
|
|
bfbe87 |
+ MaxBatchDelay time.Duration
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
path string
|
|
|
bfbe87 |
file *os.File
|
|
|
bfbe87 |
- dataref []byte
|
|
|
bfbe87 |
+ dataref []byte // mmap'ed readonly, write throws SEGV
|
|
|
bfbe87 |
data *[maxMapSize]byte
|
|
|
bfbe87 |
datasz int
|
|
|
bfbe87 |
meta0 *meta
|
|
|
bfbe87 |
@@ -66,6 +93,9 @@ type DB struct {
|
|
|
bfbe87 |
freelist *freelist
|
|
|
bfbe87 |
stats Stats
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+ batchMu sync.Mutex
|
|
|
bfbe87 |
+ batch *batch
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
rwlock sync.Mutex // Allows only one writer at a time.
|
|
|
bfbe87 |
metalock sync.Mutex // Protects meta page access.
|
|
|
bfbe87 |
mmaplock sync.RWMutex // Protects mmap access during remapping.
|
|
|
bfbe87 |
@@ -74,6 +104,10 @@ type DB struct {
|
|
|
bfbe87 |
ops struct {
|
|
|
bfbe87 |
writeAt func(b []byte, off int64) (n int, err error)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Read only mode.
|
|
|
bfbe87 |
+ // When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.
|
|
|
bfbe87 |
+ readOnly bool
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Path returns the path to currently open database file.
|
|
|
bfbe87 |
@@ -101,20 +135,34 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
|
|
bfbe87 |
if options == nil {
|
|
|
bfbe87 |
options = DefaultOptions
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+ db.NoGrowSync = options.NoGrowSync
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Set default values for later DB operations.
|
|
|
bfbe87 |
+ db.MaxBatchSize = DefaultMaxBatchSize
|
|
|
bfbe87 |
+ db.MaxBatchDelay = DefaultMaxBatchDelay
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ flag := os.O_RDWR
|
|
|
bfbe87 |
+ if options.ReadOnly {
|
|
|
bfbe87 |
+ flag = os.O_RDONLY
|
|
|
bfbe87 |
+ db.readOnly = true
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Open data file and separate sync handler for metadata writes.
|
|
|
bfbe87 |
db.path = path
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
var err error
|
|
|
bfbe87 |
- if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil {
|
|
|
bfbe87 |
+ if db.file, err = os.OpenFile(db.path, flag|os.O_CREATE, mode); err != nil {
|
|
|
bfbe87 |
_ = db.close()
|
|
|
bfbe87 |
return nil, err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- // Lock file so that other processes using Bolt cannot use the database
|
|
|
bfbe87 |
- // at the same time. This would cause corruption since the two processes
|
|
|
bfbe87 |
- // would write meta pages and free pages separately.
|
|
|
bfbe87 |
- if err := flock(db.file, options.Timeout); err != nil {
|
|
|
bfbe87 |
+ // Lock file so that other processes using Bolt in read-write mode cannot
|
|
|
bfbe87 |
+ // use the database at the same time. This would cause corruption since
|
|
|
bfbe87 |
+ // the two processes would write meta pages and free pages separately.
|
|
|
bfbe87 |
+ // The database file is locked exclusively (only one process can grab the lock)
|
|
|
bfbe87 |
+ // if !options.ReadOnly.
|
|
|
bfbe87 |
+ // The database file is locked using the shared lock (more than one process may
|
|
|
bfbe87 |
+ // hold a lock at the same time) otherwise (options.ReadOnly is set).
|
|
|
bfbe87 |
+ if err := flock(db.file, !db.readOnly, options.Timeout); err != nil {
|
|
|
bfbe87 |
_ = db.close()
|
|
|
bfbe87 |
return nil, err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
@@ -162,16 +210,6 @@ func (db *DB) mmap(minsz int) error {
|
|
|
bfbe87 |
db.mmaplock.Lock()
|
|
|
bfbe87 |
defer db.mmaplock.Unlock()
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- // Dereference all mmap references before unmapping.
|
|
|
bfbe87 |
- if db.rwtx != nil {
|
|
|
bfbe87 |
- db.rwtx.root.dereference()
|
|
|
bfbe87 |
- }
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
- // Unmap existing data before continuing.
|
|
|
bfbe87 |
- if err := db.munmap(); err != nil {
|
|
|
bfbe87 |
- return err
|
|
|
bfbe87 |
- }
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
info, err := db.file.Stat()
|
|
|
bfbe87 |
if err != nil {
|
|
|
bfbe87 |
return fmt.Errorf("mmap stat error: %s", err)
|
|
|
bfbe87 |
@@ -184,7 +222,20 @@ func (db *DB) mmap(minsz int) error {
|
|
|
bfbe87 |
if size < minsz {
|
|
|
bfbe87 |
size = minsz
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
- size = db.mmapSize(size)
|
|
|
bfbe87 |
+ size, err = db.mmapSize(size)
|
|
|
bfbe87 |
+ if err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Dereference all mmap references before unmapping.
|
|
|
bfbe87 |
+ if db.rwtx != nil {
|
|
|
bfbe87 |
+ db.rwtx.root.dereference()
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Unmap existing data before continuing.
|
|
|
bfbe87 |
+ if err := db.munmap(); err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Memory-map the data file as a byte slice.
|
|
|
bfbe87 |
if err := mmap(db, size); err != nil {
|
|
|
bfbe87 |
@@ -215,22 +266,40 @@ func (db *DB) munmap() error {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// mmapSize determines the appropriate size for the mmap given the current size
|
|
|
bfbe87 |
-// of the database. The minimum size is 4MB and doubles until it reaches 1GB.
|
|
|
bfbe87 |
-func (db *DB) mmapSize(size int) int {
|
|
|
bfbe87 |
- if size <= minMmapSize {
|
|
|
bfbe87 |
- return minMmapSize
|
|
|
bfbe87 |
- } else if size < maxMmapStep {
|
|
|
bfbe87 |
- size *= 2
|
|
|
bfbe87 |
- } else {
|
|
|
bfbe87 |
- size += maxMmapStep
|
|
|
bfbe87 |
+// of the database. The minimum size is 1MB and doubles until it reaches 1GB.
|
|
|
bfbe87 |
+// Returns an error if the new mmap size is greater than the max allowed.
|
|
|
bfbe87 |
+func (db *DB) mmapSize(size int) (int, error) {
|
|
|
bfbe87 |
+ // Double the size from 32KB until 1GB.
|
|
|
bfbe87 |
+ for i := uint(15); i <= 30; i++ {
|
|
|
bfbe87 |
+ if size <= 1<
|
|
|
bfbe87 |
+ return 1 << i, nil
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Verify the requested size is not above the maximum allowed.
|
|
|
bfbe87 |
+ if size > maxMapSize {
|
|
|
bfbe87 |
+ return 0, fmt.Errorf("mmap too large")
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // If larger than 1GB then grow by 1GB at a time.
|
|
|
bfbe87 |
+ sz := int64(size)
|
|
|
bfbe87 |
+ if remainder := sz % int64(maxMmapStep); remainder > 0 {
|
|
|
bfbe87 |
+ sz += int64(maxMmapStep) - remainder
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Ensure that the mmap size is a multiple of the page size.
|
|
|
bfbe87 |
- if (size % db.pageSize) != 0 {
|
|
|
bfbe87 |
- size = ((size / db.pageSize) + 1) * db.pageSize
|
|
|
bfbe87 |
+ // This should always be true since we're incrementing in MBs.
|
|
|
bfbe87 |
+ pageSize := int64(db.pageSize)
|
|
|
bfbe87 |
+ if (sz % pageSize) != 0 {
|
|
|
bfbe87 |
+ sz = ((sz / pageSize) + 1) * pageSize
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // If we've exceeded the max size then only grow up to the max size.
|
|
|
bfbe87 |
+ if sz > maxMapSize {
|
|
|
bfbe87 |
+ sz = maxMapSize
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- return size
|
|
|
bfbe87 |
+ return int(sz), nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// init creates a new database file and initializes its meta pages.
|
|
|
bfbe87 |
@@ -250,7 +319,6 @@ func (db *DB) init() error {
|
|
|
bfbe87 |
m.magic = magic
|
|
|
bfbe87 |
m.version = version
|
|
|
bfbe87 |
m.pageSize = uint32(db.pageSize)
|
|
|
bfbe87 |
- m.version = version
|
|
|
bfbe87 |
m.freelist = 2
|
|
|
bfbe87 |
m.root = bucket{root: 3}
|
|
|
bfbe87 |
m.pgid = 4
|
|
|
bfbe87 |
@@ -283,8 +351,15 @@ func (db *DB) init() error {
|
|
|
bfbe87 |
// Close releases all database resources.
|
|
|
bfbe87 |
// All transactions must be closed before closing the database.
|
|
|
bfbe87 |
func (db *DB) Close() error {
|
|
|
bfbe87 |
+ db.rwlock.Lock()
|
|
|
bfbe87 |
+ defer db.rwlock.Unlock()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
db.metalock.Lock()
|
|
|
bfbe87 |
defer db.metalock.Unlock()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ db.mmaplock.RLock()
|
|
|
bfbe87 |
+ defer db.mmaplock.RUnlock()
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
return db.close()
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -304,8 +379,11 @@ func (db *DB) close() error {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Close file handles.
|
|
|
bfbe87 |
if db.file != nil {
|
|
|
bfbe87 |
- // Unlock the file.
|
|
|
bfbe87 |
- _ = funlock(db.file)
|
|
|
bfbe87 |
+ // No need to unlock read-only file.
|
|
|
bfbe87 |
+ if !db.readOnly {
|
|
|
bfbe87 |
+ // Unlock the file.
|
|
|
bfbe87 |
+ _ = funlock(db.file)
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Close the file descriptor.
|
|
|
bfbe87 |
if err := db.file.Close(); err != nil {
|
|
|
bfbe87 |
@@ -323,6 +401,11 @@ func (db *DB) close() error {
|
|
|
bfbe87 |
// will cause the calls to block and be serialized until the current write
|
|
|
bfbe87 |
// transaction finishes.
|
|
|
bfbe87 |
//
|
|
|
bfbe87 |
+// Transactions should not be depedent on one another. Opening a read
|
|
|
bfbe87 |
+// transaction and a write transaction in the same goroutine can cause the
|
|
|
bfbe87 |
+// writer to deadlock because the database periodically needs to re-mmap itself
|
|
|
bfbe87 |
+// as it grows and it cannot do that while a read transaction is open.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
// IMPORTANT: You must close read-only transactions after you are finished or
|
|
|
bfbe87 |
// else the database will not reclaim old pages.
|
|
|
bfbe87 |
func (db *DB) Begin(writable bool) (*Tx, error) {
|
|
|
bfbe87 |
@@ -371,6 +454,11 @@ func (db *DB) beginTx() (*Tx, error) {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
func (db *DB) beginRWTx() (*Tx, error) {
|
|
|
bfbe87 |
+ // If the database was opened with Options.ReadOnly, return an error.
|
|
|
bfbe87 |
+ if db.readOnly {
|
|
|
bfbe87 |
+ return nil, ErrDatabaseReadOnly
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Obtain writer lock. This is released by the transaction when it closes.
|
|
|
bfbe87 |
// This enforces only one writer transaction at a time.
|
|
|
bfbe87 |
db.rwlock.Lock()
|
|
|
bfbe87 |
@@ -501,6 +589,12 @@ func (db *DB) View(fn func(*Tx) error) error {
|
|
|
bfbe87 |
return nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+// Sync executes fdatasync() against the database file handle.
|
|
|
bfbe87 |
+//
|
|
|
bfbe87 |
+// This is not necessary under normal operation, however, if you use NoSync
|
|
|
bfbe87 |
+// then it allows you to force the database file to sync against the disk.
|
|
|
bfbe87 |
+func (db *DB) Sync() error { return fdatasync(db) }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Stats retrieves ongoing performance stats for the database.
|
|
|
bfbe87 |
// This is only updated when a transaction closes.
|
|
|
bfbe87 |
func (db *DB) Stats() Stats {
|
|
|
bfbe87 |
@@ -561,18 +655,30 @@ func (db *DB) allocate(count int) (*page, error) {
|
|
|
bfbe87 |
return p, nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+func (db *DB) IsReadOnly() bool {
|
|
|
bfbe87 |
+ return db.readOnly
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Options represents the options that can be set when opening a database.
|
|
|
bfbe87 |
type Options struct {
|
|
|
bfbe87 |
// Timeout is the amount of time to wait to obtain a file lock.
|
|
|
bfbe87 |
// When set to zero it will wait indefinitely. This option is only
|
|
|
bfbe87 |
// available on Darwin and Linux.
|
|
|
bfbe87 |
Timeout time.Duration
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Sets the DB.NoGrowSync flag before memory mapping the file.
|
|
|
bfbe87 |
+ NoGrowSync bool
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to
|
|
|
bfbe87 |
+ // grab a shared lock (UNIX).
|
|
|
bfbe87 |
+ ReadOnly bool
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// DefaultOptions represent the options used if nil options are passed into Open().
|
|
|
bfbe87 |
// No timeout is used which will cause Bolt to wait indefinitely for a lock.
|
|
|
bfbe87 |
var DefaultOptions = &Options{
|
|
|
bfbe87 |
- Timeout: 0,
|
|
|
bfbe87 |
+ Timeout: 0,
|
|
|
bfbe87 |
+ NoGrowSync: false,
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Stats represents statistics about the database.
|
|
|
bfbe87 |
@@ -647,9 +753,11 @@ func (m *meta) copy(dest *meta) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// write writes the meta onto a page.
|
|
|
bfbe87 |
func (m *meta) write(p *page) {
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
- _assert(m.root.root < m.pgid, "root bucket pgid (%d) above high water mark (%d)", m.root.root, m.pgid)
|
|
|
bfbe87 |
- _assert(m.freelist < m.pgid, "freelist pgid (%d) above high water mark (%d)", m.freelist, m.pgid)
|
|
|
bfbe87 |
+ if m.root.root >= m.pgid {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("root bucket pgid (%d) above high water mark (%d)", m.root.root, m.pgid))
|
|
|
bfbe87 |
+ } else if m.freelist >= m.pgid {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("freelist pgid (%d) above high water mark (%d)", m.freelist, m.pgid))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Page id is either going to be 0 or 1 which we can determine by the transaction ID.
|
|
|
bfbe87 |
p.id = pgid(m.txid % 2)
|
|
|
bfbe87 |
@@ -675,13 +783,8 @@ func _assert(condition bool, msg string, v ...interface{}) {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-func warn(v ...interface{}) {
|
|
|
bfbe87 |
- fmt.Fprintln(os.Stderr, v...)
|
|
|
bfbe87 |
-}
|
|
|
bfbe87 |
-
|
|
|
bfbe87 |
-func warnf(msg string, v ...interface{}) {
|
|
|
bfbe87 |
- fmt.Fprintf(os.Stderr, msg+"\n", v...)
|
|
|
bfbe87 |
-}
|
|
|
bfbe87 |
+func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
|
|
|
bfbe87 |
+func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
func printstack() {
|
|
|
bfbe87 |
stack := strings.Join(strings.Split(string(debug.Stack()), "\n")[2:], "\n")
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/errors.go b/vendor/src/github.com/boltdb/bolt/errors.go
|
|
|
bfbe87 |
index aa504f1..6883786 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/errors.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/errors.go
|
|
|
bfbe87 |
@@ -36,6 +36,10 @@ var (
|
|
|
bfbe87 |
// ErrTxClosed is returned when committing or rolling back a transaction
|
|
|
bfbe87 |
// that has already been committed or rolled back.
|
|
|
bfbe87 |
ErrTxClosed = errors.New("tx closed")
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // ErrDatabaseReadOnly is returned when a mutating transaction is started on a
|
|
|
bfbe87 |
+ // read-only database.
|
|
|
bfbe87 |
+ ErrDatabaseReadOnly = errors.New("database is in read-only mode")
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// These errors can occur when putting or deleting a value or a bucket.
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/freelist.go b/vendor/src/github.com/boltdb/bolt/freelist.go
|
|
|
bfbe87 |
index 150e3e6..0161948 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/freelist.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/freelist.go
|
|
|
bfbe87 |
@@ -1,6 +1,7 @@
|
|
|
bfbe87 |
package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
import (
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
"sort"
|
|
|
bfbe87 |
"unsafe"
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
@@ -47,15 +48,14 @@ func (f *freelist) pending_count() int {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// all returns a list of all free ids and all pending ids in one sorted list.
|
|
|
bfbe87 |
func (f *freelist) all() []pgid {
|
|
|
bfbe87 |
- ids := make([]pgid, len(f.ids))
|
|
|
bfbe87 |
- copy(ids, f.ids)
|
|
|
bfbe87 |
+ m := make(pgids, 0)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
for _, list := range f.pending {
|
|
|
bfbe87 |
- ids = append(ids, list...)
|
|
|
bfbe87 |
+ m = append(m, list...)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- sort.Sort(pgids(ids))
|
|
|
bfbe87 |
- return ids
|
|
|
bfbe87 |
+ sort.Sort(m)
|
|
|
bfbe87 |
+ return pgids(f.ids).merge(m)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// allocate returns the starting page id of a contiguous list of pages of a given size.
|
|
|
bfbe87 |
@@ -67,7 +67,9 @@ func (f *freelist) allocate(n int) pgid {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
var initial, previd pgid
|
|
|
bfbe87 |
for i, id := range f.ids {
|
|
|
bfbe87 |
- _assert(id > 1, "invalid page allocation: %d", id)
|
|
|
bfbe87 |
+ if id <= 1 {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("invalid page allocation: %d", id))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Reset initial page if this is not contiguous.
|
|
|
bfbe87 |
if previd == 0 || id-previd != 1 {
|
|
|
bfbe87 |
@@ -103,13 +105,17 @@ func (f *freelist) allocate(n int) pgid {
|
|
|
bfbe87 |
// free releases a page and its overflow for a given transaction id.
|
|
|
bfbe87 |
// If the page is already free then a panic will occur.
|
|
|
bfbe87 |
func (f *freelist) free(txid txid, p *page) {
|
|
|
bfbe87 |
- _assert(p.id > 1, "cannot free page 0 or 1: %d", p.id)
|
|
|
bfbe87 |
+ if p.id <= 1 {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Free page and all its overflow pages.
|
|
|
bfbe87 |
var ids = f.pending[txid]
|
|
|
bfbe87 |
for id := p.id; id <= p.id+pgid(p.overflow); id++ {
|
|
|
bfbe87 |
// Verify that page is not already free.
|
|
|
bfbe87 |
- _assert(!f.cache[id], "page %d already freed", id)
|
|
|
bfbe87 |
+ if f.cache[id] {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("page %d already freed", id))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Add to the freelist and cache.
|
|
|
bfbe87 |
ids = append(ids, id)
|
|
|
bfbe87 |
@@ -120,15 +126,17 @@ func (f *freelist) free(txid txid, p *page) {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// release moves all page ids for a transaction id (or older) to the freelist.
|
|
|
bfbe87 |
func (f *freelist) release(txid txid) {
|
|
|
bfbe87 |
+ m := make(pgids, 0)
|
|
|
bfbe87 |
for tid, ids := range f.pending {
|
|
|
bfbe87 |
if tid <= txid {
|
|
|
bfbe87 |
// Move transaction's pending pages to the available freelist.
|
|
|
bfbe87 |
// Don't remove from the cache since the page is still free.
|
|
|
bfbe87 |
- f.ids = append(f.ids, ids...)
|
|
|
bfbe87 |
+ m = append(m, ids...)
|
|
|
bfbe87 |
delete(f.pending, tid)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
- sort.Sort(pgids(f.ids))
|
|
|
bfbe87 |
+ sort.Sort(m)
|
|
|
bfbe87 |
+ f.ids = pgids(f.ids).merge(m)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// rollback removes the pages from a given pending tx.
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/node.go b/vendor/src/github.com/boltdb/bolt/node.go
|
|
|
bfbe87 |
index c204c39..c9fb21c 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/node.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/node.go
|
|
|
bfbe87 |
@@ -2,6 +2,7 @@ package bolt
|
|
|
bfbe87 |
|
|
|
bfbe87 |
import (
|
|
|
bfbe87 |
"bytes"
|
|
|
bfbe87 |
+ "fmt"
|
|
|
bfbe87 |
"sort"
|
|
|
bfbe87 |
"unsafe"
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
@@ -70,7 +71,9 @@ func (n *node) pageElementSize() int {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// childAt returns the child node at a given index.
|
|
|
bfbe87 |
func (n *node) childAt(index int) *node {
|
|
|
bfbe87 |
- _assert(!n.isLeaf, "invalid childAt(%d) on a leaf node", index)
|
|
|
bfbe87 |
+ if n.isLeaf {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("invalid childAt(%d) on a leaf node", index))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
return n.bucket.node(n.inodes[index].pgid, n)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
@@ -111,9 +114,13 @@ func (n *node) prevSibling() *node {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// put inserts a key/value.
|
|
|
bfbe87 |
func (n *node) put(oldKey, newKey, value []byte, pgid pgid, flags uint32) {
|
|
|
bfbe87 |
- _assert(pgid < n.bucket.tx.meta.pgid, "pgid (%d) above high water mark (%d)", pgid, n.bucket.tx.meta.pgid)
|
|
|
bfbe87 |
- _assert(len(oldKey) > 0, "put: zero-length old key")
|
|
|
bfbe87 |
- _assert(len(newKey) > 0, "put: zero-length new key")
|
|
|
bfbe87 |
+ if pgid >= n.bucket.tx.meta.pgid {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", pgid, n.bucket.tx.meta.pgid))
|
|
|
bfbe87 |
+ } else if len(oldKey) <= 0 {
|
|
|
bfbe87 |
+ panic("put: zero-length old key")
|
|
|
bfbe87 |
+ } else if len(newKey) <= 0 {
|
|
|
bfbe87 |
+ panic("put: zero-length new key")
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Find insertion index.
|
|
|
bfbe87 |
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, oldKey) != -1 })
|
|
|
bfbe87 |
@@ -189,7 +196,9 @@ func (n *node) write(p *page) {
|
|
|
bfbe87 |
p.flags |= branchPageFlag
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- _assert(len(n.inodes) < 0xFFFF, "inode overflow: %d (pgid=%d)", len(n.inodes), p.id)
|
|
|
bfbe87 |
+ if len(n.inodes) >= 0xFFFF {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("inode overflow: %d (pgid=%d)", len(n.inodes), p.id))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
p.count = uint16(len(n.inodes))
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Loop over each item and write it to the page.
|
|
|
bfbe87 |
@@ -212,11 +221,20 @@ func (n *node) write(p *page) {
|
|
|
bfbe87 |
_assert(elem.pgid != p.id, "write: circular dependency occurred")
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+ // If the length of key+value is larger than the max allocation size
|
|
|
bfbe87 |
+ // then we need to reallocate the byte array pointer.
|
|
|
bfbe87 |
+ //
|
|
|
bfbe87 |
+ // See: https://github.com/boltdb/bolt/pull/335
|
|
|
bfbe87 |
+ klen, vlen := len(item.key), len(item.value)
|
|
|
bfbe87 |
+ if len(b) < klen+vlen {
|
|
|
bfbe87 |
+ b = (*[maxAllocSize]byte)(unsafe.Pointer(&b[0]))[:]
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
// Write data for the element to the end of the page.
|
|
|
bfbe87 |
copy(b[0:], item.key)
|
|
|
bfbe87 |
- b = b[len(item.key):]
|
|
|
bfbe87 |
+ b = b[klen:]
|
|
|
bfbe87 |
copy(b[0:], item.value)
|
|
|
bfbe87 |
- b = b[len(item.value):]
|
|
|
bfbe87 |
+ b = b[vlen:]
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// DEBUG ONLY: n.dump()
|
|
|
bfbe87 |
@@ -348,7 +366,9 @@ func (n *node) spill() error {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Write the node.
|
|
|
bfbe87 |
- _assert(p.id < tx.meta.pgid, "pgid (%d) above high water mark (%d)", p.id, tx.meta.pgid)
|
|
|
bfbe87 |
+ if p.id >= tx.meta.pgid {
|
|
|
bfbe87 |
+ panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", p.id, tx.meta.pgid))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
node.pgid = p.id
|
|
|
bfbe87 |
node.write(p)
|
|
|
bfbe87 |
node.spilled = true
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/page.go b/vendor/src/github.com/boltdb/bolt/page.go
|
|
|
bfbe87 |
index b3dc473..818aa1b 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/page.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/page.go
|
|
|
bfbe87 |
@@ -3,12 +3,12 @@ package bolt
|
|
|
bfbe87 |
import (
|
|
|
bfbe87 |
"fmt"
|
|
|
bfbe87 |
"os"
|
|
|
bfbe87 |
+ "sort"
|
|
|
bfbe87 |
"unsafe"
|
|
|
bfbe87 |
)
|
|
|
bfbe87 |
|
|
|
bfbe87 |
const pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-const maxAllocSize = 0xFFFFFFF
|
|
|
bfbe87 |
const minKeysPerPage = 2
|
|
|
bfbe87 |
|
|
|
bfbe87 |
const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))
|
|
|
bfbe87 |
@@ -97,7 +97,7 @@ type branchPageElement struct {
|
|
|
bfbe87 |
// key returns a byte slice of the node key.
|
|
|
bfbe87 |
func (n *branchPageElement) key() []byte {
|
|
|
bfbe87 |
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
|
|
bfbe87 |
- return buf[n.pos : n.pos+n.ksize]
|
|
|
bfbe87 |
+ return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// leafPageElement represents a node on a leaf page.
|
|
|
bfbe87 |
@@ -111,13 +111,13 @@ type leafPageElement struct {
|
|
|
bfbe87 |
// key returns a byte slice of the node key.
|
|
|
bfbe87 |
func (n *leafPageElement) key() []byte {
|
|
|
bfbe87 |
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
|
|
bfbe87 |
- return buf[n.pos : n.pos+n.ksize]
|
|
|
bfbe87 |
+ return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// value returns a byte slice of the node value.
|
|
|
bfbe87 |
func (n *leafPageElement) value() []byte {
|
|
|
bfbe87 |
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
|
|
bfbe87 |
- return buf[n.pos+n.ksize : n.pos+n.ksize+n.vsize]
|
|
|
bfbe87 |
+ return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize]
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// PageInfo represents human readable information about a page.
|
|
|
bfbe87 |
@@ -133,3 +133,40 @@ type pgids []pgid
|
|
|
bfbe87 |
func (s pgids) Len() int { return len(s) }
|
|
|
bfbe87 |
func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
bfbe87 |
func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+// merge returns the sorted union of a and b.
|
|
|
bfbe87 |
+func (a pgids) merge(b pgids) pgids {
|
|
|
bfbe87 |
+ // Return the opposite slice if one is nil.
|
|
|
bfbe87 |
+ if len(a) == 0 {
|
|
|
bfbe87 |
+ return b
|
|
|
bfbe87 |
+ } else if len(b) == 0 {
|
|
|
bfbe87 |
+ return a
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Create a list to hold all elements from both lists.
|
|
|
bfbe87 |
+ merged := make(pgids, 0, len(a)+len(b))
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Assign lead to the slice with a lower starting value, follow to the higher value.
|
|
|
bfbe87 |
+ lead, follow := a, b
|
|
|
bfbe87 |
+ if b[0] < a[0] {
|
|
|
bfbe87 |
+ lead, follow = b, a
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Continue while there are elements in the lead.
|
|
|
bfbe87 |
+ for len(lead) > 0 {
|
|
|
bfbe87 |
+ // Merge largest prefix of lead that is ahead of follow[0].
|
|
|
bfbe87 |
+ n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })
|
|
|
bfbe87 |
+ merged = append(merged, lead[:n]...)
|
|
|
bfbe87 |
+ if n >= len(lead) {
|
|
|
bfbe87 |
+ break
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Swap lead and follow.
|
|
|
bfbe87 |
+ lead, follow = follow, lead[n:]
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Append what's left in follow.
|
|
|
bfbe87 |
+ merged = append(merged, follow...)
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ return merged
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
diff --git a/vendor/src/github.com/boltdb/bolt/tx.go b/vendor/src/github.com/boltdb/bolt/tx.go
|
|
|
bfbe87 |
index c041d73..fe6c287 100644
|
|
|
bfbe87 |
--- a/vendor/src/github.com/boltdb/bolt/tx.go
|
|
|
bfbe87 |
+++ b/vendor/src/github.com/boltdb/bolt/tx.go
|
|
|
bfbe87 |
@@ -87,18 +87,21 @@ func (tx *Tx) Stats() TxStats {
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Bucket retrieves a bucket by name.
|
|
|
bfbe87 |
// Returns nil if the bucket does not exist.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (tx *Tx) Bucket(name []byte) *Bucket {
|
|
|
bfbe87 |
return tx.root.Bucket(name)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// CreateBucket creates a new bucket.
|
|
|
bfbe87 |
// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {
|
|
|
bfbe87 |
return tx.root.CreateBucket(name)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
|
|
|
bfbe87 |
// Returns an error if the bucket name is blank, or if the bucket name is too long.
|
|
|
bfbe87 |
+// The bucket instance is only valid for the lifetime of the transaction.
|
|
|
bfbe87 |
func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
|
|
|
bfbe87 |
return tx.root.CreateBucketIfNotExists(name)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
@@ -127,7 +130,8 @@ func (tx *Tx) OnCommit(fn func()) {
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Commit writes all changes to disk and updates the meta page.
|
|
|
bfbe87 |
-// Returns an error if a disk write error occurs.
|
|
|
bfbe87 |
+// Returns an error if a disk write error occurs, or if Commit is
|
|
|
bfbe87 |
+// called on a read-only transaction.
|
|
|
bfbe87 |
func (tx *Tx) Commit() error {
|
|
|
bfbe87 |
_assert(!tx.managed, "managed tx commit not allowed")
|
|
|
bfbe87 |
if tx.db == nil {
|
|
|
bfbe87 |
@@ -203,7 +207,8 @@ func (tx *Tx) Commit() error {
|
|
|
bfbe87 |
return nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
-// Rollback closes the transaction and ignores all previous updates.
|
|
|
bfbe87 |
+// Rollback closes the transaction and ignores all previous updates. Read-only
|
|
|
bfbe87 |
+// transactions must be rolled back and not committed.
|
|
|
bfbe87 |
func (tx *Tx) Rollback() error {
|
|
|
bfbe87 |
_assert(!tx.managed, "managed tx rollback not allowed")
|
|
|
bfbe87 |
if tx.db == nil {
|
|
|
bfbe87 |
@@ -234,7 +239,8 @@ func (tx *Tx) close() {
|
|
|
bfbe87 |
var freelistPendingN = tx.db.freelist.pending_count()
|
|
|
bfbe87 |
var freelistAlloc = tx.db.freelist.size()
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- // Remove writer lock.
|
|
|
bfbe87 |
+ // Remove transaction ref & writer lock.
|
|
|
bfbe87 |
+ tx.db.rwtx = nil
|
|
|
bfbe87 |
tx.db.rwlock.Unlock()
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Merge statistics.
|
|
|
bfbe87 |
@@ -248,41 +254,51 @@ func (tx *Tx) close() {
|
|
|
bfbe87 |
} else {
|
|
|
bfbe87 |
tx.db.removeTx(tx)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Clear all references.
|
|
|
bfbe87 |
tx.db = nil
|
|
|
bfbe87 |
+ tx.meta = nil
|
|
|
bfbe87 |
+ tx.root = Bucket{tx: tx}
|
|
|
bfbe87 |
+ tx.pages = nil
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Copy writes the entire database to a writer.
|
|
|
bfbe87 |
-// A reader transaction is maintained during the copy so it is safe to continue
|
|
|
bfbe87 |
-// using the database while a copy is in progress.
|
|
|
bfbe87 |
-// Copy will write exactly tx.Size() bytes into the writer.
|
|
|
bfbe87 |
+// This function exists for backwards compatibility. Use WriteTo() in
|
|
|
bfbe87 |
func (tx *Tx) Copy(w io.Writer) error {
|
|
|
bfbe87 |
- var f *os.File
|
|
|
bfbe87 |
- var err error
|
|
|
bfbe87 |
+ _, err := tx.WriteTo(w)
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
+// WriteTo writes the entire database to a writer.
|
|
|
bfbe87 |
+// If err == nil then exactly tx.Size() bytes will be written into the writer.
|
|
|
bfbe87 |
+func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
bfbe87 |
// Attempt to open reader directly.
|
|
|
bfbe87 |
+ var f *os.File
|
|
|
bfbe87 |
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY|odirect, 0); err != nil {
|
|
|
bfbe87 |
// Fallback to a regular open if that doesn't work.
|
|
|
bfbe87 |
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY, 0); err != nil {
|
|
|
bfbe87 |
- return err
|
|
|
bfbe87 |
+ return 0, err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Copy the meta pages.
|
|
|
bfbe87 |
tx.db.metalock.Lock()
|
|
|
bfbe87 |
- _, err = io.CopyN(w, f, int64(tx.db.pageSize*2))
|
|
|
bfbe87 |
+ n, err = io.CopyN(w, f, int64(tx.db.pageSize*2))
|
|
|
bfbe87 |
tx.db.metalock.Unlock()
|
|
|
bfbe87 |
if err != nil {
|
|
|
bfbe87 |
_ = f.Close()
|
|
|
bfbe87 |
- return fmt.Errorf("meta copy: %s", err)
|
|
|
bfbe87 |
+ return n, fmt.Errorf("meta copy: %s", err)
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// Copy data pages.
|
|
|
bfbe87 |
- if _, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2)); err != nil {
|
|
|
bfbe87 |
+ wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))
|
|
|
bfbe87 |
+ n += wn
|
|
|
bfbe87 |
+ if err != nil {
|
|
|
bfbe87 |
_ = f.Close()
|
|
|
bfbe87 |
- return err
|
|
|
bfbe87 |
+ return n, err
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- return f.Close()
|
|
|
bfbe87 |
+ return n, f.Close()
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
|
|
|
bfbe87 |
// CopyFile copies the entire database to file at the given path.
|
|
|
bfbe87 |
@@ -416,15 +432,39 @@ func (tx *Tx) write() error {
|
|
|
bfbe87 |
// Write pages to disk in order.
|
|
|
bfbe87 |
for _, p := range pages {
|
|
|
bfbe87 |
size := (int(p.overflow) + 1) * tx.db.pageSize
|
|
|
bfbe87 |
- buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:size]
|
|
|
bfbe87 |
offset := int64(p.id) * int64(tx.db.pageSize)
|
|
|
bfbe87 |
- if _, err := tx.db.ops.writeAt(buf, offset); err != nil {
|
|
|
bfbe87 |
- return err
|
|
|
bfbe87 |
- }
|
|
|
bfbe87 |
|
|
|
bfbe87 |
- // Update statistics.
|
|
|
bfbe87 |
- tx.stats.Write++
|
|
|
bfbe87 |
+ // Write out page in "max allocation" sized chunks.
|
|
|
bfbe87 |
+ ptr := (*[maxAllocSize]byte)(unsafe.Pointer(p))
|
|
|
bfbe87 |
+ for {
|
|
|
bfbe87 |
+ // Limit our write to our max allocation size.
|
|
|
bfbe87 |
+ sz := size
|
|
|
bfbe87 |
+ if sz > maxAllocSize-1 {
|
|
|
bfbe87 |
+ sz = maxAllocSize - 1
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Write chunk to disk.
|
|
|
bfbe87 |
+ buf := ptr[:sz]
|
|
|
bfbe87 |
+ if _, err := tx.db.ops.writeAt(buf, offset); err != nil {
|
|
|
bfbe87 |
+ return err
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Update statistics.
|
|
|
bfbe87 |
+ tx.stats.Write++
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Exit inner for loop if we've written all the chunks.
|
|
|
bfbe87 |
+ size -= sz
|
|
|
bfbe87 |
+ if size == 0 {
|
|
|
bfbe87 |
+ break
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Otherwise move offset forward and move pointer to next chunk.
|
|
|
bfbe87 |
+ offset += int64(sz)
|
|
|
bfbe87 |
+ ptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz]))
|
|
|
bfbe87 |
+ }
|
|
|
bfbe87 |
}
|
|
|
bfbe87 |
+
|
|
|
bfbe87 |
+ // Ignore file sync if flag is set on DB.
|
|
|
bfbe87 |
if !tx.db.NoSync || IgnoreNoSync {
|
|
|
bfbe87 |
if err := fdatasync(tx.db); err != nil {
|
|
|
bfbe87 |
return err
|