Skip to content

Commit 6f41c59

Browse files
committed
Entering broken phase - getting fdb() instance and config
1 parent 309660d commit 6f41c59

File tree

13 files changed

+245
-61
lines changed

13 files changed

+245
-61
lines changed

cmd/benchmark.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmd
33
import (
44
"fmt"
55
"github.com/unpackdev/fdb"
6+
"github.com/unpackdev/fdb/pkg/config"
7+
"github.com/unpackdev/fdb/pkg/types"
68
"github.com/urfave/cli/v2"
79
"log"
810
"runtime"
@@ -18,7 +20,20 @@ func BenchmarkCommand() *cli.Command {
1820
// Simulate benchmarking logic here
1921
fmt.Println("Running client benchmark...")
2022

21-
cnf := fdb.Config{}
23+
cnf := config.Config{
24+
Transports: []config.Transport{
25+
{
26+
Type: types.QUICTransportType,
27+
Enabled: true,
28+
Config: config.QuicTransport{IPv4: "127.0.0.1", Port: 4433},
29+
},
30+
/* {
31+
Type: fdb.UDS,
32+
Enabled: true,
33+
Config: fdb.UdsTransport{IPv4: "127.0.0.1", Port: 8000},
34+
},*/
35+
},
36+
}
2237

2338
fdbc, fdbcErr := fdb.New(c.Context, cnf)
2439
if fdbcErr != nil {

config.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

fdb.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package fdb
33
import (
44
"context"
55
"github.com/pkg/errors"
6+
"github.com/unpackdev/fdb/pkg/config"
67
)
78

89
type FDB struct {
910
ctx context.Context
10-
config Config
11+
config config.Config
1112
}
1213

13-
func New(ctx context.Context, config Config) (*FDB, error) {
14+
func New(ctx context.Context, config config.Config) (*FDB, error) {
1415
if err := config.Validate(); err != nil {
1516
return nil, errors.Wrap(err, "failure to validate (f)db configuration")
1617
}

mdbx.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import (
44
"context"
55
"github.com/erigontech/mdbx-go/mdbx"
66
"github.com/pkg/errors"
7+
"github.com/unpackdev/fdb/pkg/config"
78
"os"
89
)
910

1011
type Db struct {
1112
ctx context.Context
12-
opts MdbxNode
13+
opts config.MdbxNode
1314
env *mdbx.Env
1415
dbi mdbx.DBI
1516
}
1617

17-
func NewDb(ctx context.Context, opts MdbxNode) (Provider, error) {
18+
func NewDb(ctx context.Context, opts config.MdbxNode) (Provider, error) {
1819
env, err := mdbx.NewEnv()
1920
if err != nil {
2021
return nil, err

pkg/config/config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package config
2+
3+
import "github.com/unpackdev/fdb/pkg/types"
4+
5+
type Config struct {
6+
Transports []Transport `yaml:"transports"`
7+
MdbxNodes MdbxNodes `yaml:"nodes"`
8+
}
9+
10+
func (c Config) Validate() error {
11+
return nil
12+
}
13+
14+
func (c Config) GetTransportByType(transportType types.TransportType) *Transport {
15+
for _, t := range c.Transports {
16+
if t.Type == transportType {
17+
return &t
18+
}
19+
}
20+
return nil
21+
}

pkg/config/mdbx.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config
2+
3+
// MdbxNode and MdbxNodes as before
4+
type MdbxNode struct {
5+
Name string `yaml:"name"`
6+
Path string `yaml:"path"`
7+
}
8+
9+
type MdbxNodes []MdbxNode
10+
11+
func (c Config) GetMdbxNodeByName(name string) *MdbxNode {
12+
for _, node := range c.MdbxNodes {
13+
if node.Name == name {
14+
return &node
15+
}
16+
}
17+
return nil
18+
}

pkg/config/transports.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package config
2+
3+
import "github.com/unpackdev/fdb/pkg/types"
4+
5+
type TransportConfig interface {
6+
GetTransportType() types.TransportType
7+
}
8+
9+
type Transport struct {
10+
Type types.TransportType
11+
Enabled bool
12+
Config TransportConfig
13+
}
14+
15+
// QuicTransport implements TransportConfig
16+
type QuicTransport struct {
17+
Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"`
18+
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
19+
IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"`
20+
Port int `yaml:"port" json:"port" mapstructure:"port"`
21+
}
22+
23+
func (q QuicTransport) GetTransportType() types.TransportType {
24+
return q.Type
25+
}
26+
27+
// UdsTransport implements TransportConfig
28+
type UdsTransport struct {
29+
Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"`
30+
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
31+
IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"`
32+
Port int `yaml:"port" json:"port" mapstructure:"port"`
33+
}
34+
35+
func (u UdsTransport) GetTransportType() types.TransportType {
36+
return u.Type
37+
}

pkg/types/types.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package types
2+
3+
import "fmt"
4+
5+
type TransportType int
6+
7+
const (
8+
UDPTransportType TransportType = iota
9+
QUICTransportType
10+
UDSTransportType
11+
TCPTransportType
12+
)
13+
14+
type DbType string
15+
16+
func (t DbType) String() string {
17+
return string(t)
18+
}
19+
20+
const (
21+
// To be defined for database types in the future...
22+
)
23+
24+
// HandlerType represents different types of handlers
25+
type HandlerType byte
26+
27+
// FromByte converts a byte into a HandlerType
28+
func (h *HandlerType) FromByte(b byte) error {
29+
switch b {
30+
case 'W':
31+
*h = WriteHandlerType
32+
case 'R':
33+
*h = ReadHandlerType
34+
default:
35+
return fmt.Errorf("invalid action byte: %v", b)
36+
}
37+
return nil
38+
}
39+
40+
// Define the handlers as 1-byte constants
41+
const (
42+
WriteHandlerType HandlerType = 'W' // 'W' for WRITE
43+
ReadHandlerType HandlerType = 'R' // 'R' for READ
44+
)

provider_manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package fdb
33
import (
44
"context"
55
"fmt"
6+
"github.com/unpackdev/fdb/pkg/config"
67
)
78

89
type Manager struct {
910
ctx context.Context
10-
opts MdbxNodes
11+
opts config.MdbxNodes
1112
dbs map[DbType]Provider
1213
}
1314

14-
func NewManager(ctx context.Context, opts MdbxNodes) (*Manager, error) {
15+
func NewManager(ctx context.Context, opts config.MdbxNodes) (*Manager, error) {
1516
dbs := make(map[DbType]Provider)
1617
for _, node := range opts {
1718
db, err := NewDb(ctx, node)

quic_server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ import (
1515
// Handler function type for QUIC
1616
type QuicHandler func(sess quic.Connection, stream quic.Stream, message *Message)
1717

18+
type QuicConfig struct {
19+
Type TransportType `yaml:"type" json:"type" mapstructure:"type"`
20+
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
21+
IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"`
22+
Port int `yaml:"port" json:"port" mapstructure:"port"`
23+
}
24+
25+
func (c QuicConfig) GetType() TransportType {
26+
return c.Type
27+
}
28+
29+
func (c QuicConfig) IsEnabled() bool {
30+
return c.Enabled
31+
}
32+
33+
func (c QuicConfig) IsIPv4() bool {
34+
return c.IPv4 != ""
35+
}
36+
1837
// QuicServer struct represents the QUIC server
1938
type QuicServer struct {
2039
handlerRegistry map[HandlerType]QuicHandler

0 commit comments

Comments
 (0)