2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-09 16:31:42 +00:00
|
|
|
"context"
|
2020-07-28 19:17:07 +00:00
|
|
|
"database/sql"
|
2021-05-29 11:32:00 +00:00
|
|
|
"errors"
|
2021-03-26 08:33:46 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-08-09 11:09:45 +00:00
|
|
|
"strings"
|
2021-06-18 12:32:03 +00:00
|
|
|
"sync"
|
2020-10-18 08:01:46 +00:00
|
|
|
|
2020-12-16 20:24:53 +00:00
|
|
|
sqlite "github.com/mattn/go-sqlite3"
|
2021-03-26 08:33:46 +00:00
|
|
|
"github.com/schollz/sqlite3dump"
|
2021-06-06 12:39:42 +00:00
|
|
|
"golang.org/x/sync/singleflight"
|
2020-07-28 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
type database struct {
|
2021-07-01 16:51:04 +00:00
|
|
|
// Basic things
|
|
|
|
db *sql.DB // database
|
|
|
|
em sync.Mutex // command execution (insert, update, delete ...)
|
|
|
|
sg singleflight.Group // singleflight group for prepared statements
|
|
|
|
ps sync.Map // map with prepared statements
|
|
|
|
// Other things
|
2021-08-09 16:31:42 +00:00
|
|
|
pc singleflight.Group // persistant cache
|
|
|
|
pcm sync.Mutex // post creation
|
|
|
|
sp singleflight.Group // singleflight group for short path requests
|
|
|
|
spc sync.Map // shortpath cache
|
|
|
|
debug bool
|
2021-05-29 11:32:00 +00:00
|
|
|
}
|
2020-07-28 19:17:07 +00:00
|
|
|
|
2021-07-03 10:11:57 +00:00
|
|
|
func (a *goBlog) initDatabase(logging bool) (err error) {
|
|
|
|
if logging {
|
|
|
|
log.Println("Initialize database...")
|
|
|
|
}
|
2021-05-29 11:32:00 +00:00
|
|
|
// Setup db
|
2021-07-03 10:11:57 +00:00
|
|
|
db, err := a.openDatabase(a.cfg.Db.File, logging)
|
2021-06-06 12:39:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create appDB
|
|
|
|
a.db = db
|
2021-06-17 14:34:57 +00:00
|
|
|
a.shutdown.Add(func() {
|
|
|
|
if err := db.close(); err != nil {
|
|
|
|
log.Printf("Failed to close database: %v", err)
|
|
|
|
} else {
|
|
|
|
log.Println("Closed database")
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
})
|
|
|
|
if a.cfg.Db.DumpFile != "" {
|
2021-06-18 12:32:03 +00:00
|
|
|
a.hourlyHooks = append(a.hourlyHooks, func() {
|
2021-06-06 12:39:42 +00:00
|
|
|
db.dump(a.cfg.Db.DumpFile)
|
|
|
|
})
|
|
|
|
db.dump(a.cfg.Db.DumpFile)
|
|
|
|
}
|
2021-07-03 10:11:57 +00:00
|
|
|
if logging {
|
|
|
|
log.Println("Initialized database")
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-14 14:29:22 +00:00
|
|
|
func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
|
2021-06-06 12:39:42 +00:00
|
|
|
// Register driver
|
2021-08-09 16:31:42 +00:00
|
|
|
dbDriverName := "goblog_db_" + generateRandomString(15)
|
|
|
|
sql.Register(dbDriverName, &sqlite.SQLiteDriver{
|
2020-12-16 20:24:53 +00:00
|
|
|
ConnectHook: func(c *sqlite.SQLiteConn) error {
|
2021-07-17 07:33:44 +00:00
|
|
|
// Register functions
|
2021-08-09 11:09:45 +00:00
|
|
|
for n, f := range map[string]interface{}{
|
|
|
|
"mdtext": a.renderText,
|
|
|
|
"tolocal": toLocalSafe,
|
|
|
|
"toutc": toUTCSafe,
|
|
|
|
"wordcount": wordCount,
|
|
|
|
"charcount": charCount,
|
|
|
|
"urlize": urlize,
|
|
|
|
"lowerx": strings.ToLower,
|
|
|
|
} {
|
|
|
|
if err := c.RegisterFunc(n, f, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-17 07:33:44 +00:00
|
|
|
}
|
2020-12-16 20:24:53 +00:00
|
|
|
return nil
|
|
|
|
},
|
2021-08-09 16:31:42 +00:00
|
|
|
})
|
2021-06-06 12:39:42 +00:00
|
|
|
// Open db
|
2021-08-09 16:31:42 +00:00
|
|
|
db, err := sql.Open(dbDriverName, file+"?mode=rwc&_journal_mode=WAL&_busy_timeout=100&cache=shared")
|
2020-07-28 19:17:07 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, err
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2021-07-01 16:51:04 +00:00
|
|
|
numConns := 5
|
|
|
|
db.SetMaxOpenConns(numConns)
|
|
|
|
db.SetMaxIdleConns(numConns)
|
2021-05-29 11:32:00 +00:00
|
|
|
err = db.Ping()
|
2021-03-26 08:33:46 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, err
|
2021-03-26 08:33:46 +00:00
|
|
|
}
|
2021-05-29 11:32:00 +00:00
|
|
|
// Check available SQLite features
|
|
|
|
rows, err := db.Query("pragma compile_options")
|
2021-03-26 08:33:46 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, err
|
2021-03-26 08:33:46 +00:00
|
|
|
}
|
2021-05-29 11:32:00 +00:00
|
|
|
cos := map[string]bool{}
|
|
|
|
var co string
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&co)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, err
|
2021-05-29 11:32:00 +00:00
|
|
|
}
|
|
|
|
cos[co] = true
|
|
|
|
}
|
|
|
|
if _, ok := cos["ENABLE_FTS5"]; !ok {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, errors.New("sqlite not compiled with FTS5")
|
2021-05-29 11:32:00 +00:00
|
|
|
}
|
|
|
|
// Migrate DB
|
2021-06-14 14:29:22 +00:00
|
|
|
err = migrateDb(db, logging)
|
2021-05-29 11:32:00 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
return nil, err
|
2021-03-26 08:33:46 +00:00
|
|
|
}
|
2021-08-09 16:31:42 +00:00
|
|
|
// Debug
|
|
|
|
debug := false
|
|
|
|
if c := a.cfg.Db; c != nil && c.Debug {
|
|
|
|
debug = true
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
return &database{
|
2021-08-09 16:31:42 +00:00
|
|
|
db: db,
|
|
|
|
debug: debug,
|
2021-06-06 12:39:42 +00:00
|
|
|
}, nil
|
2021-03-26 08:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
// Main features
|
|
|
|
|
|
|
|
func (db *database) dump(file string) {
|
2021-07-01 16:51:04 +00:00
|
|
|
// Lock execution
|
|
|
|
db.em.Lock()
|
|
|
|
defer db.em.Unlock()
|
|
|
|
// Dump database
|
2021-06-06 12:39:42 +00:00
|
|
|
f, err := os.Create(file)
|
2021-03-26 08:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error while dump db:", err.Error())
|
2021-04-16 18:00:38 +00:00
|
|
|
return
|
2021-03-26 08:33:46 +00:00
|
|
|
}
|
2021-06-17 14:34:57 +00:00
|
|
|
if err = sqlite3dump.DumpDB(db.db, f, sqlite3dump.WithTransaction(true)); err != nil {
|
2021-03-26 08:33:46 +00:00
|
|
|
log.Println("Error while dump db:", err.Error())
|
|
|
|
}
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) close() error {
|
2021-05-29 11:32:00 +00:00
|
|
|
return db.db.Close()
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) prepare(query string) (*sql.Stmt, error) {
|
2021-07-01 16:51:04 +00:00
|
|
|
stmt, err, _ := db.sg.Do(query, func() (interface{}, error) {
|
|
|
|
// Look if statement already exists
|
|
|
|
st, ok := db.ps.Load(query)
|
|
|
|
if ok {
|
|
|
|
return st, nil
|
2020-11-09 15:40:12 +00:00
|
|
|
}
|
2021-07-01 16:51:04 +00:00
|
|
|
// ... otherwise prepare ...
|
|
|
|
st, err := db.db.Prepare(query)
|
2020-11-09 15:40:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-01 16:51:04 +00:00
|
|
|
// ... and store it
|
|
|
|
db.ps.Store(query, st)
|
|
|
|
return st, nil
|
2020-11-09 15:40:12 +00:00
|
|
|
})
|
2020-12-09 16:25:09 +00:00
|
|
|
if err != nil {
|
2021-08-09 16:31:42 +00:00
|
|
|
if db.debug {
|
|
|
|
log.Printf(`Failed to prepare query "%s": %s`, query, err.Error())
|
|
|
|
}
|
2020-12-09 16:25:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stmt.(*sql.Stmt), nil
|
2020-11-09 15:40:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 16:51:04 +00:00
|
|
|
const dbNoCache = "nocache"
|
2020-11-09 15:40:12 +00:00
|
|
|
|
2021-07-01 16:51:04 +00:00
|
|
|
func (db *database) exec(query string, args ...interface{}) (sql.Result, error) {
|
|
|
|
// Lock execution
|
|
|
|
db.em.Lock()
|
|
|
|
defer db.em.Unlock()
|
2021-08-09 16:31:42 +00:00
|
|
|
// Check if no cache arg set
|
|
|
|
cache := true
|
2021-07-01 16:51:04 +00:00
|
|
|
if len(args) > 0 && args[0] == dbNoCache {
|
2021-08-09 16:31:42 +00:00
|
|
|
cache = false
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
// Maybe prepare
|
|
|
|
var st *sql.Stmt
|
|
|
|
if cache {
|
|
|
|
st, _ = db.prepare(query)
|
|
|
|
}
|
|
|
|
// Prepare context, call hook
|
|
|
|
ctx := db.dbBefore(context.Background(), query, args...)
|
|
|
|
defer db.dbAfter(ctx, query, args...)
|
|
|
|
// Execute
|
2021-07-01 16:51:04 +00:00
|
|
|
if st != nil {
|
2021-08-09 16:31:42 +00:00
|
|
|
return st.ExecContext(ctx, args...)
|
2021-07-01 16:51:04 +00:00
|
|
|
}
|
2021-08-09 16:31:42 +00:00
|
|
|
return db.db.ExecContext(ctx, query, args...)
|
2021-05-29 11:32:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) query(query string, args ...interface{}) (*sql.Rows, error) {
|
2021-08-09 16:31:42 +00:00
|
|
|
// Check if no cache arg set
|
|
|
|
cache := true
|
2021-07-12 17:29:32 +00:00
|
|
|
if len(args) > 0 && args[0] == dbNoCache {
|
2021-08-09 16:31:42 +00:00
|
|
|
cache = false
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
// Maybe prepare
|
|
|
|
var st *sql.Stmt
|
|
|
|
if cache {
|
|
|
|
st, _ = db.prepare(query)
|
|
|
|
}
|
|
|
|
// Prepare context, call hook
|
|
|
|
ctx := db.dbBefore(context.Background(), query, args...)
|
|
|
|
defer db.dbAfter(ctx, query, args...)
|
|
|
|
// Query
|
2021-07-01 16:51:04 +00:00
|
|
|
if st != nil {
|
2021-08-09 16:31:42 +00:00
|
|
|
return st.QueryContext(ctx, args...)
|
2020-11-09 15:40:12 +00:00
|
|
|
}
|
2021-08-09 16:31:42 +00:00
|
|
|
return db.db.QueryContext(ctx, query, args...)
|
2020-11-09 15:40:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) queryRow(query string, args ...interface{}) (*sql.Row, error) {
|
2021-08-09 16:31:42 +00:00
|
|
|
// Check if no cache arg set
|
|
|
|
cache := true
|
2021-07-12 17:29:32 +00:00
|
|
|
if len(args) > 0 && args[0] == dbNoCache {
|
2021-08-09 16:31:42 +00:00
|
|
|
cache = false
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
// Maybe prepare
|
|
|
|
var st *sql.Stmt
|
|
|
|
if cache {
|
|
|
|
st, _ = db.prepare(query)
|
|
|
|
}
|
|
|
|
// Prepare context, call hook
|
|
|
|
ctx := db.dbBefore(context.Background(), query, args...)
|
|
|
|
defer db.dbAfter(ctx, query, args...)
|
|
|
|
// Query
|
2021-07-01 16:51:04 +00:00
|
|
|
if st != nil {
|
2021-08-09 16:31:42 +00:00
|
|
|
return st.QueryRowContext(ctx, args...), nil
|
2020-11-09 15:40:12 +00:00
|
|
|
}
|
2021-08-09 16:31:42 +00:00
|
|
|
return db.db.QueryRowContext(ctx, query, args...), nil
|
2020-07-30 14:47:46 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
|
|
|
|
// Other things
|
|
|
|
|
|
|
|
func (d *database) rebuildFTSIndex() {
|
|
|
|
_, _ = d.exec("insert into posts_fts(posts_fts) values ('rebuild')")
|
|
|
|
}
|