🎨 数据历史文档和资源文件支持分页和搜索 https://github.com/siyuan-note/siyuan/issues/4901

This commit is contained in:
Liang Ding 2022-08-23 11:30:51 +08:00
parent f69798f91e
commit 8b137e0d6d
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
8 changed files with 204 additions and 18 deletions

View file

@ -173,10 +173,10 @@ func InitHistoryDatabase(forceRebuild bool) {
historyDB.SetMaxOpenConns(1)
historyDB.SetConnMaxLifetime(365 * 24 * time.Hour)
historyDB.Exec("DROP TABLE history_fts_case_insensitive")
_, err = db.Exec("CREATE VIRTUAL TABLE history_fts_case_insensitive USING fts5(type UNINDEXED, op UNINDEXED, title, content, created UNINDEXED, path UNINDEXED, tokenize=\"siyuan case_insensitive\")")
historyDB.Exec("DROP TABLE histories_fts_case_insensitive")
_, err = historyDB.Exec("CREATE VIRTUAL TABLE histories_fts_case_insensitive USING fts5(type UNINDEXED, op UNINDEXED, title, content, path UNINDEXED, created UNINDEXED, tokenize=\"siyuan case_insensitive\")")
if nil != err {
logging.LogFatalf("create table [history_fts_case_insensitive] failed: %s", err)
logging.LogFatalf("create table [histories_fts_case_insensitive] failed: %s", err)
}
}
@ -1066,6 +1066,9 @@ func CloseDatabase() {
if err := db.Close(); nil != err {
logging.LogErrorf("close database failed: %s", err)
}
if err := historyDB.Close(); nil != err {
logging.LogErrorf("close history database failed: %s", err)
}
}
func queryRow(query string, args ...interface{}) *sql.Row {
@ -1095,6 +1098,16 @@ func BeginTx() (tx *sql.Tx, err error) {
return
}
func BeginHistoryTx() (tx *sql.Tx, err error) {
if tx, err = historyDB.Begin(); nil != err {
logging.LogErrorf("begin history tx failed: %s\n %s", err, logging.ShortStack())
if strings.Contains(err.Error(), "database is locked") {
os.Exit(util.ExitCodeReadOnlyDatabase)
}
}
return
}
func CommitTx(tx *sql.Tx) (err error) {
if nil == tx {
logging.LogErrorf("tx is nil")

82
kernel/sql/history.go Normal file
View file

@ -0,0 +1,82 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package sql
import (
"database/sql"
"fmt"
"strings"
)
type History struct {
Type int
Op string
Title string
Content string
Created string
Path string
}
const (
HistoriesFTSCaseInsensitiveInsert = "INSERT INTO histories_fts_case_insensitive (type, op, title, content, path, created) VALUES %s"
HistoriesPlaceholder = "(?, ?, ?, ?, ?, ?)"
)
func InsertHistories(tx *sql.Tx, histories []*History) (err error) {
if 1 > len(histories) {
return
}
var bulk []*History
for _, history := range histories {
bulk = append(bulk, history)
if 512 > len(bulk) {
continue
}
if err = insertHistories0(tx, bulk); nil != err {
return
}
bulk = []*History{}
}
if 0 < len(bulk) {
if err = insertHistories0(tx, bulk); nil != err {
return
}
}
return
}
func insertHistories0(tx *sql.Tx, bulk []*History) (err error) {
valueStrings := make([]string, 0, len(bulk))
valueArgs := make([]interface{}, 0, len(bulk)*strings.Count(HistoriesPlaceholder, "?"))
for _, b := range bulk {
valueStrings = append(valueStrings, HistoriesPlaceholder)
valueArgs = append(valueArgs, b.Type)
valueArgs = append(valueArgs, b.Op)
valueArgs = append(valueArgs, b.Title)
valueArgs = append(valueArgs, b.Content)
valueArgs = append(valueArgs, b.Path)
valueArgs = append(valueArgs, b.Created)
}
stmt := fmt.Sprintf(HistoriesFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
return
}
return
}