simplebolt

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: MIT Imports: 7 Imported by: 11

README

Simple Bolt

Build GoDoc Go Report Card License

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.
  • Requires Go 1.9 or later.
  • Note that HashMap is implemented only for API-compatibility with simpleredis, and does not have the same performance profile as the HashMap implementation in simpleredis, simplemaria (MariaDB/MySQL) or simplehstore (PostgreSQL w/ HSTORE).

Example usage

package main

import (
    "log"

    "github.com/xyproto/simplebolt"
)

func main() {
    // New bolt database struct
    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.Last(); 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)
    }
}

Contributors

  • Luis Villegas, for the linked list functionality.

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 = 5.1
)

Variables

View Source
var (
	// ErrBucketNotFound may be returned if a no Bolt bucket was found
	ErrBucketNotFound = errors.New("Bucket not found")

	// ErrKeyNotFound will be returned if the key was not found in a HashMap or KeyValue struct
	ErrKeyNotFound = errors.New("Key not found")

	// ErrDoesNotExist will be returned if an element was not found. Used in List, Set, HashMap and KeyValue.
	ErrDoesNotExist = errors.New("Does not exist")

	// ErrExistsInSet is only returned if an element is added to a Set, but it already exists
	ErrExistsInSet = errors.New("Element already exists in set")

	// ErrInvalidID is only returned if adding an element to a HashMap that contains a colon (:)
	ErrInvalidID = errors.New("Element ID can not contain \":\"")
)

Functions

This section is empty.

Types

type BoltCreator

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

BoltCreator is used for implementing pinterface.ICreator. It contains a database and provides functions for creating data structures within that database.

func NewCreator

func NewCreator(db *Database) *BoltCreator

NewCreator can create a new BoltCreator struct

func (*BoltCreator) NewHashMap

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

NewHashMap can create a new HashMap with the given ID. The HashMap elements have a name and then a key+value. For example a username for the name, then "password" as the key and a password hash as the value.

func (*BoltCreator) NewKeyValue

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

NewKeyValue can create a new KeyValue with the given ID. The KeyValue elements have a key with a corresponding value.

func (*BoltCreator) NewList

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

NewList can create a new List with the given ID

func (*BoltCreator) NewSet

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

NewSet can create a new Set with the given ID

type Database

type Database bbolt.DB

Database represents Bolt database

func New

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

New creates a new Bolt database struct, using the given file or creating a new file, as needed

func (*Database) Close

func (db *Database) Close()

Close the database

func (*Database) Path

func (db *Database) Path() string

Path returns the full path to the database file

func (*Database) Ping

func (db *Database) Ping() error

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

type HashMap

type HashMap boltBucket

HashMap is a Bolt bucket, with methods for acting like a hash map (with an ID and then key=>value)

func NewHashMap

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

NewHashMap loads or creates a new HashMap struct, with the given ID

func (*HashMap) All

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

All returns all ID's, for all hash elements

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear will remove all elements from this hash map

func (*HashMap) Del

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

Del will remove an element (for instance a user)

func (*HashMap) DelKey

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

DelKey will 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) (bool, error)

Exists will check if a given elementid exists as a hash map at all

func (*HashMap) Get

func (h *HashMap) Get(elementid, key string) (string, 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) (bool, error)

Has will check if a given elementid + key is in the hash map

func (*HashMap) Keys

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

Keys returns all names of all keys of a given owner.

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) Set

func (h *HashMap) Set(elementid, key, value string) 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

KeyValue is a Bolt bucket, with methods for acting like a key=>value store

func NewKeyValue

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

NewKeyValue loads or creates a new KeyValue struct, with the given ID

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Clear will remove all elements from this key/value

func (*KeyValue) Del

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

Del will remove a key

func (*KeyValue) Get

func (kv *KeyValue) Get(key string) (string, 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) (string, error)

Inc will 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 List

type List boltBucket

List is a Bolt bucket, with methods for acting like a list

func NewList

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

NewList loads or creates a new List struct, with the given ID

func (*List) Add

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

Add an element to the list

func (*List) All

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

All returns all elements in the list

func (*List) Clear

func (l *List) Clear() error

Clear will remove all elements from this list

func (*List) Last

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

Last will return the last element of a list

func (*List) LastN

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

LastN will return the last N elements of a list

func (*List) Remove

func (l *List) Remove() error

Remove this list

type Set

type Set boltBucket

Set is a Bolt bucket, with methods for acting like a set, only allowing unique keys

func NewSet

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

NewSet loads or creates a new Set struct, with the given ID

func (*Set) Add

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

Add an element to the set

func (*Set) All

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

All returns all elements in the set

func (*Set) Clear

func (s *Set) Clear() error

Clear will remove all elements from this set

func (*Set) Del

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

Del will remove an element from the set

func (*Set) Has

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

Has will check if a given value is in the set

func (*Set) Remove

func (s *Set) Remove() error

Remove this set

type StoredData

type StoredData interface {
	// Value returns the current value of the
	// element at which the item refers to.
	Value() []byte

	// Update resets the value of the element at
	// which the item refers to with newData.
	//
	// Returns "Empty data" error if newData is nil.
	//
	// It may also return an error in case of bbolt Update or protocol buffer
	// serialization/deserialization fail. In both cases, the data isn't updated.
	Update(newData []byte) error

	// Remove deletes from Bolt the element at which the item data refers to.
	//
	// It may return an error in case of bbolt Update or protocol buffer
	// serialization/deserialization fail. In both cases, the data isn't removed.
	Remove() error
}

StoredData is the set of methods that provides access to the element's underlying data in every data structure.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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