kvstore

package module
v0.0.0-...-51d01b7 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 9 Imported by: 0

README

Installation

go get github.com/gansidui/kvstore

Usage


package main

import (
	"log"

	"github.com/gansidui/kvstore"
)

func main() {
	db := kvstore.NewBoltDBStore()
	// db := kvstore.NewLevelDBStore()

	// open
	err := db.Open("test.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// put
	db.Put([]byte("bucket"), []byte("key"), []byte("value"))

	// get
	if value, err := db.Get([]byte("bucket"), []byte("key")); err == nil {
		log.Println(string(value))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDBClosed       = errors.New("kvstore: db closed")
	ErrKeyNotFound    = errors.New("kvstore: key not found")
	ErrBucketNotFound = errors.New("kvstore: bucket not found")
)
View Source
var (
	// reserved key for count the number of keys in bucket.
	KeyForCount = []byte("__key_for_count__")

	// reserved key for sequence
	KeyForSequence = []byte("__key_for_sequence__")
)

Functions

This section is empty.

Types

type BoltDBStore

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

func (*BoltDBStore) AllKeys

func (this *BoltDBStore) AllKeys(bucket []byte) ([][]byte, error)

func (*BoltDBStore) Close

func (this *BoltDBStore) Close()

func (*BoltDBStore) Count

func (this *BoltDBStore) Count(bucket []byte) uint64

func (*BoltDBStore) Delete

func (this *BoltDBStore) Delete(bucket, key []byte) error

func (*BoltDBStore) Get

func (this *BoltDBStore) Get(bucket, key []byte) ([]byte, error)

func (*BoltDBStore) NextSequence

func (this *BoltDBStore) NextSequence(bucket []byte) (uint64, error)

func (*BoltDBStore) Open

func (this *BoltDBStore) Open(path string) error

func (*BoltDBStore) Put

func (this *BoltDBStore) Put(bucket, key, value []byte) error

func (*BoltDBStore) Sequence

func (this *BoltDBStore) Sequence(bucket []byte) uint64

func (*BoltDBStore) SetSequence

func (this *BoltDBStore) SetSequence(bucket []byte, sequence uint64) error

type KVStore

type KVStore interface {
	// Open database file.
	Open(path string) error

	// Close database file.
	Close()

	// Put a key-value pair into the bucket.
	// It is safe to modify the contents of the arguments after Put returns but not before.
	Put(bucket, key, value []byte) error

	// Get the value of this key from bucket.
	// The returned slice is its own copy, it is safe to modify the contents of the returned slice.
	Get(bucket, key []byte) ([]byte, error)

	// Delete the key in bucket.
	Delete(bucket, key []byte) error

	// Get all keys from bucket.
	// // The returned slice is its own copy, it is safe to modify the contents of the returned slice.
	AllKeys(bucket []byte) ([][]byte, error)

	// Sequence returns the current integer for the bucket without incrementing it.
	Sequence(bucket []byte) uint64

	// SetSequence updates the sequence number for the bucket.
	SetSequence(bucket []byte, sequence uint64) error

	// NextSequence returns an autoincrementing integer for the bucket.
	NextSequence(bucket []byte) (uint64, error)

	// Count return number of key-value pairs in bucket.
	Count(bucket []byte) uint64
}

func NewBoltDBStore

func NewBoltDBStore() KVStore

func NewLevelDBStore

func NewLevelDBStore() KVStore

type LevelDBStore

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

func (*LevelDBStore) AllKeys

func (this *LevelDBStore) AllKeys(bucket []byte) ([][]byte, error)

func (*LevelDBStore) Close

func (this *LevelDBStore) Close()

func (*LevelDBStore) Count

func (this *LevelDBStore) Count(bucket []byte) uint64

func (*LevelDBStore) Delete

func (this *LevelDBStore) Delete(bucket, key []byte) error

func (*LevelDBStore) Get

func (this *LevelDBStore) Get(bucket, key []byte) ([]byte, error)

func (*LevelDBStore) NextSequence

func (this *LevelDBStore) NextSequence(bucket []byte) (uint64, error)

func (*LevelDBStore) Open

func (this *LevelDBStore) Open(path string) error

func (*LevelDBStore) Put

func (this *LevelDBStore) Put(bucket, key, value []byte) error

func (*LevelDBStore) Sequence

func (this *LevelDBStore) Sequence(bucket []byte) uint64

func (*LevelDBStore) SetSequence

func (this *LevelDBStore) SetSequence(bucket []byte, sequence uint64) error

Jump to

Keyboard shortcuts

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