cdb64

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2019 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

CDB64

Build Status GoDoc Go Report Card License

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 github.com/chrislusf/cdb64

Usage

package main

import (
	"fmt"
	"log"

	"github.com/bsm/cdb64"
)

func main() {
	w, err := cdb64.Create("/tmp/cdb64-example.cdb")
	if err != nil {
		log.Fatalln(err)
	}
	defer w.Close()

	// Write some key/value pairs.
	_ = w.Put([]byte("Alice"), []byte("Hoax"))
	_ = w.Put([]byte("Bob"), []byte("Hope"))
	_ = w.Put([]byte("Charlie"), []byte("Horse"))

	// Freeze and re-open it for reading.
	db, err := w.Freeze()
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

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

	fmt.Print(string(v))
}

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 https://github.com/chrislusf/cdb64 which in itself is based on https://github.com/colinmarc/cdb.

Example
package main

import (
	"fmt"
	"log"

	"github.com/bsm/cdb64"
)

func main() {
	w, err := cdb64.Create("/tmp/cdb64-example.cdb")
	if err != nil {
		log.Fatalln(err)
	}
	defer w.Close()

	// Write some key/value pairs.
	_ = w.Put([]byte("Alice"), []byte("Hoax"))
	_ = w.Put([]byte("Bob"), []byte("Hope"))
	_ = w.Put([]byte("Charlie"), []byte("Horse"))

	// Freeze and re-open it for reading.
	db, err := w.Freeze()
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

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

	fmt.Print(string(v))
}
Output:
Hoax

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

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

Iterator represents a sequential iterator over a CDB database.

func (*Iterator) Err

func (it *Iterator) Err() error

Err returns the current error.

func (*Iterator) Key

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

Key returns the current key, which is valid until a subsequent call to Next(). You must copy they key if you plan to use it beyond this point.

func (*Iterator) Next

func (it *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 (it *Iterator) Value() []byte

Value returns the current value, which is valid until a subsequent call to Next(). You must copy they value if you plan to use it beyond this point.

type Reader

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

Reader represents a thread-safe CDB database reader. To create a database, use Writer.

func Open

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

Open opens an existing CDB database at the given path for reading.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the reader to further reads.

func (*Reader) Get

func (r *Reader) Get(key []byte) ([]byte, error)

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

func (*Reader) Iterator

func (r *Reader) Iterator() *Iterator

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

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. Any errors during writing or finalizing are unrecoverable.

func Create

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

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

func (*Writer) Close

func (w *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 (w *Writer) Freeze() (*Reader, error)

Freeze closes the writer, then opens it for reads.

func (*Writer) Put

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

Put adds a key/value pair to the database.

Jump to

Keyboard shortcuts

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