README

Stow

GoDoc Software License Build Status Coverage Status

Usage

This package provides a persistence manager for objects backed by boltdb.

package stow

import (
  "os"
  "testing"

  "github.com/boltdb/bolt"
)

func TestJson(t *testing.T) {

  // Create a boltdb database
  db, err := bolt.Open("my.db", 0600, nil)
  if err != nil {
    t.Error(err.Error())
  }
  defer os.Remove("my.db")
  defer db.Close()


  // Defined our test Type
  type MyType struct {
    FirstName string `json:"first"`
    LastName  string `json:"last"`
  }

  // Create a Json-encoded Store, Xml and Gob are also built-in
  // We'll we store the name in a boltdb bucket named "names"
  store := NewJSONStore(db, []byte("names"))

  // Store the object
  store.Put([]byte("hello"), &MyType{"Derek", "Kered"})

  // For each element in the store
  store.ForEach(func(name Name){
    fmt.Println(name)
  })

  // Get the object
  var name MyType
  store.Pull([]byte("hello"), &name)

  // Verify
  if name.FirstName != "Derek" || name.LastName != "Kered" {
    t.Errorf("Unexpected name: %v", name)
  }
}

Installation

go get github.com/djherbis/stow
Expand ▾ Collapse ▴

Documentation

Overview

    Package stow is used to persist objects to a bolt.DB database.

    Index

    Constants

    This section is empty.

    Variables

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

      ErrNotFound indicates object is not in database.

      Functions

      func Register

      func Register(value interface{})

        Register registers the type using gob.Register for use with NewStore() and the GobCodec.

        func RegisterName

        func RegisterName(name string, value interface{})

          RegisterName registers the type using gob.RegisterName for use with NewStore() and the GobCodec.

          Types

          type Codec

          type Codec interface {
          	NewEncoder(io.Writer) Encoder
          	NewDecoder(io.Reader) Decoder
          }

            Codec provides a mechanism for storing/retriving objects as streams of data.

            type Decoder

            type Decoder interface {
            	Decode(interface{}) error
            }

              Decoder is used to decode objects

              type Encoder

              type Encoder interface {
              	Encode(interface{}) error
              }

                Encoder is used to encode objects

                type GobCodec

                type GobCodec struct{}

                  GobCodec is used to encode/decode using the Gob format.

                  func (GobCodec) NewDecoder

                  func (c GobCodec) NewDecoder(r io.Reader) Decoder

                    NewDecoder returns a new gob decoder which reads from r

                    func (GobCodec) NewEncoder

                    func (c GobCodec) NewEncoder(w io.Writer) Encoder

                      NewEncoder returns a new gob encoder which writes to w

                      type JSONCodec

                      type JSONCodec struct{}

                        JSONCodec is used to encode/decode JSON

                        func (JSONCodec) NewDecoder

                        func (c JSONCodec) NewDecoder(r io.Reader) Decoder

                          NewDecoder returns a new json decoder which reads from r

                          func (JSONCodec) NewEncoder

                          func (c JSONCodec) NewEncoder(w io.Writer) Encoder

                            NewEncoder retuns a new json encoder which writes to w

                            type Store

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

                              Store manages objects persistance.

                              func NewCustomStore

                              func NewCustomStore(db *bolt.DB, bucket []byte, codec Codec) *Store

                                NewCustomStore allows you to create a store with a custom underlying Encoding

                                func NewJSONStore

                                func NewJSONStore(db *bolt.DB, bucket []byte) *Store

                                  NewJSONStore creates a new Store, using the underlying bolt.DB "bucket" to persist objects as json.

                                  func NewStore

                                  func NewStore(db *bolt.DB, bucket []byte) *Store

                                    NewStore creates a new Store, using the underlying bolt.DB "bucket" to persist objects. NewStore uses GobEncoding, your objects must be registered via gob.Register() for this encoding to work.

                                    func NewXMLStore

                                    func NewXMLStore(db *bolt.DB, bucket []byte) *Store

                                      NewXMLStore creates a new Store, using the underlying bolt.DB "bucket" to persist objects as xml.

                                      func (*Store) DeleteAll

                                      func (s *Store) DeleteAll() error

                                        DeleteAll empties the store

                                        func (*Store) ForEach

                                        func (s *Store) ForEach(do interface{}) error

                                          ForEach will run do on each object in the store. do can be a function which takes either: 1 param which will take on each "value" or 2 params where the first param is the "key" and the second is the "value".

                                          func (*Store) Get

                                          func (s *Store) Get(key []byte, b interface{}) error

                                            Get will retreive b with key "key"

                                            func (*Store) GetKey

                                            func (s *Store) GetKey(key interface{}, b interface{}) error

                                              GetKey will retreive b with key "key". If key is []byte or string it uses the key directly. Otherwise, it marshals the given type into bytes using the stores Encoder.

                                              func (*Store) Pull

                                              func (s *Store) Pull(key []byte, b interface{}) error

                                                Pull will retreive b with key "key", and removes it from the store.

                                                func (*Store) PullKey

                                                func (s *Store) PullKey(key interface{}, b interface{}) error

                                                  PullKey will retreive b with key "key", and removes it from the store.

                                                  func (*Store) Put

                                                  func (s *Store) Put(key []byte, b interface{}) (err error)

                                                    Put will store b with key "key". If key is []byte or string it uses the key directly. Otherwise, it marshals the given type into bytes using the stores Encoder.

                                                    func (*Store) PutKey

                                                    func (s *Store) PutKey(key interface{}, b interface{}) error

                                                      PutKey will store b with key "key". If key is []byte or string it uses the key directly. Otherwise, it marshals the given type into bytes using the stores Encoder.

                                                      type XMLCodec

                                                      type XMLCodec struct{}

                                                        XMLCodec is used to encode/decode XML

                                                        func (XMLCodec) NewDecoder

                                                        func (c XMLCodec) NewDecoder(r io.Reader) Decoder

                                                          NewDecoder returns a new xml decoder which reads from r

                                                          func (XMLCodec) NewEncoder

                                                          func (c XMLCodec) NewEncoder(w io.Writer) Encoder

                                                            NewEncoder returns a new xml encoder which writes to w