2020-02-21 00:33:48 +01:00
|
|
|
// Copyright (c) 2020 Shivaram Lingamneni
|
|
|
|
// released under the MIT license
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2020-02-21 00:33:48 +01:00
|
|
|
"context"
|
2020-02-19 01:38:42 +01:00
|
|
|
"database/sql"
|
2020-05-12 18:05:40 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-02-19 01:38:42 +01:00
|
|
|
"fmt"
|
2020-05-12 18:05:40 +02:00
|
|
|
"io"
|
2020-02-19 01:38:42 +01:00
|
|
|
"runtime/debug"
|
2021-04-06 06:46:07 +02:00
|
|
|
"strings"
|
2020-02-19 01:38:42 +01:00
|
|
|
"sync"
|
2020-02-21 00:33:48 +01:00
|
|
|
"sync/atomic"
|
2020-02-19 01:38:42 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/oragono/oragono/irc/history"
|
|
|
|
"github.com/oragono/oragono/irc/logger"
|
|
|
|
"github.com/oragono/oragono/irc/utils"
|
|
|
|
)
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
var (
|
|
|
|
ErrDisallowed = errors.New("disallowed")
|
|
|
|
)
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
const (
|
2020-02-21 00:19:17 +01:00
|
|
|
// maximum length in bytes of any message target (nickname or channel name) in its
|
|
|
|
// canonicalized (i.e., casefolded) state:
|
|
|
|
MaxTargetLength = 64
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
// latest schema of the db
|
2020-02-28 11:41:08 +01:00
|
|
|
latestDbSchema = "2"
|
2020-02-19 01:38:42 +01:00
|
|
|
keySchemaVersion = "db.version"
|
2020-05-12 18:05:40 +02:00
|
|
|
// minor version indicates rollback-safe upgrades, i.e.,
|
|
|
|
// you can downgrade oragono and everything will work
|
2021-04-06 06:46:07 +02:00
|
|
|
latestDbMinorVersion = "2"
|
2020-05-12 18:05:40 +02:00
|
|
|
keySchemaMinorVersion = "db.minorversion"
|
|
|
|
cleanupRowLimit = 50
|
|
|
|
cleanupPauseTime = 10 * time.Minute
|
2020-02-19 01:38:42 +01:00
|
|
|
)
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
type e struct{}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
type MySQL struct {
|
2020-05-12 18:05:40 +02:00
|
|
|
timeout int64
|
|
|
|
trackAccountMessages uint32
|
|
|
|
db *sql.DB
|
|
|
|
logger *logger.Manager
|
2020-02-19 01:38:42 +01:00
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
insertHistory *sql.Stmt
|
|
|
|
insertSequence *sql.Stmt
|
|
|
|
insertConversation *sql.Stmt
|
2021-04-06 06:46:07 +02:00
|
|
|
insertCorrespondent *sql.Stmt
|
2020-05-12 18:05:40 +02:00
|
|
|
insertAccountMessage *sql.Stmt
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
stateMutex sync.Mutex
|
2020-02-21 00:33:48 +01:00
|
|
|
config Config
|
2020-05-12 18:05:40 +02:00
|
|
|
|
|
|
|
wakeForgetter chan e
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (mysql *MySQL) Initialize(logger *logger.Manager, config Config) {
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.logger = logger
|
2020-05-12 18:05:40 +02:00
|
|
|
mysql.wakeForgetter = make(chan e, 1)
|
2020-02-21 00:33:48 +01:00
|
|
|
mysql.SetConfig(config)
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (mysql *MySQL) SetConfig(config Config) {
|
|
|
|
atomic.StoreInt64(&mysql.timeout, int64(config.Timeout))
|
2020-05-12 18:05:40 +02:00
|
|
|
var trackAccountMessages uint32
|
|
|
|
if config.TrackAccountMessages {
|
|
|
|
trackAccountMessages = 1
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(&mysql.trackAccountMessages, trackAccountMessages)
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.stateMutex.Lock()
|
2020-02-21 00:33:48 +01:00
|
|
|
mysql.config = config
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.stateMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) getExpireTime() (expireTime time.Duration) {
|
|
|
|
mysql.stateMutex.Lock()
|
2020-02-21 00:33:48 +01:00
|
|
|
expireTime = mysql.config.ExpireTime
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.stateMutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (m *MySQL) Open() (err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
var address string
|
2020-05-17 07:00:04 +02:00
|
|
|
if m.config.SocketPath != "" {
|
|
|
|
address = fmt.Sprintf("unix(%s)", m.config.SocketPath)
|
|
|
|
} else if m.config.Port != 0 {
|
2020-02-21 00:33:48 +01:00
|
|
|
address = fmt.Sprintf("tcp(%s:%d)", m.config.Host, m.config.Port)
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
m.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@%s/%s", m.config.User, m.config.Password, address, m.config.HistoryDatabase))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:54:44 +02:00
|
|
|
if m.config.MaxConns != 0 {
|
|
|
|
m.db.SetMaxOpenConns(m.config.MaxConns)
|
|
|
|
m.db.SetMaxIdleConns(m.config.MaxConns)
|
|
|
|
}
|
|
|
|
if m.config.ConnMaxLifetime != 0 {
|
|
|
|
m.db.SetConnMaxLifetime(m.config.ConnMaxLifetime)
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
err = m.fixSchemas()
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
err = m.prepareStatements()
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
go m.cleanupLoop()
|
2020-05-12 18:05:40 +02:00
|
|
|
go m.forgetLoop()
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) fixSchemas() (err error) {
|
|
|
|
_, err = mysql.db.Exec(`CREATE TABLE IF NOT EXISTS metadata (
|
|
|
|
key_name VARCHAR(32) primary key,
|
|
|
|
value VARCHAR(32) NOT NULL
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var schema string
|
|
|
|
err = mysql.db.QueryRow(`select value from metadata where key_name = ?;`, keySchemaVersion).Scan(&schema)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = mysql.createTables()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = mysql.db.Exec(`insert into metadata (key_name, value) values (?, ?);`, keySchemaVersion, latestDbSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-12 18:05:40 +02:00
|
|
|
_, err = mysql.db.Exec(`insert into metadata (key_name, value) values (?, ?);`, keySchemaMinorVersion, latestDbMinorVersion)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
2020-02-19 01:38:42 +01:00
|
|
|
} else if err == nil && schema != latestDbSchema {
|
|
|
|
// TODO figure out what to do about schema changes
|
2020-10-27 04:29:49 +01:00
|
|
|
return fmt.Errorf("incompatible schema: got %s, expected %s", schema, latestDbSchema)
|
2020-05-12 18:05:40 +02:00
|
|
|
} else if err != nil {
|
2020-02-19 01:38:42 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
var minorVersion string
|
|
|
|
err = mysql.db.QueryRow(`select value from metadata where key_name = ?;`, keySchemaMinorVersion).Scan(&minorVersion)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// XXX for now, the only minor version upgrade is the account tracking tables
|
|
|
|
err = mysql.createComplianceTables()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
err = mysql.createCorrespondentsTable()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-12 18:05:40 +02:00
|
|
|
_, err = mysql.db.Exec(`insert into metadata (key_name, value) values (?, ?);`, keySchemaMinorVersion, latestDbMinorVersion)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
} else if err == nil && minorVersion == "1" {
|
|
|
|
// upgrade from 2.1 to 2.2: create the correspondents table
|
|
|
|
err = mysql.createCorrespondentsTable()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = mysql.db.Exec(`update metadata set value = ? where key_name = ?;`, latestDbMinorVersion, keySchemaMinorVersion)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-12 18:05:40 +02:00
|
|
|
} else if err == nil && minorVersion != latestDbMinorVersion {
|
|
|
|
// TODO: if minorVersion < latestDbMinorVersion, upgrade,
|
|
|
|
// if latestDbMinorVersion < minorVersion, ignore because backwards compatible
|
|
|
|
}
|
|
|
|
return
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) createTables() (err error) {
|
|
|
|
_, err = mysql.db.Exec(`CREATE TABLE history (
|
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
data BLOB NOT NULL,
|
|
|
|
msgid BINARY(16) NOT NULL,
|
|
|
|
KEY (msgid(4))
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:19:17 +01:00
|
|
|
_, err = mysql.db.Exec(fmt.Sprintf(`CREATE TABLE sequence (
|
2021-04-19 14:54:40 +02:00
|
|
|
history_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
|
2020-02-21 00:19:17 +01:00
|
|
|
target VARBINARY(%[1]d) NOT NULL,
|
2020-02-19 01:38:42 +01:00
|
|
|
nanotime BIGINT UNSIGNED NOT NULL,
|
2021-04-19 14:54:40 +02:00
|
|
|
KEY (target, nanotime)
|
2020-02-21 00:19:17 +01:00
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`, MaxTargetLength))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-19 14:54:40 +02:00
|
|
|
/* XXX: this table used to be:
|
|
|
|
CREATE TABLE sequence (
|
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
target VARBINARY(%[1]d) NOT NULL,
|
|
|
|
nanotime BIGINT UNSIGNED NOT NULL,
|
|
|
|
history_id BIGINT NOT NULL,
|
|
|
|
KEY (target, nanotime),
|
|
|
|
KEY (history_id)
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;
|
|
|
|
Some users may still be using the old schema.
|
|
|
|
*/
|
2020-02-19 01:38:42 +01:00
|
|
|
|
2020-02-21 00:19:17 +01:00
|
|
|
_, err = mysql.db.Exec(fmt.Sprintf(`CREATE TABLE conversations (
|
2020-02-19 01:38:42 +01:00
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
2020-02-28 11:41:08 +01:00
|
|
|
target VARBINARY(%[1]d) NOT NULL,
|
|
|
|
correspondent VARBINARY(%[1]d) NOT NULL,
|
2020-02-19 01:38:42 +01:00
|
|
|
nanotime BIGINT UNSIGNED NOT NULL,
|
|
|
|
history_id BIGINT NOT NULL,
|
2020-02-28 11:41:08 +01:00
|
|
|
KEY (target, correspondent, nanotime),
|
2020-02-19 01:38:42 +01:00
|
|
|
KEY (history_id)
|
2020-02-21 00:19:17 +01:00
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`, MaxTargetLength))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
err = mysql.createCorrespondentsTable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
err = mysql.createComplianceTables()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
func (mysql *MySQL) createCorrespondentsTable() (err error) {
|
|
|
|
_, err = mysql.db.Exec(fmt.Sprintf(`CREATE TABLE correspondents (
|
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
target VARBINARY(%[1]d) NOT NULL,
|
|
|
|
correspondent VARBINARY(%[1]d) NOT NULL,
|
|
|
|
nanotime BIGINT UNSIGNED NOT NULL,
|
|
|
|
UNIQUE KEY (target, correspondent),
|
|
|
|
KEY (target, nanotime),
|
|
|
|
KEY (nanotime)
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`, MaxTargetLength))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) createComplianceTables() (err error) {
|
|
|
|
_, err = mysql.db.Exec(fmt.Sprintf(`CREATE TABLE account_messages (
|
|
|
|
history_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
|
|
|
|
account VARBINARY(%[1]d) NOT NULL,
|
|
|
|
KEY (account, history_id)
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`, MaxTargetLength))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = mysql.db.Exec(fmt.Sprintf(`CREATE TABLE forget (
|
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
account VARBINARY(%[1]d) NOT NULL
|
|
|
|
) CHARSET=ascii COLLATE=ascii_bin;`, MaxTargetLength))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) cleanupLoop() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
mysql.logger.Error("mysql",
|
|
|
|
fmt.Sprintf("Panic in cleanup routine: %v\n%s", r, debug.Stack()))
|
|
|
|
time.Sleep(cleanupPauseTime)
|
|
|
|
go mysql.cleanupLoop()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
expireTime := mysql.getExpireTime()
|
|
|
|
if expireTime != 0 {
|
|
|
|
for {
|
|
|
|
startTime := time.Now()
|
|
|
|
rowsDeleted, err := mysql.doCleanup(expireTime)
|
|
|
|
elapsed := time.Now().Sub(startTime)
|
|
|
|
mysql.logError("error during row cleanup", err)
|
|
|
|
// keep going as long as we're accomplishing significant work
|
|
|
|
// (don't busy-wait on small numbers of rows expiring):
|
|
|
|
if rowsDeleted < (cleanupRowLimit / 10) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// crude backpressure mechanism: if the database is slow,
|
|
|
|
// give it time to process other queries
|
|
|
|
time.Sleep(elapsed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(cleanupPauseTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) doCleanup(age time.Duration) (count int, err error) {
|
2020-05-12 18:05:40 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cleanupPauseTime)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ids, maxNanotime, err := mysql.selectCleanupIDs(ctx, age)
|
2020-02-19 01:38:42 +01:00
|
|
|
if len(ids) == 0 {
|
|
|
|
mysql.logger.Debug("mysql", "found no rows to clean up")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql.logger.Debug("mysql", fmt.Sprintf("deleting %d history rows, max age %s", len(ids), utils.NanoToTimestamp(maxNanotime)))
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
if maxNanotime != 0 {
|
|
|
|
mysql.deleteCorrespondents(ctx, maxNanotime)
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
return len(ids), mysql.deleteHistoryIDs(ctx, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) deleteHistoryIDs(ctx context.Context, ids []uint64) (err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
// can't use ? binding for a variable number of arguments, build the IN clause manually
|
2021-04-06 06:46:07 +02:00
|
|
|
var inBuf strings.Builder
|
2020-02-19 01:38:42 +01:00
|
|
|
inBuf.WriteByte('(')
|
|
|
|
for i, id := range ids {
|
|
|
|
if i != 0 {
|
|
|
|
inBuf.WriteRune(',')
|
|
|
|
}
|
2020-02-20 07:45:17 +01:00
|
|
|
fmt.Fprintf(&inBuf, "%d", id)
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
inBuf.WriteRune(')')
|
2021-04-06 06:46:07 +02:00
|
|
|
inClause := inBuf.String()
|
2020-02-19 01:38:42 +01:00
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
_, err = mysql.db.ExecContext(ctx, fmt.Sprintf(`DELETE FROM conversations WHERE history_id in %s;`, inClause))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
_, err = mysql.db.ExecContext(ctx, fmt.Sprintf(`DELETE FROM sequence WHERE history_id in %s;`, inClause))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-12 18:05:40 +02:00
|
|
|
if mysql.isTrackingAccountMessages() {
|
2021-04-06 06:46:07 +02:00
|
|
|
_, err = mysql.db.ExecContext(ctx, fmt.Sprintf(`DELETE FROM account_messages WHERE history_id in %s;`, inClause))
|
2020-05-12 18:05:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
_, err = mysql.db.ExecContext(ctx, fmt.Sprintf(`DELETE FROM history WHERE id in %s;`, inClause))
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) selectCleanupIDs(ctx context.Context, age time.Duration) (ids []uint64, maxNanotime int64, err error) {
|
|
|
|
rows, err := mysql.db.QueryContext(ctx, `
|
2021-04-19 14:54:40 +02:00
|
|
|
SELECT history.id, sequence.nanotime, conversations.nanotime
|
2020-02-19 01:38:42 +01:00
|
|
|
FROM history
|
|
|
|
LEFT JOIN sequence ON history.id = sequence.history_id
|
2021-04-19 14:54:40 +02:00
|
|
|
LEFT JOIN conversations on history.id = conversations.history_id
|
2020-02-19 01:38:42 +01:00
|
|
|
ORDER BY history.id LIMIT ?;`, cleanupRowLimit)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
idset := make(map[uint64]struct{}, cleanupRowLimit)
|
|
|
|
threshold := time.Now().Add(-age).UnixNano()
|
|
|
|
for rows.Next() {
|
|
|
|
var id uint64
|
2021-04-19 14:54:40 +02:00
|
|
|
var seqNano, convNano sql.NullInt64
|
|
|
|
err = rows.Scan(&id, &seqNano, &convNano)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-19 14:54:40 +02:00
|
|
|
nanotime := extractNanotime(seqNano, convNano)
|
|
|
|
// returns 0 if not found; in that case the data is inconsistent
|
|
|
|
// and we should delete the entry
|
|
|
|
if nanotime < threshold {
|
2020-02-19 01:38:42 +01:00
|
|
|
idset[id] = struct{}{}
|
2021-04-19 14:54:40 +02:00
|
|
|
if nanotime > maxNanotime {
|
|
|
|
maxNanotime = nanotime
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ids = make([]uint64, len(idset))
|
|
|
|
i := 0
|
|
|
|
for id := range idset {
|
|
|
|
ids[i] = id
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
func (mysql *MySQL) deleteCorrespondents(ctx context.Context, threshold int64) {
|
|
|
|
result, err := mysql.db.ExecContext(ctx, `DELETE FROM correspondents WHERE nanotime <= (?);`, threshold)
|
|
|
|
if err != nil {
|
|
|
|
mysql.logError("error deleting correspondents", err)
|
|
|
|
} else {
|
|
|
|
count, err := result.RowsAffected()
|
2021-04-08 04:16:18 +02:00
|
|
|
if !mysql.logError("error deleting correspondents", err) {
|
2021-04-06 06:46:07 +02:00
|
|
|
mysql.logger.Debug(fmt.Sprintf("deleted %d correspondents entries", count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
// wait for forget queue items and process them one by one
|
|
|
|
func (mysql *MySQL) forgetLoop() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
mysql.logger.Error("mysql",
|
|
|
|
fmt.Sprintf("Panic in forget routine: %v\n%s", r, debug.Stack()))
|
|
|
|
time.Sleep(cleanupPauseTime)
|
|
|
|
go mysql.forgetLoop()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
found, err := mysql.doForget()
|
|
|
|
mysql.logError("error processing forget", err)
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(cleanupPauseTime)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-mysql.wakeForgetter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dequeue an item from the forget queue and process it
|
|
|
|
func (mysql *MySQL) doForget() (found bool, err error) {
|
|
|
|
id, account, err := func() (id int64, account string, err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cleanupPauseTime)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
row := mysql.db.QueryRowContext(ctx,
|
|
|
|
`SELECT forget.id, forget.account FROM forget LIMIT 1;`)
|
|
|
|
err = row.Scan(&id, &account)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, "", nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil || account == "" {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
|
|
|
|
var count int
|
|
|
|
for {
|
|
|
|
start := time.Now()
|
|
|
|
count, err = mysql.doForgetIteration(account)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(elapsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql.logger.Debug("mysql", "forget complete for account", account)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cleanupPauseTime)
|
|
|
|
defer cancel()
|
|
|
|
_, err = mysql.db.ExecContext(ctx, `DELETE FROM forget where id = ?;`, id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) doForgetIteration(account string) (count int, err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cleanupPauseTime)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := mysql.db.QueryContext(ctx, `
|
|
|
|
SELECT account_messages.history_id
|
|
|
|
FROM account_messages
|
|
|
|
WHERE account_messages.account = ?
|
|
|
|
LIMIT ?;`, account, cleanupRowLimit)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var ids []uint64
|
|
|
|
for rows.Next() {
|
|
|
|
var id uint64
|
|
|
|
err = rows.Scan(&id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql.logger.Debug("mysql", fmt.Sprintf("deleting %d history rows from account %s", len(ids), account))
|
|
|
|
err = mysql.deleteHistoryIDs(ctx, ids)
|
|
|
|
return len(ids), err
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
func (mysql *MySQL) prepareStatements() (err error) {
|
|
|
|
mysql.insertHistory, err = mysql.db.Prepare(`INSERT INTO history
|
|
|
|
(data, msgid) VALUES (?, ?);`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mysql.insertSequence, err = mysql.db.Prepare(`INSERT INTO sequence
|
|
|
|
(target, nanotime, history_id) VALUES (?, ?, ?);`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mysql.insertConversation, err = mysql.db.Prepare(`INSERT INTO conversations
|
2020-02-28 11:41:08 +01:00
|
|
|
(target, correspondent, nanotime, history_id) VALUES (?, ?, ?, ?);`)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
mysql.insertCorrespondent, err = mysql.db.Prepare(`INSERT INTO correspondents
|
|
|
|
(target, correspondent, nanotime) VALUES (?, ?, ?)
|
|
|
|
ON DUPLICATE KEY UPDATE nanotime = GREATEST(nanotime, ?);`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-12 18:05:40 +02:00
|
|
|
mysql.insertAccountMessage, err = mysql.db.Prepare(`INSERT INTO account_messages
|
|
|
|
(history_id, account) VALUES (?, ?);`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (mysql *MySQL) getTimeout() time.Duration {
|
|
|
|
return time.Duration(atomic.LoadInt64(&mysql.timeout))
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) isTrackingAccountMessages() bool {
|
|
|
|
return atomic.LoadUint32(&mysql.trackAccountMessages) != 0
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
func (mysql *MySQL) logError(context string, err error) (quit bool) {
|
|
|
|
if err != nil {
|
|
|
|
mysql.logger.Error("mysql", context, err.Error())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) Forget(account string) {
|
|
|
|
if mysql.db == nil || account == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := mysql.db.ExecContext(ctx, `INSERT INTO forget (account) VALUES (?);`, account)
|
|
|
|
if mysql.logError("can't insert into forget table", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// wake up the forget goroutine if it's blocked:
|
|
|
|
select {
|
|
|
|
case mysql.wakeForgetter <- e{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) AddChannelItem(target string, item history.Item, account string) (err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
if mysql.db == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == "" {
|
|
|
|
return utils.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id, err := mysql.insertBase(ctx, item)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
err = mysql.insertSequenceEntry(ctx, target, item.Message.Time.UnixNano(), id)
|
2020-05-12 18:05:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mysql.insertAccountMessageEntry(ctx, id, account)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
func (mysql *MySQL) insertSequenceEntry(ctx context.Context, target string, messageTime int64, id int64) (err error) {
|
|
|
|
_, err = mysql.insertSequence.ExecContext(ctx, target, messageTime, id)
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.logError("could not insert sequence entry", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
func (mysql *MySQL) insertConversationEntry(ctx context.Context, target, correspondent string, messageTime int64, id int64) (err error) {
|
|
|
|
_, err = mysql.insertConversation.ExecContext(ctx, target, correspondent, messageTime, id)
|
2020-02-19 01:38:42 +01:00
|
|
|
mysql.logError("could not insert conversations entry", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
func (mysql *MySQL) insertCorrespondentsEntry(ctx context.Context, target, correspondent string, messageTime int64, historyId int64) (err error) {
|
|
|
|
_, err = mysql.insertCorrespondent.ExecContext(ctx, target, correspondent, messageTime, messageTime)
|
|
|
|
mysql.logError("could not insert conversations entry", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (mysql *MySQL) insertBase(ctx context.Context, item history.Item) (id int64, err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
value, err := marshalItem(&item)
|
|
|
|
if mysql.logError("could not marshal item", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msgidBytes, err := decodeMsgid(item.Message.Msgid)
|
|
|
|
if mysql.logError("could not decode msgid", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
result, err := mysql.insertHistory.ExecContext(ctx, value, msgidBytes)
|
2020-02-19 01:38:42 +01:00
|
|
|
if mysql.logError("could not insert item", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err = result.LastInsertId()
|
|
|
|
if mysql.logError("could not insert item", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) insertAccountMessageEntry(ctx context.Context, id int64, account string) (err error) {
|
|
|
|
if account == "" || !mysql.isTrackingAccountMessages() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = mysql.insertAccountMessage.ExecContext(ctx, id, account)
|
|
|
|
mysql.logError("could not insert account-message entry", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
func (mysql *MySQL) AddDirectMessage(sender, senderAccount, recipient, recipientAccount string, item history.Item) (err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
if mysql.db == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
if senderAccount == "" && recipientAccount == "" {
|
2020-02-19 01:38:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sender == "" || recipient == "" {
|
|
|
|
return utils.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id, err := mysql.insertBase(ctx, item)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
nanotime := item.Message.Time.UnixNano()
|
|
|
|
|
|
|
|
if senderAccount != "" {
|
|
|
|
err = mysql.insertConversationEntry(ctx, senderAccount, recipient, nanotime, id)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
err = mysql.insertCorrespondentsEntry(ctx, senderAccount, recipient, nanotime, id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
if recipientAccount != "" && sender != recipient {
|
|
|
|
err = mysql.insertConversationEntry(ctx, recipientAccount, sender, nanotime, id)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 06:46:07 +02:00
|
|
|
err = mysql.insertCorrespondentsEntry(ctx, recipientAccount, sender, nanotime, id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
err = mysql.insertAccountMessageEntry(ctx, id, senderAccount)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that accountName is the unfolded name
|
|
|
|
func (mysql *MySQL) DeleteMsgid(msgid, accountName string) (err error) {
|
|
|
|
if mysql.db == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, id, data, err := mysql.lookupMsgid(ctx, msgid, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountName != "*" {
|
|
|
|
var item history.Item
|
|
|
|
err = unmarshalItem(data, &item)
|
|
|
|
// delete if the entry is corrupt
|
|
|
|
if err == nil && item.AccountName != accountName {
|
|
|
|
return ErrDisallowed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mysql.deleteHistoryIDs(ctx, []uint64{id})
|
|
|
|
mysql.logError("couldn't delete msgid", err)
|
2020-02-19 01:38:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:40 +02:00
|
|
|
func (mysql *MySQL) Export(account string, writer io.Writer) {
|
|
|
|
if mysql.db == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var lastSeen uint64
|
|
|
|
for {
|
|
|
|
rows := func() (count int) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cleanupPauseTime)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, rowsErr := mysql.db.QueryContext(ctx, `
|
|
|
|
SELECT account_messages.history_id, history.data, sequence.target FROM account_messages
|
|
|
|
INNER JOIN history ON history.id = account_messages.history_id
|
|
|
|
INNER JOIN sequence ON account_messages.history_id = sequence.history_id
|
|
|
|
WHERE account_messages.account = ? AND account_messages.history_id > ?
|
|
|
|
LIMIT ?`, account, lastSeen, cleanupRowLimit)
|
|
|
|
if rowsErr != nil {
|
|
|
|
err = rowsErr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var id uint64
|
|
|
|
var blob, jsonBlob []byte
|
|
|
|
var target string
|
|
|
|
var item history.Item
|
|
|
|
err = rows.Scan(&id, &blob, &target)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = unmarshalItem(blob, &item)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item.CfCorrespondent = target
|
|
|
|
jsonBlob, err = json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
if lastSeen < id {
|
|
|
|
lastSeen = id
|
|
|
|
}
|
|
|
|
writer.Write(jsonBlob)
|
|
|
|
writer.Write([]byte{'\n'})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
if rows == 0 || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql.logError("could not export history", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mysql *MySQL) lookupMsgid(ctx context.Context, msgid string, includeData bool) (result time.Time, id uint64, data []byte, err error) {
|
2020-02-19 01:38:42 +01:00
|
|
|
decoded, err := decodeMsgid(msgid)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-19 14:54:40 +02:00
|
|
|
cols := `sequence.nanotime, conversations.nanotime`
|
2020-05-12 18:05:40 +02:00
|
|
|
if includeData {
|
2021-04-19 14:54:40 +02:00
|
|
|
cols = `sequence.nanotime, conversations.nanotime, history.id, history.data`
|
2020-05-12 18:05:40 +02:00
|
|
|
}
|
|
|
|
row := mysql.db.QueryRowContext(ctx, fmt.Sprintf(`
|
2021-04-19 14:54:40 +02:00
|
|
|
SELECT %s FROM history
|
|
|
|
LEFT JOIN sequence ON history.id = sequence.history_id
|
|
|
|
LEFT JOIN conversations ON history.id = conversations.history_id
|
2020-05-12 18:05:40 +02:00
|
|
|
WHERE history.msgid = ? LIMIT 1;`, cols), decoded)
|
2021-04-19 14:54:40 +02:00
|
|
|
var nanoSeq, nanoConv sql.NullInt64
|
2020-05-12 18:05:40 +02:00
|
|
|
if !includeData {
|
2021-04-19 14:54:40 +02:00
|
|
|
err = row.Scan(&nanoSeq, &nanoConv)
|
2020-05-12 18:05:40 +02:00
|
|
|
} else {
|
2021-04-19 14:54:40 +02:00
|
|
|
err = row.Scan(&nanoSeq, &nanoConv, &id, &data)
|
2020-05-12 18:05:40 +02:00
|
|
|
}
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
mysql.logError("could not resolve msgid to time", err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-02-19 01:38:42 +01:00
|
|
|
return
|
|
|
|
}
|
2021-04-19 14:54:40 +02:00
|
|
|
nanotime := extractNanotime(nanoSeq, nanoConv)
|
|
|
|
if nanotime == 0 {
|
|
|
|
err = sql.ErrNoRows
|
|
|
|
return
|
|
|
|
}
|
2020-02-20 09:06:24 +01:00
|
|
|
result = time.Unix(0, nanotime).UTC()
|
2020-02-19 01:38:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:54:40 +02:00
|
|
|
func extractNanotime(seq, conv sql.NullInt64) (result int64) {
|
|
|
|
if seq.Valid {
|
|
|
|
return seq.Int64
|
|
|
|
} else if conv.Valid {
|
|
|
|
return conv.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
func (mysql *MySQL) selectItems(ctx context.Context, query string, args ...interface{}) (results []history.Item, err error) {
|
|
|
|
rows, err := mysql.db.QueryContext(ctx, query, args...)
|
2020-02-19 01:38:42 +01:00
|
|
|
if mysql.logError("could not select history items", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var blob []byte
|
|
|
|
var item history.Item
|
|
|
|
err = rows.Scan(&blob)
|
|
|
|
if mysql.logError("could not scan history item", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = unmarshalItem(blob, &item)
|
|
|
|
if mysql.logError("could not unmarshal history item", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
results = append(results, item)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
func (mysql *MySQL) betweenTimestamps(ctx context.Context, target, correspondent string, after, before, cutoff time.Time, limit int) (results []history.Item, err error) {
|
|
|
|
useSequence := correspondent == ""
|
2020-02-19 01:38:42 +01:00
|
|
|
table := "sequence"
|
|
|
|
if !useSequence {
|
|
|
|
table = "conversations"
|
|
|
|
}
|
|
|
|
|
|
|
|
after, before, ascending := history.MinMaxAsc(after, before, cutoff)
|
|
|
|
direction := "ASC"
|
|
|
|
if !ascending {
|
|
|
|
direction = "DESC"
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
var queryBuf strings.Builder
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
args := make([]interface{}, 0, 6)
|
|
|
|
fmt.Fprintf(&queryBuf,
|
|
|
|
"SELECT history.data from history INNER JOIN %[1]s ON history.id = %[1]s.history_id WHERE", table)
|
|
|
|
if useSequence {
|
|
|
|
fmt.Fprintf(&queryBuf, " sequence.target = ?")
|
2020-02-28 11:41:08 +01:00
|
|
|
args = append(args, target)
|
2020-02-19 01:38:42 +01:00
|
|
|
} else {
|
2020-02-28 11:41:08 +01:00
|
|
|
fmt.Fprintf(&queryBuf, " conversations.target = ? AND conversations.correspondent = ?")
|
|
|
|
args = append(args, target)
|
|
|
|
args = append(args, correspondent)
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
if !after.IsZero() {
|
|
|
|
fmt.Fprintf(&queryBuf, " AND %s.nanotime > ?", table)
|
|
|
|
args = append(args, after.UnixNano())
|
|
|
|
}
|
|
|
|
if !before.IsZero() {
|
|
|
|
fmt.Fprintf(&queryBuf, " AND %s.nanotime < ?", table)
|
|
|
|
args = append(args, before.UnixNano())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&queryBuf, " ORDER BY %[1]s.nanotime %[2]s LIMIT ?;", table, direction)
|
|
|
|
args = append(args, limit)
|
|
|
|
|
2020-02-21 00:33:48 +01:00
|
|
|
results, err = mysql.selectItems(ctx, queryBuf.String(), args...)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err == nil && !ascending {
|
|
|
|
history.Reverse(results)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:40:39 +02:00
|
|
|
func (mysql *MySQL) listCorrespondentsInternal(ctx context.Context, target string, after, before, cutoff time.Time, limit int) (results []history.TargetListing, err error) {
|
2021-04-06 06:46:07 +02:00
|
|
|
after, before, ascending := history.MinMaxAsc(after, before, cutoff)
|
|
|
|
direction := "ASC"
|
|
|
|
if !ascending {
|
|
|
|
direction = "DESC"
|
|
|
|
}
|
|
|
|
|
|
|
|
var queryBuf strings.Builder
|
|
|
|
args := make([]interface{}, 0, 4)
|
|
|
|
queryBuf.WriteString(`SELECT correspondents.correspondent, correspondents.nanotime from correspondents
|
|
|
|
WHERE target = ?`)
|
|
|
|
args = append(args, target)
|
|
|
|
if !after.IsZero() {
|
|
|
|
queryBuf.WriteString(" AND correspondents.nanotime > ?")
|
|
|
|
args = append(args, after.UnixNano())
|
|
|
|
}
|
|
|
|
if !before.IsZero() {
|
|
|
|
queryBuf.WriteString(" AND correspondents.nanotime < ?")
|
|
|
|
args = append(args, before.UnixNano())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&queryBuf, " ORDER BY correspondents.nanotime %s LIMIT ?;", direction)
|
|
|
|
args = append(args, limit)
|
|
|
|
query := queryBuf.String()
|
|
|
|
|
|
|
|
rows, err := mysql.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var correspondent string
|
|
|
|
var nanotime int64
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&correspondent, &nanotime)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-07 11:40:39 +02:00
|
|
|
results = append(results, history.TargetListing{
|
|
|
|
CfName: correspondent,
|
|
|
|
Time: time.Unix(0, nanotime),
|
2021-04-06 06:46:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ascending {
|
|
|
|
history.ReverseCorrespondents(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:40:39 +02:00
|
|
|
func (mysql *MySQL) ListChannels(cfchannels []string) (results []history.TargetListing, err error) {
|
|
|
|
if mysql.db == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfchannels) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var queryBuf strings.Builder
|
|
|
|
args := make([]interface{}, 0, len(results))
|
|
|
|
// https://dev.mysql.com/doc/refman/8.0/en/group-by-optimization.html
|
|
|
|
// this should be a "loose index scan"
|
|
|
|
queryBuf.WriteString(`SELECT sequence.target, MAX(sequence.nanotime) FROM sequence
|
|
|
|
WHERE sequence.target IN (`)
|
|
|
|
for i, chname := range cfchannels {
|
|
|
|
if i != 0 {
|
|
|
|
queryBuf.WriteString(", ")
|
|
|
|
}
|
|
|
|
queryBuf.WriteByte('?')
|
|
|
|
args = append(args, chname)
|
|
|
|
}
|
|
|
|
queryBuf.WriteString(") GROUP BY sequence.target;")
|
|
|
|
|
|
|
|
rows, err := mysql.db.QueryContext(ctx, queryBuf.String(), args...)
|
|
|
|
if mysql.logError("could not query channel listings", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var target string
|
|
|
|
var nanotime int64
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&target, &nanotime)
|
|
|
|
if mysql.logError("could not scan channel listings", err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
results = append(results, history.TargetListing{
|
|
|
|
CfName: target,
|
|
|
|
Time: time.Unix(0, nanotime),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
func (mysql *MySQL) Close() {
|
|
|
|
// closing the database will close our prepared statements as well
|
|
|
|
if mysql.db != nil {
|
|
|
|
mysql.db.Close()
|
|
|
|
}
|
|
|
|
mysql.db = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// implements history.Sequence, emulating a single history buffer (for a channel,
|
|
|
|
// a single user's DMs, or a DM conversation)
|
|
|
|
type mySQLHistorySequence struct {
|
2020-02-28 11:41:08 +01:00
|
|
|
mysql *MySQL
|
|
|
|
target string
|
|
|
|
correspondent string
|
|
|
|
cutoff time.Time
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2021-04-06 06:46:07 +02:00
|
|
|
func (s *mySQLHistorySequence) Between(start, end history.Selector, limit int) (results []history.Item, err error) {
|
2020-02-21 00:33:48 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), s.mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
startTime := start.Time
|
|
|
|
if start.Msgid != "" {
|
2020-05-12 18:05:40 +02:00
|
|
|
startTime, _, _, err = s.mysql.lookupMsgid(ctx, start.Msgid, false)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
2021-04-06 06:46:07 +02:00
|
|
|
return nil, err
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
endTime := end.Time
|
|
|
|
if end.Msgid != "" {
|
2020-05-12 18:05:40 +02:00
|
|
|
endTime, _, _, err = s.mysql.lookupMsgid(ctx, end.Msgid, false)
|
2020-02-19 01:38:42 +01:00
|
|
|
if err != nil {
|
2021-04-06 06:46:07 +02:00
|
|
|
return nil, err
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
results, err = s.mysql.betweenTimestamps(ctx, s.target, s.correspondent, startTime, endTime, s.cutoff, limit)
|
2021-04-06 06:46:07 +02:00
|
|
|
return results, err
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mySQLHistorySequence) Around(start history.Selector, limit int) (results []history.Item, err error) {
|
|
|
|
return history.GenericAround(s, start, limit)
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:40:39 +02:00
|
|
|
func (seq *mySQLHistorySequence) ListCorrespondents(start, end history.Selector, limit int) (results []history.TargetListing, err error) {
|
2021-04-06 06:46:07 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), seq.mysql.getTimeout())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// TODO accept msgids here?
|
|
|
|
startTime := start.Time
|
|
|
|
endTime := end.Time
|
|
|
|
|
|
|
|
results, err = seq.mysql.listCorrespondentsInternal(ctx, seq.target, startTime, endTime, seq.cutoff, limit)
|
|
|
|
seq.mysql.logError("could not read correspondents", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:40:39 +02:00
|
|
|
func (seq *mySQLHistorySequence) Cutoff() time.Time {
|
|
|
|
return seq.cutoff
|
|
|
|
}
|
|
|
|
|
|
|
|
func (seq *mySQLHistorySequence) Ephemeral() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:41:08 +01:00
|
|
|
func (mysql *MySQL) MakeSequence(target, correspondent string, cutoff time.Time) history.Sequence {
|
2020-02-19 01:38:42 +01:00
|
|
|
return &mySQLHistorySequence{
|
2020-02-28 11:41:08 +01:00
|
|
|
target: target,
|
|
|
|
correspondent: correspondent,
|
|
|
|
mysql: mysql,
|
|
|
|
cutoff: cutoff,
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
}
|