Documentation
¶
Overview ¶
Package storage provides a pluggable storage interface for the LUX indexer. Supported backends: PostgreSQL, MySQL, SQLite, MongoDB, ZapDB, Dgraph
Package storage provides unified storage with two layers:
- KV Layer: Fast key-value storage using github.com/luxfi/database
- Query Layer: SQL/Graph queries for indexed data
Default backend is SQLite. Use build tags for other backends:
go build -tags postgres go build -tags mysql go build -tags mongo go build -tags dgraph
Index ¶
- Variables
- type Backend
- type Block
- type Column
- type ColumnType
- type Config
- type Edge
- type Index
- type KV
- type Schema
- type Store
- type Table
- type Transaction
- type Unified
- func (u *Unified) Backend() Backend
- func (u *Unified) Begin(ctx context.Context) (Transaction, error)
- func (u *Unified) BeginStore(ctx context.Context) (Transaction, error)
- func (u *Unified) BeginTx(ctx context.Context) (*UnifiedTx, error)
- func (u *Unified) Close() error
- func (u *Unified) Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)
- func (u *Unified) Database() database.Database
- func (u *Unified) Delete(ctx context.Context, table string, key string) error
- func (u *Unified) Exec(ctx context.Context, q string, args ...interface{}) error
- func (u *Unified) ExecSQL(ctx context.Context, q string, args ...interface{}) error
- func (u *Unified) Get(ctx context.Context, table string, key string) ([]byte, error)
- func (u *Unified) GetBlock(ctx context.Context, table string, id string) (*Block, error)
- func (u *Unified) GetBlockByHeight(ctx context.Context, table string, height uint64) (*Block, error)
- func (u *Unified) GetEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
- func (u *Unified) GetRecentBlocks(ctx context.Context, table string, limit int) ([]*Block, error)
- func (u *Unified) GetRecentVertices(ctx context.Context, table string, limit int) ([]*Vertex, error)
- func (u *Unified) GetStats(ctx context.Context, table string) (map[string]interface{}, error)
- func (u *Unified) GetVertex(ctx context.Context, table string, id string) (*Vertex, error)
- func (u *Unified) Init(ctx context.Context) error
- func (u *Unified) InitSchema(ctx context.Context, schema Schema) error
- func (u *Unified) KV() *kv.Store
- func (u *Unified) List(ctx context.Context, table string, prefix string, limit int) ([]KV, error)
- func (u *Unified) MigrateSchema(ctx context.Context, schema Schema) error
- func (u *Unified) Ping(ctx context.Context) error
- func (u *Unified) Put(ctx context.Context, table string, key string, value []byte) error
- func (u *Unified) Query(ctx context.Context, q string, args ...interface{}) ([]map[string]interface{}, error)
- func (u *Unified) QueryEngine() query.Engine
- func (u *Unified) QuerySQL(ctx context.Context, q string, args ...interface{}) ([]map[string]interface{}, error)
- func (u *Unified) StoreBlock(ctx context.Context, table string, block *Block) error
- func (u *Unified) StoreEdge(ctx context.Context, table string, edge *Edge) error
- func (u *Unified) StoreVertex(ctx context.Context, table string, vertex *Vertex) error
- func (u *Unified) UpdateStats(ctx context.Context, table string, stats map[string]interface{}) error
- type UnifiedConfig
- type UnifiedTx
- type Vertex
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = fmt.Errorf("not found") ErrAlreadyExists = fmt.Errorf("already exists") ErrNotSupported = fmt.Errorf("not supported by this backend") ErrClosed = fmt.Errorf("store is closed") )
Errors
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend string
Backend identifies the storage backend type
func ParseBackend ¶
ParseBackend parses a backend string
type Block ¶
type Block struct {
ID string `json:"id"`
ParentID string `json:"parent_id"`
Height uint64 `json:"height"`
Timestamp time.Time `json:"timestamp"`
Status string `json:"status"`
TxCount int `json:"tx_count"`
TxIDs []string `json:"tx_ids"`
Data json.RawMessage `json:"data"`
Metadata json.RawMessage `json:"metadata"`
CreatedAt time.Time `json:"created_at"`
}
Block represents a linear chain block for storage
type Column ¶
type Column struct {
Name string
Type ColumnType
Nullable bool
Default string
Primary bool
}
Column defines a table column
type ColumnType ¶
type ColumnType string
ColumnType represents a column data type
const ( TypeText ColumnType = "text" TypeInt ColumnType = "int" TypeBigInt ColumnType = "bigint" TypeFloat ColumnType = "float" TypeBool ColumnType = "bool" TypeTimestamp ColumnType = "timestamp" TypeJSON ColumnType = "json" TypeBytes ColumnType = "bytes" )
type Config ¶
type Config struct {
Backend Backend
URL string // Connection URL (postgres://, mysql://, mongo://, etc.)
Database string // Database name (for MongoDB)
Options map[string]string // Backend-specific options
DataDir string // For file-based backends (ZapDB, SQLite)
}
Config for storage backend
type Edge ¶
type Edge struct {
Source string `json:"source"`
Target string `json:"target"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
}
Edge represents a DAG edge for storage
type Store ¶
type Store interface {
// Backend returns the query backend type (sqlite, postgres, etc.)
Backend() Backend
// Lifecycle
Init(ctx context.Context) error
Close() error
Ping(ctx context.Context) error
// Schema management
InitSchema(ctx context.Context, schema Schema) error
MigrateSchema(ctx context.Context, schema Schema) error
// Block/Vertex storage (for chain/dag indexers)
StoreBlock(ctx context.Context, table string, block *Block) error
GetBlock(ctx context.Context, table string, id string) (*Block, error)
GetBlockByHeight(ctx context.Context, table string, height uint64) (*Block, error)
GetRecentBlocks(ctx context.Context, table string, limit int) ([]*Block, error)
StoreVertex(ctx context.Context, table string, vertex *Vertex) error
GetVertex(ctx context.Context, table string, id string) (*Vertex, error)
GetRecentVertices(ctx context.Context, table string, limit int) ([]*Vertex, error)
// Edge storage (for DAG)
StoreEdge(ctx context.Context, table string, edge *Edge) error
GetEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
// Generic key-value storage
Put(ctx context.Context, table string, key string, value []byte) error
Get(ctx context.Context, table string, key string) ([]byte, error)
Delete(ctx context.Context, table string, key string) error
List(ctx context.Context, table string, prefix string, limit int) ([]KV, error)
// Stats
UpdateStats(ctx context.Context, table string, stats map[string]interface{}) error
GetStats(ctx context.Context, table string) (map[string]interface{}, error)
Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)
// Queries
Query(ctx context.Context, query string, args ...interface{}) ([]map[string]interface{}, error)
Exec(ctx context.Context, query string, args ...interface{}) error
// Transaction support (optional, may return ErrNotSupported)
Begin(ctx context.Context) (Transaction, error)
}
Store is the main storage interface for indexed data
type Transaction ¶
Transaction represents a storage transaction
type Unified ¶ added in v0.3.0
type Unified struct {
// contains filtered or unexported fields
}
Unified combines KV and Query layers into a single interface
func NewUnified ¶ added in v0.3.0
func NewUnified(cfg UnifiedConfig) (*Unified, error)
NewUnified creates a new unified storage
func NewUnifiedWithDB ¶ added in v0.3.0
NewUnifiedWithDB creates unified storage using an existing database This enables in-process mode where the indexer shares the node's database
func (*Unified) Begin ¶ added in v0.3.0
func (u *Unified) Begin(ctx context.Context) (Transaction, error)
Begin starts a transaction that satisfies the Store interface
func (*Unified) BeginStore ¶ added in v0.3.0
func (u *Unified) BeginStore(ctx context.Context) (Transaction, error)
BeginStore begins a transaction that implements the Store interface Deprecated: Use Begin instead
func (*Unified) BeginTx ¶ added in v0.3.0
BeginTx starts a transaction returning the typed UnifiedTx
func (*Unified) Count ¶ added in v0.3.0
func (u *Unified) Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)
Count counts records
func (*Unified) GetBlock ¶ added in v0.3.0
GetBlock retrieves a block (tries KV first, falls back to Query)
func (*Unified) GetBlockByHeight ¶ added in v0.3.0
func (u *Unified) GetBlockByHeight(ctx context.Context, table string, height uint64) (*Block, error)
GetBlockByHeight retrieves a block by height
func (*Unified) GetRecentBlocks ¶ added in v0.3.0
GetRecentBlocks retrieves recent blocks (query layer only)
func (*Unified) GetRecentVertices ¶ added in v0.3.0
func (u *Unified) GetRecentVertices(ctx context.Context, table string, limit int) ([]*Vertex, error)
GetRecentVertices retrieves recent vertices
func (*Unified) InitSchema ¶ added in v0.3.0
func (*Unified) MigrateSchema ¶ added in v0.3.0
func (*Unified) QueryEngine ¶ added in v0.3.0
QueryEngine returns the Query layer for direct access
func (*Unified) QuerySQL ¶ added in v0.3.0
func (u *Unified) QuerySQL(ctx context.Context, q string, args ...interface{}) ([]map[string]interface{}, error)
Query runs a SQL/Graph query
func (*Unified) StoreBlock ¶ added in v0.3.0
StoreBlock stores a block in both layers
func (*Unified) StoreVertex ¶ added in v0.3.0
StoreVertex stores a vertex in both layers
type UnifiedConfig ¶ added in v0.3.0
type UnifiedConfig struct {
// KV layer config
KV kv.Config
// Query layer config
Query query.Config
// Enable dual-write to both layers
DualWrite bool
}
UnifiedConfig configures the unified storage
func DefaultUnifiedConfig ¶ added in v0.3.0
func DefaultUnifiedConfig(dataDir string) UnifiedConfig
DefaultUnifiedConfig returns the default configuration If DATABASE_URL environment variable is set, uses PostgreSQL backend
func InProcessConfig ¶ added in v0.3.0
func InProcessConfig(nodeDB database.Database, dataDir string) UnifiedConfig
InProcessConfig returns config for in-process mode with a node database
type UnifiedTx ¶ added in v0.3.0
type UnifiedTx struct {
// contains filtered or unexported fields
}
UnifiedTx is a transaction over the unified store
func (*UnifiedTx) StoreBlock ¶ added in v0.3.0
type Vertex ¶
type Vertex struct {
ID string `json:"id"`
Type string `json:"type"`
ParentIDs []string `json:"parent_ids"`
Height uint64 `json:"height"`
Epoch uint32 `json:"epoch"`
TxIDs []string `json:"tx_ids"`
Timestamp time.Time `json:"timestamp"`
Status string `json:"status"`
Data json.RawMessage `json:"data"`
Metadata json.RawMessage `json:"metadata"`
CreatedAt time.Time `json:"created_at"`
}
Vertex represents a DAG vertex for storage
Directories
¶
| Path | Synopsis |
|---|---|
|
Package kv provides a unified key-value storage layer using github.com/luxfi/database.
|
Package kv provides a unified key-value storage layer using github.com/luxfi/database. |
|
Package query provides a query layer for indexed data.
|
Package query provides a query layer for indexed data. |