cdb

package module
v0.0.0-...-e8998c0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2013 License: BSD-2-Clause Imports: 10 Imported by: 0

README

cdb

This is a liberal fork of github.com/jbarham/go-cdb. The pkg interface has been changed to be safe for concurrent access, it now requires the use of an iterator to get multiple values from one key.

cdb is a pure Go package to read and write cdb ("constant database") files.

The cdb file format is a machine-independent format with the following features:

  • Fast lookups: A successful lookup in a large database normally takes just two disk accesses. An unsuccessful lookup takes only one.
  • Low overhead: A database uses 2048 bytes, plus 24 bytes per record, plus the space for keys and data.
  • No random limits: cdb can handle any database up to 4 gigabytes. There are no other restrictions; records don't even have to fit into memory.

See the original cdb specification and C implementation by D. J. Bernstein at http://cr.yp.to/cdb.html.

Installation

Assuming you have a working Go environment, installation is simply:

go get github.com/torbit/cdb

The package documentation can be viewed online at http://godoc.org/github.com/torbit/cdb or on the command line by running go doc github.com/torbit/cdb

The included self-test program cdb_test.go illustrates usage of the package.

Documentation

Overview

Package cdb reads and writes cdb ("constant database") files.

See the original cdb specification and C implementation by D. J. Bernstein at http://cr.yp.to/cdb.html.

Index

Constants

This section is empty.

Variables

View Source
var BadFormatError = errors.New("bad format")

Functions

func Dump

func Dump(w io.Writer, r io.Reader) (err error)

Dump reads the cdb-formatted data in r and dumps it as a series of formatted records (+klen,dlen:key->data\n) and a final newline to w. The output of Dump is suitable as input to Make. See http://cr.yp.to/cdb/cdbmake.html for details on the record format.

func Make

func Make(w io.WriteSeeker, r io.Reader) (err error)

Make reads cdb-formatted records from r and writes a cdb-format database to w. See the documentation for Dump for details on the input record format.

Types

type Cdb

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

func New

func New(r io.ReaderAt) *Cdb

New creates a new Cdb from the given ReaderAt, which should be a cdb format database.

func Open

func Open(name string) (*Cdb, error)

Open opens the named file read-only and returns a new Cdb object. The file should exist and be a cdb-format database file.

func (*Cdb) Bytes

func (c *Cdb) Bytes(key []byte) ([]byte, error)

Bytes returns the first value for this key as a []byte. Returns EOF when there is no value.

Threadsafe.

func (*Cdb) Close

func (c *Cdb) Close() (err error)

Close closes the cdb for any further reads.

func (*Cdb) Exists

func (c *Cdb) Exists(key []byte) (bool, error)

Exists returns true if there are any values for this key.

Threadsafe.

func (*Cdb) ForEachBytes

func (c *Cdb) ForEachBytes(onRecordFn func(key, val []byte) error) error

ForEachBytes calls onRecordFn for every key-val pair in the database.

The byte slices are only valid for the length of a call to onRecordFn.

If onRecordFn returns an error, iteration will stop and the error will be returned.

Threadsafe.

func (*Cdb) ForEachReader

func (c *Cdb) ForEachReader(onRecordFn func(keyReader, valReader *io.SectionReader) error) error

ForEachReader calls onRecordFn for every key-val pair in the database.

If onRecordFn returns an error, iteration will stop and the error will be returned.

Threadsafe.

func (*Cdb) Iterate

func (c *Cdb) Iterate(key []byte) *CdbIterator

Iterate returns an iterator that can be used to access all of the values for a key. Always returns a non-nil value, even if the key has no values.

Because the iterator keeps a reference to the byte slice, it shouldn't be modified until the iterator is no longer in use.

Threadsafe.

func (*Cdb) Reader

func (c *Cdb) Reader(key []byte) (*io.SectionReader, error)

Reader returns the first value for this key as an io.SectionReader. Returns EOF when there is no value.

Threadsafe.

type CdbIterator

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

func (*CdbIterator) NextBytes

func (iter *CdbIterator) NextBytes() ([]byte, error)

NextBytes returns the next value for this iterator as a []byte. Returns EOF when there are no values left.

Not threadsafe.

func (*CdbIterator) NextReader

func (iter *CdbIterator) NextReader() (*io.SectionReader, error)

NextReader returns the next value for this iterator as an io.SectionReader. Returns EOF when there are no values left.

Not threadsafe.

type Writer

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

Writer provides a simple interface for creating CDBs by wrapping the Make function.

Not threadsafe.

func NewWriter

func NewWriter(ws io.WriteSeeker) *Writer

func (*Writer) Close

func (w *Writer) Close() error

func (*Writer) Write

func (w *Writer) Write(key, val []byte) error

Jump to

Keyboard shortcuts

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