mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
* add sqlite db * add .vscode to gitignore * add vtxo repo * add sqlite repos implementations * add sqlite in db/service * update go.mod * fix sqlite * move sqlite tests to service_test.go + fixes * integration tests using sqlite + properly close statements * implement GetRoundsIds * add "tx" table to store forfeits, connectors and congestion trees * add db max conn = 1 * upsert VTXO + fix onboarding * remove json tags * Fixes * Fix * fix lint * fix config.go * Fix rm config & open db only once * Update makefile --------- Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/ark-network/ark/internal/core/domain"
|
|
"github.com/ark-network/ark/internal/core/ports"
|
|
badgerdb "github.com/ark-network/ark/internal/infrastructure/db/badger"
|
|
sqlitedb "github.com/ark-network/ark/internal/infrastructure/db/sqlite"
|
|
)
|
|
|
|
var (
|
|
eventStoreTypes = map[string]func(...interface{}) (domain.RoundEventRepository, error){
|
|
"badger": badgerdb.NewRoundEventRepository,
|
|
}
|
|
roundStoreTypes = map[string]func(...interface{}) (domain.RoundRepository, error){
|
|
"badger": badgerdb.NewRoundRepository,
|
|
"sqlite": sqlitedb.NewRoundRepository,
|
|
}
|
|
vtxoStoreTypes = map[string]func(...interface{}) (domain.VtxoRepository, error){
|
|
"badger": badgerdb.NewVtxoRepository,
|
|
"sqlite": sqlitedb.NewVtxoRepository,
|
|
}
|
|
)
|
|
|
|
const (
|
|
sqliteDbFile = "sqlite.db"
|
|
)
|
|
|
|
type ServiceConfig struct {
|
|
EventStoreType string
|
|
DataStoreType string
|
|
|
|
EventStoreConfig []interface{}
|
|
DataStoreConfig []interface{}
|
|
}
|
|
|
|
type service struct {
|
|
eventStore domain.RoundEventRepository
|
|
roundStore domain.RoundRepository
|
|
vtxoStore domain.VtxoRepository
|
|
}
|
|
|
|
func NewService(config ServiceConfig) (ports.RepoManager, error) {
|
|
eventStoreFactory, ok := eventStoreTypes[config.EventStoreType]
|
|
if !ok {
|
|
return nil, fmt.Errorf("event store type not supported")
|
|
}
|
|
roundStoreFactory, ok := roundStoreTypes[config.DataStoreType]
|
|
if !ok {
|
|
return nil, fmt.Errorf("round store type not supported")
|
|
}
|
|
vtxoStoreFactory, ok := vtxoStoreTypes[config.DataStoreType]
|
|
if !ok {
|
|
return nil, fmt.Errorf("vtxo store type not supported")
|
|
}
|
|
|
|
var eventStore domain.RoundEventRepository
|
|
var roundStore domain.RoundRepository
|
|
var vtxoStore domain.VtxoRepository
|
|
var err error
|
|
|
|
switch config.EventStoreType {
|
|
case "badger":
|
|
eventStore, err = eventStoreFactory(config.EventStoreConfig...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open event store: %s", err)
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf("unknown event store db type")
|
|
}
|
|
|
|
switch config.DataStoreType {
|
|
case "badger":
|
|
roundStore, err = roundStoreFactory(config.DataStoreConfig...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open round store: %s", err)
|
|
}
|
|
vtxoStore, err = vtxoStoreFactory(config.DataStoreConfig...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open vtxo store: %s", err)
|
|
}
|
|
case "sqlite":
|
|
if len(config.DataStoreConfig) != 1 {
|
|
return nil, fmt.Errorf("invalid data store config")
|
|
}
|
|
baseDir, ok := config.DataStoreConfig[0].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid base directory")
|
|
}
|
|
db, err := sqlitedb.OpenDb(filepath.Join(baseDir, sqliteDbFile))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
roundStore, err = roundStoreFactory(db)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open round store: %s", err)
|
|
}
|
|
vtxoStore, err = vtxoStoreFactory(db)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open vtxo store: %s", err)
|
|
}
|
|
|
|
}
|
|
|
|
return &service{eventStore, roundStore, vtxoStore}, nil
|
|
}
|
|
|
|
func (s *service) RegisterEventsHandler(handler func(round *domain.Round)) {
|
|
s.eventStore.RegisterEventsHandler(handler)
|
|
}
|
|
|
|
func (s *service) Events() domain.RoundEventRepository {
|
|
return s.eventStore
|
|
}
|
|
|
|
func (s *service) Rounds() domain.RoundRepository {
|
|
return s.roundStore
|
|
}
|
|
|
|
func (s *service) Vtxos() domain.VtxoRepository {
|
|
return s.vtxoStore
|
|
}
|
|
|
|
func (s *service) Close() {
|
|
s.eventStore.Close()
|
|
s.roundStore.Close()
|
|
s.vtxoStore.Close()
|
|
}
|