nutz

package module
v0.0.0-...-0bf9bae Latest Latest
Warning

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

Go to latest
Published: May 19, 2016 License: MIT Imports: 3 Imported by: 0

README

nutz Build Status GoDoc Coverage Status

nutz is a library for interacting with bolt databases. It removes the boilerplates and stay faithful with the bolt database view of "simple API".

Where is nutz useful

  • You want to maintain more than one database
  • Your project uses nested buckets extensively.

Installation

go get github.com/gernest/nutz

How to use

package main

import (
	"fmt"

	"github.com/gernest/nutz"
)

func main() {
	databaseName := "my-databse.db"

	// ntz.NewStorage takes the same arguments as *bolt.Open. The differnce is with
	// nutz nothing is opened.
	db := nutz.NewStorage(databaseName, 0600, nil)

	// Creates a new record in the bucket library with key  "lady morgana" and
	// value []byte("A mist of avalon")
	db.Create("library", "lady morgana", []byte("A mist of avalon"))

	// If you want to create a record which will be deep inside buckets. lets say
	// you want to store a record for a teacher. The buckets will be like
	// city>school>class>teacher.
	db.Create("city", "john Doe", []byte("scientist"), "school", "class", "teacher")

	// Retrieving records from a bolt database.
	d := db.Get("library", "lady morgana")

	fmt.Println(string(d.Data) == "A mist of avalon") //=> true

	// lets check if there was any error
	fmt.Println(d.Error) //=> nil

	// Retriving nested buckets .
	n:=db.Get("city","john Doe","school","class","teacher")

	fmt.Println(string(n.Data)=="scientist") //=> true

	// Lets delete the database
	db.DeleteDatabase()

}

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork it and submit a pull request for whatever change you want to be added to this project.

Or, open an issue for any questions.

Author

Geofrey Ernest geofreyernest@live.com

Twitter : @gernesti

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Overview

Package nutz is a helper for interactions with bolt databases.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

type Storage struct {
	// Data holds the value returned from the database.
	Data []byte

	// DataList holds key value pairs, retrievend by the GetAll method. These are the
	// values found in the bucket.
	DataList map[string][]byte

	// Error stores the last encountered error.
	Error error

	// DBName filename which will be used as the database.
	DBName string
	// contains filtered or unexported fields
}

Storage contains metadata, which enable accessing the underlying bolt databases.

func NewStorage

func NewStorage(dbname string, mode os.FileMode, opts *bolt.Options) Storage

NewStorage initializes a new storage object

func (Storage) Create

func (s Storage) Create(bucket, key string, value []byte, nested ...string) Storage

Create stores a given key value pairs . It takes an optional coma separated strings to act as nested buckets.

Example
c := NewStorage("mydatabase.bdb", 0600, nil)

// Without nesting
c.Create("my-bucket", "my-key", []byte("my-value"))

// With nested buckets
c.Create("my-bucket", "my-key", []byte("my-value"), "bucket-one", "bucket-two")

// When you nest buckets, the order of the optional coma separated strings matter.
// That is, on the above code snippet.
//  * A bucket named "my-bucket" will be created"
//  * Inside "my-bucket" a bucket "bucket-one" will be created.
//  * Inside "bucket-one" a bucket "bucket-two" will be created.
//  * Inside "bucket-two" a new record will be stored with key "my-key" and value "my-value"

func (Storage) Delete

func (s Storage) Delete(bucket, key string, nested ...string) Storage

Delete removes a record from the database

func (Storage) DeleteBucket

func (s Storage) DeleteBucket(bucket string) Storage

DeleteBucket removes a bucket and all child buckets from the database

func (Storage) DeleteDatabase

func (s Storage) DeleteDatabase() error

DeleteDatabase removes the given database file as specified in the NewStorage function from the filesystem

func (*Storage) Execute

func (s *Storage) Execute(bucket, key string, value []byte, nested []string, fn StorageFunc) Storage

Execute opens a database, and pass all arguments together with a Storage object that has an open bolt database to a given StorageFunc. This is an ideal way to tap into the *bolt.DB object and do the whole stuffs you would like do do, without even bothering if the database exist of if the connection was closed.

It makes sure the database is closed after the function exits. Note that, the nested buckets should be provided as a slice of strings.

func (Storage) Get

func (s Storage) Get(bucket, key string, nested ...string) Storage

Get retrives a record from the database. The order of the optional nested buckets matter. if a key does not exist it returns an error

func (Storage) GetAll

func (s Storage) GetAll(bucket string, nested ...string) Storage

GetAll retrieves all key value pairs in a bucket, it stores the map of key value pairs inside the DataList attribute.

func (Storage) Update

func (s Storage) Update(bucket, key string, value []byte, nested ...string) Storage

Update replaces the old data stored in a given key with a new one. If the Key is not found it returns an error. Update does not attempt to create missing buckets, instead it returns an error

This shares the same api as the Create method, except it returns an error if the record does not exist, and in case of buckets it returns a error if they dont exist.

For a nested bucket list, and missing bucket in the list, or missarrangement result in an error.

type StorageFunc

type StorageFunc func(s Storage, bucket, key string, value []byte, nested ...string) Storage

StorageFunc is the interface for a function which is used by Execute method

Jump to

Keyboard shortcuts

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