CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
catalog_counters_impl.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CATALOG_COUNTERS_IMPL_H_
6 #define CVMFS_CATALOG_COUNTERS_IMPL_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "catalog_sql.h"
12 #include "util/string.h"
13 
14 namespace catalog {
15 
16 template<typename FieldT>
17 FieldT TreeCountersBase<FieldT>::Get(const std::string &key) const {
18  FieldsMap map = GetFieldsMap();
19  if (map.find(key) != map.end())
20  return *map[key];
21  return FieldT(0);
22 }
23 
24 
25 template<typename FieldT>
28 {
29  FieldsMap map;
30  self.FillFieldsMap("self_", &map);
31  subtree.FillFieldsMap("subtree_", &map);
32  return map;
33 }
34 
35 template<typename FieldT>
36 std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const {
37  FieldsMap map_self;
38  FieldsMap map_subtree;
39  self.FillFieldsMap("", &map_self);
40  subtree.FillFieldsMap("", &map_subtree);
41 
42  std::map<std::string, FieldT> map_summed;
43 
44  typename FieldsMap::const_iterator i = map_self.begin();
45  typename FieldsMap::const_iterator iend = map_self.end();
46  for (; i != iend; ++i) {
47  map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]);
48  }
49 
50  return map_summed;
51 }
52 
53 template<typename FieldT>
55  std::map<std::string, FieldT> map_summed = GetValues();
56 
57  std::string result;
58  typename std::map<std::string, FieldT>::const_iterator j =
59  map_summed.begin();
60  typename std::map<std::string, FieldT>::const_iterator jend =
61  map_summed.end();
62  for (; j != jend; ++j) {
63  result += j->first + "," + StringifyInt(j->second) + "\n";
64  }
65  return result;
66 }
67 
68 
69 template<typename FieldT>
71  const CatalogDatabase &database,
72  const LegacyMode::Type legacy)
73 {
74  bool retval = true;
75 
76  FieldsMap map = GetFieldsMap();
77  SqlGetCounter sql_counter(database);
78 
79  typename FieldsMap::const_iterator i = map.begin();
80  typename FieldsMap::const_iterator iend = map.end();
81  for (; i != iend; ++i) {
82  bool current_retval = sql_counter.BindCounter(i->first) &&
83  sql_counter.FetchRow();
84 
85  // TODO(jblomer): nicify this
86  if (current_retval) {
87  *(const_cast<FieldT*>(i->second)) =
88  static_cast<FieldT>(sql_counter.GetCounter());
89  } else if ( (legacy == LegacyMode::kNoSpecials) &&
90  ((i->first == "self_special") ||
91  (i->first == "subtree_special")) )
92  { // NOLINT(bugprone-branch-clone)
93  *(const_cast<FieldT*>(i->second)) = FieldT(0);
94  current_retval = true;
95  } else if ( (legacy == LegacyMode::kNoExternals) &&
96  ((i->first == "self_special")
97  || (i->first == "subtree_special") ||
98  (i->first == "self_external")
99  || (i->first == "subtree_external") ||
100  (i->first == "self_external_file_size")
101  || (i->first == "subtree_external_file_size")) )
102  {
103  *(const_cast<FieldT*>(i->second)) = FieldT(0);
104  current_retval = true;
105  } else if ( (legacy == LegacyMode::kNoXattrs) &&
106  ((i->first == "self_special")
107  || (i->first == "subtree_special") ||
108  (i->first == "self_external")
109  || (i->first == "subtree_external") ||
110  (i->first == "self_external_file_size")
111  || (i->first == "subtree_external_file_size") ||
112  (i->first == "self_xattr") || (i->first == "subtree_xattr")) )
113  {
114  *(const_cast<FieldT*>(i->second)) = FieldT(0);
115  current_retval = true;
116  } else if (legacy == LegacyMode::kLegacy) {
117  *(const_cast<FieldT*>(i->second)) = FieldT(0);
118  current_retval = true;
119  }
120 
121  sql_counter.Reset();
122  retval = (retval) ? current_retval : false;
123  }
124 
125  return retval;
126 }
127 
128 
129 template<typename FieldT>
131  const CatalogDatabase &database) const
132 {
133  bool retval = true;
134 
135  const FieldsMap map = GetFieldsMap();
136  SqlUpdateCounter sql_counter(database);
137 
138  typename FieldsMap::const_iterator i = map.begin();
139  typename FieldsMap::const_iterator iend = map.end();
140  for (; i != iend; ++i) {
141  const bool current_retval =
142  sql_counter.BindCounter(i->first) &&
143  sql_counter.BindDelta(*(i->second)) &&
144  sql_counter.Execute();
145  sql_counter.Reset();
146 
147  retval = (retval) ? current_retval : false;
148  }
149 
150  return retval;
151 }
152 
153 
154 template<typename FieldT>
156  const CatalogDatabase &database) const
157 {
158  bool retval = true;
159 
160  const FieldsMap map = GetFieldsMap();
161  SqlCreateCounter sql_counter(database);
162 
163  typename FieldsMap::const_iterator i = map.begin();
164  typename FieldsMap::const_iterator iend = map.end();
165  for (; i != iend; ++i) {
166  const bool current_retval =
167  sql_counter.BindCounter(i->first) &&
168  sql_counter.BindInitialValue(*(i->second)) &&
169  sql_counter.Execute();
170  sql_counter.Reset();
171 
172  retval = (retval) ? current_retval : false;
173  }
174 
175  return retval;
176 }
177 
178 
179 template<typename FieldT>
181  self.Subtract(self);
182  subtree.Subtract(subtree);
183 }
184 
185 } // namespace catalog
186 
187 #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_
bool InsertIntoDatabase(const CatalogDatabase &database) const
bool BindCounter(const std::string &counter)
bool Execute()
Definition: sql.cc:42
bool Reset()
Definition: sql.cc:126
bool FetchRow()
Definition: sql.cc:62
std::map< std::string, const FieldT * > FieldsMap
uint64_t GetCounter() const
bool WriteToDatabase(const CatalogDatabase &database) const
bool BindCounter(const std::string &counter)
bool BindCounter(const std::string &counter)
std::map< std::string, FieldT > GetValues() const
string StringifyInt(const int64_t value)
Definition: string.cc:78
bool ReadFromDatabase(const CatalogDatabase &database, const LegacyMode::Type legacy=LegacyMode::kNoLegacy)
FieldT Get(const std::string &key) const
bool BindDelta(const int64_t delta)
std::string GetCsvMap() const
bool BindInitialValue(const int64_t value)