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 +
12#include <Poco/File.h>
13#include <Poco/Glob.h>
14#include <Poco/Path.h>
15
16namespace Mantid::Algorithms {
17
18using namespace Mantid::Kernel;
19using namespace Mantid::API;
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(ClearCache)
23
24//----------------------------------------------------------------------------------------------
25
26
27const std::string ClearCache::name() const { return "ClearCache"; }
28
30int ClearCache::version() const { return 1; }
31
33const std::string ClearCache::category() const { return "Utility"; }
34
36const std::string ClearCache::summary() const { return "Clears out selected cached information held by Mantidplot."; }
37
38//----------------------------------------------------------------------------------------------
42 declareProperty("AlgorithmCache", false, "Clears the memory cache of the last used algorithm parameters.");
43 declareProperty("InstrumentCache", false, "Clears the memory cache of the loaded instrument definitions.");
44 declareProperty("DownloadedInstrumentFileCache", false,
45 "Clears the file cache of the downloaded instrument "
46 "definitions. This can be repopulated using "
47 "DownloadInstrument.");
48 declareProperty("GeometryFileCache", false, "Clears the file cache of the triangulated detector geometries.");
49 declareProperty("WorkspaceCache", false, "Clears the memory cache of any workspaces.");
50 declareProperty("UsageServiceCache", false, "Clears the memory cache of usage data.");
51 declareProperty("FilesRemoved", 0, "The number of files removed. Memory clearance do not add to this.",
53}
54
55//----------------------------------------------------------------------------------------------
59 int filesRemoved = 0;
60 bool clearAlgCache = getProperty("AlgorithmCache");
61 bool clearInstService = getProperty("InstrumentCache");
62 bool clearInstFileCache = getProperty("DownloadedInstrumentFileCache");
63 bool clearGeometryFileCache = getProperty("GeometryFileCache");
64 bool clearUsageService = getProperty("UsageServiceCache");
65 bool clearAnalysisService = getProperty("WorkspaceCache");
66
67 bool isAnythingSelected = clearAlgCache || clearInstService || clearInstFileCache || clearGeometryFileCache ||
68 clearUsageService || clearAnalysisService;
69 if (!isAnythingSelected) {
70 g_log.warning("Nothing caches to clear. Nothing done.");
71 return;
72 }
73
74 // get the instrument directories
75 auto instrumentDirs = Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
76 Poco::Path localPath(instrumentDirs[0]);
77 localPath.makeDirectory();
78
79 if (clearAlgCache) {
80 g_log.debug("Emptying the Algorithm cache (AlgorithmCache).");
82 }
83 if (clearAnalysisService) {
84 g_log.debug("Emptying the Analysis data service (WorkspaceCache).");
86 }
87 if (clearInstService) {
88 g_log.debug("Emptying the Instrument data service (InstrumentCache).");
90 }
91 if (clearInstFileCache) {
92 g_log.debug("Removing files from the Downloaded Instrument file cache "
93 "(DownloadedInstrumentFileCache).");
94 int filecount = deleteFiles(localPath.toString(), "*.xml");
95 filecount += deleteFiles(localPath.toString(), "github.json");
96 g_log.information() << filecount << " files deleted\n";
97 filesRemoved += filecount;
98 }
99 if (clearGeometryFileCache) {
100 g_log.debug("Removing files from the triangulated detector geometry file "
101 "cache (GeometryFileCache).");
102 Poco::Path GeomPath(localPath);
103 GeomPath.append("geometryCache").makeDirectory();
104 int filecount = deleteFiles(GeomPath.toString(), "*.vtp");
105 g_log.information() << filecount << " files deleted\n";
106 filesRemoved += filecount;
107 }
108 if (clearUsageService) {
109 g_log.debug("Emptying the Usage data service (UsageServiceCache).");
110 UsageService::Instance().clear();
111 }
112 setProperty("FilesRemoved", filesRemoved);
113}
114
120int ClearCache::deleteFiles(const std::string &path, const std::string &pattern) const {
121 int filesDeleted = 0;
122
123 Poco::Path pathPattern(path);
124 pathPattern.makeDirectory();
125 pathPattern.append(pattern);
126 std::set<std::string> files;
127 Poco::Glob::glob(pathPattern, files, Poco::Glob::GLOB_CASELESS);
128
129 for (const auto &filepath : files) {
130 Poco::File file(filepath);
131 g_log.debug("Deleting file " + filepath);
132 try {
133 file.remove();
134 filesDeleted++;
135 } catch (Poco::FileException &ex) {
136 g_log.warning("Cannot delete file " + filepath + ": " + ex.what());
137 }
138 }
139 return filesDeleted;
140}
141
142} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
ClearCache : TODO: DESCRIPTION.
Definition: ClearCache.h:16
int version() const override final
Algorithm's version for identification.
Definition: ClearCache.cpp:30
void exec() override final
Execute the algorithm.
Definition: ClearCache.cpp:58
const std::string summary() const override final
Algorithm's summary for use in the GUI and help.
Definition: ClearCache.cpp:36
const std::string category() const override final
Algorithm's category for identification.
Definition: ClearCache.cpp:33
void init() override final
Initialize the algorithm's properties.
Definition: ClearCache.cpp:41
int deleteFiles(const std::string &path, const std::string &pattern) const
Deletes files on the path that match the pattern provided.
Definition: ClearCache.cpp:120
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:114
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
STL namespace.
@ Output
An output workspace.
Definition: Property.h:54