boltdbboilerplate

package module
v0.0.0-...-0237186 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2016 License: CC0-1.0 Imports: 4 Imported by: 0

README

Go Report Card

boltdb-boilerplate

boltdb-boilerplate is a simple & stupid boilerplate project wrapping around boltdb, and aim to make simple calls as one-liners.

Methods Summary

  • InitBolt: Init the database
  • Close: Close the database
  • Get: Retrieve a value by key
  • Put: Put a key/value pair into a bucket
  • Delete: Delete a key/value pair from a bucket
  • GetAllKeys: Get all keys from a bucket in [][]byte fromat
  • GetAllKeyValues: Get all key/value pairs from a bucket in []BoltPair fromat

Usage Demo

// import
import "github.com/bobintornado/boltdb-boilerplate"

// Init
buckets := []string{"ownerBucket", "sensors"}

err := boltdbboilerplate.InitBolt("./database.boltdb", buckets)
if err != nil {
  log.Fatal("Can't init boltDB")
}

// Put
err = boltdbboilerplate.Put([]byte("ownerBucket"), []byte("ownerKey"), []byte("username"))

// Get owner 
value := boltdbboilerplate.Get([]byte("ownerBucket"), []byte("ownerKey"))

// Delete
err = boltdbboilerplate.Delete([]byte("ownerBucket"), []byte("ownerKey"))

// Insert two key/value
err = boltdbboilerplate.Put([]byte("sensors"), []byte("key1"), []byte("value1"))
err = boltdbboilerplate.Put([]byte("sensors"), []byte("key2"), []byte("value2"))

// Get all keys
keys := boltdbboilerplate.GetAllKeys([]byte("sensors"))
// keys = [key1, key2]

// Get all key/value pairs
pairs := boltdbboilerplate.GetAllKeyValues([]byte("sensors"))
// pairs = [{Key:key1, Value:value1}, {Key: key2, Value:value2}]

// Close
boltdbboilerplate.Close()

Docs

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close bolt db

func Delete

func Delete(bucket []byte, key []byte) error

Delete a key from target bucket

func Get

func Get(bucket []byte, key []byte) []byte

Get value from bucket by key

func GetAllKeys

func GetAllKeys(bucket []byte) [][]byte

GetAllKeys get all keys from the target bucket

func InitBolt

func InitBolt(path string, buckets []string) error

InitBolt inits bolt database. Create the file if not exist. By default it opens the file in 0600 mode, with a 10 seconds timeout period

func Put

func Put(bucket []byte, key []byte, value []byte) error

Put a key/value pair into target bucket

Types

type BoltPair

type BoltPair struct {
	Key   []byte
	Value []byte
}

BoltPair is a struct to store key/value pair data

func GetAllKeyValues

func GetAllKeyValues(bucket []byte) []BoltPair

GetAllKeyValues get all key/value pairs from a bucket in BoltPair struct format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL