-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosqlite_grp.go
209 lines (176 loc) · 4.54 KB
/
gosqlite_grp.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// The author disclaims copyright to this source code
// as it is dedicated to the public domain.
// For more information, please refer to <https://unlicense.org>.
package gosqlite
import (
"errors"
"fmt"
"log"
"strings"
"time"
)
func (g *DBGroup) bgProc() {
// Ping the databases. Remove the ones that
// do not respond.
for {
for i := 0; i < len(g.OpenDatabases); i++ {
err := g.Ping(g.OpenDatabases[i])
msgTxt := "ok"
if err != nil {
msgTxt = err.Error()
}
if g.Verbose {
log.Println("ping:", g.OpenDatabases[i].Name, msgTxt)
}
if err != nil {
// remove from the list
newArr := make([]*DB, 0)
for j := 0; j < len(g.OpenDatabases); j++ {
if g.OpenDatabases[j].UniqueName != g.OpenDatabases[i].UniqueName {
newArr = append(newArr, g.OpenDatabases[j])
}
}
g.OpenDatabases = newArr
break
} else {
db, err := g.Get(g.OpenDatabases[i].FilePath())
if err != nil {
if g.Verbose {
log.Println(g.OpenDatabases[i].Name, "=>", err)
}
} else {
// optimize
_, err := db[0].Execute("PRAGMA optimize;")
if err != nil {
if g.Verbose {
log.Println(g.OpenDatabases[i].Name, "=>", err)
}
}
// vacuum the db
m, err := db[0].ExecuteScalare("PRAGMA freelist_count;")
if err != nil {
if g.Verbose {
log.Println(g.OpenDatabases[i].Name, "=>", err)
}
} else {
if m != nil && m.(int64) > 1500 && !g.OpenDatabases[i].Busy() {
if g.Verbose {
log.Println("<<< VACUUM >>>", g.OpenDatabases[i].Name)
}
err = g.OpenDatabases[i].Vacuum()
if err != nil && g.Verbose {
log.Println(err)
} else {
g.OpenDatabases[i].ShrinkMemory()
}
}
}
}
}
time.Sleep(time.Second)
}
time.Sleep(193 * time.Second)
}
}
// Base exposes all properties of DBGroup
// to the caller (methods and variables).
func (m *DBGroup) Base() *DBGroup {
return m
}
func initGrouper() {
if len(DBGrp.Base().OpenDatabases) > 0 {
return
}
DBGrp = &DBGroup{}
go DBGrp.bgProc()
}
func (m *DBGroup) Count() int {
return len(m.OpenDatabases)
}
func (m *DBGroup) Get(srchTxt string) ([]*DB, error) {
var arryDB []*DB
sLower := strings.ToLower(srchTxt)
for i := 0; i < m.Count(); i++ {
if m.OpenDatabases[i].UniqueName == srchTxt {
arryDB = append(arryDB, m.OpenDatabases[i])
} else if strings.ToLower(m.OpenDatabases[i].FilePath()) == sLower {
arryDB = append(arryDB, m.OpenDatabases[i])
} else if strings.Contains(strings.ToLower(m.OpenDatabases[i].FilePath()), sLower) {
arryDB = append(arryDB, m.OpenDatabases[i])
}
}
if len(arryDB) == 0 {
// try to open the database, if the file exists on disk.
fp := srchTxt
if fileOrDirExists(srchTxt) {
// db, err := Open(fp)
db, err := Open(fp,
/*"PRAGMA main.journal_mode = DELETE",*/
"PRAGMA main.secure_delete = ON")
if err != nil {
/* empty array */
arryDB = append(arryDB, &DB{})
} else {
/* opened successfully */
//db.JournalMode = "DELETE"
arryDB = append(arryDB, db)
m.Add(db)
return arryDB, err
}
} else {
/* return an empty db, so that the caller does not
get null-pointer panic */
arryDB = append(arryDB, &DB{})
return arryDB, errors.New("no such database in the DBGroup")
}
}
/* db already existed in the grp*/
return arryDB, nil
}
func (m *DBGroup) Ping(db *DB) error {
if db.Ping() == 0 {
return nil
}
return errors.New("bad connection")
}
func (g *DBGroup) Add(db *DB) {
if g == nil || db == nil {
return
}
for i := 0; i < len(g.OpenDatabases); i++ {
if g.OpenDatabases[i].UniqueName == db.UniqueName {
return
}
}
g.OpenDatabases = append(g.OpenDatabases, db)
//go db.daemon()
}
// Remove closes the database and removes it from
// the array of OpenDatabase.
func (g *DBGroup) Remove(db *DB) {
if db == nil {
return
}
indx := -1
for i := 0; i < len(g.OpenDatabases); i++ {
if g.OpenDatabases[i].UniqueName == db.UniqueName {
indx = i
break
}
}
if indx < 0 {
return
}
err := db.Close()
if err != nil {
// bad parameter or other API misuse: means the database is already closed.
if !strings.Contains(err.Error(), "bad parameter or other API misuse") &&
!strings.Contains(err.Error(), "not an error") {
fmt.Println("DBGroups.Remove() =>", err)
}
}
db = nil
// remove from array of OpenDatabases
g.OpenDatabases[len(g.OpenDatabases)-1], g.OpenDatabases[indx] = g.OpenDatabases[indx], g.OpenDatabases[len(g.OpenDatabases)-1]
g.OpenDatabases = g.OpenDatabases[:len(g.OpenDatabases)-1]
}