gethethdb

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

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 10 Imported by: 0

README

TreeDB geth ethdb adapter

This nested module provides a reusable adapter from TreeDB to go-ethereum's ethdb.KeyValueStore / ethdb.Batch interfaces without adding a go-ethereum dependency to gomap's root module or core TreeDB packages.

Package

import gethethdb "github.com/snissn/gomap/TreeDB/integration/gethethdb"

Primary APIs:

  • Open(path string, options *OpenOptions) (*Database, error)
  • OpenWithOptions(opts treedb.Options) (*Database, error)
  • Wrap(db *treedb.DB) *Database

Open(nil options) defaults to treedb.ProfileCommandWALDurable. Writable opens reject non-command-WAL TreeDB options; this adapter is intended for geth persistent hot-KV durability/recovery through TreeDB command WAL. When callers leave command-WAL growth knobs at zero, the adapter uses a 256 MiB command-WAL frame cap, a 256 MiB active-segment rotation target, and a 512 MiB auto-checkpoint byte trigger. The frame cap lets geth's large beacon skeleton-header batches fit in one durable frame; the separate rotation and checkpoint knobs keep active command-WAL growth bounded, including read-only inspection of an existing command-WAL directory.

Semantics

  • No adapter-side key codec is used. TreeDB's native raw-KV parity handles empty keys, nil point keys, and nil values.
  • DB-level DeleteRange calls public TreeDB.DB.DeleteRange directly.
  • Nil range bounds remain unbounded; empty range bounds remain concrete empty byte-string bounds.
  • NewIterator(prefix, start) iterates prefix keys starting at prefix || start.
  • SyncKeyValue maps to TreeDB.DB.Checkpoint().
  • Compact(nil, nil) maps to TreeDB's high-level CompactStorage full-storage compaction sequence.
  • Bounded Compact(start, limit) calls with non-nil limit are accepted as advisory no-ops because TreeDB does not currently expose geth-style range-scoped compaction. A nil limit also handles the final tail range in geth's 16/256-range compaction sweeps and runs one full TreeDB storage compaction.

Validation

Run from this directory:

go test ./... -count=1
go test ./... -run 'TestCommandWALReopen|TestDatabaseSuite' -count=1
go test ./... -bench 'BenchmarkAdapterVsDirect/(Put|Get|BatchWrite|Iterator|DeleteRange|BatchDeleteRange)' -benchtime=100x -count=1

Dependency strategy

The module pins resolvable requirements at their declared module paths:

  • github.com/ethereum/go-ethereum for the standard ethdb interfaces.
  • github.com/snissn/gomap for TreeDB.

No replace directive is required for normal consumers. When this adapter is imported from a geth fork whose module path is github.com/ethereum/go-ethereum (such as snissn/go-ethereum-nitro), the fork's main module supplies the ethdb package. If a downstream experiment needs a local gomap checkout or a newer untagged gomap commit, add the github.com/snissn/gomap replace in that experiment's main module or Go workspace rather than relying on this module to propagate replaces.

Documentation

Overview

Package gethethdb adapts TreeDB to go-ethereum's ethdb.KeyValueStore.

The adapter is intentionally kept in an integration submodule so gomap's root module and core TreeDB packages do not depend on go-ethereum. It uses TreeDB's native raw-KV byte-string semantics: nil point keys are canonicalized by TreeDB to empty keys, nil values to zero-length values, and nil range bounds remain unbounded.

Index

Constants

View Source
const (

	// EnvReadIntegrity selects value-log read checksum verification for the geth
	// TreeDB adapter. Empty preserves the selected profile/default. Use
	// "unsafe-skip-checksums" only for explicitly labeled benchmark ceilings.
	EnvReadIntegrity = "TREEDB_GETH_READ_INTEGRITY"
	// EnvReadIntegrityFallback is a generic alias honored when EnvReadIntegrity is unset.
	EnvReadIntegrityFallback = "TREEDB_READ_INTEGRITY"
)

Variables

View Source
var (
	ErrClosed   = treedb.ErrClosed
	ErrNotFound = treedb.ErrKeyNotFound
)

Adapter errors are aliases to TreeDB errors so callers can use errors.Is.

Functions

This section is empty.

Types

type Batch

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

Batch implements ethdb.Batch.

func (*Batch) Close

func (b *Batch) Close()

Close releases the underlying TreeDB batch when present. It matches newer geth ethdb.Batch interfaces and is harmless as an extra method for older geth versions where Batch has no Close method.

func (*Batch) Delete

func (b *Batch) Delete(key []byte) error

Delete inserts the key removal into the batch.

func (*Batch) DeleteRange

func (b *Batch) DeleteRange(start, end []byte) error

DeleteRange records a range deletion in [start, end).

func (*Batch) Put

func (b *Batch) Put(key []byte, value []byte) error

Put inserts the given value into the batch.

func (*Batch) Replay

func (b *Batch) Replay(w ethdb.KeyValueWriter) error

Replay replays the batch contents in submitted order. TreeDB's internal batch replay may compact or sort point-only batches, so the adapter keeps a small copied operation log specifically for ethdb Replay semantics.

func (*Batch) Reset

func (b *Batch) Reset()

Reset resets the batch for reuse.

func (*Batch) ValueSize

