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