| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/catalog_counters_impl.h |
| Date: | 2026-07-19 02:35:15 |
| 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 | 12540 | TreeCountersBase<FieldT>::GetFieldsMap() const { | |
| 29 | 12540 | FieldsMap map; | |
| 30 |
2/4✓ Branch 2 taken 12540 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12540 times.
✗ Branch 6 not taken.
|
12540 | self.FillFieldsMap("self_", &map); |
| 31 |
2/4✓ Branch 2 taken 12540 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12540 times.
✗ Branch 6 not taken.
|
12540 | subtree.FillFieldsMap("subtree_", &map); |
| 32 | 12540 | return map; | |
| 33 | } | ||
| 34 | |||
| 35 | template<typename FieldT> | ||
| 36 | 169 | std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const { | |
| 37 | 169 | FieldsMap map_self; | |
| 38 | 169 | FieldsMap map_subtree; | |
| 39 |
2/4✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 169 times.
✗ Branch 6 not taken.
|
169 | self.FillFieldsMap("", &map_self); |
| 40 |
2/4✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 169 times.
✗ Branch 6 not taken.
|
169 | subtree.FillFieldsMap("", &map_subtree); |
| 41 | |||
| 42 | 169 | std::map<std::string, FieldT> map_summed; | |
| 43 | |||
| 44 | 169 | typename FieldsMap::const_iterator i = map_self.begin(); | |
| 45 | 169 | typename FieldsMap::const_iterator const iend = map_self.end(); | |
| 46 |
2/2✓ Branch 1 taken 2028 times.
✓ Branch 2 taken 169 times.
|
2197 | for (; i != iend; ++i) { |
| 47 |
3/6✓ Branch 2 taken 2028 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2028 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 2028 times.
✗ Branch 11 not taken.
|
2028 | map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]); |
| 48 | } | ||
| 49 | |||
| 50 | 338 | return map_summed; | |
| 51 | 169 | } | |
| 52 | |||
| 53 | template<typename FieldT> | ||
| 54 | 25 | std::string TreeCountersBase<FieldT>::GetCsvMap() const { | |
| 55 |
1/2✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
|
25 | std::map<std::string, FieldT> map_summed = GetValues(); |
| 56 | |||
| 57 | 25 | std::string result; | |
| 58 | 25 | typename std::map<std::string, FieldT>::const_iterator j = map_summed.begin(); | |
| 59 | 25 | typename std::map<std::string, FieldT>::const_iterator const jend = map_summed | |
| 60 | 25 | .end(); | |
| 61 |
2/2✓ Branch 1 taken 300 times.
✓ Branch 2 taken 25 times.
|
325 | for (; j != jend; ++j) { |
| 62 |
5/10✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 300 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 300 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 300 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 300 times.
✗ Branch 16 not taken.
|
300 | result += j->first + "," + StringifyInt(j->second) + "\n"; |
| 63 | } | ||
| 64 | 50 | return result; | |
| 65 | 25 | } | |
| 66 | |||
| 67 | |||
| 68 | template<typename FieldT> | ||
| 69 | 6887 | bool TreeCountersBase<FieldT>::ReadFromDatabase(const CatalogDatabase &database, | |
| 70 | const LegacyMode::Type legacy) { | ||
| 71 | 6887 | bool retval = true; | |
| 72 | |||
| 73 |
1/2✓ Branch 1 taken 6887 times.
✗ Branch 2 not taken.
|
6887 | FieldsMap map = GetFieldsMap(); |
| 74 |
1/2✓ Branch 1 taken 6887 times.
✗ Branch 2 not taken.
|
6887 | SqlGetCounter sql_counter(database); |
| 75 | |||
| 76 | 6887 | typename FieldsMap::const_iterator i = map.begin(); | |
| 77 | 6887 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 78 |
2/2✓ Branch 1 taken 165288 times.
✓ Branch 2 taken 6887 times.
|
172175 | for (; i != iend; ++i) { |
| 79 |
1/2✓ Branch 2 taken 165288 times.
✗ Branch 3 not taken.
|
165288 | bool current_retval = sql_counter.BindCounter(i->first) |
| 80 |
3/6✓ Branch 0 taken 165288 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 165288 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 165288 times.
✗ Branch 6 not taken.
|
165288 | && sql_counter.FetchRow(); |
| 81 | |||
| 82 | // TODO(jblomer): nicify this | ||
| 83 |
1/2✓ Branch 0 taken 165288 times.
✗ Branch 1 not taken.
|
165288 | if (current_retval) { |
| 84 | 165288 | *(const_cast<FieldT *>(i->second)) = static_cast<FieldT>( | |
| 85 |
1/2✓ Branch 1 taken 165288 times.
✗ Branch 2 not taken.
|
165288 | 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 165288 times.
✗ Branch 2 not taken.
|
165288 | sql_counter.Reset(); |
| 119 |
1/2✓ Branch 0 taken 165288 times.
✗ Branch 1 not taken.
|
330576 | retval = (retval) ? current_retval : false; |
| 120 | } | ||
| 121 | |||
| 122 | 6887 | return retval; | |
| 123 | 172175 | } | |
| 124 | |||
| 125 | |||
| 126 | template<typename FieldT> | ||
| 127 | 2956 | bool TreeCountersBase<FieldT>::WriteToDatabase( | |
| 128 | const CatalogDatabase &database) const { | ||
| 129 | 2956 | bool retval = true; | |
| 130 | |||
| 131 |
1/2✓ Branch 1 taken 2956 times.
✗ Branch 2 not taken.
|
2956 | const FieldsMap map = GetFieldsMap(); |
| 132 |
1/2✓ Branch 1 taken 2956 times.
✗ Branch 2 not taken.
|
2956 | SqlUpdateCounter sql_counter(database); |
| 133 | |||
| 134 | 2956 | typename FieldsMap::const_iterator i = map.begin(); | |
| 135 | 2956 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 136 |
2/2✓ Branch 1 taken 70944 times.
✓ Branch 2 taken 2956 times.
|
73900 | for (; i != iend; ++i) { |
| 137 |
1/2✓ Branch 2 taken 70944 times.
✗ Branch 3 not taken.
|
70944 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 138 |
2/4✓ Branch 2 taken 70944 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 70944 times.
✗ Branch 5 not taken.
|
70944 | && sql_counter.BindDelta(*(i->second)) |
| 139 |
3/6✓ Branch 0 taken 70944 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 70944 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 70944 times.
✗ Branch 6 not taken.
|
141888 | && sql_counter.Execute(); |
| 140 |
1/2✓ Branch 1 taken 70944 times.
✗ Branch 2 not taken.
|
70944 | sql_counter.Reset(); |
| 141 | |||
| 142 |
1/2✓ Branch 0 taken 70944 times.
✗ Branch 1 not taken.
|
70944 | retval = (retval) ? current_retval : false; |
| 143 | } | ||
| 144 | |||
| 145 | 2956 | return retval; | |
| 146 | 2956 | } | |
| 147 | |||
| 148 | |||
| 149 | template<typename FieldT> | ||
| 150 | 2522 | bool TreeCountersBase<FieldT>::InsertIntoDatabase( | |
| 151 | const CatalogDatabase &database) const { | ||
| 152 | 2522 | bool retval = true; | |
| 153 | |||
| 154 |
1/2✓ Branch 1 taken 2522 times.
✗ Branch 2 not taken.
|
2522 | const FieldsMap map = GetFieldsMap(); |
| 155 |
1/2✓ Branch 1 taken 2522 times.
✗ Branch 2 not taken.
|
2522 | SqlCreateCounter sql_counter(database); |
| 156 | |||
| 157 | 2522 | typename FieldsMap::const_iterator i = map.begin(); | |
| 158 | 2522 | typename FieldsMap::const_iterator const iend = map.end(); | |
| 159 |
2/2✓ Branch 1 taken 60528 times.
✓ Branch 2 taken 2522 times.
|
63050 | for (; i != iend; ++i) { |
| 160 |
1/2✓ Branch 2 taken 60528 times.
✗ Branch 3 not taken.
|
60528 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 161 |
2/4✓ Branch 2 taken 60528 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60528 times.
✗ Branch 5 not taken.
|
60528 | && sql_counter.BindInitialValue(*(i->second)) |
| 162 |
3/6✓ Branch 0 taken 60528 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 60528 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 60528 times.
✗ Branch 6 not taken.
|
121056 | && sql_counter.Execute(); |
| 163 |
1/2✓ Branch 1 taken 60528 times.
✗ Branch 2 not taken.
|
60528 | sql_counter.Reset(); |
| 164 | |||
| 165 |
1/2✓ Branch 0 taken 60528 times.
✗ Branch 1 not taken.
|
60528 | retval = (retval) ? current_retval : false; |
| 166 | } | ||
| 167 | |||
| 168 | 2522 | return retval; | |
| 169 | 2522 | } | |
| 170 | |||
| 171 | |||
| 172 | template<typename FieldT> | ||
| 173 | 1111 | void TreeCountersBase<FieldT>::SetZero() { | |
| 174 | 1111 | self.Subtract(self); | |
| 175 | 1111 | subtree.Subtract(subtree); | |
| 176 | 1111 | } | |
| 177 | |||
| 178 | } // namespace catalog | ||
| 179 | |||
| 180 | #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_ | ||
| 181 |