sqlite: added pragma support for wal

This commit is contained in:
Tigor Hutasuhut 2024-08-27 09:10:58 +07:00
parent 9e1a192f93
commit 97341ec698
9 changed files with 106 additions and 1 deletions

2
go/.gitignore vendored
View file

@ -1,4 +1,6 @@
models/
*.db
*.db-shm
*.db-wal
gen/
.env

View file

@ -36,6 +36,8 @@ func init() {
flags.Float32(key, v, desc)
case float64:
flags.Float64(key, v, desc)
case []string:
flags.StringSlice(key, v, desc)
default:
flags.String(key, fmt.Sprintf("%v", v), desc)
}
@ -70,6 +72,7 @@ func main() {
defer cancel()
ctx = config.WithContext(ctx, cfg)
if err := Cmd.ExecuteContext(ctx); err != nil {
slog.ErrorContext(ctx, err.Error(), "error", err)
os.Exit(1)
}
}

View file

@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
"connectrpc.com/connect"
@ -76,6 +77,25 @@ var Cmd = &cobra.Command{
"dsn", dsn,
)
}
for i, pragma := range cfg.Strings("db.pragma") {
split := strings.SplitN(pragma, "=", 2)
if len(split) < 2 {
return errs.Failw("failed to parse db pragma. expected key=value format",
"index", i+1,
"pragma", pragma,
)
}
key, value := split[0], split[1]
query := fmt.Sprintf("pragma %s = %s", key, value)
_, err = sqldb.Exec(query)
if err != nil {
return errs.Wrapw(err, "failed to execute pragma",
"index", i+1,
"pragma", pragma,
"query", query,
)
}
}
cleanups = append(cleanups, sqldb.Close)
if err := otelsql.RegisterDBStatsMetrics(sqldb, otelsql.WithAttributes(semconv.DBSystemSqlite)); err != nil {
return errs.Wrapw(

View file

@ -35,6 +35,7 @@ var DefaultConfig = Entries{
{"db.driver", "sqlite3", "Database driver", false},
{"db.path", path.Join(xdg.Home, ".local", "share", "bluemage", "data.db"), "Database path", false},
{"db.pragma", []string{"journal_mode=wal", "synchronous=normal", "temp_store=memory", "mmap_size=30000000000"}, `Database pragma options key=value comma separated values (or repeat). Example: '--db.pragma "journal_mode=wal,synchronous=normal"'`, false},
{"pubsub.db.path", path.Join(xdg.Home, ".local", "share", "bluemage", "pubsub.db"), "PubSub database path", false},
{"pubsub.db.timeout", "5s", "PubSub database timeout", false},

View file

@ -44,5 +44,5 @@ func (SQLLogger) Log(ctx context.Context, level sqldblogger.Level, msg string, d
qc.Args = data["args"]
return
}
slog.ErrorContext(ctx, strings.ReplaceAll(msg, "\n", " "), "lvl", level.String(), "data", data)
slog.DebugContext(ctx, strings.ReplaceAll(msg, "\n", " "), "lvl", level.String(), "data", data)
}

21
go/server/log_handlers.go Normal file
View file

@ -0,0 +1,21 @@
package server
import (
"context"
"connectrpc.com/connect"
logsv1 "github.com/tigorlazuardi/bluemage/go/gen/proto/logs/v1"
"github.com/tigorlazuardi/bluemage/go/gen/proto/logs/v1/logsv1connect"
)
type LogHandlers struct {
logsv1connect.UnimplementedLogServiceHandler
}
func (lo *LogHandlers) Log(ctx context.Context, request *connect.Request[logsv1.LogRequest]) (*connect.Response[logsv1.LogResponse], error) {
panic("not implemented") // TODO: Implement
}
func (lo *LogHandlers) Tail(ctx context.Context, request *connect.Request[logsv1.TailRequest], response *connect.ServerStream[logsv1.TailResponse]) error {
panic("not implemented") // TODO: Implement
}

View file

@ -4,3 +4,6 @@ deps:
- name: buf.build/bufbuild/protovalidate
commit: a6c49f84cc0f4e038680d390392e2ab0
digest: b5:e968392e88ff7915adcbd1635d670b45bff8836ec2415d81fc559ca5470a695dbdc30030bad8bc5764647c731079e9e7bba0023ea25c4e4a1672a7d2561d4a19
- name: buf.build/protocolbuffers/wellknowntypes
commit: f17e05fe4a764a3482b8e033daec742e
digest: b5:405a06d8a554f01c830c643879f54feffe2087a6927643dc9418403653ff4033227a2ce9b2b19b8ad350a611ef6576b4df109c96e5dfb6164191eb73d779fb21

View file

@ -1,3 +1,4 @@
version: v2
deps:
- buf.build/bufbuild/protovalidate
- buf.build/protocolbuffers/wellknowntypes:v21.12

View file

@ -0,0 +1,54 @@
syntax = "proto3";
package logs.v1;
service LogService {
rpc Log(LogRequest) returns (LogResponse) {}
rpc Tail(TailRequest) returns (stream TailResponse) {}
}
message LogRequest {
Level level = 1;
string search = 2;
string trace_id = 3;
int64 from = 4;
int64 to = 5;
int64 limit = 6;
}
message LogResponse {
repeated Log logs = 1;
}
message Log {
int64 timestamp = 1;
Level level = 2;
string message = 3;
Origin origin = 4;
string trace_id = 5;
string span_id = 6;
optional bytes details = 7;
optional bytes error = 8;
}
enum Level {
LEVEL_UNSPECIFIED = 0;
LEVEL_DEBUG = 1;
LEVEL_INFO = 2;
LEVEL_WARN = 3;
LEVEL_ERROR = 4;
}
message Origin {
string file = 1;
int64 line = 2;
string function = 3;
}
message TailRequest {
Level level = 1;
}
message TailResponse {
Log log = 1;
}