Skip to content

Commit e044d01

Browse files
committed
Clear metadatastore cache from settings
1 parent 8dedec2 commit e044d01

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

activity_browser/app/actions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@
9797
from .metadatastore_open import MetaDataStoreOpen
9898
from .node_select_open import NodeSelectOpen
9999
from .save_parameters_to_excel import SaveParametersToExcel
100+
from .metadatastore_cache_clear import MetaDataStoreCacheClear
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from activity_browser import app
2+
from activity_browser.app.actions.base import ABAction, exception_dialogs
3+
from activity_browser.ui.icons import qicons
4+
5+
import bw2data as bd
6+
7+
from .project.project_switch import ProjectSwitch
8+
9+
10+
class MetaDataStoreCacheClear(ABAction):
11+
12+
icon = qicons.right
13+
text = "Clear Metadata Store Cache"
14+
tool_tip = "Clear the Metadata Store cache and reload the current project"
15+
16+
@staticmethod
17+
@exception_dialogs
18+
def run():
19+
app.metadata.clear_cache()
20+
ProjectSwitch.run(bd.projects.current, reload=True)

activity_browser/app/actions/project/project_switch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class ProjectSwitch(ABAction):
3737

3838
@staticmethod
3939
@exception_dialogs
40-
def run(project_name: str):
40+
def run(project_name: str, reload: bool = False):
4141
# compare the new to the current project name and switch to the new one if the two are not the same
42-
if project_name == bd.projects.current:
42+
if project_name == bd.projects.current and not reload:
4343
logger.debug(f"Brightway2 already selected: {project_name}")
4444
return
4545

activity_browser/app/pages/settings/metadatastore.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from qtpy import QtWidgets
44

55
from activity_browser.app import settings
6+
from activity_browser.app.actions.metadatastore_cache_clear import MetaDataStoreCacheClear
67
from activity_browser.app.pages.settings.base import BaseSettingsChapter
78

89

@@ -26,6 +27,13 @@ def __init__(self, parent=None):
2627
"Disable if you experience performance issues with large databases."
2728
)
2829

30+
# Clear cache button
31+
self.clear_cache_button = QtWidgets.QPushButton("Clear Cache")
32+
self.clear_cache_button.setToolTip(
33+
"Clear the metadata store cache and reload the current project. "
34+
"Use this if you experience issues with outdated or corrupted cache data."
35+
)
36+
2937
self.build_layout()
3038
self.connect_signals()
3139
self.reset()
@@ -36,6 +44,9 @@ def connect_signals(self):
3644
self.caching_checkbox.stateChanged.connect(lambda: self.changed.emit())
3745
self.searcher_checkbox.stateChanged.connect(lambda: self.changed.emit())
3846

47+
# Connect clear cache button
48+
self.clear_cache_button.clicked.connect(MetaDataStoreCacheClear.run)
49+
3950
def build_layout(self):
4051
"""Build the chapter layout."""
4152
layout = QtWidgets.QVBoxLayout()
@@ -47,10 +58,13 @@ def build_layout(self):
4758
metadatastore_layout.addWidget(self.caching_checkbox)
4859
metadatastore_layout.addWidget(self.searcher_checkbox)
4960

61+
# Add clear cache button
62+
metadatastore_layout.addWidget(self.clear_cache_button)
63+
5064
# Add description label
5165
description = QtWidgets.QLabel(
5266
"These settings control the behavior of the metadata store, "
53-
"which manages activity and exchange metadata for improved performance."
67+
"which manages activity data for improved performance."
5468
)
5569
description.setWordWrap(True)
5670
description.setStyleSheet("color: gray; font-size: 10pt;")

activity_browser/bwutils/metadata/metadata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ def auto_complete(self, word: str, context: Optional[set] = None, database: Opti
248248
completions = self.searcher.auto_complete(word, context=context, database=database)
249249
return completions
250250

251+
def clear_cache(self):
252+
from activity_browser.bwutils import filesystem
253+
254+
cache_path = filesystem.get_project_ab_path() / "metadatastore_cache.pkl"
255+
if cache_path.exists():
256+
cache_path.unlink()
257+
logger.info("Metadata store cache cleared.")
258+
else:
259+
logger.info("No metadata store cache found to clear.")
260+
251261

252262
def get_query_parameters(query: str) -> tuple[dict[str, str], str]:
253263
"""Extract key-value pairs from a query string of the form 'key1:value1 key2:value2'."""

0 commit comments

Comments
 (0)