Belle II Software development
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/logging/Logger.h>
12
13using namespace std;
14using namespace Belle2;
15
16REG_MODULE(PruneDataStore);
17
19 Module()
20{
21 setDescription(R"DOC(
22 Clears the content of the DataStore while it keeps entries listed in the matchEntries option.
23 The EventMetaData object will always be kept, as it is required by the framework to properly
24 work with the DataStore.
25
26 This logic can be inverted to remove only the entries matched by the regex
27 in the matchEntries parameter by setting the parameter to
28 keepMatchedEntries to False.
29
30 Note:
31 Also all Relations will be cleared if they are not matched by one entry
32 in the matchEntries list. You have to ensure the objects referenced by
33 kept relations are also matched by one entry in the keepEntries list so
34 a relation does not point to nirvana.
35 )DOC");
36 addParam("matchEntries", m_matchEntries,
37 "name of all DataStore entries (with regular expression syntax) to match. "
38 "For example, you can use 'Raw.*' to match all Raw-Objects.",
40 addParam("keepMatchedEntries", m_keepMatchedEntries,
41 "If true, all entries matched by the regular expression are kept. "
42 "If false, matched entries will be removed.",
45
46}
47
49{
50 // prepare the regex_matchers, otherwise this nede to be done for each DataStore item
51 for (auto& kEntry : m_matchEntries) {
52 m_compiled_regex.push_back(compileAndCatch(kEntry));
53 }
54 // also get the regex for the implicit keeps
55 for (auto& kEntry : m_keepEntriesImplicit) {
57 }
58}
59
60std::regex PruneDataStoreModule::compileAndCatch(std::string& regexString) const
61{
62 try {
63 return std::regex(regexString);
64 } catch (const std::regex_error& e) {
65 B2FATAL("Regex '" << regexString << "' cannot be compiled: " << e.what());
66 // keep the compiler happy and return something
67 return std::regex();
68 }
69}
70
72{
74
75 // iterate through all StoreEntries and check the Regex expression for each entry
76 for (auto& datastore_item : storemap) {
77 std::string const& datastore_key = datastore_item.first;
78
79 // check if this entry is in our to keep list
80 bool toKeep = !m_keepMatchedEntries;
81 for (auto const& regx : m_compiled_regex) {
82 if (std::regex_match(datastore_key, regx)) {
83 toKeep = m_keepMatchedEntries;
84 }
85 }
86
87 // check for implicit keeps
88 for (auto const& regx : m_compiled_regex_implicit) {
89 if (std::regex_match(datastore_key, regx)) {
90 // always keep, no matter of the keepMatchedEntries configuration
91 toKeep = true;
92 }
93 }
94
95 if (toKeep) {
96 B2DEBUG(100,
97 "StoreArray entry " << datastore_key << " will be not pruned from the datastore");
98 } else {
99 B2DEBUG(100,
100 "StoreArray entry " << datastore_key << " will be pruned from the datastore");
101 }
102
103 if (!toKeep) {
104 // recreate to clear its content
105 datastore_item.second.invalidate();
106 }
107 }
108
109}
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:54
Base class for Modules.
Definition: Module.h:72
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
@ 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.
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< 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.
std::regex compileAndCatch(std::string &regexString) const
Compile a regex expression and catch the exception if the regex string is not valid.
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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
STL namespace.