simplebolt

package module
v0.0.0-...-26268dd Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 8 Imported by: 0

README

Simple Bolt

Build Status GoDoc

Simple way to use the Bolt database. Similar design to simpleredis.

Online API Documentation

godoc.org

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values
  • Deals mainly with strings

Example usage

package main

import (
	"log"

	"github.com/xyproto/simplebolt"
)

func main() {
	// New bolt database
	db, err := simplebolt.New("bolt.db")
	if err != nil {
		log.Fatalf("Could not create database! %s", err)
	}
	defer db.Close()

	// Create a list named "greetings"
	list, err := simplebolt.NewList(db, "greetings")
	if err != nil {
		log.Fatalf("Could not create a list! %s", err)
	}

	// Add "hello" to the list
	if err := list.Add("hello"); err != nil {
		log.Fatalf("Could not add an item to the list! %s", err)
	}

	// Get the last item of the list
	if item, err := list.GetLast(); err != nil {
		log.Fatalf("Could not fetch the last item from the list! %s", err)
	} else {
		log.Println("The value of the stored item is:", item)
	}

	// Remove the list
	if err := list.Remove(); err != nil {
		log.Fatalf("Could not remove the list! %s", err)
	}
}

Version, license and author

Documentation

Overview

Package simplebolt provides a simple way to use the Bolt database. The API design is similar to xyproto/simpleredis, and the database backends are interchangeable, by using the xyproto/pinterface package.

Index

Constants

View Source
const (
	// Version number. Stable API within major version numbers.
	Version = 3.0
)

Variables

View Source
var (
	ErrBucketNotFound = errors.New("Bucket not found")
	ErrKeyNotFound    = errors.New("Key not found")
	ErrDoesNotExist   = errors.New("Does not exist")
	ErrFoundIt        = errors.New("Found it")
	ErrExistsInSet    = errors.New("Element already exists in set")
	ErrInvalidID      = errors.New("Element ID can not contain \":\"")
)

Functions

This section is empty.

Types

type BoltCreator

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

func NewCreator

func NewCreator(db *Database) *BoltCreator

func (*BoltCreator) NewHashMap

func (b *BoltCreator) NewHashMap(id string) (pinterface.IHashMap, error)

func (*BoltCreator) NewKeyValue

func (b *BoltCreator) NewKeyValue(id string) (pinterface.IKeyValue, error)

func (*BoltCreator) NewList

func (b *BoltCreator) NewList(id string) (pinterface.IList, error)

func (*BoltCreator) NewSet

func (b *BoltCreator) NewSet(id string) (pinterface.ISet, error)

type Database

type Database bolt.DB

A Bolt database

func New

func New(filename string) (*Database, error)

Create a new bolt database

func (*Database) Close

func (db *Database) Close()

Close the database

func (*Database) Ping

func (db *Database) Ping() error

Ping the database (only for fulfilling the pinterface.IHost interface)

type HashMap

type HashMap boltBucket

func NewHashMap

func NewHashMap(db *Database, id string) (*HashMap, error)

Create a new HashMap

func (*HashMap) All

func (h *HashMap) All() (results []string, err error)

Get all elementid's for all hash elements

func (*HashMap) Clear

func (h *HashMap) Clear() error

Remove all elements from this hash map

func (*HashMap) Del

func (h *HashMap) Del(elementid string) error

Remove an element (for instance a user)

func (*HashMap) DelKey

func (h *HashMap) DelKey(elementid, key string) error

Remove a key for an entry in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

func (h *HashMap) Exists(elementid string) (found bool, err error)

Check if a given elementid exists as a hash map at all

func (*HashMap) Get

func (h *HashMap) Get(elementid, key string) (val string, err error)

Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password")

func (*HashMap) Has

func (h *HashMap) Has(elementid, key string) (found bool, err error)

Check if a given elementid + key is in the hash map

func (*HashMap) Keys

func (h *HashMap) Keys(owner string) ([]string, error)

Check if a given elementid + key is in the hash map

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) Set

func (h *HashMap) Set(elementid, key, value string) (err error)

Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")

type KeyValue

type KeyValue boltBucket

func NewKeyValue

func NewKeyValue(db *Database, id string) (*KeyValue, error)

Create a new key/value if it does not already exist

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Remove all elements from this key/value

func (*KeyValue) Del

func (kv *KeyValue) Del(key string) error

Remove a key

func (*KeyValue) Get

func (kv *KeyValue) Get(key string) (val string, err error)

Get a value given a key Returns an error if the key was not found

func (*KeyValue) Inc

func (kv *KeyValue) Inc(key string) (val string, err error)

Increase the value of a key, returns the new value Returns an empty string if there were errors, or "0" if the key does not already exist.

func (*KeyValue) Remove

func (kv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) Set

func (kv *KeyValue) Set(key, value string) error

Set a key and value

type KeyValueB

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

func NewKeyValueB

func NewKeyValueB(db *Database, id string) (*KeyValueB, error)

Create a new key/value if it does not already exist

func (*KeyValueB) Begin

func (kv *KeyValueB) Begin(w bool) error

func (*KeyValueB) Commit

func (kv *KeyValueB) Commit() error

func (*KeyValueB) Get

func (kv *KeyValueB) Get(key string) (val string, err error)

Get a value given a key Returns an error if the key was not found

func (*KeyValueB) Set

func (kv *KeyValueB) Set(key, value string) error

Set a key and value

type List

type List boltBucket

The wrapped datatypes

func NewList

func NewList(db *Database, id string) (*List, error)

Create a new list

func (*List) Add

func (l *List) Add(value string) error

Add an element to the list

func (*List) All

func (l *List) All() (results []string, err error)

Get all elements of a list

func (*List) Clear

func (l *List) Clear() error

Remove all elements from this list

func (*List) Last

func (l *List) Last() (result string, err error)

Get the last element of a list

func (*List) LastN

func (l *List) LastN(n int) (results []string, err error)

Get the last N elements of a list

func (*List) Remove

func (l *List) Remove() error

Remove this list

type Set

type Set boltBucket

func NewSet

func NewSet(db *Database, id string) (*Set, error)

Create a new key/value if it does not already exist

func (*Set) Add

func (s *Set) Add(value string) error

Add an element to the set

func (*Set) All

func (s *Set) All() (values []string, err error)

Get all values of the set

func (*Set) Clear

func (s *Set) Clear() error

Remove all elements from this set

func (*Set) Del

func (s *Set) Del(value string) error

Remove an element from the set

func (*Set) Has

func (s *Set) Has(value string) (exists bool, err error)

Check if a given value is in the set

func (*Set) Remove

func (s *Set) Remove() error

Remove this set

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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