Handle the caching in the DocSearch class
This commit is contained in:
@@ -26,13 +26,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import shutil
|
|
||||||
import logging
|
import logging
|
||||||
|
import shutil
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
from functools import partial
|
|
||||||
from zipfile import ZipFile, is_zipfile
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
from functools import partial
|
||||||
|
from pathlib import Path
|
||||||
|
from time import time
|
||||||
|
from zipfile import ZipFile, is_zipfile
|
||||||
|
|
||||||
from PyQt5.QtCore import QCoreApplication, QRegularExpression
|
from PyQt5.QtCore import QCoreApplication, QRegularExpression
|
||||||
|
|
||||||
@@ -315,6 +316,7 @@ class DocSearch:
|
|||||||
|
|
||||||
# Project Cache
|
# Project Cache
|
||||||
self._uuid = ""
|
self._uuid = ""
|
||||||
|
self._time = 0.0
|
||||||
self._cache: dict[str, str] = {}
|
self._cache: dict[str, str] = {}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -341,24 +343,15 @@ class DocSearch:
|
|||||||
self._escape = not state
|
self._escape = not state
|
||||||
return
|
return
|
||||||
|
|
||||||
def clearTextCache(self, tHandle: str | None) -> None:
|
|
||||||
"""Clear text cache for a given item, or all items if None."""
|
|
||||||
if tHandle is None:
|
|
||||||
self._cache = {}
|
|
||||||
logger.debug("Search cache cleared")
|
|
||||||
elif tHandle in self._cache:
|
|
||||||
self._cache.pop(tHandle, None)
|
|
||||||
logger.debug("Search cache cleared for '%s'", tHandle)
|
|
||||||
return
|
|
||||||
|
|
||||||
def iterSearch(
|
def iterSearch(
|
||||||
self, project: NWProject, search: str
|
self, project: NWProject, search: str
|
||||||
) -> Iterable[tuple[NWItem, list[tuple[int, int, str]], bool]]:
|
) -> Iterable[tuple[NWItem, list[tuple[int, int, str]], bool]]:
|
||||||
"""Iteratively search through documents in a project."""
|
"""Iteratively search through documents in a project."""
|
||||||
if project.data.uuid != self._uuid:
|
if project.data.uuid != self._uuid or time() - self._time > 20.0:
|
||||||
self.clearTextCache(None)
|
self._cache = {}
|
||||||
|
|
||||||
self._uuid = project.data.uuid
|
self._uuid = project.data.uuid
|
||||||
|
self._time = time()
|
||||||
self._regEx.setPattern(self._buildPattern(search))
|
self._regEx.setPattern(self._buildPattern(search))
|
||||||
logger.debug("Searching with pattern '%s'", self._regEx.pattern())
|
logger.debug("Searching with pattern '%s'", self._regEx.pattern())
|
||||||
|
|
||||||
@@ -367,8 +360,7 @@ class DocSearch:
|
|||||||
for item in project.tree:
|
for item in project.tree:
|
||||||
if item.isFileType():
|
if item.isFileType():
|
||||||
tHandle = item.itemHandle
|
tHandle = item.itemHandle
|
||||||
text = self._cache.get(tHandle)
|
if (text := self._cache.get(tHandle)) is None:
|
||||||
if text is None:
|
|
||||||
text = storage.getDocument(tHandle).readDocument() or ""
|
text = storage.getDocument(tHandle).readDocument() or ""
|
||||||
self._cache[tHandle] = text
|
self._cache[tHandle] = text
|
||||||
|
|
||||||
|
|||||||
@@ -91,7 +91,6 @@ class GuiDocEditor(QPlainTextEdit):
|
|||||||
|
|
||||||
# Custom Signals
|
# Custom Signals
|
||||||
statusMessage = pyqtSignal(str)
|
statusMessage = pyqtSignal(str)
|
||||||
docTextSaved = pyqtSignal(str)
|
|
||||||
docCountsChanged = pyqtSignal(str, int, int, int)
|
docCountsChanged = pyqtSignal(str, int, int, int)
|
||||||
editedStatusChanged = pyqtSignal(bool)
|
editedStatusChanged = pyqtSignal(bool)
|
||||||
loadDocumentTagRequest = pyqtSignal(str, Enum)
|
loadDocumentTagRequest = pyqtSignal(str, Enum)
|
||||||
@@ -489,7 +488,6 @@ class GuiDocEditor(QPlainTextEdit):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
self.setDocumentChanged(False)
|
self.setDocumentChanged(False)
|
||||||
self.docTextSaved.emit(tHandle)
|
|
||||||
|
|
||||||
oldHeader = self._nwItem.mainHeading
|
oldHeader = self._nwItem.mainHeading
|
||||||
oldCount = SHARED.project.index.getHandleHeaderCount(tHandle)
|
oldCount = SHARED.project.index.getHandleHeaderCount(tHandle)
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class GuiItemDetails(QWidget):
|
|||||||
logger.debug("Create: GuiItemDetails")
|
logger.debug("Create: GuiItemDetails")
|
||||||
|
|
||||||
# Internal Variables
|
# Internal Variables
|
||||||
self._itemHandle = None
|
self._handle = None
|
||||||
|
|
||||||
# Sizes
|
# Sizes
|
||||||
hSp = CONFIG.pxInt(6)
|
hSp = CONFIG.pxInt(6)
|
||||||
@@ -194,7 +194,7 @@ class GuiItemDetails(QWidget):
|
|||||||
|
|
||||||
def clearDetails(self) -> None:
|
def clearDetails(self) -> None:
|
||||||
"""Clear all the data values."""
|
"""Clear all the data values."""
|
||||||
self._itemHandle = None
|
self._handle = None
|
||||||
self.labelIcon.clear()
|
self.labelIcon.clear()
|
||||||
self.labelData.clear()
|
self.labelData.clear()
|
||||||
self.statusIcon.clear()
|
self.statusIcon.clear()
|
||||||
@@ -210,11 +210,11 @@ class GuiItemDetails(QWidget):
|
|||||||
|
|
||||||
def refreshDetails(self) -> None:
|
def refreshDetails(self) -> None:
|
||||||
"""Reload the content of the details panel."""
|
"""Reload the content of the details panel."""
|
||||||
self.updateViewBox(self._itemHandle)
|
self.updateViewBox(self._handle)
|
||||||
|
|
||||||
def updateTheme(self) -> None:
|
def updateTheme(self) -> None:
|
||||||
"""Update theme elements."""
|
"""Update theme elements."""
|
||||||
self.updateViewBox(self._itemHandle)
|
self.updateViewBox(self._handle)
|
||||||
return
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -233,7 +233,7 @@ class GuiItemDetails(QWidget):
|
|||||||
self.clearDetails()
|
self.clearDetails()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._itemHandle = tHandle
|
self._handle = tHandle
|
||||||
iPx = int(round(0.8*SHARED.theme.baseIconSize))
|
iPx = int(round(0.8*SHARED.theme.baseIconSize))
|
||||||
|
|
||||||
# Label
|
# Label
|
||||||
@@ -295,7 +295,7 @@ class GuiItemDetails(QWidget):
|
|||||||
"""Update the counts if the handle is the same as the one we're
|
"""Update the counts if the handle is the same as the one we're
|
||||||
already showing. Otherwise, do nothing.
|
already showing. Otherwise, do nothing.
|
||||||
"""
|
"""
|
||||||
if tHandle == self._itemHandle:
|
if tHandle == self._handle:
|
||||||
self.cCountData.setText(f"{cC:n}")
|
self.cCountData.setText(f"{cC:n}")
|
||||||
self.wCountData.setText(f"{wC:n}")
|
self.wCountData.setText(f"{wC:n}")
|
||||||
self.pCountData.setText(f"{pC:n}")
|
self.pCountData.setText(f"{pC:n}")
|
||||||
|
|||||||
@@ -135,8 +135,6 @@ class GuiProjectSearch(QWidget):
|
|||||||
self.setLayout(self.outerBox)
|
self.setLayout(self.outerBox)
|
||||||
self.updateTheme()
|
self.updateTheme()
|
||||||
|
|
||||||
SHARED.slowClockTick.connect(self._cacheTimeout)
|
|
||||||
|
|
||||||
logger.debug("Ready: GuiProjectSearch")
|
logger.debug("Ready: GuiProjectSearch")
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -205,17 +203,6 @@ class GuiProjectSearch(QWidget):
|
|||||||
super().keyPressEvent(event)
|
super().keyPressEvent(event)
|
||||||
return
|
return
|
||||||
|
|
||||||
##
|
|
||||||
# Public Slots
|
|
||||||
##
|
|
||||||
|
|
||||||
@pyqtSlot(str)
|
|
||||||
def clearSearchCache(self, tHandle: str) -> None:
|
|
||||||
"""Process document content change."""
|
|
||||||
if not self._blocked:
|
|
||||||
self._search.clearTextCache(tHandle)
|
|
||||||
return
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Private Slots
|
# Private Slots
|
||||||
##
|
##
|
||||||
@@ -284,13 +271,6 @@ class GuiProjectSearch(QWidget):
|
|||||||
CONFIG.searchProjRegEx = state
|
CONFIG.searchProjRegEx = state
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
|
||||||
def _cacheTimeout(self) -> None:
|
|
||||||
"""Clear the cache after a period of inactivity."""
|
|
||||||
if not self._blocked and time() - self._time > CACHE_TIMEOUT:
|
|
||||||
self._search.clearTextCache(None)
|
|
||||||
return
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Internal Functions
|
# Internal Functions
|
||||||
##
|
##
|
||||||
|
|||||||
@@ -262,7 +262,6 @@ class GuiMain(QMainWindow):
|
|||||||
self.docEditor.requestProjectItemSelected.connect(self.projView.setSelectedHandle)
|
self.docEditor.requestProjectItemSelected.connect(self.projView.setSelectedHandle)
|
||||||
self.docEditor.requestProjectItemRenamed.connect(self.projView.renameTreeItem)
|
self.docEditor.requestProjectItemRenamed.connect(self.projView.renameTreeItem)
|
||||||
self.docEditor.requestNewNoteCreation.connect(self.projView.createNewNote)
|
self.docEditor.requestNewNoteCreation.connect(self.projView.createNewNote)
|
||||||
self.docEditor.docTextSaved.connect(self.projSearch.clearSearchCache)
|
|
||||||
|
|
||||||
self.docViewer.documentLoaded.connect(self.docViewerPanel.updateHandle)
|
self.docViewer.documentLoaded.connect(self.docViewerPanel.updateHandle)
|
||||||
self.docViewer.loadDocumentTagRequest.connect(self._followTag)
|
self.docViewer.loadDocumentTagRequest.connect(self._followTag)
|
||||||
|
|||||||
Reference in New Issue
Block a user