-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize access to the sqlite database
- Loading branch information
1 parent
cd367a4
commit 63c7c96
Showing
8 changed files
with
120 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"net/url" | ||
"runtime" | ||
) | ||
|
||
type OpenOptions struct { | ||
CacheSize int | ||
Params map[string]string | ||
} | ||
|
||
func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *sql.DB, wdb *sql.DB, err error) { | ||
uri := &url.URL{ | ||
Scheme: "file", | ||
Opaque: dbFile, | ||
} | ||
query := uri.Query() | ||
if opts.Params != nil { | ||
for k, v := range opts.Params { | ||
query.Set(k, v) | ||
} | ||
} | ||
query.Set("_txlock", "immediate") | ||
uri.RawQuery = query.Encode() | ||
|
||
readConn, err := sql.Open("sqlite3", uri.String()) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer func() { | ||
if err != nil { | ||
readConn.Close() | ||
} | ||
}() | ||
readConn.SetMaxOpenConns(max(4, runtime.NumCPU())) | ||
|
||
writeConn, err := sql.Open("sqlite3", uri.String()) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer func() { | ||
if err != nil { | ||
writeConn.Close() | ||
} | ||
}() | ||
writeConn.SetMaxOpenConns(1) | ||
|
||
if _, err = writeConn.ExecContext(ctx, fmt.Sprintf(` | ||
PRAGMA journal_mode = WAL; | ||
PRAGMA busy_timeout = 5000; | ||
PRAGMA synchronous = NORMAL; | ||
PRAGMA cache_size = -%d; | ||
PRAGMA foreign_keys = true; | ||
PRAGMA temp_store = memory; | ||
`, opts.CacheSize)); err != nil { | ||
return nil, nil, fmt.Errorf("configure db: %w", err) | ||
} | ||
|
||
if _, err = writeConn.ExecContext(ctx, Schema); err != nil { | ||
return nil, nil, fmt.Errorf("init db: %w", err) | ||
} | ||
|
||
return readConn, writeConn, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters