+1
Daily
4,872
stars
320
forks
104
watchers
25
prs
95
issues
0
releases
README
BuntDB is a low-level, in-memory, key/value store in pure Go.
It persists to disk, is ACID compliant, and uses locking for multiple
readers and a single writer. It supports custom indexes and geospatial
data. It's ideal for projects that need a dependable database and favor
speed over data size.
Features
========
- In-memory database for fast reads and writes
- Embeddable with a simple API
- Spatial indexing for up to 20 dimensions; Useful for Geospatial data
- Index fields inside JSON documents
- Collate i18n Indexes using the optional collate package
- Create custom indexes for any data type
- Support for multi value indexes; Similar to a SQL multi column index
- Built-in types that are easy to get up & running; String, Uint, Int, Float
- Flexible iteration of data; ascending, descending, and ranges
- Durable append-only file format for persistence
- Option to evict old items with an expiration TTL
- ACID semantics with locking transactions that support rollbacks
Getting Started
===============
Installing
To start using BuntDB, install Go and run go get:
sh
$ go get -u github.com/tidwall/buntdb
This will retrieve the library.
Opening a database
The primary object in BuntDB is a DB. To open or create your
database, use the buntdb.Open() function:
go
package main
import (
"log"
"github.com/tidwall/buntdb"
)
func main() {
// 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()
...
}
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.
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.
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.
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(Top issues
#37Auto Shrink dont get triggered7 comments · 2018-06-07#82feature request: data recovery tool5 comments · 2022-01-23#72Odd database corruption5 comments · 2021-03-26#123[FR] Bit index on json property support3 comments · 2025-07-26#83DescendKeys works incorrectly3 comments · 2022-02-19#74Noticed Deadlock? Sometimes buntdb gets stuck by a deadlock3 comments · 2021-05-25#84Does the package support GO v1.17.22 comments · 2022-03-16#103What happens if two funcs write to the db at the exact same time?1 comments · 2023-05-05