func (b *Batch) ValueSize() int

ValueSize returns the total key/value bytes queued in this batch, matching geth adapter convention rather than TreeDB's internal encoded byte size.

func (*Batch) Write

func (b *Batch) Write() error

Write flushes the batch to TreeDB.

type Database

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

Database implements ethdb.KeyValueStore on top of TreeDB.

func Open

func Open(path string, options *OpenOptions) (*Database, error)

Open opens a TreeDB-backed ethdb.KeyValueStore at path.

func OpenWithOptions

func OpenWithOptions(opts treedb.Options) (*Database, error)

OpenWithOptions opens TreeDB with caller-supplied options and wraps it as an ethdb.KeyValueStore. Writable options must enable TreeDB command WAL. When WALMaxSegmentBytes and command-WAL growth knobs are left at zero, the adapter applies geth-sized defaults before open so command-WAL activation from persisted format config also uses the larger cap.

func Wrap

func Wrap(db *treedb.DB) *Database

Wrap adapts an already-open TreeDB handle. The caller owns the handle through the returned Database; Close closes the wrapped TreeDB handle.

func (*Database) Close

func (d *Database) Close() error

Close closes the wrapped TreeDB handle. It is safe to call more than once.

func (*Database) Compact

func (d *Database) Compact(start []byte, limit []byte) error

Compact accepts geth ethdb compaction requests.

TreeDB currently has no range-scoped compaction equivalent to geth's LevelDB/Pebble range compaction. Bounded ranges are therefore treated as advisory no-ops so geth range sweeps do not multiply full-storage compaction cost. A nil limit represents either geth's whole-DB request (nil, nil) or the final tail range in geth range sweeps, and runs TreeDB's high-level storage compaction entrypoint.

func (*Database) Delete

func (d *Database) Delete(key []byte) error

Delete removes the key from the key-value data store.

func (*Database) DeleteRange

func (d *Database) DeleteRange(start, end []byte) error

DeleteRange deletes all keys in [start, end). Nil bounds remain unbounded; empty bounds are passed through as concrete empty byte-string bounds.

func (*Database) Get

func (d *Database) Get(key []byte) ([]byte, error)

Get retrieves the given key if it is present.

func (*Database) Has

func (d *Database) Has(key []byte) (bool, error)

Has retrieves if a key is present in the key-value data store.

func (*Database) NewBatch

func (d *Database) NewBatch() ethdb.Batch

NewBatch creates a write-only batch. After Database.Close it still returns a batch object so geth's operations-after-close expectations are preserved; that batch accepts queued mutations but Write returns ErrClosed.

func (*Database) NewBatchWithSize

func (d *Database) NewBatchWithSize(size int) ethdb.Batch

NewBatchWithSize creates a write-only batch with a best-effort size hint.

func (*Database) NewIterator

func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator

NewIterator creates a binary-alphabetical iterator over keys with prefix, starting at prefix||start.

func (*Database) Put

func (d *Database) Put(key []byte, value []byte) error

Put inserts the given value into the key-value data store.

func (*Database) Stat

func (d *Database) Stat() (string, error)

Stat returns TreeDB diagnostic stats as a stable key=value text block.

func (*Database) SyncKeyValue

func (d *Database) SyncKeyValue() error

SyncKeyValue forces a TreeDB checkpoint/durability boundary for pending raw-KV command-WAL writes.

func (*Database) TreeDB

func (d *Database) TreeDB() *treedb.DB

TreeDB exposes the wrapped TreeDB handle for integration tests and advanced maintenance tooling. Callers must not close it separately from Database.Close.

type Iterator

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

Iterator implements ethdb.Iterator.

func (*Iterator) Error

func (it *Iterator) Error() error

Error returns any accumulated iterator error.

func (*Iterator) Key

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

Key returns the current key view.

func (*Iterator) Next

func (it *Iterator) Next() bool

Next moves the iterator to the next key/value pair.

func (*Iterator) Release

func (it *Iterator) Release()

Release releases iterator resources. It is idempotent.

func (*Iterator) Value

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

Value returns the current value view.

type OpenOptions

type OpenOptions struct {
	// Profile selects a public TreeDB profile. Empty defaults to
	// treedb.ProfileCommandWALDurable. Non-command-WAL write profiles are
	// rejected because this adapter is intended for geth/Nitro persistent KV
	// durability through TreeDB command WAL.
	Profile treedb.Profile

	// Options, when non-nil, supplies TreeDB options to use. Open copies the
	// struct, forces Dir to the Open path when path is non-empty, applies
	// ReadOnly/KeepRecent/MemtableMode overrides below, applies geth-safe WAL
	// sizing defaults for zero-valued knobs, and still requires CommandWAL for
	// writable opens.
	Options *treedb.Options

	// ReadOnly opens the TreeDB directory read-only.
	ReadOnly bool

	// KeepRecent and MemtableMode are optional TreeDB knobs useful for downstream
	// integrations. Zero/empty leave the selected profile defaults intact.
	KeepRecent   uint64
	MemtableMode string
}

OpenOptions configures Open.

func DefaultOpenOptions

func DefaultOpenOptions() OpenOptions

DefaultOpenOptions returns the production-oriented adapter defaults.

Jump to

Keyboard shortcuts

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