rumble

package module
v0.0.0-...-2c92bad Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2017 License: MIT Imports: 5 Imported by: 0

README

rumble GoDoc Build Status Coverage Status

RumbleDB is an abstraction for boltdb that aims to provide a clean API similiar to that of gopkg.in/mgo.v2 without hiding boltdb away from you completely and without locking you in to a specefic encoding.

Why?

The mgo API is awesome, and so is boltdb.

Install

RumbleDB will follow the gopkg.in repository scheme. Rumble is currently unstable.

go get -u gopkg.in/kylewolfe/rumble.v0

RumbleDB Out of the Box

var db *rumble.DB
var err error
   
if db, err = rumble.New("test.db"); err != nil {
	panic(err)
}
       
// structs
bucket := db.Bucket("foo")
for i := 0; i < 3; i++ {
	foo := &struct {
		Id    bson.ObjectId `rumble:"key"`
		Fizz  string
		Count int
	}{
		Fizz:  "buzz",
		Count: i,
	}
	if err = bucket.Put(foo); err != nil {
		panic(err)
	}
	fmt.Printf("newly created id: %s\n", foo.Id.Hex()) // ids generated on the fly like mgo
}

// iteration
i := bucket.NewIter()
foo := &struct {
	Id    bson.ObjectId `rumble:"key"`
	Fizz  string
	Count int
}{}
for i.Next(foo) {
	fmt.Printf("created: %s\n", foo.Id.Time())
}

// maps
bucket = db.Bucket("bar")
m := bson.M{"foo": "bar"}
if err = bucket.Put(m); err != nil {
	panic(err)
}
fmt.Println(m)

// newly created id: 56c4ffb89e56a73ced4227d6
// newly created id: 56c4ffb89e56a73ced4227d7
// newly created id: 56c4ffb89e56a73ced4227d8
// created: 2016-02-17 18:18:16 -0500 EST
// created: 2016-02-17 18:18:16 -0500 EST
// created: 2016-02-17 18:18:16 -0500 EST
// map[_key:[86 196 255 185 158 86 167 60 237 66 39 217] foo:bar]

Bring Your Own Encoding

RumbleDB provides encoding functionality from bson out of the box, but you can use whatever you'd like.

db, _ := rumble.New("my.db")
db.Marshal = json.Marshal
db.Unmarshal = json.Unmarshal

You can also use your own ID format

var i uint32 = 0
db.NewId = func() []byte {
	return []byte(i := atomic.AddUint32(&rowCounter, 1))
}

Documentation

Overview

Package rumble is a simple key / document store wrapper for boltdb with an API comparable to mgo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

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

Bucket represents a BoltDB bucket. Methods run from Bucket are thread safe as they wrap thread safe operations exposed by BoltDB.

func (*Bucket) Count

func (b *Bucket) Count() int

Count returns the number of entries in the Bucket

func (*Bucket) Delete

func (b *Bucket) Delete(k []byte) (err error)

Delete is a wrarpper for BoltDB's Delete method.

func (*Bucket) Get

func (b *Bucket) Get(k []byte, v interface{}) (err error)

Get is a wrapper for BoltDb's Get method.

func (*Bucket) Iterate

func (b *Bucket) Iterate(predicate func(k, v []byte) bool) *Iterator

Iterate returns a new Iterator with the given predicate. If a predicate is given, it will be called against every key:value pair and must return true for the record to be returned by the iterator. A nil predicate may be given to return all values from the bucket. This is useful when a quick check can be done against the data before it is passed to the DB.UnmarshalerFunc

func (*Bucket) Put

func (b *Bucket) Put(v ...interface{}) (err error)

Put is a wrapper for BoltDB's Update method, equivalant to an upsert.

type DB

type DB struct {
	Bolt          *bolt.DB
	MarshalFunc   func(interface{}) ([]byte, error)
	UnmarshalFunc func([]byte, interface{}) error
	NewKeyFunc    func() []byte
}

DB is an abstraction for BoltDB that provides an API compaprable to mgo.

func New

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

New returns a new rumble.DB at the given path with permissions of 0600 and a timeout of 1 second

func (*DB) Bucket

func (db *DB) Bucket(name string) *Bucket

Bucket returns a new Bucket after executing bolt's CreateBucketIfNotExists. Bucket will panic on any error from bolt.

func (*DB) Buckets

func (db *DB) Buckets() []*Bucket

Buckets returns a slice of *Bucket that are present in the current DB

func (*DB) DeleteBucket

func (db *DB) DeleteBucket(name string) error

DeleteBucket returns the result from bolt's DeleteBucket operation

type Iterator

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

Iterator iterates over a channel of KV and Unmarshals the value

func (*Iterator) Next

func (i *Iterator) Next(v interface{}) bool

Next Unmarshals the current document into the given interface. Next will return false when the Pipeline channel has been closed and the last record read.

type KV

type KV struct {
	Key, Value []byte
}

KV represents a key / value pair within BoltDB

Jump to

Keyboard shortcuts

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