bucketstore

package module
v0.0.0-...-7a0528a Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2016 License: MIT Imports: 9 Imported by: 0

README

Bucketstore

Build Status

Bucketstore is a pure Go embedded database engine to store JSON structure data. It is based on Bolt.

This software is in the development stage. The code and api may be changed drastically.

Table of contents

Installation

Run go get.

$ go get github.com/kohkimakimoto/bucketstore

Usage

The following code is an example to operate Bucketstore as a simple key/value store.

package main

import (
	"github.com/kohkimakimoto/bucketstore"
	"fmt"
)

func main() {
	db, err := bucketstore.Open("my.db", 0600, nil)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	bucket := db.Bucket("MyBucket")

	// put key/value item
	err = bucket.PutRaw([]byte("user001"), []byte(`{"name": "kohkimakimoto", "age": 36}`))
	if err != nil {
		panic(err)
	}

	// get value
	v, err := bucket.GetRaw([]byte("user001"))
	if err != nil {
		panic(err)
	}

	fmt.Println(string(v))
	// {"age":36,"name":"kohkimakimoto"}
}
Data Model

Bucketstore has 3 data models and structures flexible datastore.

  • Database: Database is the top-level object stores all buckets.
  • Bucket: Bucket is a collection of items.
  • Item: Items is a key/value pair. The value is a JSON structure data.
Select items by using a query
func main() {
	// open database
	db, err := bucketstore.Open("my.db", 0600, nil)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	bucket := db.Bucket("MyBucket")

	// put data (ignore errors)
	bucket.PutRaw([]byte("user001"), []byte(`{"name": "hoge", "age": 20}`))
	bucket.PutRaw([]byte("user002"), []byte(`{"name": "foo", "age": 31}`))
	bucket.PutRaw([]byte("user003"), []byte(`{"name": "bar", "age": 18}`))
	bucket.PutRaw([]byte("user004"), []byte(`{"name": "aaa", "age": 40}`))
	bucket.PutRaw([]byte("user005"), []byte(`{"name": "xxx", "age": 41}`))
	bucket.PutRaw([]byte("user006"), []byte(`{"name": "ccc", "age": 50}`))

    // query
	q := bucket.Query()
	q.Filter = &bucketstore.PropValueRangeFilter{
		Property: "age",
		Min: 20,
		Max: 40,
	}
	items, err := q.AsList()
	if err != nil {
		panic(err)
	}

	for _, item := range items {
		fmt.Println(string(item.Key), string(item.Value))
	}
	// user001 {"age":20,"name":"hoge"}
	// user002 {"age":31,"name":"foo"}
	// user004 {"age":40,"name":"aaa"}
}

Author

Kohki Makimoto kohki.makimoto@gmail.com

License

The MIT License (MIT)

Todo

  • documentation
  • tests

Documentation

Index

Constants

View Source
const (
	ValueTypeBool    = 0x01
	ValueTypeString  = 0x02
	ValueTypeFloat64 = 0x03
	ValueTypeNil     = 0x04
)

Variables

This section is empty.

Functions

func BytesToFloat64

func BytesToFloat64(b []byte) float64

func BytesToUint64

func BytesToUint64(b []byte) uint64

func StringToBytes

func StringToBytes(v string) []byte

func Uint64ToBytes

func Uint64ToBytes(v uint64) []byte

Types

type BaseBucket

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

BaseBucket is a low level bucket that is used inside of bucketstore.

func (*BaseBucket) Cursor

func (b *BaseBucket) Cursor() *bolt.Cursor

func (*BaseBucket) Data

func (b *BaseBucket) Data() *bolt.Bucket

func (*BaseBucket) Delete

func (b *BaseBucket) Delete(key []byte) error

func (*BaseBucket) ForEach

func (b *BaseBucket) ForEach(fn func(k, v []byte) error) error

func (*BaseBucket) Get

func (b *BaseBucket) Get(key []byte) []byte

func (*BaseBucket) IndexCursor

func (b *BaseBucket) IndexCursor(propName string) *IndexCursor

func (*BaseBucket) IndexProperties

func (b *BaseBucket) IndexProperties() (props []string, err error)

func (*BaseBucket) NextSequence

func (b *BaseBucket) NextSequence() (uint64, error)

func (*BaseBucket) Put

func (b *BaseBucket) Put(key []byte, value []byte) error

func (*BaseBucket) Stats

func (b *BaseBucket) Stats() bolt.BucketStats

type Bucket

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

func (*Bucket) BaseBucket

func (bucket *Bucket) BaseBucket() *BaseBucket

func (*Bucket) Delete

func (bucket *Bucket) Delete(key []byte) error

func (*Bucket) Exists

func (bucket *Bucket) Exists() (bool, error)

func (*Bucket) Get

func (bucket *Bucket) Get(key []byte) (*Item, error)

func (*Bucket) GetRaw

func (bucket *Bucket) GetRaw(key []byte) (value []byte, err error)

func (*Bucket) NextSequence

func (bucket *Bucket) NextSequence() (uint64, error)

func (*Bucket) NextSequenceBytes

