store

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotExist is returned when the supplied key to Get() or Delete()
	// does not exists in the database.
	ErrKeyNotExist = errors.New("store: key does not exist")

	// ErrBadValue is returned when the value supplied to Put() in the database
	// could not be encoded or is nil.
	ErrBadValue = errors.New("store: bad value")
)

Functions

func Byte

func Byte(b string) []byte

Byte is converting a string to []byte stream.

Types

type Store

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

Store represents the key/value store. Use the Open() method to open one, and Close() to close one.

func Open

func Open(path string, o *opt.Options) (*Store, error)

Open a key/value store. "path" is the full path to LevelDB database file. The DB will be created if not exist, unless ErrorIfMissing is true. You can pass in options to LevelDB via "o".

func (*Store) Close

func (s *Store) Close() error

Close closes the key/value database file.

func (*Store) Delete

func (s *Store) Delete(key []byte) error

Delete an entry of a given key from the store. If no such key is present in the store it will not return an error.

s.Delete(store.Byte("key42"))

func (*Store) Get

func (s *Store) Get(key []byte, value interface{}) error

Get a value from the store. "value" must be a pointer to a type, because of underlying gob. If the key is not present in the store, an ErrKeyNotExist is returned.

	type MyStruct struct {
    	Numbers []int
	}
	var val MyStruct
	if err := s.Get(store.Byte("key42"), &val); err == store.ErrKeyNotExist {
    	// "key42" not found
	}
	if err != nil {
		// some other error occurred
	}

The value passed to Get() can be nil. Which gives you the same result as Exist().

	if err := s.Get(store.Byte("key42"), nil); err == nil {
    	fmt.Println("entry is present")
	}

func (*Store) Put

func (s *Store) Put(key []byte, value interface{}) error

Put is creating an entry in the store. It overwrites any previous existing value for the given key. The passed value is gob-encoded and stored. The value cannot be nil and Put() returns an ErrBadValue.

err := s.Put(store.Byte("key42"), 42)
err := s.Put(store.Byte("key42"), "the question to live and the universe.")
m := map[string]int{
	"foo": 0,
	"bar": 1
}
err := s.Put(store.Byte("key42"), m)

Jump to

Keyboard shortcuts

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