zkv

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 11 Imported by: 2

README

zkv

Simple key/value storage focused on high data compression.

Note: library is unstable, compatibility with older versions is not guaranteed.

Keys stored in memory, while values stored on disk.

Data stored in log based structure. Records grouped into blocks to increase compression level.

Features:

  • High compression ratio;

Disadvantages:

  • Keys stored in memory;
  • Medium speed writes;
  • Slow reads;
  • Deleting or replacing data does not recover free space;
  • Every write request blocks whole storage.

File structure:

header               [3]byte // []byte("zkv")
version              [1]byte // major version
compressor id        [1]byte

[]blocks
	block length     [8]byte // compressed block length
	data length      [8]byte // decompressed block length

	[]record
		action       [1]byte // 1 - add/overwrite record, 2 - remove record
		key          []byte  // binary-encoded
		value        []byte  // binary-encoded, only for records with action == actionAdd

Usage

Open or create new storage:

import "github.com/nxshock/zkv"

db, err := Open("path_to_file.zkv")
defer db.Close() // don't forget to close storage

Open storage with custom config:

config := &zkv.Config{
	BlockDataSize: 64 * 1024,  // set custom block size

	Compressor:    zkv.ZstdCompressor, // choose from [NoneCompressor, XzCompressor, ZstdCompressor]
	                                   // or create custom compressor that match zkv.Compressor interface

	ReadOnly:      false}             // set true if storage must be read only

db, err := OpenWithConfig("path_to_file.zkv", config)

List of available compressors:

  1. zkv.ZstdCompressor (default) - medium compression ratio, fast compression and medium speed decompression;
  2. zkv.XzCompressor - high compression ratio, slow speed;
  3. zkv.NoneCompressor - no compression, high speed.

Write data:

err := db.Set(key, value) // key and value can be any type

Read data:

var value ValueType
err := db.Get(key, &value)

Delete data:

err := db.Delete(key) // returns nil error if key does not exists

Flush data on disk (for example to prevent loosing buffered data):

err := db.Flush()

Often calls reduce compression ratio because written data on disk does not grouped into blocks. It you want to update data on disk on every record write, open storage with Config.BlockDataSize = 1.

Get number of stored records:

count := db.Count()

Iterate over keys and values in written order:

f := func(keyBytes, valueBytes []byte) bool {
	var key KeyType
	var value ValueType

	err := zkv.Decode(keyBytes, &key)
	err = zkv.Decode(valueBytes, &value)

	// now you can work with key and value

	return true // return true to continue iterating else return false
}

err := db.Iterate(f)

This provides maximum possible read speed.

Shrink storage size by deleting overwrited records from file:

err := db.Shrink(newFilePath)

Used libraries

  • binary - generic and Fast Binary Serializer for Go;
  • xz - Go language package supports the reading and writing of xz compressed streams;
  • zstd - provides compression to and decompression of Zstandard content.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)
View Source
var NoneCompressor = new(noneCompressor)

NoneCompressor provides no compression.

View Source
var XzCompressor = new(xzCompressor)

XzCompressor provides LZMA2 compression

View Source
var ZstdCompressor = new(zstdCompressor)

ZstdCompressor provides Zstandard compression

Functions

func Decode added in v0.2.1

func Decode(valueBytes []byte, valuePtr interface{}) error

Decode reads value from encoded []byte

func Encode added in v0.2.1

func Encode(value interface{}) ([]byte, error)

Encode transmits the data item to encoded []byte

Types

type Compressor added in v0.2.1

type Compressor interface {
	Compress([]byte) ([]byte, error)
	Decompress([]byte) ([]byte, error)
	Id() int8
	Init() error
}

Compressor represents compressor interface

type Config added in v0.2.1

type Config struct {
	BlockDataSize int64
	Compressor    Compressor
	ReadOnly      bool
}

Config represents storage config options

type Db added in v0.2.1

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

Db represents key/value storage.

func Open

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

Open opens storage with default config options.

func OpenWithConfig added in v0.2.1

func OpenWithConfig(path string, config *Config) (*Db, error)

OpenWithConfig opens storage with specified config options.

func (*Db) Close added in v0.2.1

func (db *Db) Close() error

Close saves buffered data and closes storage.

func (*Db) Config added in v0.2.1

func (db *Db) Config() Config

Config returens storage config (read only)

func (*Db) Count added in v0.2.1

func (db *Db) Count() int

Count returns number of stored key/value pairs.

func (*Db) Delete added in v0.2.1

func (db *Db) Delete(key interface{}) error

Delete deletes value of specified key.

func (*Db) Flush added in v0.2.1

func (db *Db) Flush() error

Flush saves buffered data on disk.

func (*Db) Get added in v0.2.1

func (db *Db) Get(key interface{}, valuePtr interface{}) error

Get returns value of specified key.

func (*Db) Iterate added in v0.2.1

func (db *Db) Iterate(f func(gobKeyBytes, gobValueBytes []byte) (continueIteration bool)) error

Iterate provedes fastest possible method of all record iteration.

func (*Db) Set added in v0.2.1

func (db *Db) Set(key interface{}, value interface{}) error

Set saves value for specified key. key and value can be any type.

func (*Db) Shrink added in v0.2.1

func (db *Db) Shrink(filePath string) error

Shrink compacts storage by removing replaced records and saves new file to specified path.

Jump to

Keyboard shortcuts

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