-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
executable file
·128 lines (108 loc) · 3.61 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package sqlitehench
import (
"fmt"
"strings"
)
func NewDBAccess(d DBAccess) *DBAccess {
if d.MaxIdleConns < 1 {
d.MaxIdleConns = 1
}
// Setting d.MaxOpenConns to 1, reduces chances of database
// getting locked.
if d.MaxOpenConns < 1 {
d.MaxOpenConns = 1
}
if len(d.PRAGMA) == 0 {
// See https://sqlite.org/pragma.html#pragma_auto_vacuum
// The defaut for auto_vacuum is NONE, because of the following statement (above URL).
//
// "When the auto-vacuum mode is 1 or "full", the freelist pages are moved
// to the end of the database file and the database file is truncated to remove
// the freelist pages at every transaction commit. Note, however, that auto-vacuum
// only truncates the freelist pages from the file. Auto-vacuum does not defragment
// the database nor repack individual database pages the way that the VACUUM command
// does. In fact, because it moves pages aroundwithin the file, auto-vacuum can
// actually make fragmentation worse."
//
d.PRAGMA = append(d.PRAGMA, "PRAGMA auto_vacuum = NONE;")
// WAL is set as the default mode.
// See https://sqlite.org/pragma.html#pragma_journal_mode for more details.
d.PRAGMA = append(d.PRAGMA, "PRAGMA journal_mode = WAL;")
// This allows multiple writers and readers.
// https://www.sqlite.org/pragma.html#pragma_wal_autocheckpoint
d.PRAGMA = append(d.PRAGMA, "PRAGMA wal_checkpoint(PASSIVE);")
}
if d.driverName == "" {
d.driverName = "sqlite3"
}
d.PRAGMA = fixPragmaTextAndOrder(d.PRAGMA)
// Shrik databases?
for i := 0; i < len(d.PRAGMA); i++ {
if !strings.Contains(strings.ToUpper(d.PRAGMA[i]), strings.ToUpper("PRAGMA auto_vacuum")) {
d.ShrinkDatabaseFiles = true
break
}
}
if d.ShrinkDatabaseFiles {
// Start the watchlist maint to prevent the list
// from growing out of proportion.
go d.maintWatchList()
go d.shrinkAllDB()
}
// var rmt RemoteSQLite
// // RemoteSQLite exposes its entire type for
// // the caller (via Base(), so there is no need
// // to initialise here.
// d.Remote = &IRemoteSQLiteHndlr{&rmt}
return &d
}
// fixPragmaTextAndOrder edits the pragma entries:
// smicolon at the end, wall journal_mode to accompany
// with checkpoint and also some formatting mistakes.
func fixPragmaTextAndOrder(pragArry []string) []string {
if len(pragArry) == 0 {
return pragArry
}
//Fix the PRAGMA text
for i := 0; i < len(pragArry); i++ {
pragArry[i] = strings.Trim(pragArry[i], " ")
// take out extra spaces
for {
if !strings.Contains(pragArry[i], " ") {
break
}
pragArry[i] = strings.ReplaceAll(pragArry[i], " ", " ")
}
pragArry[i] = strings.ToLower(pragArry[i])
if !strings.HasSuffix(pragArry[i], ";") {
pragArry[i] = fmt.Sprintf("%s;", pragArry[i])
}
if !strings.Contains(pragArry[i], "pragma ") {
pragArry[i] = strings.ReplaceAll(pragArry[i], "pragma", "pragma ")
}
}
// if set to wal, make sure pragma wal_checkpoint(passive) is included
wallExists := false
for i := 0; i < len(pragArry); i++ {
if strings.Contains(pragArry[i], "journal_mode = wal") || strings.Contains(pragArry[i], "journal_mode=wal") {
wallExists = true
break
}
}
if wallExists {
// the wall and the checkpoint pragma statements
// have to be in consecutive order.
var s []string
s = append(s, "pragma journal_mode = wal;")
s = append(s, "pragma wal_checkpoint(passive);")
for i := 0; i < len(pragArry); i++ {
if !strings.Contains(pragArry[i], "wal_checkpoint") &&
!(strings.Contains(pragArry[i], "journal_mode = wal") || strings.Contains(pragArry[i], "journal_mode=wal")) {
s = append(s, pragArry[i])
break
}
}
pragArry = s
}
return pragArry
}