cdb

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2020 License: MIT Imports: 6 Imported by: 3

README

CDB golang implementation

Go Report Card GoDoc

cdb is a fast, reliable, simple package for creating and reading constant databases see docs for more details

Advantages

  • Iterator support
  • Thread safe for reading
  • Lazily key, value reading using io.SectionReader
  • Buffered disc write

Example

f, _ := os.Create("test.cdb")
defer f.Close()

handle := cdb.New()

data := []struct {
    key, value string
}{
    {"key1", "value1"},
    {"key2", "value2"},
    {"key3", "value3"},
    {"key4", "value4"},
    {"key5", "value5"},
    {"key6", "value6"},
    {"key7", "value7"},
}

writer, err := handle.GetWriter(f)
if err != nil {
    t.Error(err)
}

for _, c := range data {
    writer.Put([]byte(c.key), []byte(c.value))
}

writer.Close()
reader, _ := handle.GetReader(f)

for _, c := range data {
    value, err := reader.Get([]byte(c.key))
}

Performance tricks

  • File mmap shows better performance. Example.

Documentation

Overview

Package cdb provides interfaces for a data structure of the Constant Database proposed by Daniel J. Bernstein http://cr.yp.to/cdb.html

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyCDB = errors.New("cdb is empty")
View Source
var ErrEntryNotFound = errors.New("cdb entry not found")

EntryDoesNotExists could be returned for Get method is cdb has no such key

View Source
var ErrOutOfMemory = errors.New("OutOfMemory. CDB can handle any database up to 4 gigabytes")

ErrOutOfMemory tells that it was an attempt to create a cdb database up to 4 gigabytes

Functions

func NewHash

func NewHash() hash.Hash32

NewHash returns new instance of hash.Hash32

Types

type CDB

type CDB struct {
	Hasher
}

CDB is an associative array: it maps strings (“keys”) to strings (“data”).

func New

func New() *CDB

New returns a new instance of CDB struct.

func (*CDB) GetReader

func (cdb *CDB) GetReader(reader io.ReaderAt) (Reader, error)

GetReader returns a new Reader object.

func (*CDB) GetWriter

func (cdb *CDB) GetWriter(writer io.WriteSeeker) (Writer, error)

GetWriter returns a new Writer object.

func (*CDB) SetHash

func (cdb *CDB) SetHash(hasher Hasher)

SetHash tells the cdb to use the given hash function for calculations. Given hash will be used only for new instances of Reader, Writer.

h := cdb.New() h.GetWriter(f) ... do some work ( here we use default Hasher ) h.SetHash(fnv.Hash32) h.GetReader(f) - only new instances will be use fnv.Hash32

type Hasher

type Hasher func() hash.Hash32

Hasher is a callback for creating a new instance of hash.Hash32.

type Iterator

type Iterator interface {
	// Next moves the iterator to the next record. Returns true on success otherwise returns false.
	Next() (bool, error)
	// Record returns the current record. This method is lazy. It means, a data is read on require.
	Record() Record
	// HasNext tells if the iterator can be moved to the next record.
	HasNext() bool
	// Key returns key's []byte slice. It is usually easier to use and
	// faster then iterator.Record().Key().
	// Because it doesn't requiers allocation for record copy.
	Key() ([]byte, error)
	// ValueBytes returns values's []byte slice. It is usually easier to use and
	// faster then iterator.Record().Key().
	// Because it doesn't requiers allocation for record copy.
	Value() ([]byte, error)
}

Iterator provides API for iterating through database's records. Do not share object between multiple goroutines.

type Reader

type Reader interface {
	// Get returns the first value associated with the given key
	Get(key []byte) ([]byte, error)
	// Has returns true if the given key exists, otherwise returns false.
	Has(key []byte) (bool, error)
	// Iterator returns a new Iterator object that points on the first record.
	Iterator() (Iterator, error)
	// IteratorAt returns a new Iterator object that points on the first record associated with the given key.
	IteratorAt(key []byte) (Iterator, error)
	// Size returns the size of the dataset
	Size() int
}

Reader provides API for retrieving values, iterating through dataset. All methods are thread safe.

type Record

type Record interface {
	// Key returns io.Reader with given record's key and key size.
	Key() (io.Reader, uint32)
	// Value returns io.Reader with given record's value and value size.
	Value() (io.Reader, uint32)
}

Record provides API for reading record key, value.

type Writer

type Writer interface {
	// Put saves a new associated pair <key, value> into databases. Returns an error on failure.
	Put(key []byte, value []byte) error
	// Close commits database, makes it possible for reading.
	Close() error
}

Writer provides API for creating database.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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