Mantid
Loading...
Searching...
No Matches
ClearCache.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include "MantidKernel/Glob.h"
13#include <filesystem>
14
15namespace Mantid::Algorithms {
16
17using namespace Mantid::Kernel;
18using namespace Mantid::API;
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(ClearCache)
22
23//----------------------------------------------------------------------------------------------
24
25
26const std::string ClearCache::name() const { return "ClearCache"; }
27
29int ClearCache::version() const { return 1; }
30
32const std::string ClearCache::category() const { return "Utility"; }
33
35const std::string ClearCache::summary() const { return "Clears out selected cached information held by Mantidplot."; }
36
37//----------------------------------------------------------------------------------------------
41 declareProperty("AlgorithmCache", false, "Clears the memory cache of the last used algorithm parameters.");
42 declareProperty("InstrumentCache", false, "Clears the memory cache of the loaded instrument definitions.");
43 declareProperty("DownloadedInstrumentFileCache", false,
44 "Clears the file cache of the downloaded instrument "
45 "definitions. This can be repopulated using "
46 "DownloadInstrument.");
47 declareProperty("GeometryFileCache", false, "Clears the file cache of the triangulated detector geometries.");
48 declareProperty("WorkspaceCache", false, "Clears the memory cache of any workspaces.");
49 declareProperty("UsageServiceCache", false, "Clears the memory cache of usage data.");
50 declareProperty("FilesRemoved", 0, "The number of files removed. Memory clearance do not add to this.",
52}
53
54//----------------------------------------------------------------------------------------------
58 int filesRemoved = 0;
59 bool clearAlgCache = getProperty("AlgorithmCache");
60 bool clearInstService = getProperty("InstrumentCache");
61 bool clearInstFileCache = getProperty("DownloadedInstrumentFileCache");
62 bool clearGeometryFileCache = getProperty("GeometryFileCache");
63 bool clearUsageService = getProperty("UsageServiceCache");
64 bool clearAnalysisService = getProperty("WorkspaceCache");
65
66 bool isAnythingSelected = clearAlgCache || clearInstService || clearInstFileCache || clearGeometryFileCache ||
67 clearUsageService || clearAnalysisService;
68 if (!isAnythingSelected) {
69 g_log.warning("Nothing caches to clear. Nothing done.");
70 return;
71 }
72
73 // get the instrument directories
74 auto instrumentDirs = Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
75 std::filesystem::path localPath(instrumentDirs[0]);
76
77 if (clearAlgCache) {
78 g_log.debug("Emptying the Algorithm cache (AlgorithmCache).");
79 AlgorithmManager::Instance().clear();
80 }
81 if (clearAnalysisService) {
82 g_log.debug("Emptying the Analysis data service (WorkspaceCache).");
83 AnalysisDataService::Instance().clear();
84 }
85 if (clearInstService) {
86 g_log.debug("Emptying the Instrument data service (InstrumentCache).");
87 InstrumentDataService::Instance().clear();
88 }
89 if (clearInstFileCache) {
90 g_log.debug("Removing files from the Downloaded Instrument file cache "
91 "(DownloadedInstrumentFileCache).");
92 int filecount = deleteFiles(localPath.string(), "*.xml");
93 filecount += deleteFiles(localPath.string(), "github.json");
94 g_log.information() << filecount << " files deleted\n";
95 filesRemoved += filecount;
96 }
97 if (clearGeometryFileCache) {
98 g_log.debug("Removing files from the triangulated detector geometry file "
99 "cache (GeometryFileCache).");
100 std::filesystem::path GeomPath = localPath / "geometryCache";
101 int filecount = deleteFiles(GeomPath.string(), "*.vtp");
102 g_log.information() << filecount << " files deleted\n";
103 filesRemoved += filecount;
104 }
105 if (clearUsageService) {
106 g_log.debug("Emptying the Usage data service (UsageServiceCache).");
107 UsageService::Instance().clear();
108 }
109 setProperty("FilesRemoved", filesRemoved);
110}
111
117int ClearCache::deleteFiles(const std::string &path, const std::string &pattern) const {
118 int filesDeleted = 0;
119
120 std::filesystem::path pathPattern = std::filesystem::path(path) / pattern;
121 std::set<std::string> files;
122 Mantid::Kernel::Glob::glob(pathPattern.string(), files);
123
124 for (const auto &filepath : files) {
125 g_log.debug("Deleting file " + filepath);
126 try {
127 std::filesystem::remove(filepath);
128 filesDeleted++;
129 } catch (std::filesystem::filesystem_error &ex) {
130 g_log.warning("Cannot delete file " + filepath + ": " + ex.what());
131 }
132 }
133 return filesDeleted;
134}
135
136} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
ClearCache : TODO: DESCRIPTION.
Definition ClearCache.h:16
int version() const override final
Algorithm's version for identification.
void exec() override final
Execute the algorithm.
const std::string summary() const override final
Algorithm's summary for use in the GUI and help.
const std::string category() const override final
Algorithm's category for identification.
void init() override final
Initialize the algorithm's properties.
int deleteFiles(const std::string &path, const std::string &pattern) const
Deletes files on the path that match the pattern provided.
static void glob(const std::string &pathPattern, std::set< std::string > &files, int options=0)
Creates a set of files that match the given pathPattern.
Definition Glob.cpp:35
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
STL namespace.
@ Output
An output workspace.
Definition Property.h:54