cdb

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

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

Go to latest
Published: Feb 28, 2018 License: MIT, MIT Imports: 14 Imported by: 0

README

CDB

GoDoc build

This is a native Go implementation of cdb, a constant key/value database with some very nice properties. From the design doc:

cdb is a fast, reliable, simple package for creating and reading constant databases. Its database structure provides several 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. Databases are stored in a machine-independent format.

This version is modified by Sudhi Herle:

  • addition of a SHA256 checksum over the entire CDB (appended to end of file) and verified when opened

  • uses Zi Long Tan's superfast hash

Usage

writer, err := cdb.Create("/tmp/example.cdb")
if err != nil {
  log.Fatal(err)
}

// Write some key/value pairs to the database.
writer.Put([]byte("Alice"), []byte("Practice"))
writer.Put([]byte("Bob"), []byte("Hope"))
writer.Put([]byte("Charlie"), []byte("Horse"))

// Freeze the database, and open it for reads.
db, err := writer.Freeze()
if err != nil {
  log.Fatal(err)
}

// Fetch a value.
v, err := db.Get([]byte("Alice"))
if err != nil {
  log.Fatal(err)
}

log.Println(string(v))
// => Practice

// Iterate over the database
iter := db.Iter()
for iter.Next() {
    log.Printf("The key %s has a value of length %d\n", string(iter.Key()), len(iter.Value()))
}

if err := iter.Err(); err != nil {
    log.Fatal(err)
}

Documentation

Overview

Package cdb provides a native implementation of cdb, a constant key/value database with some very nice properties.

For more information on cdb, see the original design doc at http://cr.yp.to/cdb.html.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTooMuchData = errors.New("CDB files are limited to 4GB of data")

Functions

func Hash32

func Hash32(key []byte) uint32

This is all that is needed

Types

type CDB

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

CDB represents an open CDB database. It can only be used for reads; to create a database, use Writer.

func New

func New(reader io.ReaderAt, hasher hash.Hash32) (*CDB, error)

New opens a new CDB instance for the given io.ReaderAt. It can only be used for reads; to create a database, use Writer.

If hasher is nil, it will default to the CDB hash function. If a database was created with a particular hash function, that same hash function must be passed to New, or the database will return incorrect results.

func Open

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

Open opens an existing CDB database at the given path.

func (*CDB) Close

func (cdb *CDB) Close() error

Close closes the database to further reads.

func (*CDB) Get

func (cdb *CDB) Get(key []byte) ([]byte, error)

Get returns the value for a given key, or nil if it can't be found.

func (*CDB) Iter

func (cdb *CDB) Iter() *Iterator

Iter creates an Iterator that can be used to iterate the database.

type Iterator

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

Iterator represents a sequential iterator over a CDB database.

Example
package main

import ()

func main() {
}
Output:

func (*Iterator) Err

func (iter *Iterator) Err() error

Err returns the current error.

func (*Iterator) Key

func (iter *Iterator) Key() []byte

Key returns the current key.

func (*Iterator) Next

func (iter *Iterator) Next() bool

Next reads the next key/value pair and advances the iterator one record. It returns false when the scan stops, either by reaching the end of the database or an error. After Next returns false, the Err method will return any error that occurred while iterating.

func (*Iterator) Value

func (iter *Iterator) Value() []byte

Value returns the current value.

type Writer

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

Writer provides an API for creating a CDB database record by record.

Close or Freeze must be called to finalize the database, or the resulting file will be invalid.

func Create

func Create(path string) (*Writer, error)

Create opens a CDB database at the given path. If the file exists, it will be overwritten.

func NewWriter

func NewWriter(writer *os.File, hasher hash.Hash32) (*Writer, error)

NewWriter opens a CDB database for the given io.WriteSeeker.

If hasher is nil, it will default to the CDB hash function.

func (*Writer) Close

func (cdb *Writer) Close() error

Close finalizes the database, then closes it to further writes.

Close or Freeze must be called to finalize the database, or the resulting file will be invalid.

func (*Writer) Freeze

func (cdb *Writer) Freeze() (*CDB, error)

Freeze finalizes the database, then opens it for reads. If the stream cannot be converted to a io.ReaderAt, Freeze will return os.ErrInvalid.

Close or Freeze must be called to finalize the database, or the resulting file will be invalid.

func (*Writer) Put

func (cdb *Writer) Put(key, value []byte) error

Put adds a key/value pair to the database. If the amount of data written would exceed the limit, Put returns ErrTooMuchData.

Jump to

Keyboard shortcuts

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