boltdb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: Apache-2.0 Imports: 12 Imported by: 2

README

Valkeyrie BoltDB

GoDoc Build Status Go Report Card

valkeyrie provides a Go native library to store metadata using Distributed Key/Value stores (or common databases).

Compatibility

A storage backend in valkeyrie implements (fully or partially) the Store interface.

Calls BoltDB
Put 🟢
Get 🟢
Delete 🟢
Exists 🟢
Watch 🔴
WatchTree 🔴
NewLock (Lock/Unlock) 🔴
List 🟢
DeleteTree 🟢
AtomicPut 🟢
AtomicDelete 🟢

Examples

package main

import (
	"context"
	"log"

	"github.com/kvtools/boltdb"
	"github.com/kvtools/valkeyrie"
)

func main() {
	ctx := context.Background()

	config := &boltdb.Config{
		Bucket: "example",
	}

	kv, err := valkeyrie.NewStore(ctx, boltdb.StoreName, []string{"/tmp/mydatabase"}, config)
	if err != nil {
		log.Fatal("Cannot create store")
	}

	key := "foo"

	err = kv.Put(ctx, key, []byte("bar"), nil)
	if err != nil {
		log.Fatalf("Error trying to put value at key: %v", key)
	}

	pair, err := kv.Get(ctx, key, nil)
	if err != nil {
		log.Fatalf("Error trying accessing value at key: %v", key)
	}

	log.Printf("value: %s", string(pair.Value))

	err = kv.Delete(ctx, key)
	if err != nil {
		log.Fatalf("Error trying to delete key %v", key)
	}
}

Documentation

Overview

Package boltdb contains the BoltDB store implementation.

Index

Constants

View Source
const StoreName = "boltdb"

StoreName the name of the store.

Variables

View Source
var (
	// ErrMultipleEndpointsUnsupported is thrown when multiple endpoints specified for BoltDB.
	// Endpoint has to be a local file path.
	ErrMultipleEndpointsUnsupported = errors.New("boltdb supports one endpoint and should be a file path")
	// ErrBoltBucketOptionMissing is thrown when boltBucket config option is missing.
	ErrBoltBucketOptionMissing = errors.New("boltBucket config option missing")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Bucket            string
	PersistConnection bool
	ConnectionTimeout time.Duration
}

Config the BoltDB configuration.

type Store

type Store struct {

	// By default, valkeyrie opens and closes the BoltDB connection for every get/put operation.
	// This allows multiple apps to use a BoltDB at the same time.
	// PersistConnection flag provides an option to override ths behavior.
	// ie: open the connection in New and use it till Close is called.
	PersistConnection bool
	// contains filtered or unexported fields
}

Store implements the store.Store interface.

func New

func New(_ context.Context, endpoints []string, options *Config) (*Store, error)

New creates a new BoltDB client.

func (*Store) AtomicDelete

func (b *Store) AtomicDelete(_ context.Context, key string, previous *store.KVPair) (bool, error)

AtomicDelete deletes a value at "key" if the key has not been modified in the meantime, throws an error if this is the case.

func (*Store) AtomicPut

func (b *Store) AtomicPut(_ context.Context, key string, value []byte, previous *store.KVPair, _ *store.WriteOptions) (bool, *store.KVPair, error)

AtomicPut puts a value at "key" if the key has not been modified since the last Put, throws an error if this is the case.

func (*Store) Close

func (b *Store) Close() error

Close the db connection to the BoltDB.

func (*Store) Delete

func (b *Store) Delete(_ context.Context, key string) error

Delete the value for the given key.

func (*Store) DeleteTree

func (b *Store) DeleteTree(_ context.Context, keyPrefix string) error

DeleteTree deletes a range of keys with a given prefix.

func (*Store) Exists

func (b *Store) Exists(_ context.Context, key string, _ *store.ReadOptions) (bool, error)

Exists checks if the key exists inside the store.

func (*Store) Get

func (b *Store) Get(_ context.Context, key string, _ *store.ReadOptions) (*store.KVPair, error)

Get the value at "key". BoltDB doesn't provide an inbuilt last modified index with every kv pair. It's implemented by an atomic counter maintained by the valkeyrie and appended to the value passed by the client.

func (*Store) List

func (b *Store) List(_ context.Context, keyPrefix string, _ *store.ReadOptions) ([]*store.KVPair, error)

List returns the range of keys starting with the passed in prefix.

func (*Store) NewLock

func (b *Store) NewLock(_ context.Context, _ string, _ *store.LockOptions) (store.Locker, error)

NewLock has to implemented at the library level since it's not supported by BoltDB.

func (*Store) Put

func (b *Store) Put(_ context.Context, key string, value []byte, _ *store.WriteOptions) error

Put the key, value pair. Index number metadata is prepended to the value.

func (*Store) Watch

func (b *Store) Watch(_ context.Context, _ string, _ *store.ReadOptions) (<-chan *store.KVPair, error)

Watch has to implemented at the library level since it's not supported by BoltDB.

func (*Store) WatchTree

func (b *Store) WatchTree(_ context.Context, _ string, _ *store.ReadOptions) (<-chan []*store.KVPair, error)

WatchTree has to implemented at the library level since it's not supported by BoltDB.

Jump to

Keyboard shortcuts

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