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