Belle II Software  release-06-00-14
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 
13 using namespace std;
14 using namespace Belle2;
15 
16 REG_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.",
39  m_matchEntries);
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.",
43  m_keepMatchedEntries);
44  setPropertyFlags(c_ParallelProcessingCertified);
45 
46 }
47 
48 void PruneDataStoreModule::initialize()
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) {
56  m_compiled_regex_implicit.push_back(compileAndCatch(kEntry));
57  }
58 }
59 
60 std::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 
71 void PruneDataStoreModule::event()
72 {
73  auto& storemap = DataStore::Instance().getStoreEntryMap(DataStore::c_Event);
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 }
Base class for Modules.
Definition: Module.h:72
Clears the content of the DataStore while it keeps entries matching the regex expression in the match...
#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.