youdb

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 9 Imported by: 0

README

youdb

A Bolt wrapper that allows easy store hash, zset data.

Install

go get -u github.com/ego008/youdb

Example

package main

import (
	"fmt"
	"github.com/ego008/youdb"
)

func main() {
	db, err := youdb.Open("my.db")
	if err != nil {
		fmt.Println("open err", err)
		return
	}
	defer db.Close()

	// hash

	db.Hset("mytest", []byte("key1"), []byte("value1"))

	rs := db.Hget("mytest", []byte("key1"))
	if rs.State == "ok" {
		fmt.Println(rs.Data[0], rs.String())
	} else {
		fmt.Println("key not found")
	}

	data := [][]byte{}
	data = append(data, []byte("k1"), []byte("12987887762987"))
	data = append(data, []byte("k2"), []byte("abc"))
	data = append(data, []byte("k3"), []byte("qwasww"))
	data = append(data, []byte("k4"), []byte("444"))
	data = append(data, []byte("k5"), []byte("555"))
	data = append(data, []byte("k6"), []byte("aaaa556"))
	data = append(data, []byte("k7"), []byte("77777"))
	data = append(data, []byte("k8"), []byte("88888"))

	db.Hmset("myhmset", data...)

	rs = db.Hmget("myhmset", [][]byte{[]byte("k1"), []byte("k2"), []byte("k3"), []byte("k4")})
	if rs.State == "ok" {
		for _, v := range rs.List() {
			fmt.Println(v.Key.String(), v.Value.String())
		}
	}

	fmt.Println(db.Hincr("num", []byte("k1"), 2))

	k, _ := youdb.DS2b("19822112")
	v := uint64(121211121212233)
	db.Hset("mytestnum", k, youdb.I2b(v))
	r := db.Hget("mytestnum", k)
	if r.State == "ok" {
		fmt.Println(r.Int64(), r.Data[0].Int64(), string(r.Data[0]), youdb.B2i(r.Data[0]))
	} else {
		fmt.Println(r.State, r.Int64())
	}

	// zet

	db.Zset("mytest", []byte("key1"), 100)

	rs2 := db.Zget("mytest", []byte("key1"))
	if rs2.State == "ok" {
		fmt.Println(rs2.Int64())
	}

	fmt.Println(db.Zincr("num", []byte("k1"), 2))
}

Who's using youdb?

  • goyoubbs - A forum/discussion software written in Go.

Documentation

Overview

Package youdb is a Bolt wrapper that allows easy store hash, zset data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func B2ds

func B2ds(v []byte) string

B2ds return a Digit string of v v (8-byte big endian) -> uint64(123456) -> "123456".

func B2i

func B2i(v []byte) uint64

B2i return an int64 of v v (8-byte big endian) -> uint64(123456).

func B2s

func B2s(b []byte) string

B2s converts byte slice to a string without memory allocation. []byte("abc") -> "abc" s

func Bconcat

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

Bconcat concat a list of byte

func DS2b

func DS2b(v string) []byte

DS2b returns an 8-byte big endian representation of Digit string v ("123456") -> uint64(123456) -> 8-byte big endian.

func DS2i

func DS2i(v string) uint64

DS2i returns uint64 of Digit string v ("123456") -> uint64(123456).

func I2b

func I2b(v uint64) []byte

I2b returns an 8-byte big endian representation of v v uint64(123456) -> 8-byte big endian.

func S2b

func S2b(s string) []byte

S2b converts string to a byte slice without memory allocation. "abc" -> []byte("abc")

Types

type BS

type BS []byte

func (BS) Bytes

func (b BS) Bytes() []byte

func (BS) Int

func (b BS) Int() int

Int is a convenience wrapper over Get for int value of a hashmap.

func (BS) Int64

func (b BS) Int64() int64

Int64 is a convenience wrapper over Get for int64 value of a hashmap.

func (BS) JSON

func (b BS) JSON(v interface{}) error

JSON parses the JSON-encoded Reply Entry value and stores the result in the value pointed to by v.

func (BS) String

func (b BS) String() string

func (BS) Uint

func (b BS) Uint() uint

Uint is a convenience wrapper over Get for uint value of a hashmap.

func (BS) Uint64

func (b BS) Uint64() uint64

Uint64 is a convenience wrapper over Get for uint64 value of a hashmap.

type DB

type DB struct {
	*bolt.DB
}

DB embeds a bolt.DB.

func Open

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

Open creates/opens a bolt.DB at specified path, and returns a DB enclosing the same.

func (*DB) Close

func (db *DB) Close() error

Close closes the embedded bolt.DB.

func (*DB) Hdel

func (db *DB) Hdel(name string, key []byte) error

Hdel delete specified key of a hashmap.

func (*DB) HdelBucket

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

HdelBucket delete all keys in a hashmap.

func (*DB) Hget

func (db *DB) Hget(name string, key []byte) *Reply

Hget get the value related to the specified key of a hashmap.

func (*DB) HgetInt

func (db *DB) HgetInt(name string, key []byte) (val uint64)

HgetInt get the value related to the specified key of a hashmap.

func (*DB) Hincr

