| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/catalog_counters_impl.h |
| Date: | 2026-06-28 02:36:10 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 75 | 109 | 68.8% |
| Branches: | 57 | 159 | 35.8% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 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_counters.h" | ||
| 12 | #include "catalog_sql.h" | ||
| 13 | #include "util/string.h" | ||
| 14 | |||
| 15 | namespace catalog { | ||
| 16 | |||
| 17 | template<typename FieldT> | ||
| 18 | ✗ | FieldT TreeCountersBase<FieldT>::Get(const std::string &key) const { | |
| 19 | ✗ | FieldsMap map = GetFieldsMap(); | |
| 20 | ✗ | if (map.find(key) != map.end()) | |
| 21 | ✗ | return *map[key]; | |
| 22 | ✗ | return FieldT(0); | |
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | template<typename FieldT> | ||
| 27 | typename TreeCountersBase<FieldT>::FieldsMap | ||
| 28 | 14500 | TreeCountersBase<FieldT>::GetFieldsMap() const { | |
| 29 | 14500 | FieldsMap map; | |
| 30 |
2/4✓ Branch 2 taken 14500 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14500 times.
✗ Branch 6 not taken.
|
14500 | self.FillFieldsMap("self_", &map); |
| 31 |
2/4✓ Branch 2 taken 14500 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14500 times.
✗ Branch 6 not taken.
|
14500 | subtree.FillFieldsMap("subtree_", &map); |
| 32 | 14500 | return map; | |
| 33 | } | ||
| 34 | |||
| 35 | template<typename FieldT> | ||
| 36 | 142 | std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const { | |
| 37 | 142 | FieldsMap map_self; | |
| 38 | 142 | FieldsMap map_subtree; | |
| 39 |
2/4✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 142 times.
✗ Branch 6 not taken.
|
142 | self.FillFieldsMap("", &map_self); |
| 40 |
2/4✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 142 times.
✗ Branch 6 not taken.
|
142 | subtree.FillFieldsMap("", &map_subtree); |
| 41 | |||
| 42 | 142 | std::map<std::string, FieldT> map_summed; | |
| 43 | |||
| 44 | 142 | typename FieldsMap::const_iterator i = map_self.begin(); | |
| 45 | 142 | typename FieldsMap::const_iterator const iend = map_self.end(); | |
| 46 |
2/2✓ Branch 1 taken 1704 times.
✓ Branch 2 taken 142 times.
|
1846 | for (; i != iend; ++i) { |
| 47 |
3/6✓ Branch 2 taken 1704 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1704 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 1704 times.
✗ Branch 11 not taken.
|
1704 | map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]); |
| 48 | } | ||
| 49 | |||
| 50 | 284 | return map_summed; | |
| 51 | 142 | } | |
| 52 | |||
| 53 | template<typename FieldT> | ||
| 54 | 1 | std::string TreeCountersBase<FieldT>::GetCsvMap() const { | |
| 55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::map<std::string, FieldT> map_summed = GetValues(); |
| 56 | |||
| 57 | 1 | std::string result; | |
| 58 | 1 | typename std::map<std::string, FieldT>::const_iterator j = map_summed.begin(); | |
| 59 | 1 | typename std::map<std::string, FieldT>::const_iterator const jend = map_summed | |
| 60 | 1 | .end(); | |
| 61 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
|
13 | for (; j != jend; ++j) { |
| 62 |
5/10✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 12 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 12 times.
✗ Branch 16 not taken.
|
12 | result += j->first + "," + StringifyInt(j->second) + "\n"; |
| 63 | } | ||
| 64 | 2 | return result; | |
| 65 | 1 | } | |
| 66 | |||
| 67 | |||
| 68 | template<typename FieldT> | ||
| 69 | 8205 | bool TreeCountersBase<FieldT>::ReadFromDatabase(const CatalogDatabase &database, | |
| 70 | const LegacyMode::Type legacy) { | ||
| 71 | 8205 | bool retval = true; | |
| 72 | |||
| 73 |
1/2✓ Branch 1 taken 8205 times.
✗ Branch 2 not taken.
|
8205 | FieldsMap map = GetFieldsMap(); |
| 74 |
1/2✓ Branch 1 taken 8205 times.
✗ Branch 2 not taken.
|
8205 | SqlGetCounter sql_counter(database); |
| 75 | |||
| 76 | 8205 | typename FieldsMap::const_iterator i = map.begin(); | |
| 77 | 8205 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 78 |
2/2✓ Branch 1 taken 196920 times.
✓ Branch 2 taken 8205 times.
|
205125 | for (; i != iend; ++i) { |
| 79 |
1/2✓ Branch 2 taken 196920 times.
✗ Branch 3 not taken.
|
196920 | bool current_retval = sql_counter.BindCounter(i->first) |
| 80 |
3/6✓ Branch 0 taken 196920 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 196920 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 196920 times.
✗ Branch 6 not taken.
|
196920 | && sql_counter.FetchRow(); |
| 81 | |||
| 82 | // TODO(jblomer): nicify this | ||
| 83 |
1/2✓ Branch 0 taken 196920 times.
✗ Branch 1 not taken.
|
196920 | if (current_retval) { |
| 84 | 196920 | *(const_cast<FieldT *>(i->second)) = static_cast<FieldT>( | |
| 85 |
1/2✓ Branch 1 taken 196920 times.
✗ Branch 2 not taken.
|
196920 | sql_counter.GetCounter()); |
| 86 | ✗ | } else if ( | |
| 87 | (legacy == LegacyMode::kNoSpecials) | ||
| 88 | ✗ | && ((i->first == "self_special") | |
| 89 | ✗ | || (i->first | |
| 90 | ✗ | == "subtree_special"))) { // NOLINT(bugprone-branch-clone) | |
| 91 | ✗ | *(const_cast<FieldT *>(i->second)) = FieldT(0); | |
| 92 | ✗ | current_retval = true; | |
| 93 | ✗ | } else if ((legacy == LegacyMode::kNoExternals) | |
| 94 | ✗ | && ((i->first == "self_special") | |
| 95 | ✗ | || (i->first == "subtree_special") | |
| 96 | ✗ | || (i->first == "self_external") | |
| 97 | ✗ | || (i->first == "subtree_external") | |
| 98 | ✗ | || (i->first == "self_external_file_size") | |
| 99 | ✗ | || (i->first == "subtree_external_file_size"))) { | |
| 100 | ✗ | *(const_cast<FieldT *>(i->second)) = FieldT(0); | |
| 101 | ✗ | current_retval = true; | |
| 102 | ✗ | } else if ((legacy == LegacyMode::kNoXattrs) | |
| 103 | ✗ | && ((i->first == "self_special") | |
| 104 | ✗ | || (i->first == "subtree_special") | |
| 105 | ✗ | || (i->first == "self_external") | |
| 106 | ✗ | || (i->first == "subtree_external") | |
| 107 | ✗ | || (i->first == "self_external_file_size") | |
| 108 | ✗ | || (i->first == "subtree_external_file_size") | |
| 109 | ✗ | || (i->first == "self_xattr") | |
| 110 | ✗ | || (i->first == "subtree_xattr"))) { | |
| 111 | ✗ | *(const_cast<FieldT *>(i->second)) = FieldT(0); | |
| 112 | ✗ | current_retval = true; | |
| 113 | ✗ | } else if (legacy == LegacyMode::kLegacy) { | |
| 114 | ✗ | *(const_cast<FieldT *>(i->second)) = FieldT(0); | |
| 115 | ✗ | current_retval = true; | |
| 116 | } | ||
| 117 | |||
| 118 |
1/2✓ Branch 1 taken 196920 times.
✗ Branch 2 not taken.
|
196920 | sql_counter.Reset(); |
| 119 |
1/2✓ Branch 0 taken 196920 times.
✗ Branch 1 not taken.
|
393840 | retval = (retval) ? current_retval : false; |
| 120 | } | ||
| 121 | |||
| 122 | 8205 | return retval; | |
| 123 | 205125 | } | |
| 124 | |||
| 125 | |||
| 126 | template<typename FieldT> | ||
| 127 | 3419 | bool TreeCountersBase<FieldT>::WriteToDatabase( | |
| 128 | const CatalogDatabase &database) const { | ||
| 129 | 3419 | bool retval = true; | |
| 130 | |||
| 131 |
1/2✓ Branch 1 taken 3419 times.
✗ Branch 2 not taken.
|
3419 | const FieldsMap map = GetFieldsMap(); |
| 132 |
1/2✓ Branch 1 taken 3419 times.
✗ Branch 2 not taken.
|
3419 | SqlUpdateCounter sql_counter(database); |
| 133 | |||
| 134 | 3419 | typename FieldsMap::const_iterator i = map.begin(); | |
| 135 | 3419 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 136 |
2/2✓ Branch 1 taken 82056 times.
✓ Branch 2 taken 3419 times.
|
85475 | for (; i != iend; ++i) { |
| 137 |
1/2✓ Branch 2 taken 82056 times.
✗ Branch 3 not taken.
|
82056 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 138 |
2/4✓ Branch 2 taken 82056 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 82056 times.
✗ Branch 5 not taken.
|
82056 | && sql_counter.BindDelta(*(i->second)) |
| 139 |
3/6✓ Branch 0 taken 82056 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 82056 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 82056 times.
✗ Branch 6 not taken.
|
164112 | && sql_counter.Execute(); |
| 140 |
1/2✓ Branch 1 taken 82056 times.
✗ Branch 2 not taken.
|
82056 | sql_counter.Reset(); |
| 141 | |||
| 142 |
1/2✓ Branch 0 taken 82056 times.
✗ Branch 1 not taken.
|
82056 | retval = (retval) ? current_retval : false; |
| 143 | } | ||
| 144 | |||
| 145 | 3419 | return retval; | |
| 146 | 3419 | } | |
| 147 | |||
| 148 | |||
| 149 | template<typename FieldT> | ||
| 150 | 2869 | bool TreeCountersBase<FieldT>::InsertIntoDatabase( | |
| 151 | const CatalogDatabase &database) const { | ||
| 152 | 2869 | bool retval = true; | |
| 153 | |||
| 154 |
1/2✓ Branch 1 taken 2869 times.
✗ Branch 2 not taken.
|
2869 | const FieldsMap map = GetFieldsMap(); |
| 155 |
1/2✓ Branch 1 taken 2869 times.
✗ Branch 2 not taken.
|
2869 | SqlCreateCounter sql_counter(database); |
| 156 | |||
| 157 | 2869 | typename FieldsMap::const_iterator i = map.begin(); | |
| 158 | 2869 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 159 |
2/2✓ Branch 1 taken 68856 times.
✓ Branch 2 taken 2869 times.
|
71725 | for (; i != iend; ++i) { |
| 160 |
1/2✓ Branch 2 taken 68856 times.
✗ Branch 3 not taken.
|
68856 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 161 |
2/4✓ Branch 2 taken 68856 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68856 times.
✗ Branch 5 not taken.
|
68856 | && sql_counter.BindInitialValue(*(i->second)) |
| 162 |
3/6✓ Branch 0 taken 68856 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 68856 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 68856 times.
✗ Branch 6 not taken.
|
137712 | && sql_counter.Execute(); |
| 163 |
1/2✓ Branch 1 taken 68856 times.
✗ Branch 2 not taken.
|
68856 | sql_counter.Reset(); |
| 164 | |||
| 165 |
1/2✓ Branch 0 taken 68856 times.
✗ Branch 1 not taken.
|
68856 | retval = (retval) ? current_retval : false; |
| 166 | } | ||
| 167 | |||
| 168 | 2869 | return retval; | |
| 169 | 2869 | } | |
| 170 | |||
| 171 | |||
| 172 | template<typename FieldT> | ||
| 173 | 1206 | void TreeCountersBase<FieldT>::SetZero() { | |
| 174 | 1206 | self.Subtract(self); | |
| 175 | 1206 | subtree.Subtract(subtree); | |
| 176 | 1206 | } | |
| 177 | |||
| 178 | } // namespace catalog | ||
| 179 | |||
| 180 | #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_ | ||
| 181 |