use remote libsql
This commit is contained in:
parent
ccc56508a6
commit
3b04dbe330
10
go.mod
10
go.mod
|
@ -4,6 +4,11 @@ go 1.22.0
|
|||
|
||||
require github.com/foxcpp/maddy v0.7.1
|
||||
|
||||
require (
|
||||
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
|
||||
nhooyr.io/websocket v1.8.10 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 // indirect
|
||||
github.com/emersion/go-imap v1.2.2-0.20220928192137-6fac715be9cf // indirect
|
||||
|
@ -11,10 +16,11 @@ require (
|
|||
github.com/emersion/go-msgauth v0.6.8 // indirect
|
||||
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
|
||||
github.com/emersion/go-smtp v0.20.2-0.20240121112028-434ddca4792e // indirect
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 // indirect
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1
|
||||
github.com/tursodatabase/libsql-client-go v0.0.0-20240401075953-8e79a99d828a
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.26.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
||||
|
|
10
go.sum
10
go.sum
|
@ -1,5 +1,7 @@
|
|||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h1:goHVqTbFX3AIo0tzGr14pgfAW2ZfPChKO21Z9MGf/gk=
|
||||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
|
||||
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
|
||||
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emersion/go-imap v1.2.2-0.20220928192137-6fac715be9cf h1:EUDVFh7Cpdv9jClkevx7T0++JQqND+TP5UlCmZAm3YA=
|
||||
|
@ -21,6 +23,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
|||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 h1:6PfEMwfInASh9hkN83aR0j4W/eKaAZt/AURtXAXlas0=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475/go.mod h1:20nXSmcf0nAscrzqsXeC2/tA3KkV2eCiJqYuyAgl+ss=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 h1:JLvn7D+wXjH9g4Jsjo+VqmzTUpl/LX7vfr6VOfSWTdM=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06/go.mod h1:FUkZ5OHjlGPjnM2UyGJz9TypXQFgYqw6AFNO1UiROTM=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
@ -29,6 +33,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
|
|||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1 h1:lQwP++jvwcQiPqqIXIvabCIvfh8bzibpjYvT4s0jWmA=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1/go.mod h1:sb520Yr+GHBsfL43FQgQ+rLFfuJkItgRWlTgbIQHVxA=
|
||||
github.com/tursodatabase/libsql-client-go v0.0.0-20240401075953-8e79a99d828a h1:LMz5RmEKz1epPZgUO3MtA5/X9Peudqm4rUoteSjLkho=
|
||||
github.com/tursodatabase/libsql-client-go v0.0.0-20240401075953-8e79a99d828a/go.mod h1:2Fu26tjM011BLeR5+jwTfs6DX/fNMEWV/3CBZvggrA4=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
|
||||
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
|
||||
|
@ -40,6 +46,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
|
|||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
|
@ -76,3 +84,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
|
||||
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||
|
|
|
@ -6,16 +6,15 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/foxcpp/maddy/framework/config"
|
||||
"github.com/foxcpp/maddy/framework/module"
|
||||
|
||||
"github.com/tursodatabase/go-libsql"
|
||||
_ "github.com/tursodatabase/libsql-client-go/libsql"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
type table struct {
|
||||
modName string
|
||||
instName string
|
||||
|
||||
|
@ -31,31 +30,28 @@ type Table struct {
|
|||
}
|
||||
|
||||
func NewTable(modName, instName string, _, _ []string) (module.Module, error) {
|
||||
return &Table{
|
||||
return &table{
|
||||
modName: modName,
|
||||
instName: instName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Table) Name() string {
|
||||
func (s *table) Name() string {
|
||||
return s.modName
|
||||
}
|
||||
|
||||
func (s *Table) InstanceName() string {
|
||||
func (s *table) InstanceName() string {
|
||||
return s.instName
|
||||
}
|
||||
|
||||
func (s *Table) Init(cfg *config.Map) error {
|
||||
func (s *table) Init(cfg *config.Map) error {
|
||||
var (
|
||||
initQueries []string
|
||||
primaryUrl string
|
||||
authToken string
|
||||
dbName = "local.db"
|
||||
)
|
||||
cfg.StringList("init", false, false, nil, &initQueries)
|
||||
|
||||
cfg.String("url", false, true, "", &primaryUrl)
|
||||
cfg.String("token", false, true, "", &authToken)
|
||||
|
||||
cfg.String("lookup", false, true, "", &s.lookup)
|
||||
cfg.String("add", false, false, "", &s.add)
|
||||
|
@ -68,38 +64,9 @@ func (s *Table) Init(cfg *config.Map) error {
|
|||
}
|
||||
|
||||
var err error
|
||||
s.dir, err = os.MkdirTemp("", "libsql-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating temporary directory: %w", err)
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(s.dir, dbName)
|
||||
|
||||
syncInterval := time.Minute
|
||||
|
||||
s.connector, err = libsql.NewEmbeddedReplicaConnector(dbPath, primaryUrl,
|
||||
libsql.WithAuthToken(authToken),
|
||||
libsql.WithSyncInterval(syncInterval),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating connector: %w", err)
|
||||
}
|
||||
|
||||
s.sql = sql.OpenDB(s.connector)
|
||||
s.sql, err = sql.Open("libsql", primaryUrl)
|
||||
|
||||
if len(initQueries) > 0 {
|
||||
// bldr := ws4.NewRequestBuilder()
|
||||
// for _, init := range initQueries {
|
||||
// bldr.AddStatement(init)
|
||||
// }
|
||||
// req, err := bldr.Build()
|
||||
// if err != nil {
|
||||
// return config.NodeErr(cfg.Block, "failed to init db: %v", err)
|
||||
// }
|
||||
// _, _, err = db.Send(req)
|
||||
// if err != nil {
|
||||
// return config.NodeErr(cfg.Block, "failed to init db: %v", err)
|
||||
// }
|
||||
tx, err := s.sql.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -118,17 +85,17 @@ func (s *Table) Init(cfg *config.Map) error {
|
|||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Table) Close() error {
|
||||
func (s *table) Close() error {
|
||||
defer os.RemoveAll(s.dir)
|
||||
defer s.connector.Close()
|
||||
|
||||
return s.sql.Close()
|
||||
}
|
||||
|
||||
func (s *Table) Lookup(ctx context.Context, val string) (value string, ok bool, err error) {
|
||||
func (s *table) Lookup(ctx context.Context, val string) (value string, ok bool, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: lookup: %w", s.modName, err)
|
||||
|
@ -141,7 +108,7 @@ func (s *Table) Lookup(ctx context.Context, val string) (value string, ok bool,
|
|||
return value, err == nil, err
|
||||
}
|
||||
|
||||
func (s *Table) LookupMulti(ctx context.Context, val string) (lis []string, err error) {
|
||||
func (s *table) LookupMulti(ctx context.Context, val string) (lis []string, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: lookupMulti: %w", s.modName, err)
|
||||
|
@ -168,7 +135,7 @@ func (s *Table) LookupMulti(ctx context.Context, val string) (lis []string, err
|
|||
return
|
||||
}
|
||||
|
||||
func (s *Table) Keys() (lis []string, err error) {
|
||||
func (s *table) Keys() (lis []string, err error) {
|
||||
if s.list == "" {
|
||||
return nil, fmt.Errorf("%s: table is not mutable (no 'list' query)", s.modName)
|
||||
}
|
||||
|
@ -198,7 +165,7 @@ func (s *Table) Keys() (lis []string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (s *Table) RemoveKey(k string) (err error) {
|
||||
func (s *table) RemoveKey(k string) (err error) {
|
||||
if s.del == "" {
|
||||
return fmt.Errorf("%s: table is not mutable (no 'del' query)", s.modName)
|
||||
}
|
||||
|
@ -210,7 +177,7 @@ func (s *Table) RemoveKey(k string) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Table) SetKey(k, v string) (err error) {
|
||||
func (s *table) SetKey(k, v string) (err error) {
|
||||
if s.set == "" {
|
||||
return fmt.Errorf("%s: table is not mutable (no 'set' query)", s.modName)
|
||||
}
|
||||
|
@ -222,13 +189,13 @@ func (s *Table) SetKey(k, v string) (err error) {
|
|||
if err != nil {
|
||||
return fmt.Errorf("%s: set %s: %w", s.modName, k, err)
|
||||
}
|
||||
var n int64
|
||||
if n, err = res.RowsAffected(); err != nil && n == 0 {
|
||||
_, err = s.sql.ExecContext(context.TODO(), s.add, k, v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: add %s: %w", s.modName, k, err)
|
||||
}
|
||||
var n int64
|
||||
if n, err = res.RowsAffected(); err != nil && n == 0 {
|
||||
_, err = s.sql.ExecContext(context.TODO(), s.add, k, v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: add %s: %w", s.modName, k, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user