bolt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: Apache-2.0 Imports: 3 Imported by: 9

README

bolt

Go Report Card

Wrapper for etcd-io/bbolt for simple key value store use cases.

Example usage
package main

import (
	"fmt"

	"github.com/mlctrez/bolt"
	"go.etcd.io/bbolt"	
)


func main() {

	// error handling omitted for example
	db, _ := bolt.New("temp/bolt.db")
	defer func() { _ = db.Close() }()

	bucketOne := bolt.Key("BucketOne")
	_ = db.CreateBuckets(bolt.Keys{bucketOne})

	// single updates
	_ = db.Put(bucketOne, &stringStorage{key: "keyOne", value: "valueOne"})

	// multiple updates with transaction rollback
	_ = db.Update(func(tx *bbolt.Tx) error {
		bucketTx := bucketOne.Bucket(tx)
		for i := 0; i < 5; i++ {
			data := fmt.Sprintf("data_%d", i)
			err := bucketTx.Put(&stringStorage{key: bolt.Key(data), value: data})
			if err != nil {
				_ = tx.Rollback()
				return err
			}
		}
		return nil
	})

	// simple read
	simpleRead := &stringStorage{key: "keyOne"}
	_ = db.Get(bucketOne, simpleRead)
	fmt.Println(simpleRead.value)

	badLookup := &stringStorage{key: "doesnotexist"}
	if err := db.Get(bucketOne, badLookup); err == bolt.ErrValueNotFound {
		fmt.Println(err)
	}
}


var _ bolt.ValueProvider = (*stringStorage)(nil)
var _ bolt.ValueReceiver = (*stringStorage)(nil)

type stringStorage struct {
	key   bolt.Key
	value string
}

func (s *stringStorage) Value() ([]byte, error) {
	return []byte(s.value), nil
}

func (s *stringStorage) Key() bolt.Key {
	return s.key
}

func (s *stringStorage) SetValue(bytes []byte) error {
	s.value = string(bytes)
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrValueNotFound = errors.New("value not found")

Functions

This section is empty.

Types

type Bolt

type Bolt struct {
	// contains filtered or unexported fields
}

func New

func New(path string) (*Bolt, error)

func NewWithOptions added in v1.0.0

func NewWithOptions(path string, boltOptions *bbolt.Options) (*Bolt, error)

func (*Bolt) Close

func (s *Bolt) Close() error

func (*Bolt) CreateBuckets

func (s *Bolt) CreateBuckets(buckets Keys) error

func (*Bolt) Delete added in v0.0.3

func (s *Bolt) Delete(bucket Key, key Key) error

func (*Bolt) Get

func (s *Bolt) Get(bucket Key, receiver ValueReceiver) error

func (*Bolt) Put

func (s *Bolt) Put(bucket Key, provider ValueProvider) error

func (*Bolt) Update

func (s *Bolt) Update(update func(tx *bbolt.Tx) error) error

func (*Bolt) View

func (s *Bolt) View(view func(tx *bbolt.Tx) error) error

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

func (*Bucket) Delete added in v0.0.3

func (b *Bucket) Delete(key Key) error

func (*Bucket) Get

func (b *Bucket) Get(r ValueReceiver) error

func (*Bucket) Put

func (b *Bucket) Put(p ValueProvider) error

type HasKey

type HasKey interface {
	Key() Key
}

type Key

type Key string

func (Key) B

func (b Key) B() []byte

func (Key) Bucket

func (b Key) Bucket(tx *bbolt.Tx) *Bucket

func (Key) CreateBucket

func (b Key) CreateBucket(tx *bbolt.Tx) error

type Keys

type Keys []Key

func (Keys) CreateBucket

func (keys Keys) CreateBucket(tx *bbolt.Tx) error

type Value added in v0.0.5

type Value struct {
	K Key
	V []byte
}

func (*Value) Key added in v0.0.5

func (v *Value) Key() Key

func (*Value) SetValue added in v0.0.5

func (v *Value) SetValue(bytes []byte) error

func (*Value) Value added in v0.0.5

func (v *Value) Value() ([]byte, error)

type ValueProvider

type ValueProvider interface {
	HasKey
	Value() ([]byte, error)
}

type ValueReceiver

type ValueReceiver interface {
	HasKey
	SetValue([]byte) error
}

Jump to

Keyboard shortcuts

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