Documentation
¶
Overview ¶
Package query provides a query layer 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 Engine
- type Index
- type Result
- type Row
- type SQLite
- func (s *SQLite) Backend() Backend
- func (s *SQLite) Begin(ctx context.Context) (Tx, error)
- func (s *SQLite) Close() error
- func (s *SQLite) Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)
- func (s *SQLite) Exec(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (s *SQLite) GetBlock(ctx context.Context, table string, id string) (*Block, error)
- func (s *SQLite) GetBlockByHeight(ctx context.Context, table string, height uint64) (*Block, error)
- func (s *SQLite) GetBlockRange(ctx context.Context, table string, start, end uint64) ([]*Block, error)
- func (s *SQLite) GetEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
- func (s *SQLite) GetIncomingEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
- func (s *SQLite) GetRecentBlocks(ctx context.Context, table string, limit int) ([]*Block, error)
- func (s *SQLite) GetRecentVertices(ctx context.Context, table string, limit int) ([]*Vertex, error)
- func (s *SQLite) GetVertex(ctx context.Context, table string, id string) (*Vertex, error)
- func (s *SQLite) GetVerticesByEpoch(ctx context.Context, table string, epoch uint32) ([]*Vertex, error)
- func (s *SQLite) Init(ctx context.Context) error
- func (s *SQLite) InitSchema(ctx context.Context, schema Schema) error
- func (s *SQLite) InsertBlock(ctx context.Context, table string, block *Block) error
- func (s *SQLite) InsertEdge(ctx context.Context, table string, edge *Edge) error
- func (s *SQLite) InsertVertex(ctx context.Context, table string, vertex *Vertex) error
- func (s *SQLite) Migrate(ctx context.Context, schema Schema) error
- func (s *SQLite) Ping(ctx context.Context) error
- func (s *SQLite) Query(ctx context.Context, query string, args ...interface{}) ([]Row, error)
- func (s *SQLite) Sum(ctx context.Context, table string, column, where string, args ...interface{}) (float64, error)
- type Schema
- type Table
- type Tx
- type Vertex
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = fmt.Errorf("not found") ErrExists = fmt.Errorf("already exists") ErrNotSupported = fmt.Errorf("not supported") ErrClosed = fmt.Errorf("engine closed") )
Errors
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend string
Backend identifies the query backend type
func AvailableBackends ¶
func AvailableBackends() []Backend
AvailableBackends returns the list of backends compiled into this build
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 for query 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
Database string // Database name (for MongoDB)
DataDir string // For file-based backends
Options map[string]string // Backend-specific options
}
Config for the query layer
type Edge ¶
type Edge struct {
Source string `json:"source"`
Target string `json:"target"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
}
Edge for query storage
type Engine ¶
type Engine interface {
// Backend returns the 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
Migrate(ctx context.Context, schema Schema) error
// Block queries
InsertBlock(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)
GetBlockRange(ctx context.Context, table string, start, end uint64) ([]*Block, error)
// Vertex queries
InsertVertex(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)
GetVerticesByEpoch(ctx context.Context, table string, epoch uint32) ([]*Vertex, error)
// Edge queries
InsertEdge(ctx context.Context, table string, edge *Edge) error
GetEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
GetIncomingEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)
// Generic queries
Query(ctx context.Context, query string, args ...interface{}) ([]Row, error)
Exec(ctx context.Context, query string, args ...interface{}) (Result, error)
// Aggregations
Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)
Sum(ctx context.Context, table string, column, where string, args ...interface{}) (float64, error)
// Transaction support
Begin(ctx context.Context) (Tx, error)
}
Engine is the query layer interface
type Index ¶
Index defines a database index
func DefaultIndexes ¶
func DefaultIndexes() []Index
DefaultIndexes returns the default index definitions
type SQLite ¶
type SQLite struct {
// contains filtered or unexported fields
}
SQLite implements the Engine interface using SQLite
func (*SQLite) GetBlockByHeight ¶
func (*SQLite) GetBlockRange ¶
func (*SQLite) GetIncomingEdges ¶
func (*SQLite) GetRecentBlocks ¶
func (*SQLite) GetRecentVertices ¶
func (*SQLite) GetVerticesByEpoch ¶
func (*SQLite) InsertBlock ¶
func (*SQLite) InsertEdge ¶
func (*SQLite) InsertVertex ¶
type Table ¶
Table defines a database table
func VerticesSchema ¶
func VerticesSchema() Table
VerticesSchema returns the default vertices table schema
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 for query storage