Belle II Software prerelease-11-00-00d
PruneDataStoreModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/modules/core/PruneDataStoreModule.h>
10#include <framework/datastore/DataStore.h>
11#include <framework/datastore/StoreEntry.h>
12#include <framework/logging/LogSystem.h>
13#include <framework/logging/Logger.h>
14
15using namespace std;
16using namespace Belle2;
17
18REG_MODULE(PruneDataStore);
19
21 Module()
22{
23 setDescription(R"DOC(
24 Clears the content of the DataStore while it keeps entries listed in the matchEntries option.
25 The EventMetaData object will always be kept, as it is required by the framework to properly
26 work with the DataStore.
27
28 This logic can be inverted to remove only the entries matched by the regex
29 in the matchEntries parameter by setting the parameter to
30 keepMatchedEntries to False.
31
32 Note:
33 Also all Relations will be cleared if they are not matched by one entry
34 in the matchEntries list. You have to ensure the objects referenced by
35 kept relations are also matched by one entry in the keepEntries list so
36 a relation does not point to nirvana.
37 )DOC");
38 addParam("matchEntries", m_matchEntries,
39 "name of all DataStore entries (with regular expression syntax) to match. "
40 "For example, you can use 'Raw.*' to match all Raw-Objects.",
42 addParam("keepMatchedEntries", m_keepMatchedEntries,
43 "If true, all entries matched by the regular expression are kept. "
44 "If false, matched entries will be removed.",
47
48}
49
51{
52 // prepare the regex_matchers, otherwise this nede to be done for each DataStore item
53 for (auto& kEntry : m_matchEntries) {
54 m_compiled_regex.push_back(compileAndCatch(kEntry));
55 }
56 // also get the regex for the implicit keeps
57 for (auto& kEntry : m_keepEntriesImplicit) {
59 }
60 // the cached prune decisions of a previous run are not valid any more
61 m_entriesToPrune.clear();
62 m_entriesToKeep.clear();
63 m_cacheValid = false;
64}
65
66std::regex PruneDataStoreModule::compileAndCatch(std::string& regexString) const
67{
68 try {
69 return std::regex(regexString);
70 } catch (const std::regex_error& e) {
71 B2FATAL("Regex '" << regexString << "' cannot be compiled: " << e.what());
72 // keep the compiler happy and return something
73 return std::regex();
74 }
75}
76
78{
79 m_entriesToPrune.clear();
80 m_entriesToKeep.clear();
81
82 // iterate through all StoreEntries and check the Regex expression for each entry
83 for (auto& datastore_item : storemap) {
84 std::string const& datastore_key = datastore_item.first;
85
86 // check if this entry is in our to keep list
87 bool toKeep = !m_keepMatchedEntries;
88 for (auto const& regx : m_compiled_regex) {
89 if (std::regex_match(datastore_key, regx)) {
90 toKeep = m_keepMatchedEntries;
91 }
92 }
93
94 // check for implicit keeps
95 for (auto const& regx : m_compiled_regex_implicit) {
96 if (std::regex_match(datastore_key, regx)) {
97 // always keep, no matter of the keepMatchedEntries configuration
98 toKeep = true;
99 }
100 }
101
102 if (toKeep) {
103 m_entriesToKeep.push_back(&datastore_key);
104 } else {
105 m_entriesToPrune.push_back(&datastore_item.second);
106 }
107 }
108
109 m_cacheValid = true;
110}
111
113{
115
116 // Matching the regular expressions against every DataStore key is expensive,
117 // while the set of DataStore entries is frozen once the initialize() methods
118 // of all modules have been called: everything which registers an entry
119 // (DataStore::registerEntry, DataStore::copyEntriesTo) is only reached from
120 // there. So the regular expressions are evaluated on the first event only.
121 if (not m_cacheValid) {
122 updateCache(storemap);
123 }
124
125 if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 100, PACKAGENAME())) {
126 for (auto const* datastore_key : m_entriesToKeep) {
127 B2DEBUG(100,
128 "StoreArray entry " << *datastore_key << " will be not pruned from the datastore");
129 }
130 for (auto const* entry : m_entriesToPrune) {
131 B2DEBUG(100,
132 "StoreArray entry " << entry->name << " will be pruned from the datastore");
133 }
134 }
135
136 for (auto* entry : m_entriesToPrune) {
137 // recreate to clear its content
138 entry->invalidate();
139 }
140}
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition DataStore.h:325
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
static DataStore & Instance()
Instance of singleton Store.
Definition DataStore.cc:53
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition DataStore.h:87
@ c_Debug
Debug: for code development.
Definition LogConfig.h:26
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition LogSystem.cc:28
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
void initialize() override
Prepare regex checks.
bool m_cacheValid
Whether the caches above have already been filled for this run.
void event() override
Prune datastore.
std::vector< std::string > m_keepEntriesImplicit
Branches to always keep because the are required by the framework to properly work with the datastore...
std::vector< const std::string * > m_entriesToKeep
Names of the DataStore entries which are kept, only used for debug output.
std::vector< std::regex > m_compiled_regex_implicit
Caching the regex expression for the keep check.
bool m_keepMatchedEntries
If true, all entries matched by the RegEx expression are kept.
std::vector< std::regex > m_compiled_regex
Caching the regex expression for the keep check.
std::vector< std::string > m_matchEntries
Storing the option of branches to keep.
void updateCache(DataStore::StoreEntryMap &storemap)
Evaluate the regular expressions for all entries of the given DataStore map and fill the m_entriesToP...
std::regex compileAndCatch(std::string &regexString) const
Compile a regex expression and catch the exception if the regex string is not valid.
std::vector< DataStore::StoreEntry * > m_entriesToPrune
DataStore entries which have to be pruned.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.