cdb64

package module
v0.0.0-...-1320fef Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 9 Imported by: 1

README

CDB64

This is a native Go implementation of cdb, a constant key/value database with some very nice properties, but without the 4GB size limit.

Adapted from the original 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 4096 bytes, plus 32 bytes per record, plus the space for keys and data.
  • No random limits: cdb can handle any database up to 16 exabytes. There are no other restrictions; records don't even have to fit into memory. Databases are stored in a machine-independent format.

This repo is based on https://github.com/colinmarc/cdb

Usage

writer, err := cdb64.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 cdb64 provides a native implementation of cdb, a fast constant key/value database, but without the 4GB size limitation.

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

This is based on the code from https://github.com/colinmarc/cdb

Example
writer, err := 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)
}

fmt.Println(string(v))
Output:

Practice

Index

Examples

Constants

This section is empty.

Variables

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

Functions

This section is empty.

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.

Example
db, err := Open("./test/test.cdb")
if err != nil {
	log.Fatal(err)
}

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

fmt.Println(string(v))
Output:

bar

func New

func New(reader io.ReaderAt, hasher HashFunc) (*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 HashFunc

type HashFunc func() hash.Hash64
type Header [256]table

type Iterator

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

Iterator represents a sequential iterator over a CDB database.

Example
db, err := Open("./test/test.cdb")
if err != nil {
	log.Fatal(err)
}

// Create an iterator for the database.
iter := db.Iter()
for iter.Next() {
	// Do something with iter.Key()/iter.Value()
}

if err := iter.Err(); err != nil {
	log.Fatal(err)
}
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.

Example
writer, err := 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"))

// It's important to call Close or Freeze when you're finished writing
// records.
writer.Close()
Output:

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 io.WriteSeeker, hasher HashFunc) (*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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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