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