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 <string> |
9 |
|
|
|
10 |
|
|
#include "catalog_sql.h" |
11 |
|
|
|
12 |
|
|
namespace catalog { |
13 |
|
|
|
14 |
|
|
template<typename FieldT> |
15 |
|
|
FieldT TreeCountersBase<FieldT>::Get(const std::string &key) const { |
16 |
|
|
FieldsMap map = GetFieldsMap(); |
17 |
|
|
if (map.find(key) != map.end()) |
18 |
|
|
return *map[key]; |
19 |
|
|
return FieldT(0); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
template<typename FieldT> |
24 |
|
|
typename TreeCountersBase<FieldT>::FieldsMap |
25 |
|
|
TreeCountersBase<FieldT>::GetFieldsMap() const |
26 |
|
|
{ |
27 |
|
213 |
FieldsMap map; |
28 |
|
213 |
self.FillFieldsMap("self_", &map); |
29 |
|
213 |
subtree.FillFieldsMap("subtree_", &map); |
30 |
|
|
return map; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
template<typename FieldT> |
35 |
|
|
bool TreeCountersBase<FieldT>::ReadFromDatabase( |
36 |
|
|
const CatalogDatabase &database, |
37 |
|
|
const LegacyMode::Type legacy) |
38 |
|
|
{ |
39 |
|
120 |
bool retval = true; |
40 |
|
|
|
41 |
|
120 |
FieldsMap map = GetFieldsMap(); |
42 |
|
120 |
SqlGetCounter sql_counter(database); |
43 |
|
|
|
44 |
|
120 |
typename FieldsMap::const_iterator i = map.begin(); |
45 |
|
120 |
typename FieldsMap::const_iterator iend = map.end(); |
46 |
✓✓ |
3000 |
for (; i != iend; ++i) { |
47 |
|
|
bool current_retval = sql_counter.BindCounter(i->first) && |
48 |
✓✗✓✗
|
2880 |
sql_counter.FetchRow(); |
49 |
|
|
|
50 |
|
|
// TODO(jblomer): nicify this |
51 |
✓✗ |
2880 |
if (current_retval) { |
52 |
|
2880 |
*(const_cast<FieldT*>(i->second)) = |
53 |
|
|
static_cast<FieldT>(sql_counter.GetCounter()); |
54 |
|
|
} else if ( (legacy == LegacyMode::kNoSpecials) && |
55 |
|
|
((i->first == "self_special") || |
56 |
|
|
(i->first == "subtree_special")) ) |
57 |
|
|
{ |
58 |
|
|
*(const_cast<FieldT*>(i->second)) = FieldT(0); |
59 |
|
|
current_retval = true; |
60 |
|
|
} else if ( (legacy == LegacyMode::kNoExternals) && |
61 |
|
|
((i->first == "self_special") |
62 |
|
|
|| (i->first == "subtree_special") || |
63 |
|
|
(i->first == "self_external") |
64 |
|
|
|| (i->first == "subtree_external") || |
65 |
|
|
(i->first == "self_external_file_size") |
66 |
|
|
|| (i->first == "subtree_external_file_size")) ) |
67 |
|
|
{ |
68 |
|
|
*(const_cast<FieldT*>(i->second)) = FieldT(0); |
69 |
|
|
current_retval = true; |
70 |
|
|
} else if ( (legacy == LegacyMode::kNoXattrs) && |
71 |
|
|
((i->first == "self_special") |
72 |
|
|
|| (i->first == "subtree_special") || |
73 |
|
|
(i->first == "self_external") |
74 |
|
|
|| (i->first == "subtree_external") || |
75 |
|
|
(i->first == "self_external_file_size") |
76 |
|
|
|| (i->first == "subtree_external_file_size") || |
77 |
|
|
(i->first == "self_xattr") || (i->first == "subtree_xattr")) ) |
78 |
|
|
{ |
79 |
|
|
*(const_cast<FieldT*>(i->second)) = FieldT(0); |
80 |
|
|
current_retval = true; |
81 |
|
|
} else if (legacy == LegacyMode::kLegacy) { |
82 |
|
|
*(const_cast<FieldT*>(i->second)) = FieldT(0); |
83 |
|
|
current_retval = true; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
2880 |
sql_counter.Reset(); |
87 |
✓✗ |
2880 |
retval = (retval) ? current_retval : false; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
120 |
return retval; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
template<typename FieldT> |
95 |
|
|
bool TreeCountersBase<FieldT>::WriteToDatabase( |
96 |
|
|
const CatalogDatabase &database) const |
97 |
|
|
{ |
98 |
|
22 |
bool retval = true; |
99 |
|
|
|
100 |
|
22 |
const FieldsMap map = GetFieldsMap(); |
101 |
|
22 |
SqlUpdateCounter sql_counter(database); |
102 |
|
|
|
103 |
|
22 |
typename FieldsMap::const_iterator i = map.begin(); |
104 |
|
22 |
typename FieldsMap::const_iterator iend = map.end(); |
105 |
✓✓ |
550 |
for (; i != iend; ++i) { |
106 |
|
|
const bool current_retval = |
107 |
|
|
sql_counter.BindCounter(i->first) && |
108 |
|
|
sql_counter.BindDelta(*(i->second)) && |
109 |
✓✗✓✗ ✓✗ |
528 |
sql_counter.Execute(); |
110 |
|
528 |
sql_counter.Reset(); |
111 |
|
|
|
112 |
✓✗ |
528 |
retval = (retval) ? current_retval : false; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
22 |
return retval; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
template<typename FieldT> |
120 |
|
|
bool TreeCountersBase<FieldT>::InsertIntoDatabase( |
121 |
|
|
const CatalogDatabase &database) const |
122 |
|
|
{ |
123 |
|
64 |
bool retval = true; |
124 |
|
|
|
125 |
|
64 |
const FieldsMap map = GetFieldsMap(); |
126 |
|
64 |
SqlCreateCounter sql_counter(database); |
127 |
|
|
|
128 |
|
64 |
typename FieldsMap::const_iterator i = map.begin(); |
129 |
|
64 |
typename FieldsMap::const_iterator iend = map.end(); |
130 |
✓✓ |
1600 |
for (; i != iend; ++i) { |
131 |
|
|
const bool current_retval = |
132 |
|
|
sql_counter.BindCounter(i->first) && |
133 |
|
|
sql_counter.BindInitialValue(*(i->second)) && |
134 |
✓✗✓✗ ✓✗ |
1536 |
sql_counter.Execute(); |
135 |
|
1536 |
sql_counter.Reset(); |
136 |
|
|
|
137 |
✓✗ |
1536 |
retval = (retval) ? current_retval : false; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
64 |
return retval; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
template<typename FieldT> |
145 |
|
|
void TreeCountersBase<FieldT>::SetZero() { |
146 |
|
7 |
self.Subtract(self); |
147 |
|
7 |
subtree.Subtract(subtree); |
148 |
|
7 |
} |
149 |
|
|
|
150 |
|
|
} // namespace catalog |
151 |
|
|
|
152 |
|
|
#endif // CVMFS_CATALOG_COUNTERS_IMPL_H_ |