-
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 b8e51e4
Showing
11 changed files
with
129 additions
and
53 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,74 @@ | ||
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() | ||
|
||
pragmas := 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) | ||
|
||
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())) | ||
|
||
if _, err = readConn.ExecContext(ctx, pragmas); err != nil { | ||
return nil, nil, fmt.Errorf("configure db conn: %w", err) | ||
} | ||
|
||
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, pragmas); err != nil { | ||
return nil, nil, fmt.Errorf("configure db conn: %w", err) | ||
} | ||
|
||
if _, err = writeConn.ExecContext(ctx, Schema); err != nil { | ||
return nil, nil, fmt.Errorf("init db: %w", err) | ||
} | ||
|
||
return readConn, writeConn, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.