fskv

package module
v0.0.0-...-4eb4a7b Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2017 License: MIT Imports: 9 Imported by: 0

README

Golang Binary Wrapper

codecov Codacy Badge Go Report Card

Install

go get -u github.com/nickalie/fskv

Example of usage

Storage is safe to be used in multi-thread and multi-process environments.

package main

import (
	"github.com/nickalie/fskv"
	"log"
	"fmt"
)

func main()  {
	db, err := fskv.NewFSKV("data")

	if err != nil {
		log.Fatal(err)
	}

	db.Set("mykey", []byte("somevalue"))

	value, _ := db.Get("mykey")

	fmt.Println("Got: " + string(value))
}

Buckets

Buckets are collections of key/value pairs within the storage. You can create any amount of nested buckets

bucket, _ := db.GetBucket("mybucket")
bucket.Set("some_key", []byte("some_value"))
value, _ := bucket.Get("another_key")
childBucket, := bucket.GetBucket("childbucket")

Iterating

To iterate over the keys:

db.Scan("", func(key string, value []byte) bool {
		fmt.Printf("key: %s, value: %s\n", key, string(value))
		return false
	})

You can specify prefix to iterate over keys with that prefix.

Documentation

Overview

Package fskv provides simple file system based key-value storage.

Index

Constants

This section is empty.

Variables

View Source
var ErrLocked = errors.New("Locked")

ErrLocked returned in case is key locked for modifications

Functions

This section is empty.

Types

type Bucket

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

Bucket represents a collection of key/value pairs inside the storage.

func (*Bucket) Get

func (b *Bucket) Get(key string) ([]byte, error)

Get retrieves the value for a key in the bucket. Returns an error value if the key does not exist.

func (*Bucket) GetBucket

func (b *Bucket) GetBucket(name string) (*Bucket, error)

GetBucket creates a new bucket if it doesn't already exist and returns a reference to it. Returns an error if the bucket name is invalid.

func (*Bucket) Remove

func (b *Bucket) Remove(keys ...string) error

Remove removes a key from the bucket. If the key does not exist then nothing is done. If no keys provided whole bucket will be removed.

func (*Bucket) Scan

func (b *Bucket) Scan(prefix string, fn func(key string, value []byte) bool)

Scan executes a function for each key/value pair in a bucket if key has prefix. If the provided function returns false then the iteration is stopped.

func (*Bucket) Set

func (b *Bucket) Set(key string, value []byte) error

Set sets the value for a key in the bucket. If the key exist then its previous value will be overwritten.

type DB

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

DB represents a collection of buckets and key-value pairs that persist on disk.

func Open

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

Open creates and opens a database at the given path. If the directory does not exist then it will be created automatically.

func OpenWithFactory

func OpenWithFactory(fn func() interface{}) (*DB, error)

OpenWithFactory creates and opens a database with the given function. Function should return afero.Fs

func (*DB) Get

func (b *DB) Get(key string) ([]byte, error)

Get retrieves the value for a key. Returns an error value if the key does not exist.

func (*DB) GetBucket

func (b *DB) GetBucket(name string) (*Bucket, error)

GetBucket creates a new bucket if it doesn't already exist and returns a reference to it. Returns an error if the bucket name is invalid.

func (*DB) Remove

func (b *DB) Remove(keys ...string) error

Remove removes a key. If the key does not exist then nothing is done. If no keys provided whole content of the storage wil be removed.

func (*DB) Scan

func (b *DB) Scan(prefix string, f func(key string, value []byte) bool)

Scan executes a function for each key/value pair in a bucket if key has prefix. If the provided function returns false then the iteration is stopped.

func (*DB) Set

func (b *DB) Set(key string, value []byte) error

Set sets the value for a key. If the key exist then its previous value will be overwritten.

Jump to

Keyboard shortcuts

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