func (db *DB) Hincr(name string, key []byte, step int64) (uint64, error)

Hincr increment the number stored at key in a hashmap by step.

func (*DB) Hmdel

func (db *DB) Hmdel(name string, keys [][]byte) error

Hmdel delete specified multiple keys of a hashmap.

func (*DB) Hmget

func (db *DB) Hmget(name string, keys [][]byte) *Reply

Hmget get the values related to the specified multiple keys of a hashmap.

func (*DB) Hmset

func (db *DB) Hmset(name string, kvs ...[]byte) error

Hmset set multiple key-value pairs of a hashmap in one method call.

func (*DB) HnextSequence

func (db *DB) HnextSequence(name string) (uint64, error)

HnextSequence updates the sequence number for the bucket.

func (*DB) Hrscan

func (db *DB) Hrscan(name string, keyStart []byte, limit int) *Reply

Hrscan list key-value pairs of a hashmap with keys in range (key_start, key_end], in reverse order.

func (*DB) Hscan

func (db *DB) Hscan(name string, keyStart []byte, limit int) *Reply

Hscan list key-value pairs of a hashmap with keys in range (key_start, key_end].

func (*DB) Hsequence

func (db *DB) Hsequence(name string) uint64

Hsequence returns the current integer for the bucket without incrementing it.

func (*DB) Hset

func (db *DB) Hset(name string, key, val []byte) error

Hset set the byte value in argument as value of the key of a hashmap.

func (*DB) HsetSequence

func (db *DB) HsetSequence(name string, v uint64) error

HsetSequence updates the sequence number for the bucket.

func (*DB) Zdel

func (db *DB) Zdel(name string, key []byte) error

Zdel delete specified key of a zset.

func (*DB) ZdelBucket

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

ZdelBucket delete all keys in a zset.

func (*DB) Zget

func (db *DB) Zget(name string, key []byte) *Reply

Zget get the score related to the specified key of a zset.

func (*DB) Zincr

func (db *DB) Zincr(name string, key []byte, step int64) (uint64, error)

Zincr increment the number stored at key in a zset by step.

func (*DB) Zmdel

func (db *DB) Zmdel(name string, keys [][]byte) error

Zmdel delete specified multiple keys of a zset.

func (*DB) Zmget

func (db *DB) Zmget(name string, keys [][]byte) *Reply

Zmget get the values related to the specified multiple keys of a zset.

func (*DB) Zmset

func (db *DB) Zmset(name string, kvs ...[]byte) error

Zmset et multiple key-score pairs of a zset in one method call.

func (*DB) ZnextSequence

func (db *DB) ZnextSequence(name string) (uint64, error)

ZnextSequence updates the sequence number for the bucket.

func (*DB) Zrscan

func (db *DB) Zrscan(name string, keyStart, scoreStart []byte, limit int) *Reply

Zrscan list key-score pairs of a zset, in reverse order.

func (*DB) Zscan

func (db *DB) Zscan(name string, keyStart, scoreStart []byte, limit int) *Reply

Zscan list key-score pairs in a zset, where key-score in range (key_start+score_start, score_end].

func (*DB) Zsequence

func (db *DB) Zsequence(name string) uint64

Zsequence returns the current integer for the bucket without incrementing it.

func (*DB) Zset

func (db *DB) Zset(name string, key []byte, val uint64) error

Zset set the score of the key of a zset.

func (*DB) ZsetSequence

func (db *DB) ZsetSequence(name string, v uint64) error

ZsetSequence updates the sequence number for the bucket.

type Entry

type Entry struct {
	Key, Value BS
}

Entry a key-value pair.

type Reply

type Reply struct {
	State string
	Data  []BS
}

Reply a holder for a Entry list of a hashmap.

func (*Reply) Bytes

func (r *Reply) Bytes() []byte

func (*Reply) Dict

func (r *Reply) Dict() map[string][]byte

Dict retrieves the key/value pairs from reply of a hashmap.

func (*Reply) Int

func (r *Reply) Int() int

Int is a convenience wrapper over Get for int value of a hashmap.

func (*Reply) Int64

func (r *Reply) Int64() int64

Int64 is a convenience wrapper over Get for int64 value of a hashmap.

func (*Reply) JSON

func (r *Reply) JSON(v interface{}) error

JSON parses the JSON-encoded Reply Entry value and stores the result in the value pointed to by v.

func (*Reply) KvEach

func (r *Reply) KvEach(fn func(key, value BS)) int

func (*Reply) KvLen

func (r *Reply) KvLen() int

func (*Reply) List

func (r *Reply) List() []Entry

List retrieves the key/value pairs from reply of a hashmap.

func (*Reply) NotFound

func (r *Reply) NotFound() bool

func (*Reply) OK

func (r *Reply) OK() bool

func (*Reply) String

func (r *Reply) String() string

String is a convenience wrapper over Get for string value.

func (*Reply) Uint

func (r *Reply) Uint() uint

Uint is a convenience wrapper over Get for uint value of a hashmap.

func (*Reply) Uint64

func (r *Reply) Uint64() uint64

Uint64 is a convenience wrapper over Get for uint64 value of a hashmap.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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