2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MergeTree/ActiveDataPartSet.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
ActiveDataPartSet::ActiveDataPartSet(const Strings & names)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & name : names)
|
|
|
|
addImpl(name);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ActiveDataPartSet::add(const String & name)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
addImpl(name);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ActiveDataPartSet::addImpl(const String & name)
|
|
|
|
{
|
2017-08-15 11:59:08 +00:00
|
|
|
auto part_info = MergeTreePartInfo::fromPartName(name);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-15 11:59:08 +00:00
|
|
|
if (!getContainingPartImpl(part_info).empty())
|
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-15 16:21:13 +00:00
|
|
|
/// Parts contained in `part` are located contiguously in `part_info_to_name`, overlapping with the place where the part itself would be inserted.
|
2017-08-15 11:59:08 +00:00
|
|
|
auto it = part_info_to_name.lower_bound(part_info);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Let's go left.
|
2017-08-15 11:59:08 +00:00
|
|
|
while (it != part_info_to_name.begin())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
--it;
|
2017-08-15 11:59:08 +00:00
|
|
|
if (!part_info.contains(it->first))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
++it;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-15 11:59:08 +00:00
|
|
|
part_info_to_name.erase(it++);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Let's go to the right.
|
2017-08-15 11:59:08 +00:00
|
|
|
while (it != part_info_to_name.end() && part_info.contains(it->first))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-08-15 11:59:08 +00:00
|
|
|
part_info_to_name.erase(it++);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 11:59:08 +00:00
|
|
|
part_info_to_name.emplace(part_info, name);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String ActiveDataPartSet::getContainingPart(const String & part_name) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2017-08-15 11:59:08 +00:00
|
|
|
return getContainingPartImpl(MergeTreePartInfo::fromPartName(part_name));
|
2016-06-08 13:08:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-08-15 11:59:08 +00:00
|
|
|
String ActiveDataPartSet::getContainingPartImpl(const MergeTreePartInfo & part_info) const
|
2016-06-08 13:08:20 +00:00
|
|
|
{
|
2017-08-15 16:21:13 +00:00
|
|
|
/// A part can only be covered/overlapped by the previous or next one in `part_info_to_name`.
|
2017-08-15 11:59:08 +00:00
|
|
|
auto it = part_info_to_name.lower_bound(part_info);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-15 11:59:08 +00:00
|
|
|
if (it != part_info_to_name.end())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-08-15 11:59:08 +00:00
|
|
|
if (it->first.contains(part_info))
|
|
|
|
return it->second;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 11:59:08 +00:00
|
|
|
if (it != part_info_to_name.begin())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
--it;
|
2017-08-15 11:59:08 +00:00
|
|
|
if (it->first.contains(part_info))
|
|
|
|
return it->second;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 18:16:11 +00:00
|
|
|
return String();
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Strings ActiveDataPartSet::getParts() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Strings res;
|
2017-08-15 11:59:08 +00:00
|
|
|
res.reserve(part_info_to_name.size());
|
|
|
|
for (const auto & kv : part_info_to_name)
|
|
|
|
res.push_back(kv.second);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ActiveDataPartSet::size() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2017-08-15 11:59:08 +00:00
|
|
|
return part_info_to_name.size();
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|