func (bucket *Bucket) NextSequenceBytes() ([]byte, error)

func (*Bucket) Put

func (bucket *Bucket) Put(item *Item) (err error)

func (*Bucket) PutRaw

func (bucket *Bucket) PutRaw(key []byte, value []byte) (err error)

func (*Bucket) Query

func (bucket *Bucket) Query() *Query

type DB

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

func Open

func Open(path string, mode os.FileMode, options *Options) (*DB, error)

func (*DB) Batch

func (db *DB) Batch(fn func(*Tx) error) error

func (*DB) Begin

func (db *DB) Begin(writable bool) (*Tx, error)

func (*DB) Bucket

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

func (*DB) Close

func (db *DB) Close() error

func (*DB) Conn

func (db *DB) Conn() *bolt.DB

func (*DB) DeleteBucket

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

func (*DB) GoString

func (db *DB) GoString() string

GoString returns the Go string representation of the database.

func (*DB) Path

func (db *DB) Path() string

func (*DB) String

func (db *DB) String() string

String returns the string representation of the database.

func (*DB) Update

func (db *DB) Update(fn func(*Tx) error) error

func (*DB) View

func (db *DB) View(fn func(*Tx) error) error

type Filter

type Filter interface {
	// contains filtered or unexported methods
}

type Index

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

func (*Index) Data

func (idx *Index) Data() (key []byte, value []byte)

func (*Index) MustValueBytes

func (idx *Index) MustValueBytes() []byte

func (*Index) ValueBytes

func (idx *Index) ValueBytes() ([]byte, error)

func (*Index) ValueType

func (idx *Index) ValueType() byte

type IndexCursor

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

func (*IndexCursor) Bucket

func (ic *IndexCursor) Bucket() *BaseBucket

func (*IndexCursor) First

func (ic *IndexCursor) First() *Index

func (*IndexCursor) Get

func (ic *IndexCursor) Get(value interface{}) *Index

func (*IndexCursor) Last

func (ic *IndexCursor) Last() *Index

func (*IndexCursor) Next

func (ic *IndexCursor) Next() *Index

func (*IndexCursor) Prev

func (ic *IndexCursor) Prev() *Index

func (*IndexCursor) SeekFirst

func (ic *IndexCursor) SeekFirst(valueType byte, seek []byte) *Index

SeekFirst moves cursor to fist key matching `seek` prefix

func (*IndexCursor) SeekLast

func (ic *IndexCursor) SeekLast(valueType byte, seek []byte) *Index

SeekFirst moves cursor to last key matching `seek` prefix

type Item

type Item struct {
	Key   []byte
	Value []byte
}

type KeyPrefixFilter

type KeyPrefixFilter struct {
	Prefix  []byte
	OrderBy OrderBy
}

type KeyRangeFilter

type KeyRangeFilter struct {
	Min     []byte
	Max     []byte
	OrderBy OrderBy
}

type Options

type Options struct {
	*bolt.Options
}

func NewOptions

func NewOptions() *Options

type OrderBy

type OrderBy int
const (
	OrderByAsc  OrderBy = 0
	OrderByDesc OrderBy = 1
)

type OrderByFilter

type OrderByFilter struct {
	OrderBy OrderBy
}

type PropValueMatchFilter

type PropValueMatchFilter struct {
	Property string
	Match    interface{}
	OrderBy  OrderBy
}

type PropValuePrefixFilter

type PropValuePrefixFilter struct {
	Property string
	Prefix   interface{}
	OrderBy  OrderBy
}

type PropValueRangeFilter

type PropValueRangeFilter struct {
	Property string
	Min      interface{}
	Max      interface{}
	OrderBy  OrderBy
}

type Query

type Query struct {
	Offset uint64
	Limit  uint64
	Filter Filter
	// contains filtered or unexported fields
}

func (*Query) AsList

func (q *Query) AsList() (items []*Item, err error)

func (*Query) AsSingle

func (q *Query) AsSingle() (item *Item, err error)

type Tx

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

func (*Tx) Bucket

func (tx *Tx) Bucket(name string) (*Bucket, error)

func (*Tx) BucketNames

func (tx *Tx) BucketNames(fn func(name string) error) error

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) CopyFile

func (tx *Tx) CopyFile(path string, mode os.FileMode) error

func (*Tx) CreateBucket

func (tx *Tx) CreateBucket(name string) (*Bucket, error)

func (*Tx) CreateBucketIfNotExists

func (tx *Tx) CreateBucketIfNotExists(name string) (*Bucket, error)

func (*Tx) DeleteBucket

func (tx *Tx) DeleteBucket(name string) error

func (*Tx) InternalTx

func (tx *Tx) InternalTx() *bolt.Tx

func (*Tx) Rollback

func (tx *Tx) Rollback() error

func (*Tx) WriteTo

func (tx *Tx) WriteTo(w io.Writer) (n int64, err error)

Directories

Path Synopsis
demo
v
bolt
Package bolt implements a low-level key/value store in pure Go.
Package bolt implements a low-level key/value store in pure Go.

Jump to

Keyboard shortcuts

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