Remove locking from SQlite access

This commit is contained in:
Patrick Pacher 2022-08-02 10:13:12 +02:00
parent 912ad59b9e
commit 84b66e9949
No known key found for this signature in database
GPG key ID: E8CD2DA160925A6D

View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"sync"
"time" "time"
"zombiezen.com/go/sqlite" "zombiezen.com/go/sqlite"
@ -21,7 +20,7 @@ import (
) )
// InMemory is the "file path" to open a new in-memory database. // InMemory is the "file path" to open a new in-memory database.
const InMemory = "file:inmemdb" const InMemory = "file:inmem.db"
// Available connection types as their string representation. // Available connection types as their string representation.
const ( const (
@ -49,9 +48,6 @@ type (
Schema *orm.TableSchema Schema *orm.TableSchema
pool *puddle.Pool[*sqlite.Conn] pool *puddle.Pool[*sqlite.Conn]
l sync.Mutex
// conn *sqlite.Conn
} }
// Conn is a network connection that is stored in a SQLite database and accepted // Conn is a network connection that is stored in a SQLite database and accepted
@ -121,6 +117,7 @@ func New(path string) (*Database, error) {
sqlite.OpenWAL, sqlite.OpenWAL,
sqlite.OpenSharedCache, sqlite.OpenSharedCache,
sqlite.OpenMemory, sqlite.OpenMemory,
sqlite.OpenURI,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to open sqlite at %s: %w", path, err) return nil, fmt.Errorf("failed to open sqlite at %s: %w", path, err)
@ -197,9 +194,6 @@ func (db *Database) withConn(ctx context.Context, fn func(conn *sqlite.Conn) err
// It uses orm.RunQuery() under the hood so please refer to the orm package for // It uses orm.RunQuery() under the hood so please refer to the orm package for
// more information about available options. // more information about available options.
func (db *Database) Execute(ctx context.Context, sql string, args ...orm.QueryOption) error { func (db *Database) Execute(ctx context.Context, sql string, args ...orm.QueryOption) error {
db.l.Lock()
defer db.l.Unlock()
return db.withConn(ctx, func(conn *sqlite.Conn) error { return db.withConn(ctx, func(conn *sqlite.Conn) error {
return orm.RunQuery(ctx, conn, sql, args...) return orm.RunQuery(ctx, conn, sql, args...)
}) })
@ -264,9 +258,6 @@ func (db *Database) Cleanup(ctx context.Context, threshold time.Time) (int, erro
// as JSON to w. // as JSON to w.
// Any error aborts dumping rows and is returned. // Any error aborts dumping rows and is returned.
func (db *Database) dumpTo(ctx context.Context, w io.Writer) error { //nolint:unused func (db *Database) dumpTo(ctx context.Context, w io.Writer) error { //nolint:unused
db.l.Lock()
defer db.l.Unlock()
var conns []Conn var conns []Conn
err := db.withConn(ctx, func(conn *sqlite.Conn) error { err := db.withConn(ctx, func(conn *sqlite.Conn) error {
return sqlitex.ExecuteTransient(conn, "SELECT * FROM connections", &sqlitex.ExecOptions{ return sqlitex.ExecuteTransient(conn, "SELECT * FROM connections", &sqlitex.ExecOptions{
@ -310,9 +301,6 @@ func (db *Database) Save(ctx context.Context, conn Conn) error {
updateSets = append(updateSets, fmt.Sprintf("%s = :%s", key, key)) updateSets = append(updateSets, fmt.Sprintf("%s = :%s", key, key))
} }
db.l.Lock()
defer db.l.Unlock()
// TODO(ppacher): make sure this one can be cached to speed up inserting // TODO(ppacher): make sure this one can be cached to speed up inserting
// and save some CPU cycles for the user // and save some CPU cycles for the user
sql := fmt.Sprintf( sql := fmt.Sprintf(