mirror of
https://github.com/grassrootseconomics/cic-chain-events.git
synced 2024-12-23 11:57:31 +01:00
Mohammed Sohail
df88d9df16
* update config to better defaults * add docs, inline and md * add context support throughout * replace json with goccy/go-json for better decoding of large JSON * update graphql fetcher: replace ioutil with io * test runner script (until CI is ready) * update CI build config
109 lines
2.5 KiB
Go
109 lines
2.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/knadh/goyesql/v2"
|
|
"github.com/zerodha/logf"
|
|
)
|
|
|
|
type queries struct {
|
|
CommitBlock string `query:"commit-block"`
|
|
GetMissingBlocks string `query:"get-missing-blocks"`
|
|
GetSearchBounds string `query:"get-search-bounds"`
|
|
InitSyncerMeta string `query:"init-syncer-meta"`
|
|
SetSearchLowerBound string `query:"set-search-lower-bound"`
|
|
}
|
|
|
|
type PostgresStoreOpts struct {
|
|
DSN string
|
|
InitialLowerBound uint64
|
|
Logg logf.Logger
|
|
Queries goyesql.Queries
|
|
}
|
|
|
|
type PostgresStore struct {
|
|
logg logf.Logger
|
|
pool *pgxpool.Pool
|
|
queries queries
|
|
}
|
|
|
|
func NewPostgresStore(o PostgresStoreOpts) (Store[pgx.Rows], error) {
|
|
postgresStore := &PostgresStore{
|
|
logg: o.Logg,
|
|
}
|
|
|
|
if err := goyesql.ScanToStruct(&postgresStore.queries, o.Queries, nil); err != nil {
|
|
return nil, fmt.Errorf("failed to scan queries %v", err)
|
|
}
|
|
|
|
parsedConfig, err := pgxpool.ParseConfig(o.DSN)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dbPool, err := pgxpool.NewWithConfig(context.Background(), parsedConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, err = dbPool.Exec(context.Background(), postgresStore.queries.InitSyncerMeta, o.InitialLowerBound)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
postgresStore.pool = dbPool
|
|
|
|
return postgresStore, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetSearchBounds(ctx context.Context, batchSize uint64, headCursor uint64, headBlockLag uint64) (uint64, uint64, error) {
|
|
var (
|
|
lowerBound uint64
|
|
upperBound uint64
|
|
)
|
|
|
|
if err := s.pool.QueryRow(
|
|
ctx,
|
|
s.queries.GetSearchBounds,
|
|
batchSize,
|
|
headCursor,
|
|
headBlockLag,
|
|
).Scan(&lowerBound, &upperBound); err != nil {
|
|
s.logg.Error("pgx error", "error", err)
|
|
return 0, 0, err
|
|
}
|
|
|
|
return lowerBound, upperBound, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetMissingBlocks(ctx context.Context, lowerBound uint64, upperBound uint64) (pgx.Rows, error) {
|
|
rows, err := s.pool.Query(ctx, s.queries.GetMissingBlocks, lowerBound, upperBound)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return rows, nil
|
|
}
|
|
|
|
func (s *PostgresStore) SetSearchLowerBound(ctx context.Context, newLowerBound uint64) error {
|
|
_, err := s.pool.Exec(ctx, s.queries.SetSearchLowerBound, newLowerBound)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *PostgresStore) CommitBlock(ctx context.Context, block uint64) error {
|
|
_, err := s.pool.Exec(ctx, s.queries.CommitBlock, block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|