mcdb

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

Multi-CDB

Write Once Read Many Constant Database. It wraps github.com/colinmarc/cdb (See the original cdb specification and C implementation by D. J. Bernstein at http://cr.yp.to/cdb.html) to hide the 4GiB size limit.

The database(s) can be written only once, and you must set the number of tables to use beforehand, or use the automatic grow facility (see Writer.Put).

Rationale

CDB is fast and very simple. For read-only access, it's ideal. The size limit is too low nowadays.

CLI

./cmd/cdb is a command-line program that can dump in cdbmake-format, and load from that format.

Versions

v0 - no versioning

Uses FNV for bucket distribution, and FNV for CDB hashing.

v1 - version in filename

Uses FNV for bucket distribution, and the default CDB hashing.

Documentation

Overview

Package mcdb contains multiple Constant Databases (DJB's cdb), as one cdb can only contain 4GiB maximum.

This is a thin wrapper over github.com/colinmarc/cdb.

Index

Constants

View Source
const (
	FileName   = "mcdb-v%d-%d,%b.cdb"
	FileNameV0 = "mcdb-%d,%b.cdb"
)
View Source
const DefaultVersion = Version(1)

Variables

This section is empty.

Functions

func Dump added in v0.4.0

func Dump(bw *bufio.Writer, key, val []byte) error

Dump the given data key, value pair in cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value)

func DumpKey added in v0.5.0

func DumpKey(bw *bufio.Writer, key []byte) error

DumpKey the given data key in cdbmake format ("+%d:%s\n", len(key), key)

func Parse added in v0.4.0

func Parse(br *bufio.Reader, key, val []byte) ([]byte, []byte, error)

Parse one key,value pair from the bufio.Reader of cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value).

Types

type Config added in v0.5.0

type Config struct {
	OnlyKeys, Simple bool
}

type HashFunc added in v0.2.0

type HashFunc func([]byte) uint32

type Iterator

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

Iterator iterates through all keys of all CDB files.

func (*Iterator) Dump added in v0.1.0

func (m *Iterator) Dump(w io.Writer) error

Dump the current Iterator position data in cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value).

func (*Iterator) Err

func (m *Iterator) Err() error

Err returns the last error.

func (*Iterator) Key

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

Key returns the current key (after Next).

func (*Iterator) Next

func (m *Iterator) Next() bool

Next advances the iterator, if possible.

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

Value returns the current value (after Next).

type Reader

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

Reader is a reader for multiple CDB files.

func NewReader

func NewReader(dir string) (*Reader, error)

NewReader opens the multiple CDB files for reading.

func (*Reader) Close

func (m *Reader) Close() error

Close the underlying readers.

func (*Reader) Dump added in v0.1.0

func (m *Reader) Dump(w io.Writer) error

Dump all the underlying data in cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value)

func (*Reader) DumpContext added in v0.3.0

func (m *Reader) DumpContext(ctx context.Context, w io.Writer) error

DumpContext dumps all the underlying data in cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value)

func (*Reader) Get

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

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

func (*Reader) Iter

func (m *Reader) Iter() *Iterator

Iter returns an iterator.

type Version added in v0.2.0

type Version uint8

func (Version) String added in v0.2.0

func (v Version) String() string

type Writer

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

Writer is the writer. It needs the number of tables beforehand.

func NewWriter

func NewWriter(dir string, n int) (*Writer, error)

NewWriter returns a new Writer.

The next power-of-two number of tables are created, so for example with n=3, 4 tables are created, containing maximum 16GiB of data.

If n<=0 then automatic growing is enabled, with the starting number of tables being abs(n)

func (*Writer) Close

func (m *Writer) Close() error

Close the underlying writers.

func (*Writer) Load added in v0.1.0

func (m *Writer) Load(r io.Reader) error

Load the Writer from cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value).

func (*Writer) LoadContext added in v0.3.0

func (m *Writer) LoadContext(ctx context.Context, r io.Reader) error

LoadContext the Writer from cdbmake format ("+%d,%d:%s->%s\n", len(key), len(value), key, value).

func (*Writer) Put

func (m *Writer) Put(key, val []byte) error

Put the key into one of the underlying writers.

If the amount of data written would exceed the limit, and the auto-growing is disabled, then Put returns ErrTooMuchData.

When growing, the number of tables doubles, and all data is copied.

Directories

Path Synopsis
cmd
cdb

Jump to

Keyboard shortcuts

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