Skip to content

Commit 6159115

Browse files
committed
#3587 note: try to prevent typing lag on slow network shared, by improving backlink scans
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 4619469 commit 6159115

5 files changed

Lines changed: 55 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Added an optional editor setting to show character, word and line counts in the
66
status bar (for [#536](https://github.com/pbek/QOwnNotes/issues/536))
7+
- Try to fix typing lag with Notes on a network share by avoiding repeated backlink
8+
scans while typing when the backlink inputs did not change, and by computing
9+
backlink-relative note paths without touching every note file on disk
10+
(for [#3587](https://github.com/pbek/QOwnNotes/issues/3587))
711

812
## 26.6.3
913

src/entities/note.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,12 +2768,18 @@ QString Note::getFullFilePathForFile(const QString &fileName) {
27682768
return canonicalFilePath;
27692769
}
27702770

2771-
QString Note::getFilePathRelativeToNote(const Note &note, const QString &connectionName) const {
2772-
const QDir dir(fullNoteFilePath(connectionName));
2771+
QString Note::getFilePathRelativeToNote(const Note &note, const QString &connectionName,
2772+
bool resolveFileSystemPaths) const {
2773+
const QDir dir(resolveFileSystemPaths
2774+
? fullNoteFilePath(connectionName)
2775+
: relativeNoteFilePath(QStringLiteral("/"), connectionName));
27732776

27742777
// for some reason there is a leading "../" too much
27752778
static const QRegularExpression re(QStringLiteral(R"(^\.\.\/)"));
2776-
QString path = dir.relativeFilePath(note.fullNoteFilePath(connectionName)).remove(re);
2779+
const QString notePath = resolveFileSystemPaths
2780+
? note.fullNoteFilePath(connectionName)
2781+
: note.relativeNoteFilePath(QStringLiteral("/"), connectionName);
2782+
QString path = dir.relativeFilePath(notePath).remove(re);
27772783

27782784
// if "note" is the current note we want to use the real filename
27792785
if (path == QChar('.')) {
@@ -4737,7 +4743,8 @@ QVector<int> Note::findBacklinkedNoteIds(const QString &connectionName) const {
47374743
continue;
47384744
}
47394745

4740-
const QString relativePathToNote = note.getFilePathRelativeToNote(*this, connectionName);
4746+
const QString relativePathToNote =
4747+
note.getFilePathRelativeToNote(*this, connectionName, false);
47414748

47424749
// We now don't escape slashes in the relative file path, but previously we did,
47434750
// so we need to search for both
@@ -5054,7 +5061,8 @@ QHash<Note, QSet<LinkHit>> Note::findReverseLinkNotes(const QString &connectionN
50545061
continue;
50555062
}
50565063

5057-
const QString relativePathToNote = note.getFilePathRelativeToNote(*this, connectionName);
5064+
const QString relativePathToNote =
5065+
note.getFilePathRelativeToNote(*this, connectionName, false);
50585066

50595067
// Search legacy note:// protocol links
50605068
addTextToBacklinkNoteHashIfFound(note, QStringLiteral("<") + noteUrl + QStringLiteral(">"));

src/entities/note.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ class Note {
134134

135135
static QString getFullFilePathForFile(const QString &fileName);
136136

137-
QString getFilePathRelativeToNote(
138-
const Note &note, const QString &connectionName = QStringLiteral("memory")) const;
137+
QString getFilePathRelativeToNote(const Note &note,
138+
const QString &connectionName = QStringLiteral("memory"),
139+
bool resolveFileSystemPaths = true) const;
139140

140141
static int storeDirtyNotesToDisk(Note &currentNote, bool *currentNoteChanged = nullptr,
141142
bool *noteWasRenamed = nullptr,

src/managers/navigationmanager.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <QRegularExpression>
3131
#include <QScrollBar>
3232
#include <QSqlDatabase>
33+
#include <QSqlQuery>
34+
#include <QStringList>
3335
#include <QTabWidget>
3436
#include <QTextCursor>
3537
#include <QUrl>
@@ -108,6 +110,14 @@ void NavigationManager::updateFileNavigationTab() {
108110

109111
void NavigationManager::updateBacklinkNavigationTab() {
110112
const Note note = _mainWindow->currentNote;
113+
const QString cacheKey = backlinkNavigationCacheKey();
114+
const bool backlinkTabVisible = _ui->navigationTabWidget->indexOf(_ui->backlinkTab) >= 0;
115+
if (!backlinkTabVisible && !_lastBacklinkNavigationCacheKey.isEmpty() &&
116+
(_lastBacklinkNavigationCacheKey == cacheKey)) {
117+
return;
118+
}
119+
120+
_lastBacklinkNavigationCacheKey = cacheKey;
111121
Note noteCopy(note);
112122
const quint64 requestId = ++_mainWindow->_backlinkNavigationUpdateRequestId;
113123
QPointer<MainWindow> window(_mainWindow);
@@ -151,6 +161,29 @@ void NavigationManager::updateBacklinkNavigationTab() {
151161
Q_UNUSED(future)
152162
}
153163

164+
QString NavigationManager::backlinkNavigationCacheKey() const {
165+
const Note note = _mainWindow->currentNote;
166+
QStringList keyParts{QString::number(note.getId()), QString::number(note.getNoteSubFolderId()),
167+
note.getFileName(), note.getName()};
168+
169+
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"), false);
170+
if (!db.isValid() || !db.isOpen()) {
171+
return keyParts.join(QLatin1Char('|'));
172+
}
173+
174+
QSqlQuery query(db);
175+
query.prepare(QStringLiteral(
176+
"SELECT COUNT(*), MAX(modified), MAX(file_last_modified) FROM note WHERE id != :id"));
177+
query.bindValue(QStringLiteral(":id"), note.getId());
178+
179+
if (query.exec() && query.first()) {
180+
keyParts << query.value(0).toString() << query.value(1).toString()
181+
<< query.value(2).toString();
182+
}
183+
184+
return keyParts.join(QLatin1Char('|'));
185+
}
186+
154187
void NavigationManager::selectNavigationItemAtPosition(int position) {
155188
if (_ui->navigationWidget->isVisible()) {
156189
_ui->navigationWidget->selectItemForCursorPosition(position);

src/managers/navigationmanager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ class NavigationManager : public QObject {
5151
void on_actionJump_to_navigation_panel_triggered();
5252

5353
private:
54+
QString backlinkNavigationCacheKey() const;
5455
void updateNoteTextStatistics(QOwnNotesMarkdownTextEdit *textEdit);
5556

5657
MainWindow *_mainWindow;
5758
Ui::MainWindow *_ui;
59+
QString _lastBacklinkNavigationCacheKey;
5860
QOwnNotesMarkdownTextEdit *_lastStatisticsTextEdit = nullptr;
5961
int _lastStatisticsNoteId = 0;
6062
int _lastStatisticsRevision = -1;

0 commit comments

Comments
 (0)