SoFunction
Updated on 2025-03-05

GoLang BoltDB database detailed explanation

illustrate

Bolt is a pureKey/ValueThe program of the model. The goal of this project is to provide a simple, fast, and reliable database for projects that do not require a complete database server such as Postgres or MySQL.

BoltDB just needs to link it to your application code to use the API provided by BoltDB to efficiently access data. Moreover, BoltDB supports fully serializable ACID transactions, allowing applications to handle complex operations more easily.

BoltDB design originated from LMDB and has the following characteristics:

  • Written in Go
  • No server is needed to run
  • Support data structures
  • Directly use the API to access data, no query statement
  • Supports fully serializable ACID transactions, which is stronger than LevelDB
  • Data is saved in memory mapped files. No wal, thread compression and garbage collection
  • Through COW technology, lock-free reading and writing concurrency can be achieved, but lock-free writing and writing concurrency cannot be achieved. This is destined to have super high reading performance, but average writing performance, which is suitable for scenarios where more reading and less writing are available.

BoltDB is aKey/Value(key/value) storage, which means there is no likeSQL RDBMS (MySQL, PostgreSQL, etc.)tables in, no rows, no columns. Instead, data is stored as key-value pairs (as in Golang Maps). Key-value pairs are stored in Buckets, and they are designed to group similar pairs (this is similar to tables in RDBMS). Therefore, in order to obtain the Value, you need to know the bucket and key where the Value is located.

go get -u /boltdb/bolt

Open the database

db, err := (dbfile, 0600, nil)
if err != nil {
    (err)
}
defer ()

Update transactions

err := (func(tx *) error {
    ...
    return nil
})

illustrate:

  • Through this interface, the data update operation can be implemented.
  • Will be processed as a transaction. If the operation in Update() returns nil, the transaction will be submitted, otherwise the transaction will roll back

Read-only operation

err := (func(tx *) error {
    ...
    return nil
})

illustrate:

Through this interface, you can and can only perform data query operations. Batch update transactions

err := (func(tx *) error {
    ...
    return nil
})

illustrate:

  • Through this interface, multiple data update operations can be achieved
  • All updates will be processed as a transaction. If the operation in Update() returns nil, the transaction will be committed, otherwise the transaction will roll back.

Manual transaction management

// Start a writable transaction.
tx, err := (true)
if err != nil {
    return err
}
defer ()
// Use the transaction...
_, err := ([]byte("MyBucket"))
if err != nil {
    return err
}
// Commit the transaction and check for error.
if err := (); err != nil {
    return err
}

illustrate:

Create transactions by yourself and manage transaction commits and rollbacks, without using the encapsulated writing provided by BoltDB. Example

package main
import (
	"fmt"
	"log"

	"/boltdb/bolt"
)
var dbfile = ""
var bdb *
var bucket = []byte("MyBuckets")
func main() {
	var err error
	bdb, err = (dbfile, 0600, nil)
	if err != nil {
		(err)
	}
	defer func() {
		_ = ()
	}()
	CreateBuckets()
	updateData()
	selectData()
}
func CreateBuckets() error {
	return (func(tx *) error {
		_, err := (bucket)
		return err
	})
}
func updateData() error {
	return (func(tx *) error {
		bk := (bucket)
		if bk != nil {
			e1 := ([]byte("name"), []byte("rao"))
			if e1 != nil {
				return e1
			}
			e2 := ([]byte("age"), []byte("12"))
			if e2 != nil {
				return e2
			}
		}
		return nil
	})
}
func selectData() error {
	var name, age []byte
	return (func(tx *) error {
		bk := (bucket)
		if bk != nil {
			name = ([]byte("name"))
			age = ([]byte("age"))
		}
		("%s\n", name)
		("%s\n", age)
		return nil
	})
}

This is the end of this article about the detailed explanation of GoLang BoltDB database. For more related Go BoltDB content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!