| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/catalog_counters_impl.h |
| Date: | 2025-12-21 02:39: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 | 13527 | TreeCountersBase<FieldT>::GetFieldsMap() const { | |
| 28 | 13527 | FieldsMap map; | |
| 29 |
2/4✓ Branch 2 taken 13527 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13527 times.
✗ Branch 6 not taken.
|
13527 | self.FillFieldsMap("self_", &map); |
| 30 |
2/4✓ Branch 2 taken 13527 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13527 times.
✗ Branch 6 not taken.
|
13527 | subtree.FillFieldsMap("subtree_", &map); |
| 31 | 13527 | return map; | |
| 32 | } | ||
| 33 | |||
| 34 | template<typename FieldT> | ||
| 35 | 70 | std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const { | |
| 36 | 70 | FieldsMap map_self; | |
| 37 | 70 | FieldsMap map_subtree; | |
| 38 |
2/4✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 70 times.
✗ Branch 6 not taken.
|
70 | self.FillFieldsMap("", &map_self); |
| 39 |
2/4✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 70 times.
✗ Branch 6 not taken.
|
70 | subtree.FillFieldsMap("", &map_subtree); |
| 40 | |||
| 41 | 70 | std::map<std::string, FieldT> map_summed; | |
| 42 | |||
| 43 | 70 | typename FieldsMap::const_iterator i = map_self.begin(); | |
| 44 | 70 | typename FieldsMap::const_iterator iend = map_self.end(); | |
| 45 |
2/2✓ Branch 1 taken 840 times.
✓ Branch 2 taken 70 times.
|
910 | for (; i != iend; ++i) { |
| 46 |
3/6✓ Branch 2 taken 840 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 840 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 840 times.
✗ Branch 11 not taken.
|
840 | map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]); |
| 47 | } | ||
| 48 | |||
| 49 | 140 | return map_summed; | |
| 50 | 70 | } | |
| 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 | 7440 | bool TreeCountersBase<FieldT>::ReadFromDatabase(const CatalogDatabase &database, | |
| 69 | const LegacyMode::Type legacy) { | ||
| 70 | 7440 | bool retval = true; | |
| 71 | |||
| 72 |
1/2✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
|
7440 | FieldsMap map = GetFieldsMap(); |
| 73 |
1/2✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
|
7440 | SqlGetCounter sql_counter(database); |
| 74 | |||
| 75 | 7440 | typename FieldsMap::const_iterator i = map.begin(); | |
| 76 | 7440 | typename FieldsMap::const_iterator iend = map.end(); | |
| 77 |
2/2✓ Branch 1 taken 178560 times.
✓ Branch 2 taken 7440 times.
|
186000 | for (; i != iend; ++i) { |
| 78 |
1/2✓ Branch 2 taken 178560 times.
✗ Branch 3 not taken.
|
178560 | bool current_retval = sql_counter.BindCounter(i->first) |
| 79 |
3/6✓ Branch 0 taken 178560 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 178560 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 178560 times.
✗ Branch 6 not taken.
|
178560 | && sql_counter.FetchRow(); |
| 80 | |||
| 81 | // TODO(jblomer): nicify this | ||
| 82 |
1/2✓ Branch 0 taken 178560 times.
✗ Branch 1 not taken.
|
178560 | if (current_retval) { |
| 83 | 178560 | *(const_cast<FieldT *>(i->second)) = static_cast<FieldT>( | |
| 84 |
1/2✓ Branch 1 taken 178560 times.
✗ Branch 2 not taken.
|
178560 | 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 178560 times.
✗ Branch 2 not taken.
|
178560 | sql_counter.Reset(); |
| 118 |
1/2✓ Branch 0 taken 178560 times.
✗ Branch 1 not taken.
|
357120 | retval = (retval) ? current_retval : false; |
| 119 | } | ||
| 120 | |||
| 121 | 7440 | return retval; | |
| 122 | 186000 | } | |
| 123 | |||
| 124 | |||
| 125 | template<typename FieldT> | ||
| 126 | 2680 | bool TreeCountersBase<FieldT>::WriteToDatabase( | |
| 127 | const CatalogDatabase &database) const { | ||
| 128 | 2680 | bool retval = true; | |
| 129 | |||
| 130 |
1/2✓ Branch 1 taken 2680 times.
✗ Branch 2 not taken.
|
2680 | const FieldsMap map = GetFieldsMap(); |
| 131 |
1/2✓ Branch 1 taken 2680 times.
✗ Branch 2 not taken.
|
2680 | SqlUpdateCounter sql_counter(database); |
| 132 | |||
| 133 | 2680 | typename FieldsMap::const_iterator i = map.begin(); | |
| 134 | 2680 | typename FieldsMap::const_iterator iend = map.end(); | |
| 135 |
2/2✓ Branch 1 taken 64320 times.
✓ Branch 2 taken 2680 times.
|
67000 | for (; i != iend; ++i) { |
| 136 |
1/2✓ Branch 2 taken 64320 times.
✗ Branch 3 not taken.
|
64320 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 137 |
2/4✓ Branch 2 taken 64320 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 64320 times.
✗ Branch 5 not taken.
|
64320 | && sql_counter.BindDelta(*(i->second)) |
| 138 |
3/6✓ Branch 0 taken 64320 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 64320 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 64320 times.
✗ Branch 6 not taken.
|
128640 | && sql_counter.Execute(); |
| 139 |
1/2✓ Branch 1 taken 64320 times.
✗ Branch 2 not taken.
|
64320 | sql_counter.Reset(); |
| 140 | |||
| 141 |
1/2✓ Branch 0 taken 64320 times.
✗ Branch 1 not taken.
|
64320 | retval = (retval) ? current_retval : false; |
| 142 | } | ||
| 143 | |||
| 144 | 2680 | return retval; | |
| 145 | 2680 | } | |
| 146 | |||
| 147 | |||
| 148 | template<typename FieldT> | ||
| 149 | 3064 | bool TreeCountersBase<FieldT>::InsertIntoDatabase( | |
| 150 | const CatalogDatabase &database) const { | ||
| 151 | 3064 | bool retval = true; | |
| 152 | |||
| 153 |
1/2✓ Branch 1 taken 3064 times.
✗ Branch 2 not taken.
|
3064 | const FieldsMap map = GetFieldsMap(); |
| 154 |
1/2✓ Branch 1 taken 3064 times.
✗ Branch 2 not taken.
|
3064 | SqlCreateCounter sql_counter(database); |
| 155 | |||
| 156 | 3064 | typename FieldsMap::const_iterator i = map.begin(); | |
| 157 | 3064 | typename FieldsMap::const_iterator iend = map.end(); | |
| 158 |
2/2✓ Branch 1 taken 73536 times.
✓ Branch 2 taken 3064 times.
|
76600 | for (; i != iend; ++i) { |
| 159 |
1/2✓ Branch 2 taken 73536 times.
✗ Branch 3 not taken.
|
73536 | const bool current_retval = sql_counter.BindCounter(i->first) |
| 160 |
2/4✓ Branch 2 taken 73536 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 73536 times.
✗ Branch 5 not taken.
|
73536 | && sql_counter.BindInitialValue(*(i->second)) |
| 161 |
3/6✓ Branch 0 taken 73536 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 73536 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 73536 times.
✗ Branch 6 not taken.
|
147072 | && sql_counter.Execute(); |
| 162 |
1/2✓ Branch 1 taken 73536 times.
✗ Branch 2 not taken.
|
73536 | sql_counter.Reset(); |
| 163 | |||
| 164 |
1/2✓ Branch 0 taken 73536 times.
✗ Branch 1 not taken.
|
73536 | retval = (retval) ? current_retval : false; |
| 165 | } | ||
| 166 | |||
| 167 | 3064 | return retval; | |
| 168 | 3064 | } | |
| 169 | |||
| 170 | |||
| 171 | template<typename FieldT> | ||
| 172 | 1014 | void TreeCountersBase<FieldT>::SetZero() { | |
| 173 | 1014 | self.Subtract(self); | |
| 174 | 1014 | subtree.Subtract(subtree); | |
| 175 | 1014 | } | |
| 176 | |||
| 177 | } // namespace catalog | ||
| 178 | |||
| 179 | #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_ | ||
| 180 |