zbolt

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 8 Imported by: 1

README

zbolt

boltdb wrapper

install

go get -u dukangxu.com/git/dukangxu/zbolt

example

package main

import (
	"dukangxu.com/git/dukangxu/zbolt"
	"fmt"
	"log"
)

func main() {
	db, err := zbolt.Open("z.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	tx := db.NewTx(true)
	defer tx.Rollback()
	bucket := []byte("test")
	//put
	var bs [][]byte
	bs = append(bs, []byte("key1"), []byte("value1"))
	bs = append(bs, []byte("key2"), []byte("value2"))
	tx.Put(bucket, bs...)

	//get
	var keys [][]byte
	keys = append(keys, []byte("key1"))
	keys = append(keys, []byte("key2"))
	gets := tx.Get(bucket, keys...)
	if tx.Error() == nil {
		for i := 0; i < len(gets); i += 2 {
			fmt.Println("key", string(gets[i]), "value", string(gets[i+1]))
		}
	}
	//delete
	keys = [][]byte{[]byte("key1")}
	if err := tx.Delete(bucket, keys...); err != nil {
		log.Fatal(err)
	}

	//get
	keys = append(keys, []byte("key1"))
	keys = append(keys, []byte("key2"))
	gets = tx.Get(bucket, keys...)
	if tx.Error() == nil {
		for i := 0; i < len(gets); i += 2 {
			fmt.Println("key", string(gets[i]), "value", string(gets[i+1]))
		}
	}
	//commit
	if err := tx.Commit(); err != nil {
		log.Fatal(err)
	}
}

output:
key key1 value value1
key key2 value value2
key key2 value value2

acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func BucketNameConcat

func BucketNameConcat(slices ...[]byte) []byte

BucketNameConcat concat bucket name

func BytesConcat

func BytesConcat(slices ...[]byte) []byte

BytesConcat concat bytes

func BytesToString

func BytesToString(b []byte) string

BytesToString parse bytes to string

func BytesToUint64

func BytesToUint64(v []byte) uint64

BytesToUint64 parse bytes to unit64

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes parse string to bytes

func Uint64ToBytes

func Uint64ToBytes(v uint64) []byte

Uint64ToBytes parse uint64 to bytes

Types

type DB

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

DB database struct, contain boltdb DB struct

func NewDB added in v0.0.3

func NewDB(db *bolt.DB) *DB

NewDB assemble DB struct, input boltdb DB struct

func Open

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

Open create DB struct, open file to save db

func (*DB) Close

func (db *DB) Close() error

Close close DB

func (*DB) NewTx

func (db *DB) NewTx(writable bool) *Tx

NewTx create transaction struct

type Tx

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

Tx transaction struct, contain boltdb Tx and error

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commit data at the end

func (*Tx) Delete

func (tx *Tx) Delete(name []byte, keys ...[]byte) error

Delete delete value in bucket by keys, input multiple key, like [key1, key2, ...]

func (*Tx) DeleteBucket

func (tx *Tx) DeleteBucket(name []byte) error

DeleteBucket delete bucket

func (*Tx) Error

func (tx *Tx) Error(errs ...error) error

Error set Tx error or return Tx error

func (*Tx) ForEach

func (tx *Tx) ForEach(name []byte, fn func(k, v []byte) error) error

ForEach traveral all key value in bucket

func (*Tx) Get

func (tx *Tx) Get(name []byte, keys ...[]byte) [][]byte

Get get values from bucket by keys, input multiple and return multiple, like [key1, kye2, ...]

func (*Tx) Next

func (tx *Tx) Next(name []byte, key []byte, limit int) [][]byte

Next get limit count value after key in bucket

func (*Tx) NextSequence

func (tx *Tx) NextSequence(name []byte) (uint64, error)

NextSequence get next sequence in bucket, if bucket not exist, create it, begin with 0, next 1

func (*Tx) Prev

func (tx *Tx) Prev(name []byte, key []byte, limit int) [][]byte

Prev get limit count value front key in bucket

func (*Tx) Put

func (tx *Tx) Put(name []byte, kvs ...[]byte) error

Put put keys values to bucket, input multiple key value, like [key1,value1,key2,value2, ...]

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback rollback data when some error happened

func (*Tx) Sequence

func (tx *Tx) Sequence(name []byte) uint64

Sequence get current sequence in bucket, if bucket not exist, create it, begin with 0

func (*Tx) SortDelete

func (tx *Tx) SortDelete(name []byte, keys ...[]byte) error

SortDelete delete key value in bucket with sort

func (*Tx) SortDeleteBucket

func (tx *Tx) SortDeleteBucket(name []byte) error

SortDeleteBucket sort delete bucket

func (*Tx) SortNext

func (tx *Tx) SortNext(name []byte, key []byte, limit int) [][]byte

SortNext get limit count key value after key in bucket with sort

func (*Tx) SortPrev

func (tx *Tx) SortPrev(name []byte, key []byte, limit int) [][]byte

SortPrev get limit count key value front key in bucket with sort

func (*Tx) SortPut

func (tx *Tx) SortPut(name []byte, sortKey []byte, kvs ...[]byte) error

SortPut sort put key value to bucket, like timeline as sortKey

Jump to

Keyboard shortcuts

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