buntdb/README.md

635 lines
20 KiB
Markdown
Raw Normal View History

2016-07-20 01:15:00 +03:00
<p align="center">
2017-11-28 22:37:21 +03:00
<img
src="logo.png"
2016-07-20 01:15:00 +03:00
width="307" height="150" border="0" alt="BuntDB">
<br>
2016-07-31 19:51:47 +03:00
<a href="https://travis-ci.org/tidwall/buntdb"><img src="https://img.shields.io/travis/tidwall/buntdb.svg?style=flat-square" alt="Build Status"></a>
2016-08-01 23:10:01 +03:00
<a href="http://gocover.io/github.com/tidwall/buntdb"><img src="https://img.shields.io/badge/coverage-95%25-brightgreen.svg?style=flat-square" alt="Code Coverage"></a>
2016-07-31 19:51:47 +03:00
<a href="https://goreportcard.com/report/github.com/tidwall/buntdb"><img src="https://goreportcard.com/badge/github.com/tidwall/buntdb?style=flat-square" alt="Go Report Card"></a>
<a href="https://godoc.org/github.com/tidwall/buntdb"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
2016-07-20 01:15:00 +03:00
</p>
2017-11-28 22:37:21 +03:00
BuntDB is a low-level, in-memory, key/value store in pure Go.
2016-07-20 01:15:00 +03:00
It persists to disk, is ACID compliant, and uses locking for multiple
2017-11-28 22:37:21 +03:00
readers and a single writer. It supports custom indexes and geospatial
data. It's ideal for projects that need a dependable database and favor
2016-07-20 01:15:00 +03:00
speed over data size.
Features
========
2016-07-25 16:54:53 +03:00
- In-memory database for [fast reads and writes](#performance)
2016-07-20 01:15:00 +03:00
- Embeddable with a [simple API](https://godoc.org/github.com/tidwall/buntdb)
- [Spatial indexing](#spatial-indexes) for up to 20 dimensions; Useful for Geospatial data
2016-08-03 03:24:56 +03:00
- Index fields inside [JSON](#json-indexes) documents
2016-09-03 05:49:05 +03:00
- [Collate i18n Indexes](#collate-i18n-indexes) using the optional [collate package](https://github.com/tidwall/collate)
2016-07-20 01:23:14 +03:00
- Create [custom indexes](#custom-indexes) for any data type
2016-08-18 19:08:30 +03:00
- Support for [multi value indexes](#multi-value-index); Similar to a SQL multi column index
2016-07-20 01:15:00 +03:00
- [Built-in types](#built-in-types) that are easy to get up & running; String, Uint, Int, Float
- Flexible [iteration](#iterating) of data; ascending, descending, and ranges
2016-09-03 05:49:05 +03:00
- [Durable append-only file](#append-only-file) format for persistence
2016-07-20 01:15:00 +03:00
- Option to evict old items with an [expiration](#data-expiration) TTL
2016-09-02 16:19:50 +03:00
- Tight codebase, under 2K loc using the `cloc` command
2016-07-20 01:15:00 +03:00
- ACID semantics with locking [transactions](#transactions) that support rollbacks
2016-09-03 05:49:05 +03:00
2016-07-20 01:15:00 +03:00
Getting Started
===============
## Installing
To start using BuntDB, install Go and run `go get`:
```sh
2016-08-07 01:55:51 +03:00
$ go get -u github.com/tidwall/buntdb
2016-07-20 01:15:00 +03:00
```
This will retrieve the library.
## Opening a database
2017-11-28 22:37:21 +03:00
The primary object in BuntDB is a `DB`. To open or create your
2016-07-20 01:15:00 +03:00
database, use the `buntdb.Open()` function:
```go
package main
import (
2018-05-03 19:42:12 +03:00
"log"
2016-07-20 01:15:00 +03:00
2018-05-03 19:42:12 +03:00
"github.com/tidwall/buntdb"
2016-07-20 01:15:00 +03:00
)
func main() {
2018-05-03 19:42:12 +03:00
// Open the data.db file. It will be created if it doesn't exist.
db, err := buntdb.Open("data.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
...
2016-07-20 01:15:00 +03:00
}
```
It's also possible to open a database that does not persist to disk by using `:memory:` as the path of the file.
```go
buntdb.Open(":memory:") // Open a file that does not persist to disk.
```
2016-07-20 01:15:00 +03:00
## Transactions
All reads and writes must be performed from inside a transaction. BuntDB can have one write transaction opened at a time, but can have many concurrent read transactions. Each transaction maintains a stable view of the database. In other words, once a transaction has begun, the data for that transaction cannot be changed by other transactions.
Transactions run in a function that exposes a `Tx` object, which represents the transaction state. While inside a transaction, all database operations should be performed using this object. You should never access the origin `DB` object while inside a transaction. Doing so may have side-effects, such as blocking your application.
2016-07-21 12:48:36 +03:00
When a transaction fails, it will roll back, and revert all changes that occurred to the database during that transaction. There's a single return value that you can use to close the transaction. For read/write transactions, returning an error this way will force the transaction to roll back. When a read/write transaction succeeds all changes are persisted to disk.
2016-07-20 01:15:00 +03:00
### Read-only Transactions
A read-only transaction should be used when you don't need to make changes to the data. The advantage of a read-only transaction is that there can be many running concurrently.
```go
err := db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
...
return nil
2016-07-20 01:15:00 +03:00
})
```
### Read/write Transactions
2017-11-28 22:37:21 +03:00
A read/write transaction is used when you need to make changes to your data. There can only be one read/write transaction running at a time. So make sure you close it as soon as you are done with it.
2016-07-20 01:15:00 +03:00
```go
err := db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
...
return nil
2016-07-20 01:15:00 +03:00
})
```
## Setting and getting key/values
2016-07-21 12:48:36 +03:00
To set a value you must open a read/write transaction:
2016-07-20 01:15:00 +03:00
```go
err := db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
_, _, err := tx.Set("mykey", "myvalue", nil)
return err
2016-07-20 01:15:00 +03:00
})
```
To get the value:
```go
err := db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
val, err := tx.Get("mykey")
if err != nil{
return err
}
fmt.Printf("value is %s\n", val)
return nil
2016-07-20 01:15:00 +03:00
})
```
2017-11-28 22:38:51 +03:00
Getting non-existent values will cause an `ErrNotFound` error.
2016-07-20 01:15:00 +03:00
### Iterating
All keys/value pairs are ordered in the database by the key. To iterate over the keys:
```go
err := db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
err := tx.Ascend("", func(key, value string) bool {
fmt.Printf("key: %s, value: %s\n", key, value)
})
return err
2016-07-20 01:15:00 +03:00
})
```
There is also `AscendGreaterOrEqual`, `AscendLessThan`, `AscendRange`, `AscendEqual`, `Descend`, `DescendLessOrEqual`, `DescendGreaterThan`, `DescendRange`, and `DescendEqual`. Please see the [documentation](https://godoc.org/github.com/tidwall/buntdb) for more information on these functions.
2016-07-20 01:15:00 +03:00
## Custom Indexes
2016-07-22 04:48:43 +03:00
Initially all data is stored in a single [B-tree](https://en.wikipedia.org/wiki/B-tree) with each item having one key and one value. All of these items are ordered by the key. This is great for quickly getting a value from a key or [iterating](#iterating) over the keys. Feel free to peruse the [B-tree implementation](https://github.com/tidwall/btree).
2016-07-20 01:15:00 +03:00
You can also create custom indexes that allow for ordering and [iterating](#iterating) over values. A custom index also uses a B-tree, but it's more flexible because it allows for custom ordering.
For example, let's say you want to create an index for ordering names:
```go
db.CreateIndex("names", "*", buntdb.IndexString)
```
This will create an index named `names` which stores and sorts all values. The second parameter is a pattern that is used to filter on keys. A `*` wildcard argument means that we want to accept all keys. `IndexString` is a built-in function that performs case-insensitive ordering on the values
Now you can add various names:
```go
db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Set("user:0:name", "tom", nil)
tx.Set("user:1:name", "Randi", nil)
tx.Set("user:2:name", "jane", nil)
tx.Set("user:4:name", "Janet", nil)
tx.Set("user:5:name", "Paula", nil)
tx.Set("user:6:name", "peter", nil)
tx.Set("user:7:name", "Terri", nil)
return nil
2016-07-20 01:15:00 +03:00
})
```
Finally you can iterate over the index:
```go
db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Ascend("names", func(key, val string) bool {
fmt.Printf(buf, "%s %s\n", key, val)
return true
})
return nil
2016-07-20 01:15:00 +03:00
})
```
The output should be:
```
user:2:name jane
user:4:name Janet
user:5:name Paula
user:6:name peter
user:1:name Randi
user:7:name Terri
user:0:name tom
```
The pattern parameter can be used to filter on keys like this:
```go
db.CreateIndex("names", "user:*", buntdb.IndexString)
```
Now only items with keys that have the prefix `user:` will be added to the `names` index.
### Built-in types
2017-11-28 22:37:21 +03:00
Along with `IndexString`, there is also `IndexInt`, `IndexUint`, and `IndexFloat`.
2016-07-20 01:15:00 +03:00
These are built-in types for indexing. You can choose to use these or create your own.
So to create an index that is numerically ordered on an age key, we could use:
```go
db.CreateIndex("ages", "user:*:age", buntdb.IndexInt)
```
And then add values:
```go
db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Set("user:0:age", "35", nil)
tx.Set("user:1:age", "49", nil)
tx.Set("user:2:age", "13", nil)
tx.Set("user:4:age", "63", nil)
tx.Set("user:5:age", "8", nil)
tx.Set("user:6:age", "3", nil)
tx.Set("user:7:age", "16", nil)
return nil
2016-07-20 01:15:00 +03:00
})
```
```go
db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Ascend("ages", func(key, val string) bool {
fmt.Printf(buf, "%s %s\n", key, val)
return true
})
return nil
2016-07-20 01:15:00 +03:00
})
```
The output should be:
```
2017-11-28 22:47:27 +03:00
user:6:age 3
user:5:age 8
user:2:age 13
user:7:age 16
user:0:age 35
user:1:age 49
user:4:age 63
2016-07-20 01:15:00 +03:00
```
2016-08-03 03:24:56 +03:00
## Spatial Indexes
2016-07-20 11:20:28 +03:00
BuntDB has support for spatial indexes by storing rectangles in an [R-tree](https://en.wikipedia.org/wiki/R-tree). An R-tree is organized in a similar manner as a [B-tree](https://en.wikipedia.org/wiki/B-tree), and both are balanced trees. But, an R-tree is special because it can operate on data that is in multiple dimensions. This is super handy for Geospatial applications.
2016-07-20 01:15:00 +03:00
To create a spatial index use the `CreateSpatialIndex` function:
```go
db.CreateSpatialIndex("fleet", "fleet:*:pos", buntdb.IndexRect)
```
Then `IndexRect` is a built-in function that converts rect strings to a format that the R-tree can use. It's easy to use this function out of the box, but you might find it better to create a custom one that renders from a different format, such as [Well-known text](https://en.wikipedia.org/wiki/Well-known_text) or [GeoJSON](http://geojson.org/).
To add some lon,lat points to the `fleet` index:
```go
db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Set("fleet:0:pos", "[-115.567 33.532]", nil)
tx.Set("fleet:1:pos", "[-116.671 35.735]", nil)
tx.Set("fleet:2:pos", "[-113.902 31.234]", nil)
return nil
2016-07-20 01:15:00 +03:00
})
```
And then you can run the `Intersects` function on the index:
```go
db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Intersects("fleet", "[-117 30],[-112 36]", func(key, val string) bool {
...
return true
})
return nil
2016-07-20 01:15:00 +03:00
})
```
This will get all three positions.
2018-01-13 02:38:11 +03:00
### k-Nearest Neighbors
Use the `Nearby` function to get all the positions in order of nearest to farthest :
```go
db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Nearby("fleet", "[-113 33]", func(key, val string, dist float64) bool {
...
return true
})
return nil
2018-01-13 02:38:11 +03:00
})
```
2016-08-03 03:24:56 +03:00
### Spatial bracket syntax
2016-07-22 02:14:42 +03:00
2016-08-02 17:40:34 +03:00
The bracket syntax `[-117 30],[-112 36]` is unique to BuntDB, and it's how the built-in rectangles are processed. But, you are not limited to this syntax. Whatever Rect function you choose to use during `CreateSpatialIndex` will be used to process the parameter, in this case it's `IndexRect`.
2016-07-20 01:15:00 +03:00
2017-11-28 22:37:21 +03:00
- **2D rectangle:** `[10 15],[20 25]`
2016-07-22 02:14:42 +03:00
*Min XY: "10x15", Max XY: "20x25"*
2017-11-28 22:37:21 +03:00
- **3D rectangle:** `[10 15 12],[20 25 18]`
2016-07-22 02:14:42 +03:00
*Min XYZ: "10x15x12", Max XYZ: "20x25x18"*
2017-11-28 22:37:21 +03:00
- **2D point:** `[10 15]`
2016-07-22 02:14:42 +03:00
*XY: "10x15"*
2017-11-28 22:53:46 +03:00
- **LonLat point:** `[-112.2693 33.5123]`
2016-07-22 02:14:42 +03:00
*LatLon: "33.5123 -112.2693"*
2017-11-28 22:53:46 +03:00
- **LonLat bounding box:** `[-112.26 33.51],[-112.18 33.67]`
2016-07-22 02:14:42 +03:00
*Min LatLon: "33.51 -112.26", Max LatLon: "33.67 -112.18"*
**Notice:** The longitude is the Y axis and is on the left, and latitude is the X axis and is on the right.
2016-07-20 01:15:00 +03:00
2017-11-28 22:37:21 +03:00
You can also represent `Infinity` by using `-inf` and `+inf`.
2016-08-02 17:40:34 +03:00
For example, you might have the following points (`[X Y M]` where XY is a point and M is a timestamp):
```
[3 9 1]
[3 8 2]
[4 8 3]
[4 7 4]
[5 7 5]
[5 6 6]
```
You can then do a search for all points with `M` between 2-4 by calling `Intersects`.
```go
tx.Intersects("points", "[-inf -inf 2],[+inf +inf 4]", func(key, val string) bool {
2018-05-03 19:42:12 +03:00
println(val)
return true
2016-08-02 17:40:34 +03:00
})
```
Which will return:
```
[3 8 2]
[4 8 3]
[4 7 4]
```
2016-08-03 03:24:56 +03:00
## JSON Indexes
2016-08-11 07:12:36 +03:00
Indexes can be created on individual fields inside JSON documents. BuntDB uses [GJSON](https://github.com/tidwall/gjson) under the hood.
2016-08-02 17:40:34 +03:00
2016-08-03 03:24:56 +03:00
For example:
2016-08-02 17:40:34 +03:00
2016-08-03 03:24:56 +03:00
```go
package main
import (
2018-05-03 19:42:12 +03:00
"fmt"
2016-08-03 03:24:56 +03:00
2018-05-03 19:42:12 +03:00
"github.com/tidwall/buntdb"
2016-08-03 03:24:56 +03:00
)
func main() {
2018-05-03 19:42:12 +03:00
db, _ := buntdb.Open(":memory:")
db.CreateIndex("last_name", "*", buntdb.IndexJSON("name.last"))
db.CreateIndex("age", "*", buntdb.IndexJSON("age"))
db.Update(func(tx *buntdb.Tx) error {
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
return nil
})
db.View(func(tx *buntdb.Tx) error {
fmt.Println("Order by last name")
tx.Ascend("last_name", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
fmt.Println("Order by age")
tx.Ascend("age", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
fmt.Println("Order by age range 30-50")
tx.AscendRange("age", `{"age":30}`, `{"age":50}`, func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
return nil
})
2016-08-03 03:24:56 +03:00
}
```
Results:
2016-08-02 17:40:34 +03:00
2016-08-03 03:24:56 +03:00
```
Order by last name
3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
Order by age
4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
2016-08-02 17:40:34 +03:00
2016-08-16 21:42:34 +03:00
Order by age range 30-50
1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
```
2016-09-03 21:00:34 +03:00
2016-08-18 19:08:30 +03:00
## Multi Value Index
2017-11-28 22:37:21 +03:00
With BuntDB it's possible to join multiple values on a single index.
2016-08-18 19:08:30 +03:00
This is similar to a [multi column index](http://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html) in a traditional SQL database.
In this example we are creating a multi value index on "name.last" and "age":
```go
2016-08-19 21:56:43 +03:00
db, _ := buntdb.Open(":memory:")
db.CreateIndex("last_name_age", "*", buntdb.IndexJSON("name.last"), buntdb.IndexJSON("age"))
db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil)
tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil)
return nil
2016-08-19 21:56:43 +03:00
})
db.View(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Ascend("last_name_age", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
return nil
2016-08-19 21:56:43 +03:00
})
// Output:
// 5: {"name":{"first":"Sam","last":"Anderson"},"age":51}
// 3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
// 4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
// 1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
// 6: {"name":{"first":"Melinda","last":"Prichard"},"age":44}
// 2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
```
## Descending Ordered Index
Any index can be put in descending order by wrapping it's less function with `buntdb.Desc`.
2016-08-18 19:08:30 +03:00
2016-08-19 21:56:43 +03:00
```go
2018-05-03 19:43:07 +03:00
db.CreateIndex("last_name_age", "*",
buntdb.IndexJSON("name.last"),
buntdb.Desc(buntdb.IndexJSON("age")))
2016-08-18 19:08:30 +03:00
```
2016-08-19 21:56:43 +03:00
This will create a multi value index where the last name is ascending and the age is descending.
2016-08-02 17:40:34 +03:00
2016-09-03 21:00:34 +03:00
## Collate i18n Indexes
Using the external [collate package](https://github.com/tidwall/collate) it's possible to create
2016-11-16 13:05:59 +03:00
indexes that are sorted by the specified language. This is similar to the [SQL COLLATE keyword](https://msdn.microsoft.com/en-us/library/ms174596.aspx) found in traditional databases.
2016-09-03 21:00:34 +03:00
To install:
```
go get -u github.com/tidwall/collate
```
For example:
```go
import "github.com/tidwall/collate"
// To sort case-insensitive in French.
db.CreateIndex("name", "*", collate.IndexString("FRENCH_CI"))
2017-11-28 22:37:21 +03:00
// To specify that numbers should sort numerically ("2" < "12")
2016-09-03 21:00:34 +03:00
// and use a comma to represent a decimal point.
2017-11-28 22:37:21 +03:00
db.CreateIndex("amount", "*", collate.IndexString("FRENCH_NUM"))
2016-09-03 21:00:34 +03:00
```
There's also support for Collation on JSON indexes:
```go
db.CreateIndex("last_name", "*", collate.IndexJSON("CHINESE_CI", "name.last"))
```
Check out the [collate project](https://github.com/tidwall/collate) for more information.
2016-08-03 03:45:52 +03:00
## Data Expiration
2016-07-20 01:15:00 +03:00
Items can be automatically evicted by using the `SetOptions` object in the `Set` function to set a `TTL`.
```go
db.Update(func(tx *buntdb.Tx) error {
2018-05-03 19:42:12 +03:00
tx.Set("mykey", "myval", &buntdb.SetOptions{Expires:true, TTL:time.Second})
return nil
2016-07-20 01:15:00 +03:00
})
```
Now `mykey` will automatically be deleted after one second. You can remove the TTL by setting the value again with the same key/value, but with the options parameter set to nil.
2018-04-30 14:40:01 +03:00
## Delete while iterating
BuntDB does not currently support deleting a key while in the process of iterating.
As a workaround you'll need to delete keys following the completion of the iterator.
```go
var delkeys []string
tx.AscendKeys("object:*", func(k, v string) bool {
if someCondition(k) == true {
delkeys = append(delkeys, k)
}
return true // continue
})
for _, k := range delkeys {
if _, err = tx.Delete(k); err != nil {
return err
}
}
```
2016-07-20 22:26:24 +03:00
## Append-only File
2017-11-28 22:37:21 +03:00
BuntDB uses an AOF (append-only file) which is a log of all database changes that occur from operations like `Set()` and `Delete()`.
2016-07-20 22:26:24 +03:00
The format of this file looks like:
```
set key:1 value1
set key:2 value2
set key:1 value3
del key:2
...
```
When the database opens again, it will read back the aof file and process each command in exact order.
This read process happens one time when the database opens.
From there on the file is only appended.
As you may guess this log file can grow large over time.
There's a background routine that automatically shrinks the log file when it gets too large.
There is also a `Shrink()` function which will rewrite the aof file so that it contains only the items in the database.
The shrink operation does not lock up the database so read and write transactions can continue while shrinking is in process.
2016-07-20 22:26:24 +03:00
### Durability and fsync
2016-07-27 19:24:18 +03:00
By default BuntDB executes an `fsync` once every second on the [aof file](#append-only-file). Which simply means that there's a chance that up to one second of data might be lost. If you need higher durability then there's an optional database config setting `Config.SyncPolicy` which can be set to `Always`.
2016-07-20 22:26:24 +03:00
2016-07-27 19:24:18 +03:00
The `Config.SyncPolicy` has the following options:
2016-07-20 22:26:24 +03:00
- `Never` - fsync is managed by the operating system, less safe
- `EverySecond` - fsync every second, fast and safer, this is the default
2016-07-27 19:24:18 +03:00
- `Always` - fsync after every write, very durable, slower
2016-07-20 22:26:24 +03:00
2017-11-28 22:37:21 +03:00
## Config
2016-08-23 19:16:01 +03:00
Here are some configuration options that can be use to change various behaviors of the database.
- **SyncPolicy** adjusts how often the data is synced to disk. This value can be Never, EverySecond, or Always. Default is EverySecond.
- **AutoShrinkPercentage** is used by the background process to trigger a shrink of the aof file when the size of the file is larger than the percentage of the result of the previous shrunk file. For example, if this value is 100, and the last shrink process resulted in a 100mb file, then the new aof file must be 200mb before a shrink is triggered. Default is 100.
- **AutoShrinkMinSize** defines the minimum size of the aof file before an automatic shrink can occur. Default is 32MB.
- **AutoShrinkDisabled** turns off automatic background shrinking. Default is false.
To update the configuration you should call `ReadConfig` followed by `SetConfig`. For example:
```go
var config buntdb.Config
if err := db.ReadConfig(&config); err != nil{
2018-05-03 19:42:12 +03:00
log.Fatal(err)
2016-08-23 19:16:01 +03:00
}
if err := db.SetConfig(config); err != nil{
2018-05-03 19:42:12 +03:00
log.Fatal(err)
2016-08-23 19:16:01 +03:00
}
```
2016-07-25 16:54:53 +03:00
## Performance
How fast is BuntDB?
2017-11-28 22:37:21 +03:00
Here are some example [benchmarks](https://github.com/tidwall/raft-buntdb#raftstore-performance-comparison) when using BuntDB in a Raft Store implementation.
2016-07-25 16:54:53 +03:00
You can also run the standard Go benchmark tool from the project root directory:
```
go test --bench=.
```
### BuntDB-Benchmark
There's a [custom utility](https://github.com/tidwall/buntdb-benchmark) that was created specifically for benchmarking BuntDB.
*These are the results from running the benchmarks on a MacBook Pro 15" 2.8 GHz Intel Core i7:*
```
$ buntdb-benchmark -q
GET: 4609604.74 operations per second
SET: 248500.33 operations per second
ASCEND_100: 2268998.79 operations per second
ASCEND_200: 1178388.14 operations per second
ASCEND_400: 679134.20 operations per second
ASCEND_800: 348445.55 operations per second
DESCEND_100: 2313821.69 operations per second
DESCEND_200: 1292738.38 operations per second
DESCEND_400: 675258.76 operations per second
DESCEND_800: 337481.67 operations per second
SPATIAL_SET: 134824.60 operations per second
SPATIAL_INTERSECTS_100: 939491.47 operations per second
SPATIAL_INTERSECTS_200: 561590.40 operations per second
SPATIAL_INTERSECTS_400: 306951.15 operations per second
SPATIAL_INTERSECTS_800: 159673.91 operations per second
```
2016-07-25 16:55:46 +03:00
To install this utility:
2016-07-25 16:54:53 +03:00
```
2016-07-25 17:12:49 +03:00
go get github.com/tidwall/buntdb-benchmark
2016-07-25 16:54:53 +03:00
```
2016-07-20 22:26:24 +03:00
2016-07-20 01:15:00 +03:00
## Contact
Josh Baker [@tidwall](http://twitter.com/tidwall)
## License
BuntDB source code is available under the MIT [License](/LICENSE).