Directory: | cvmfs/ |
---|---|
File: | cvmfs/catalog_counters_impl.h |
Date: | 2025-02-02 02:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 78 | 110 | 70.9% |
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 | 393 | TreeCountersBase<FieldT>::GetFieldsMap() const | |
28 | { | ||
29 | 393 | FieldsMap map; | |
30 |
2/4✓ Branch 2 taken 393 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 393 times.
✗ Branch 6 not taken.
|
393 | self.FillFieldsMap("self_", &map); |
31 |
2/4✓ Branch 2 taken 393 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 393 times.
✗ Branch 6 not taken.
|
393 | subtree.FillFieldsMap("subtree_", &map); |
32 | 393 | return map; | |
33 | } | ||
34 | |||
35 | template<typename FieldT> | ||
36 | 4 | std::map<std::string, FieldT> TreeCountersBase<FieldT>::GetValues() const { | |
37 | 4 | FieldsMap map_self; | |
38 | 4 | FieldsMap map_subtree; | |
39 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | self.FillFieldsMap("", &map_self); |
40 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | subtree.FillFieldsMap("", &map_subtree); |
41 | |||
42 | 4 | std::map<std::string, FieldT> map_summed; | |
43 | |||
44 | 4 | typename FieldsMap::const_iterator i = map_self.begin(); | |
45 | 4 | typename FieldsMap::const_iterator iend = map_self.end(); | |
46 |
2/2✓ Branch 1 taken 48 times.
✓ Branch 2 taken 4 times.
|
52 | for (; i != iend; ++i) { |
47 |
3/6✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 48 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 48 times.
✗ Branch 11 not taken.
|
48 | map_summed[i->first] = *(map_self[i->first]) + *(map_subtree[i->first]); |
48 | } | ||
49 | |||
50 | 8 | return map_summed; | |
51 | 4 | } | |
52 | |||
53 | template<typename FieldT> | ||
54 | 1 | std::string TreeCountersBase<FieldT>::GetCsvMap() const { | |
55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::map<std::string, FieldT> map_summed = GetValues(); |
56 | |||
57 | 1 | std::string result; | |
58 | 1 | typename std::map<std::string, FieldT>::const_iterator j = | |
59 | 1 | map_summed.begin(); | |
60 | 1 | typename std::map<std::string, FieldT>::const_iterator jend = | |
61 | 1 | map_summed.end(); | |
62 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
|
13 | for (; j != jend; ++j) { |
63 |
5/10✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 12 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 12 times.
✗ Branch 16 not taken.
|
12 | result += j->first + "," + StringifyInt(j->second) + "\n"; |
64 | } | ||
65 | 2 | return result; | |
66 | 1 | } | |
67 | |||
68 | |||
69 | template<typename FieldT> | ||
70 | 219 | bool TreeCountersBase<FieldT>::ReadFromDatabase( | |
71 | const CatalogDatabase &database, | ||
72 | const LegacyMode::Type legacy) | ||
73 | { | ||
74 | 219 | bool retval = true; | |
75 | |||
76 |
1/2✓ Branch 1 taken 219 times.
✗ Branch 2 not taken.
|
219 | FieldsMap map = GetFieldsMap(); |
77 |
1/2✓ Branch 1 taken 219 times.
✗ Branch 2 not taken.
|
219 | SqlGetCounter sql_counter(database); |
78 | |||
79 | 219 | typename FieldsMap::const_iterator i = map.begin(); | |
80 | 219 | typename FieldsMap::const_iterator iend = map.end(); | |
81 |
2/2✓ Branch 1 taken 5256 times.
✓ Branch 2 taken 219 times.
|
5475 | for (; i != iend; ++i) { |
82 |
2/4✓ Branch 2 taken 5256 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5256 times.
✗ Branch 5 not taken.
|
10512 | bool current_retval = sql_counter.BindCounter(i->first) && |
83 |
2/4✓ Branch 1 taken 5256 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5256 times.
✗ Branch 4 not taken.
|
5256 | sql_counter.FetchRow(); |
84 | |||
85 | // TODO(jblomer): nicify this | ||
86 |
1/2✓ Branch 0 taken 5256 times.
✗ Branch 1 not taken.
|
5256 | if (current_retval) { |
87 | 5256 | *(const_cast<FieldT*>(i->second)) = | |
88 |
1/2✓ Branch 1 taken 5256 times.
✗ Branch 2 not taken.
|
5256 | static_cast<FieldT>(sql_counter.GetCounter()); |
89 | ✗ | } else if ( (legacy == LegacyMode::kNoSpecials) && | |
90 | ✗ | ((i->first == "self_special") || | |
91 | ✗ | (i->first == "subtree_special")) ) | |
92 | { // NOLINT(bugprone-branch-clone) | ||
93 | ✗ | *(const_cast<FieldT*>(i->second)) = FieldT(0); | |
94 | ✗ | current_retval = true; | |
95 | ✗ | } else if ( (legacy == LegacyMode::kNoExternals) && | |
96 | ✗ | ((i->first == "self_special") | |
97 | ✗ | || (i->first == "subtree_special") || | |
98 | ✗ | (i->first == "self_external") | |
99 | ✗ | || (i->first == "subtree_external") || | |
100 | ✗ | (i->first == "self_external_file_size") | |
101 | ✗ | || (i->first == "subtree_external_file_size")) ) | |
102 | { | ||
103 | ✗ | *(const_cast<FieldT*>(i->second)) = FieldT(0); | |
104 | ✗ | current_retval = true; | |
105 | ✗ | } else if ( (legacy == LegacyMode::kNoXattrs) && | |
106 | ✗ | ((i->first == "self_special") | |
107 | ✗ | || (i->first == "subtree_special") || | |
108 | ✗ | (i->first == "self_external") | |
109 | ✗ | || (i->first == "subtree_external") || | |
110 | ✗ | (i->first == "self_external_file_size") | |
111 | ✗ | || (i->first == "subtree_external_file_size") || | |
112 | ✗ | (i->first == "self_xattr") || (i->first == "subtree_xattr")) ) | |
113 | { | ||
114 | ✗ | *(const_cast<FieldT*>(i->second)) = FieldT(0); | |
115 | ✗ | current_retval = true; | |
116 | ✗ | } else if (legacy == LegacyMode::kLegacy) { | |
117 | ✗ | *(const_cast<FieldT*>(i->second)) = FieldT(0); | |
118 | ✗ | current_retval = true; | |
119 | } | ||
120 | |||
121 |
1/2✓ Branch 1 taken 5256 times.
✗ Branch 2 not taken.
|
5256 | sql_counter.Reset(); |
122 |
1/2✓ Branch 0 taken 5256 times.
✗ Branch 1 not taken.
|
10512 | retval = (retval) ? current_retval : false; |
123 | } | ||
124 | |||
125 | 219 | return retval; | |
126 | 5475 | } | |
127 | |||
128 | |||
129 | template<typename FieldT> | ||
130 | 75 | bool TreeCountersBase<FieldT>::WriteToDatabase( | |
131 | const CatalogDatabase &database) const | ||
132 | { | ||
133 | 75 | bool retval = true; | |
134 | |||
135 |
1/2✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
|
75 | const FieldsMap map = GetFieldsMap(); |
136 |
1/2✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
|
75 | SqlUpdateCounter sql_counter(database); |
137 | |||
138 | 75 | typename FieldsMap::const_iterator i = map.begin(); | |
139 | 75 | typename FieldsMap::const_iterator iend = map.end(); | |
140 |
2/2✓ Branch 1 taken 1800 times.
✓ Branch 2 taken 75 times.
|
1875 | for (; i != iend; ++i) { |
141 | 1800 | const bool current_retval = | |
142 |
1/2✓ Branch 2 taken 1800 times.
✗ Branch 3 not taken.
|
1800 | sql_counter.BindCounter(i->first) && |
143 |
3/6✓ Branch 0 taken 1800 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1800 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1800 times.
✗ Branch 7 not taken.
|
3600 | sql_counter.BindDelta(*(i->second)) && |
144 |
2/4✓ Branch 1 taken 1800 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1800 times.
✗ Branch 4 not taken.
|
1800 | sql_counter.Execute(); |
145 |
1/2✓ Branch 1 taken 1800 times.
✗ Branch 2 not taken.
|
1800 | sql_counter.Reset(); |
146 | |||
147 |
1/2✓ Branch 0 taken 1800 times.
✗ Branch 1 not taken.
|
1800 | retval = (retval) ? current_retval : false; |
148 | } | ||
149 | |||
150 | 75 | return retval; | |
151 | 75 | } | |
152 | |||
153 | |||
154 | template<typename FieldT> | ||
155 | 92 | bool TreeCountersBase<FieldT>::InsertIntoDatabase( | |
156 | const CatalogDatabase &database) const | ||
157 | { | ||
158 | 92 | bool retval = true; | |
159 | |||
160 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | const FieldsMap map = GetFieldsMap(); |
161 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | SqlCreateCounter sql_counter(database); |
162 | |||
163 | 92 | typename FieldsMap::const_iterator i = map.begin(); | |
164 | 92 | typename FieldsMap::const_iterator iend = map.end(); | |
165 |
2/2✓ Branch 1 taken 2208 times.
✓ Branch 2 taken 92 times.
|
2300 | for (; i != iend; ++i) { |
166 | 2208 | const bool current_retval = | |
167 |
1/2✓ Branch 2 taken 2208 times.
✗ Branch 3 not taken.
|
2208 | sql_counter.BindCounter(i->first) && |
168 |
3/6✓ Branch 0 taken 2208 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2208 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2208 times.
✗ Branch 7 not taken.
|
4416 | sql_counter.BindInitialValue(*(i->second)) && |
169 |
2/4✓ Branch 1 taken 2208 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2208 times.
✗ Branch 4 not taken.
|
2208 | sql_counter.Execute(); |
170 |
1/2✓ Branch 1 taken 2208 times.
✗ Branch 2 not taken.
|
2208 | sql_counter.Reset(); |
171 | |||
172 |
1/2✓ Branch 0 taken 2208 times.
✗ Branch 1 not taken.
|
2208 | retval = (retval) ? current_retval : false; |
173 | } | ||
174 | |||
175 | 92 | return retval; | |
176 | 92 | } | |
177 | |||
178 | |||
179 | template<typename FieldT> | ||
180 | 26 | void TreeCountersBase<FieldT>::SetZero() { | |
181 | 26 | self.Subtract(self); | |
182 | 26 | subtree.Subtract(subtree); | |
183 | 26 | } | |
184 | |||
185 | } // namespace catalog | ||
186 | |||
187 | #endif // CVMFS_CATALOG_COUNTERS_IMPL_H_ | ||
188 |