forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
287 lines (269 loc) · 6.74 KB
/
database.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"context"
"database/sql"
"errors"
"log"
"os"
"strings"
"sync"
"github.com/dgraph-io/ristretto"
"github.com/google/uuid"
sqlite "github.com/mattn/go-sqlite3"
"github.com/schollz/sqlite3dump"
"golang.org/x/sync/singleflight"
)
type database struct {
// Basic things
db *sql.DB // database
em sync.Mutex // command execution (insert, update, delete ...)
sg singleflight.Group // singleflight group for prepared statements
psc *ristretto.Cache // prepared statement cache
// Other things
pc singleflight.Group // persistant cache
pcm sync.Mutex // post creation
sp singleflight.Group // singleflight group for short path requests
spc *ristretto.Cache // shortpath cache
debug bool
}
func (a *goBlog) initDatabase(logging bool) (err error) {
if a.db != nil && a.db.db != nil {
return
}
if logging {
log.Println("Initialize database...")
}
// Setup db
db, err := a.openDatabase(a.cfg.Db.File, logging)
if err != nil {
return err
}
// Create appDB
a.db = db
a.shutdown.Add(func() {
if err := db.close(); err != nil {
log.Printf("Failed to close database: %v", err)
} else {
log.Println("Closed database")
}
})
if a.cfg.Db.DumpFile != "" {
a.hourlyHooks = append(a.hourlyHooks, func() {
db.dump(a.cfg.Db.DumpFile)
})
db.dump(a.cfg.Db.DumpFile)
}
if logging {
log.Println("Initialized database")
}
return nil
}
func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
// Register driver
dbDriverName := "goblog_db_" + uuid.NewString()
sql.Register(dbDriverName, &sqlite.SQLiteDriver{
ConnectHook: func(c *sqlite.SQLiteConn) error {
// Register functions
for n, f := range map[string]any{
"mdtext": a.renderText,
"tolocal": toLocalSafe,
"toutc": toUTCSafe,
"wordcount": wordCount,
"charcount": charCount,
"urlize": urlize,
"lowerx": strings.ToLower,
"lowerunescaped": lowerUnescapedPath,
} {
if err := c.RegisterFunc(n, f, true); err != nil {
return err
}
}
return nil
},
})
// Open db
db, err := sql.Open(dbDriverName, file+"?mode=rwc&_journal=WAL&_timeout=100&cache=shared&_fk=1")
if err != nil {
return nil, err
}
numConns := 5
db.SetMaxOpenConns(numConns)
db.SetMaxIdleConns(numConns)
err = db.Ping()
if err != nil {
return nil, err
}
// Check available SQLite features
rows, err := db.Query("pragma compile_options")
if err != nil {
return nil, err
}
cos := map[string]struct{}{}
var co string
for rows.Next() {
err = rows.Scan(&co)
if err != nil {
return nil, err
}
cos[co] = struct{}{}
}
if _, ok := cos["ENABLE_FTS5"]; !ok {
return nil, errors.New("sqlite not compiled with FTS5")
}
// Migrate DB
err = migrateDb(db, logging)
if err != nil {
return nil, err
}
// Debug
debug := false
if c := a.cfg.Db; c != nil && c.Debug {
debug = true
}
psc, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1000,
MaxCost: 100,
BufferItems: 64,
IgnoreInternalCost: true,
})
if err != nil {
return nil, err
}
spc, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 5000,
MaxCost: 500,
BufferItems: 64,
IgnoreInternalCost: true,
})
if err != nil {
return nil, err
}
return &database{
db: db,
debug: debug,
psc: psc,
spc: spc,
}, nil
}
// Main features
func (db *database) dump(file string) {
if db == nil || db.db == nil {
return
}
// Lock execution
db.em.Lock()
defer db.em.Unlock()
// Dump database
f, err := os.Create(file)
if err != nil {
log.Println("Error while dump db:", err.Error())
return
}
if err = sqlite3dump.DumpDB(db.db, f, sqlite3dump.WithTransaction(true)); err != nil {
log.Println("Error while dump db:", err.Error())
}
}
func (db *database) close() error {
if db == nil || db.db == nil {
return nil
}
return db.db.Close()
}
func (db *database) prepare(query string, args ...any) (*sql.Stmt, []any, error) {
if db == nil || db.db == nil {
return nil, nil, errors.New("database not initialized")
}
if len(args) > 0 && args[0] == dbNoCache {
return nil, args[1:], nil
}
stmt, err, _ := db.sg.Do(query, func() (any, error) {
// Look if statement already exists
st, ok := db.psc.Get(query)
if ok {
return st, nil
}
// ... otherwise prepare ...
st, err := db.db.Prepare(query)
if err != nil {
return nil, err
}
// ... and store it
db.psc.Set(query, st, 1)
return st, nil
})
if err != nil {
if db.debug {
log.Printf(`Failed to prepare query "%s": %s`, query, err.Error())
}
return nil, args, err
}
return stmt.(*sql.Stmt), args, nil
}
const dbNoCache = "nocache"
func (db *database) Exec(query string, args ...any) (sql.Result, error) {
return db.ExecContext(context.Background(), query, args...)
}
func (db *database) ExecContext(c context.Context, query string, args ...any) (sql.Result, error) {
if db == nil || db.db == nil {
return nil, errors.New("database not initialized")
}
// Maybe prepare
st, args, _ := db.prepare(query, args...)
// Lock execution
db.em.Lock()
defer db.em.Unlock()
// Prepare context, call hook
ctx := db.dbBefore(c, query, args...)
defer db.dbAfter(ctx, query, args...)
// Execute
if st != nil {
return st.ExecContext(ctx, args...)
}
return db.db.ExecContext(ctx, query, args...)
}
func (db *database) Query(query string, args ...any) (*sql.Rows, error) {
return db.QueryContext(context.Background(), query, args...)
}
func (db *database) QueryContext(c context.Context, query string, args ...any) (rows *sql.Rows, err error) {
if db == nil || db.db == nil {
return nil, errors.New("database not initialized")
}
// Maybe prepare
st, args, _ := db.prepare(query, args...)
// Prepare context, call hook
ctx := db.dbBefore(c, query, args...)
// Query
if st != nil {
rows, err = st.QueryContext(ctx, args...)
} else {
rows, err = db.db.QueryContext(ctx, query, args...)
}
// Call hook
db.dbAfter(ctx, query, args...)
return
}
func (db *database) QueryRow(query string, args ...any) (*sql.Row, error) {
return db.QueryRowContext(context.Background(), query, args...)
}
func (db *database) QueryRowContext(c context.Context, query string, args ...any) (row *sql.Row, err error) {
if db == nil || db.db == nil {
return nil, errors.New("database not initialized")
}
// Maybe prepare
st, args, _ := db.prepare(query, args...)
// Prepare context, call hook
ctx := db.dbBefore(c, query, args...)
// Query
if st != nil {
row = st.QueryRowContext(ctx, args...)
} else {
row = db.db.QueryRowContext(ctx, query, args...)
}
// Call hook
db.dbAfter(ctx, query, args...)
return
}
// Other things
func (d *database) rebuildFTSIndex() {
_, _ = d.Exec("insert into posts_fts(posts_fts) values ('rebuild')")
}