Skip to content

Commit 3fcf203

Browse files
committed
robot: switch to kvbrain
1 parent d66156a commit 3fcf203

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

command/robot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package command
33
import (
44
"log/slog"
55

6-
"github.com/zephyrtronium/robot/brain/sqlbrain"
6+
"github.com/zephyrtronium/robot/brain/kvbrain"
77
"github.com/zephyrtronium/robot/channel"
88
"github.com/zephyrtronium/robot/privacy"
99
)
@@ -12,6 +12,6 @@ import (
1212
type Robot struct {
1313
Log *slog.Logger
1414
Channels map[string]*channel.Channel // TODO(zeph): syncmap[string]channel.Channel
15-
Brain *sqlbrain.Brain
15+
Brain *kvbrain.Brain
1616
Privacy *privacy.List
1717
}

config.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/BurntSushi/toml"
14+
"github.com/dgraph-io/badger/v4"
15+
"github.com/dgraph-io/badger/v4/options"
1416
"gitlab.com/zephyrtronium/pick"
1517
"gitlab.com/zephyrtronium/sq"
1618
"gitlab.com/zephyrtronium/tmi"
@@ -20,7 +22,7 @@ import (
2022
"golang.org/x/time/rate"
2123

2224
"github.com/zephyrtronium/robot/auth"
23-
"github.com/zephyrtronium/robot/brain/sqlbrain"
25+
"github.com/zephyrtronium/robot/brain/kvbrain"
2426
"github.com/zephyrtronium/robot/channel"
2527
"github.com/zephyrtronium/robot/message"
2628
"github.com/zephyrtronium/robot/privacy"
@@ -60,12 +62,9 @@ func (robo *Robot) SetSecrets(file string) error {
6062

6163
// SetSources opens the brain and privacy list wrappers around the respective
6264
// databases. Use [loadDBs] to open the databases themselves from DSNs.
63-
func (robo *Robot) SetSources(ctx context.Context, brain, priv *sq.DB) error {
65+
func (robo *Robot) SetSources(ctx context.Context, brain *badger.DB, priv *sq.DB) error {
6466
var err error
65-
robo.brain, err = sqlbrain.Open(ctx, brain)
66-
if err != nil {
67-
return fmt.Errorf("couldn't open brain: %w", err)
68-
}
67+
robo.brain = kvbrain.New(brain)
6968
robo.privacy, err = privacy.Open(ctx, priv)
7069
if err != nil {
7170
return fmt.Errorf("couldn't open privacy list: %w", err)
@@ -154,19 +153,16 @@ func (robo *Robot) SetTwitchChannels(ctx context.Context, global Global, channel
154153
return nil
155154
}
156155

157-
func loadDBs(ctx context.Context, cfg DBCfg) (brain, priv *sq.DB, err error) {
158-
brain, err = sq.Open("sqlite3", cfg.Brain)
156+
func loadDBs(ctx context.Context, cfg DBCfg) (brain *badger.DB, priv *sq.DB, err error) {
157+
opts := badger.DefaultOptions(cfg.Brain)
158+
// TODO(zeph): logger?
159+
opts = opts.WithLogger(nil)
160+
opts = opts.WithCompression(options.None)
161+
opts = opts.WithBloomFalsePositive(0)
162+
brain, err = badger.Open(opts.FromSuperFlag(cfg.Brainflag))
159163
if err != nil {
160164
return nil, nil, fmt.Errorf("couldn't open brain db: %w", err)
161165
}
162-
if err := brain.Ping(ctx); err != nil {
163-
return nil, nil, fmt.Errorf("couldn't connect to brain db: %w", err)
164-
}
165-
166-
if cfg.Privacy == cfg.Brain {
167-
priv = brain
168-
return brain, priv, nil
169-
}
170166

171167
priv, err = sq.Open("sqlite3", cfg.Privacy)
172168
if err != nil {
@@ -331,8 +327,9 @@ type ClientCfg struct {
331327

332328
// DBCfg is the configuration of databases.
333329
type DBCfg struct {
334-
Brain string `toml:"brain"`
335-
Privacy string `toml:"privacy"`
330+
Brain string `toml:"brain"`
331+
Brainflag string `toml:"brainflag"`
332+
Privacy string `toml:"privacy"`
336333
}
337334

338335
// Rate is a rate limit configuration.

example.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ name = 'zephyrtronium'
1616
# contact is a description of how to contact the owner.
1717
contact = '/w zephyrtronium'
1818

19-
# db is a table of databases used by the bot. Each one may be the same as or
20-
# different from any other. Each value is a connection string for SQLite3.
19+
# db is a table of databases used by the bot.
2120
[db]
22-
brain = 'file:$ROBOT_SQLITE'
21+
# brain is the directory in which learned knowledge is stored.
22+
brain = '$ROBOT_KNOWLEDGE'
23+
# brainflag configures the brain database as a Badger "superflag" string.
24+
brainflag = ''
25+
# privacy is an SQLite3 connection string for the database where privacy
26+
# information is stored.
2327
privacy = 'file:$ROBOT_SQLITE'
2428

2529
# global includes chat settings that apply to all channels.

main.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
"github.com/urfave/cli/v3"
14-
"github.com/zephyrtronium/robot/brain/sqlbrain"
1514
"github.com/zephyrtronium/robot/privacy"
1615
)
1716

@@ -27,7 +26,6 @@ var app = cli.Command{
2726
Action: cliInit,
2827
Flags: []cli.Flag{
2928
&flagConfig,
30-
&flagOrder,
3129
},
3230
},
3331
{
@@ -64,7 +62,6 @@ func main() {
6462

6563
func cliInit(ctx context.Context, cmd *cli.Command) error {
6664
slog.SetDefault(loggerFromFlags(cmd))
67-
order := int(cmd.Int("order"))
6865
r, err := os.Open(cmd.String("config"))
6966
if err != nil {
7067
return fmt.Errorf("couldn't open config file: %w", err)
@@ -73,13 +70,10 @@ func cliInit(ctx context.Context, cmd *cli.Command) error {
7370
if err != nil {
7471
return fmt.Errorf("couldn't load config: %w", err)
7572
}
76-
brain, priv, err := loadDBs(ctx, cfg.DB)
73+
_, priv, err := loadDBs(ctx, cfg.DB)
7774
if err != nil {
7875
return err
7976
}
80-
if err := sqlbrain.Create(ctx, brain, order); err != nil {
81-
return fmt.Errorf("couldn't initialize brain: %w", err)
82-
}
8377
if err := privacy.Init(ctx, priv); err != nil {
8478
return fmt.Errorf("couldn't initialize privacy list: %w", err)
8579
}
@@ -162,18 +156,6 @@ var (
162156
}
163157
},
164158
}
165-
166-
flagOrder = cli.IntFlag{
167-
Name: "order",
168-
Required: true,
169-
Usage: "Prefix length for Markov chains",
170-
Action: func(ctx context.Context, cmd *cli.Command, i int64) error {
171-
if i <= 0 {
172-
return errors.New("order must be positive")
173-
}
174-
return nil
175-
},
176-
}
177159
)
178160

179161
func loggerFromFlags(cmd *cli.Command) *slog.Logger {

robot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import (
1818
"golang.org/x/time/rate"
1919

2020
"github.com/zephyrtronium/robot/auth"
21-
"github.com/zephyrtronium/robot/brain/sqlbrain"
21+
"github.com/zephyrtronium/robot/brain/kvbrain"
2222
"github.com/zephyrtronium/robot/channel"
2323
"github.com/zephyrtronium/robot/privacy"
2424
)
2525

2626
// Robot is the overall configuration for the bot.
2727
type Robot struct {
2828
// brain is the brain.
29-
brain *sqlbrain.Brain
29+
brain *kvbrain.Brain
3030
// privacy is the privacy.
3131
privacy *privacy.List
3232
// channels are the channels.
@@ -164,7 +164,7 @@ func validateTwitch(ctx context.Context, tok *oauth2.Token) error {
164164
if err := json.Unmarshal(body, &s); err != nil {
165165
return fmt.Errorf("couldn't unmarshal token validation response: %w", err)
166166
}
167-
slog.InfoContext(ctx, "token validation", slog.String("token", tok.AccessToken), slog.Any("result", s))
167+
slog.InfoContext(ctx, "token validation", slog.Any("result", s))
168168
if resp.StatusCode == http.StatusUnauthorized {
169169
// Token expired or otherwise invalid. We need a refresh.
170170
return fmt.Errorf("token validation failed: %s (%w)", s.Message, errNeedRefresh)

0 commit comments

Comments
 (0)