CernVM-FS  2.13.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  FieldsMap map;
29  self.FillFieldsMap("self_", &map);
30  subtree.FillFieldsMap("subtree_", &map);
31  return map;
32 }
33 
34 template<typename FieldT>
35 std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const {
36  FieldsMap map_self;
37  FieldsMap map_subtree;
38  self.FillFieldsMap("", &map_self);
39  subtree.FillFieldsMap("", &map_subtree);
40 
41  std::map<std::string, FieldT> map_summed;
42 
43  typename FieldsMap::const_iterator i = map_self.begin();
44  typename FieldsMap::const_iterator iend = map_self.end();
45  for (; i != iend; ++i) {
46  map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]);
47  }
48 
49  return map_summed;
50 }
51 
52 template<typename FieldT>
54  std::map<std::string, FieldT> map_summed = GetValues();
55 
56  std::string result;
57  typename std::map<std::string, FieldT>::const_iterator j = map_summed.begin();
58  typename std::map<std::string, FieldT>::const_iterator jend = map_summed
59  .end();
60  for (; j != jend; ++j) {
61  result += j->first + "," + StringifyInt(j->second) + "\n";
62  }
63  return result;
64 }
65 
66 
67 template<typename FieldT>
69  const LegacyMode::Type legacy) {
70  bool retval = true;
71 
72  FieldsMap map = GetFieldsMap();
73  SqlGetCounter sql_counter(database);
74 
75  typename FieldsMap::const_iterator i = map.begin();
76  typename FieldsMap::const_iterator iend = map.end();
77  for (; i != iend; ++i) {
78  bool current_retval = sql_counter.BindCounter(i->first)
79  && sql_counter.FetchRow();
80 
81  // TODO(jblomer): nicify this
82  if (current_retval) {
83  *(const_cast<FieldT *>(i->second)) = static_cast<FieldT>(
84  sql_counter.GetCounter());
85  } else if (
86  (legacy == LegacyMode::kNoSpecials)
87  && ((i->first == "self_special")
88  || (i->first
89  == "subtree_special"))) { // NOLINT(bugprone-branch-clone)
90  *(const_cast<FieldT *>(i->second)) = FieldT(0);
91  current_retval = true;
92  } else if ((legacy == LegacyMode::kNoExternals)
93  && ((i->first == "self_special")
94  || (i->first == "subtree_special")
95  || (i->first == "self_external")
96  || (i->first == "subtree_external")
97  || (i->first == "self_external_file_size")
98  || (i->first == "subtree_external_file_size"))) {
99  *(const_cast<FieldT *>(i->second)) = FieldT(0);
100  current_retval = true;
101  } else if ((legacy == LegacyMode::kNoXattrs)
102  && ((i->first == "self_special")
103  || (i->first == "subtree_special")
104  || (i->first == "self_external")
105  || (i->first == "subtree_external")
106  || (i->first == "self_external_file_size")
107  || (i->first == "subtree_external_file_size")
108  || (i->first == "self_xattr")
109  || (i->first == "subtree_xattr"))) {
110  *(const_cast<FieldT *>(i->second)) = FieldT(0);
111  current_retval = true;
112  } else if (legacy == LegacyMode::kLegacy) {
113  *(const_cast<FieldT *>(i->second)) = FieldT(0);
114  current_retval = true;
115  }
116 
117  sql_counter.Reset();
118  retval = (retval) ? current_retval : false;
119  }
120 
121  return retval;
122 }
123 
124 
125 template<typename FieldT>
127  const CatalogDatabase &database) const {
128  bool retval = true;
129 
130  const FieldsMap map = GetFieldsMap();
131  SqlUpdateCounter sql_counter(database);
132 
133  typename FieldsMap::const_iterator i = map.begin();
134  typename FieldsMap::const_iterator iend = map.end();
135  for (; i != iend; ++i) {
136  const bool current_retval = sql_counter.BindCounter(i->first)
137  && sql_counter.BindDelta(*(i->second))
138  && sql_counter.Execute();
139  sql_counter.Reset();
140 
141  retval = (retval) ? current_retval : false;
142  }
143 
144  return retval;
145 }
146 
147 
148 template<typename FieldT>
150  const CatalogDatabase &database) const {
151  bool retval = true;
152 
153  const FieldsMap map = GetFieldsMap();
154  SqlCreateCounter sql_counter(database);
155 
156  typename FieldsMap::const_iterator i = map.begin();
157  typename FieldsMap::const_iterator iend = map.end();
158  for (; i != iend; ++i) {
159  const bool current_retval = sql_counter.BindCounter(i->first)
160  && sql_counter.BindInitialValue(*(i->second))
161  && sql_counter.Execute();
162  sql_counter.Reset();
163 
164  retval = (retval) ? current_retval : false;
165  }
166 
167  return retval;
168 }
169 
170 
171 template<typename FieldT>
173  self.Subtract(self);
174  subtree.Subtract(subtree);
175 }
176 
177 } // namespace catalog
178 
179 #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_
bool InsertIntoDatabase(const CatalogDatabase &database) const
bool BindCounter(const std::string &counter)
bool Execute()
Definition: sql.cc:41
bool Reset()
Definition: sql.cc:127
bool FetchRow()
Definition: sql.cc:61
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:77
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)