Faulty logic of OStoreDB::createBestArchiveMountPolicy
The reduce method:
template<typename InputIt, typename T, typename ReduceOp>
T reduce(InputIt begin, InputIt end, const T& init, ReduceOp op) {
T ret = init;
for (InputIt elt = begin; elt != end; elt++) {
ret = op(ret, *elt);
}
return ret;
}
common::dataStructures::MountPolicy
OStoreDB::createBestArchiveMountPolicy(const std::list<common::dataStructures::MountPolicy>& mountPolicies) {
if (mountPolicies.empty()) {
throw cta::exception::Exception("In OStoreDB::createBestArchiveMountPolicy(), empty mount policy list.");
}
//We need to get the most advantageous mountPolicy
//As the Init element of the reduce function is the first element of the list, we start the reduce with the second element (++mountPolicyInQueueList.begin())
common::dataStructures::MountPolicy bestMountPolicy = cta::utils::reduce(
++mountPolicies.begin(),
mountPolicies.end(),
mountPolicies.front(),
[](const common::dataStructures::MountPolicy& mp1, const common::dataStructures::MountPolicy& mp2) {
common::dataStructures::MountPolicy mp = mp1;
if (mp1.archivePriority > mp2.archivePriority) {
mp.archivePriority = mp1.archivePriority;
}
if (mp1.archiveMinRequestAge < mp2.archiveMinRequestAge) {
mp.archiveMinRequestAge = mp1.archiveMinRequestAge;
}
return mp;
});
return bestMountPolicy;
}
Note: there is no particular ordering applied to the input mountPolicies! The result will be completely order dependent - first element always returned.
Would the following not make more sense ?
common::dataStructures::MountPolicy
OStoreDB::createBestArchiveMountPolicy(const std::list<common::dataStructures::MountPolicy>& mountPolicies) {
if (mountPolicies.empty()) {
throw cta::exception::Exception("In OStoreDB::createBestArchiveMountPolicy(), empty mount policy list.");
}
//We need to get the most advantageous mountPolicy
//As the Init element of the reduce function is the first element of the list, we start the reduce with the second element (++mountPolicyInQueueList.begin())
common::dataStructures::MountPolicy bestMountPolicy = cta::utils::reduce(
++mountPolicies.begin(),
mountPolicies.end(),
mountPolicies.front(),
[](const common::dataStructures::MountPolicy& mp1, const common::dataStructures::MountPolicy& mp2) {
common::dataStructures::MountPolicy mp = mp1;
if (mp1.archivePriority < mp2.archivePriority && mp1.archiveMinRequestAge > mp2.archiveMinRequestAge) {
mp = mp2;
}
return mp;
});
return bestMountPolicy;
}
Edited by Jaroslav Guenther