cdbmap

package module
v0.0.0-...-9e15769 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2014 License: BSD-2-Clause Imports: 9 Imported by: 0

README

go-cdbmap

go-cdbmap is a pure Go package to read and write cdb ("constant database") files, forked from jbarham's go-cdb library.

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/clee/go-cdbmap

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

The usage is extremely simple; here is an example program that shows usage of the entire API.

package main

import (
	"github.com/clee/go-cdbmap"
	"fmt"
	"os"
)

func main() {
	// Read a cdb-formatted file into a map[string][]string
	m, err := cdbmap.FromFile("example.cdb")
	if err != nil {
		panic(err)
	}

	// Or, if you already have the file open...
	r, err := os.Open("example.cdb")
	if err != nil {
		panic(err)
	}
	m, err = cdbmap.Read(r)
	if err != nil {
		panic(err)
	}

	// Now that we have a map, we can loop over the keys...
	for key, values := range m {
		fmt.Printf("key: %s [\n", key)

		// And all of the values, too
		for _, value := range values {
			fmt.Printf("\t%s\n", value)
		}
		fmt.Printf("]\n")
	}

	// Take a map[string][]string and turn it into a cdb file
	cdbmap.ToFile(m, "/tmp/test.cdb")

	// Or, again, if you already have an open writeable file...
	w, err = os.Open("/tmp/test.cdb")
	if err != nil {
		panic(err)
	}
	cdbmap.Write(m, w)
}

Utilities

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

Documentation

Overview

Package cdbmap 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 (
	HeaderSize = uint32(256 * 8)
)

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 FromFile

func FromFile(filename string) (map[string][]string, error)

FromFile is a convenience function that reads a CDB-formatted file from the specified filename, and returns the CDB contents in map[string][]string form (or an error if the map can't be written for some reason).

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 Read

func Read(r io.ReaderAt) (map[string][]string, error)

Return the map of all the keys/values

func ToFile

func ToFile(m map[string][]string, f string) (err error)

ToFile is a convenience function that writes a map to the provided filename in CDB format.

func Write

func Write(m map[string][]string, w io.WriteSeeker) (err error)

Write takes the map in m and writes it to an io.WriteSeeker

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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