cdb

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderSize is the size of the CDB header (256 hash table pointers)
	HeaderSize = 2048
	// NumTables is the number of hash tables in a CDB file
	NumTables = 256
)

Variables

View Source
var (
	// ErrNotFound is returned when a key is not found in the database
	ErrNotFound = errors.New("key not found")
	// ErrInvalidFormat is returned when the CDB file is malformed
	ErrInvalidFormat = errors.New("invalid CDB format")
)

Functions

func SetPermissions

func SetPermissions(filename string, perms os.FileMode) error

SetPermissions sets the file permissions (must be called before Finalize)

Types

type CDB

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

CDB represents an open CDB database

func Open

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

Open opens a CDB database file for reading

func (*CDB) Close added in v1.0.6

func (c *CDB) Close() error

Close closes the CDB database

func (*CDB) Get

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

Get returns the first value associated with the given key

func (*CDB) GetAll

func (c *CDB) GetAll(key []byte) ([][]byte, error)

GetAll returns all values associated with the given key

func (*CDB) GetFile

func (c *CDB) GetFile() *os.File

GetFile returns the underlying file (for internal use)

func (*CDB) GetN

func (c *CDB) GetN(key []byte, n int) ([]byte, error)

GetN returns the nth value associated with the given key (1-based)

type DuplicateMode

type DuplicateMode int

DuplicateMode defines how to handle duplicate keys

const (
	// DuplicateModeAllow allows duplicate keys (default)
	DuplicateModeAllow DuplicateMode = iota
	// DuplicateModeWarn allows duplicates but warns
	DuplicateModeWarn
	// DuplicateModeError rejects duplicates with an error
	DuplicateModeError
	// DuplicateModeReplace replaces existing key with new value
	DuplicateModeReplace
	// DuplicateModeUnique ignores duplicate keys (keeps first)
	DuplicateModeUnique
	// DuplicateModeZeroFill zero-fills duplicate records
	DuplicateModeZeroFill
)

type Iterator

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

Iterator provides sequential access to all records in a CDB database

func NewIterator

func NewIterator(cdb *CDB) *Iterator

NewIterator creates a new iterator for sequential scanning of the database

func (*Iterator) Err

func (it *Iterator) Err() error

Err returns any error that occurred during iteration

func (*Iterator) Next

func (it *Iterator) Next() (key, value []byte, ok bool)

Next advances the iterator to the next record and returns the key and value. It returns false when there are no more records or an error occurred. Use Err() to check for errors after Next returns false.

func (*Iterator) Reset

func (it *Iterator) Reset()

Reset resets the iterator to the beginning of the database

type Writer

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

Writer is used to create CDB databases

func Create

func Create(finalFile, tempFile string) (*Writer, error)

Create creates a new CDB writer If tempFile is empty, finalFile + ".tmp" will be used If tempFile is "-", the file will be created in-place (no temp file)

func (*Writer) Abort

func (w *Writer) Abort() error

Abort cancels the database creation and removes the temp file

func (*Writer) Copy

func (w *Writer) Copy(r *CDB) error

Copy copies all records from a reader to this writer

func (*Writer) Finalize

func (w *Writer) Finalize() error

Finalize completes the database creation by writing hash tables and atomically renaming. The atomic rename ensures that readers with the old file open continue to work, while new opens immediately see the new version. This allows zero-downtime updates.

func (*Writer) Put

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

Put adds a key-value pair to the database

func (*Writer) PutString

func (w *Writer) PutString(key, value string) error

PutString is a convenience method for adding string key-value pairs

func (*Writer) SetDuplicateMode

func (w *Writer) SetDuplicateMode(mode DuplicateMode)

SetDuplicateMode sets how duplicate keys should be handled

func (*Writer) WriteFrom

func (w *Writer) WriteFrom(r io.Reader, mapFormat bool) error

WriteFrom reads records from a reader and writes them to the CDB

Jump to

Keyboard shortcuts

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