query

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

const (
	BackendSQLite   Backend = "sqlite"
	BackendPostgres Backend = "postgres"
	BackendMySQL    Backend = "mysql"
	BackendMongo    Backend = "mongo"
	BackendDgraph   Backend = "dgraph"
)

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

func New

func New(cfg Config) (Engine, error)

New creates a new query engine based on config This function is implemented in backend-specific files with build tags

type Index

type Index struct {
	Name    string
	Table   string
	Columns []string
	Unique  bool
}

Index defines a database index

func DefaultIndexes

func DefaultIndexes() []Index

DefaultIndexes returns the default index definitions

type Result

type Result struct {
	RowsAffected int64
	LastInsertID int64
}

Result represents an exec result

type Row

type Row map[string]interface{}

Row represents a query result row

type SQLite

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

SQLite implements the Engine interface using SQLite

func NewSQLite

func NewSQLite(cfg Config) (*SQLite, error)

NewSQLite creates a new SQLite query engine

func (*SQLite) Backend

func (s *SQLite) Backend() Backend

Backend returns the backend type

func (*SQLite) Begin

func (s *SQLite) Begin(ctx context.Context) (Tx, error)

func (*SQLite) Close

func (s *SQLite) Close() error

func (*SQLite) Count

func (s *SQLite) Count(ctx context.Context, table string, where string, args ...interface{}) (int64, error)

func (*SQLite) Exec

func (s *SQLite) Exec(ctx context.Context, query string, args ...interface{}) (Result, error)

func (*SQLite) GetBlock

func (s *SQLite) GetBlock(ctx context.Context, table string, id string) (*Block, error)

func (*SQLite) GetBlockByHeight

func (s *SQLite) GetBlockByHeight(ctx context.Context, table string, height uint64) (*Block, error)

func (*SQLite) GetBlockRange

func (s *SQLite) GetBlockRange(ctx context.Context, table string, start, end uint64) ([]*Block, error)

func (*SQLite) GetEdges

func (s *SQLite) GetEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)

func (*SQLite) GetIncomingEdges

func (s *SQLite) GetIncomingEdges(ctx context.Context, table string, vertexID string) ([]*Edge, error)

func (*SQLite) GetRecentBlocks

func (s *SQLite) GetRecentBlocks(ctx context.Context, table string, limit int) ([]*Block, error)

func (*SQLite) GetRecentVertices

func (s *SQLite) GetRecentVertices(ctx context.Context, table string, limit int) ([]*Vertex, error)

func (*SQLite) GetVertex

func (s *SQLite) GetVertex(ctx context.Context, table string, id string) (*Vertex, error)

func (*SQLite) GetVerticesByEpoch

func (s *SQLite) GetVerticesByEpoch(ctx context.Context, table string, epoch uint32) ([]*Vertex, error)

func (*SQLite) Init

func (s *SQLite) Init(ctx context.Context) error

func (*SQLite) InitSchema

func (s *SQLite) InitSchema(ctx context.Context, schema Schema) error

func (*SQLite) InsertBlock

func (s *SQLite) InsertBlock(ctx context.Context, table string, block *Block) error

func (*SQLite) InsertEdge

func (s *SQLite) InsertEdge(ctx context.Context, table string, edge *Edge) error

func (*SQLite) InsertVertex

func (s *SQLite) InsertVertex(ctx context.Context, table string, vertex *Vertex) error

func (*SQLite) Migrate

func (s *SQLite) Migrate(ctx context.Context, schema Schema) error

func (*SQLite) Ping

func (s *SQLite) Ping(ctx context.Context) error

func (*SQLite) Query

func (s *SQLite) Query(ctx context.Context, query string, args ...interface{}) ([]Row, error)

func (*SQLite) Sum

func (s *SQLite) Sum(ctx context.Context, table string, column, where string, args ...interface{}) (float64, error)

type Schema

type Schema struct {
	Name    string
	Tables  []Table
	Indexes []Index
}

Schema defines the database schema

func DefaultSchema

func DefaultSchema() Schema

DefaultSchema returns the default complete schema

type Table

type Table struct {
	Name    string
	Columns []Column
}

Table defines a database table

func BlocksSchema

func BlocksSchema() Table

BlocksSchema returns the default blocks table schema

func EdgesSchema

func EdgesSchema() Table

EdgesSchema returns the default edges table schema

func VerticesSchema

func VerticesSchema() Table

VerticesSchema returns the default vertices table schema

type Tx

type Tx interface {
	Engine
	Commit() error
	Rollback() error
}

Tx represents a query transaction

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

Jump to

Keyboard shortcuts

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