Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Golang profile imageYusuf Turhan Papurcu
Yusuf Turhan Papurcu forGolang

Posted on

     

Using BoltDB as internal database 💾

If you are looking for a small database for your fun project, I have something you might like. It is small, works as a key-value store and it's pure go.

What is Bolt?

Bolt is a pure Go key/value store inspired by Howard Chu's LMDB project. The goal of the project is to provide a simple, fast, and reliable database for projects that don't require a full database server such as Postgres or MySQL.

Since Bolt is meant to be used as such a low-level piece of functionality, simplicity is key. The API will be small and only focus on getting values and setting values. That's it.

How to use Bolt?

Installing

Bolt doesn't need any install other thango get. It just works as a library. So your only need is adding it togo.mod file.

go get github.com/boltdb/bolt

Creating database

After you can create your database like this.

db,err:=bolt.Open("my.db",0600,&bolt.Options{})iferr!=nil{log.Fatal(err)}deferdb.Close()
Enter fullscreen modeExit fullscreen mode

In here you can add timeout to your database. Or you can set readonly if you don't need writing into. Just set them in&bolt.Options{}.

Writing into database

You need to create bucket first. After than you can write your key/value data into bucket. Alsotx.CreateBucketIfNotExists([]byte) function is life saver.

db.Update(func(tx*bolt.Tx)error{bucket,err:=tx.CreateBucketIfNotExists([]byte("todo"))iferr!=nil{returnfmt.Errorf("create bucket: %s",err)}returnbucket.Put([]byte("task-1"),[]byte("Test BoltDB"))})
Enter fullscreen modeExit fullscreen mode

If you have a bucket already you can write into it like this. Caution checking bucket is nil is important. Because if you try to usePut on nil bucket your program will panic.

db.Update(func(tx*bolt.Tx)error{bucket:=tx.Bucket([]byte("todo"))ifbucket==nil{returnfmt.Errorf("get bucket: FAILED")}returnbucket.Put([]byte("task-1"),[]byte("Test BoltDB additions"))})
Enter fullscreen modeExit fullscreen mode

Querying data from database

Querying is as simple as writing. Just call your bucket and ask it for your data.

db.View(func(tx*bolt.Tx)error{b:=tx.Bucket([]byte("todo"))ifb==nil{returnfmt.Errorf("get bucket: FAILED")}fmt.Println(b.Get([]byte("task-1")))// should return nil to complete the transactionreturnnil})
Enter fullscreen modeExit fullscreen mode

And here is simple way to iterate on all keys in one bucket.

db.View(func(tx*bolt.Tx)error{b:=tx.Bucket([]byte("todo"))ifb==nil{returnfmt.Errorf("get bucket: FAILED")}// we need cursor for iterationc:=b.Cursor()fork,v:=c.First();k!=nil;k,v=c.Next(){fmt.Println("Key: ",string(k)," Value: ",string(v))}// should return nil to complete the transactionreturnnil})
Enter fullscreen modeExit fullscreen mode

Conclusion

  • Bolt is a pure Go key/value store
  • Bolt is native go library
  • Bolt is supported everywhere which go is supported
  • Creating, Writing and Querying inside Bolt is really easy.

Simple and powerful toolkit for BoltDB

Storm is a simple and powerful toolkit for BoltDB. Basically, Storm provides indexes, a wide range of methods to store and fetch data, an advanced query system, and much more.
GitHub:https://github.com/asdine/storm

Future Readings

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
marcello_h profile image
Marcelloh
Go trainer / Principal Go Engineer / Senior Software Architect
  • Location
    The Netherlands
  • Joined

I personally like: /github.com/xujiajun/nutsdb
(faster than BoltDB)

CollapseExpand
 
leningr1988 profile image
Lenin Guerrero
  • Email
  • Education
    Systems Engineer
  • Work
    Sr Software Developer On MercadoLibre
  • Joined

excellent article, I am implementing the Storm toolkit but, I have doubts about in which cases I should use buckets, what are they for, I think the documentation falls short and the other thing is that I am saving a struct that contains another struct, but I can not find data in the internal struct which I have as inline. Has anyone worked with this lib and knows how to make that query?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

All About Golang

bold

More fromGolang

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp