hookdb
v1.21.5
Documentation
pkg.go.dev ↗Package hookdb wraps a database to fire async callbacks on write operations. This enables real-time indexing without polling.
Functions
BlockPrefixes
func BlockPrefixes() [][]byte
BlockPrefixes returns common block-related key prefixes to watch
EVMPrefixes
func EVMPrefixes() [][]byte
EVMPrefixes returns EVM-specific key prefixes to watch
Types
Config
type Config struct {
// BufferSize for the async event channel (default: 10000)
BufferSize int
// Workers is the number of async workers (default: 4)
Workers int
}
Config for the hook database
DefaultConfig
func DefaultConfig() Config
DefaultConfig returns sensible defaults
Database
type Database struct {
// contains filtered or unexported fields
}
Database wraps another database to fire callbacks on writes
New
func New(db database.Database, cfg Config) *Database
New wraps a database with hook capabilities
Database.Backup
func (db *Database) Backup(w io.Writer, since uint64) (uint64, error)
Database.Close
func (db *Database) Close() error
Database.Compact
func (db *Database) Compact(start, limit []byte) error
Database.Delete
func (db *Database) Delete(key []byte) error
Database.Get
func (db *Database) Get(key []byte) ([]byte, error)
Database.Has
func (db *Database) Has(key []byte) (bool, error)
Database.HealthCheck
func (db *Database) HealthCheck(ctx context.Context) (interface{}, error)
Database.Load
func (db *Database) Load(r io.Reader) error
Database.NewBatch
func (db *Database) NewBatch() database.Batch
Database.NewIterator
func (db *Database) NewIterator() database.Iterator
Database.NewIteratorWithPrefix
func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator
Database.NewIteratorWithStart
func (db *Database) NewIteratorWithStart(start []byte) database.Iterator
Database.NewIteratorWithStartAndPrefix
func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator
Database.Put
func (db *Database) Put(key, value []byte) error
Database.RegisterHandler
func (db *Database) RegisterHandler(prefix []byte, handler Handler)
RegisterHandler adds a handler for a specific key prefix Pass nil prefix to receive all events
Database.Sync
func (db *Database) Sync() error
Event
type Event struct {
Type EventType
Key []byte
Value []byte
Prefix []byte // Which prefix triggered this event
}
Event represents a database write event
EventType
type EventType int
EventType indicates the type of database operation
EventPut (3 declarations)
const ( EventPut EventType = iota EventDelete EventBatchWrite )
Handler
type Handler func(Event)
Handler is called when a write event occurs Handlers are called asynchronously and should not block
IndexerEvent
type IndexerEvent struct {
ChainID string `json:"chain_id"`
Type string `json:"type"` // "put" or "delete"
Prefix string `json:"prefix"`
Key string `json:"key"`
Value []byte `json:"value,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
IndexerEvent is sent to the indexer service
IndexerNotifier
type IndexerNotifier struct {
// contains filtered or unexported fields
}
IndexerNotifier sends database events to an indexer service
NewNotifier
func NewNotifier(cfg NotifierConfig) *IndexerNotifier
NewNotifier creates a new indexer notifier
IndexerNotifier.Close
func (n *IndexerNotifier) Close()
Close stops the notifier and flushes remaining events
IndexerNotifier.Handler
func (n *IndexerNotifier) Handler(chainID string) Handler
Handler returns a hook handler for a specific chain
NotifierConfig
type NotifierConfig struct {
// Endpoint is the indexer service URL (e.g., "http://localhost:8090/api/v2/events")
Endpoint string
// ChainID identifies this chain
ChainID string
// BatchSize is the max events per batch (default: 100)
BatchSize int
// FlushInterval is how often to flush batches (default: 100ms)
FlushInterval time.Duration
// Timeout for HTTP requests (default: 5s)
Timeout time.Duration
}
NotifierConfig configures the indexer notifier
PrefixHandler
type PrefixHandler struct {
Prefix []byte
Handler Handler
}
PrefixHandler registers a handler for a specific key prefix