cdb

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

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

Go to latest
Published: Jun 3, 2013 License: Apache-2.0 Imports: 12 Imported by: 9

README

cdb.go - By John Barham

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

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/jbarham/cdb.go

The package documentation can be viewed online at http://gopkgdoc.appspot.com/pkg/github.com/jbarham/cdb.go or on the command line by running go doc github.com/jbarham/cdb.go.

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

Utilities

The cdb.go package includes ports of the programs cdbdump and cdbmake from the original implementation.

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

View Source
const MaxCdbSize = 1 << 32

MaxCdbSize is the maximum CDB size: 4Gb

Variables

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

BadFormatError is the "bad format" error

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 DumpMap

func DumpMap(r io.Reader, work func(Element) error) error

DumpMap calls work function for every element in the CDB if the function returns error, then quits with that error

func DumpToChan

func DumpToChan(c chan<- Element, r io.Reader) error

DumpToChan dumps elements into the given channel, does not close it!

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.

func MakeFactory

func MakeFactory(w io.WriteSeeker) (adder AdderFunc, closer CloserFunc, err error)

MakeFactory creates CDB and returns an adder function which should be called with each Element, and a closer, which finalizes the CDB.

func MakeFromChan

func MakeFromChan(w io.WriteSeeker, c <-chan Element) error

MakeFromChan makes CDB reading elements from the channel, does not close it!

Types

type AdderFunc

type AdderFunc func(Element) error

AdderFunc is the element appender

type Cdb

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

Cdb is the reader struct

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) Close

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

Close closes the cdb for any further reads.

func (*Cdb) Data

func (c *Cdb) Data(key []byte) (data []byte, err error)

Data returns the first data value for the given key. If no such record exists, it returns EOF.

func (*Cdb) Find

func (c *Cdb) Find(key []byte) (rdata *io.SectionReader, err error)

Find returns the first data value for the given key as a SectionReader. Find is the same as FindStart followed by FindNext.

func (*Cdb) FindNext

func (c *Cdb) FindNext(key []byte) (rdata *io.SectionReader, err error)

FindNext returns the next data value for the given key as a SectionReader. If there are no more records for the given key, it returns EOF. FindNext acts as an iterator: The iteration should be initialized by calling FindStart and all subsequent calls to FindNext should use the same key value.

func (*Cdb) FindStart

func (c *Cdb) FindStart()

FindStart resets the cdb to search for the first record under a new key.

type CdbWriter

type CdbWriter struct {
	Filename string
	// contains filtered or unexported fields
}

CdbWriter is the CDB writer

func NewWriter

func NewWriter(cdb_fn string) (*CdbWriter, error)

NewWriter returns a CdbWriter which writes to the given filename

func (*CdbWriter) Close

func (cw *CdbWriter) Close() error

Close closes (finalizes) the writer

func (*CdbWriter) Put

func (cw *CdbWriter) Put(elt Element)

Put puts an Element into the writer

func (*CdbWriter) PutPair

func (cw *CdbWriter) PutPair(key []byte, val []byte)

PutPair puts a key, val pair to the writer

type CloserFunc

type CloserFunc func() error

CloserFunc is the Close

type Element

type Element struct {
	Key  []byte
	Data []byte
}

Element is the Key:Data pair

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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