ClickHouse/dbms/src/Storages/StorageID.h

80 lines
2.1 KiB
C++
Raw Normal View History

2019-12-05 11:42:13 +00:00
#pragma once
#include <Core/Types.h>
2019-12-27 19:30:22 +00:00
#include <Core/UUID.h>
2019-12-05 11:42:13 +00:00
#include <Common/quoteString.h>
2019-12-27 19:30:22 +00:00
#include <IO/WriteHelpers.h>
2019-12-05 11:42:13 +00:00
#include <tuple>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2019-12-10 19:48:16 +00:00
static constexpr char const * TABLE_WITH_UUID_NAME_PLACEHOLDER = "_";
2019-12-05 11:42:13 +00:00
struct StorageID
{
String database_name;
String table_name;
2019-12-27 19:30:22 +00:00
UUID uuid;
2019-12-05 11:42:13 +00:00
2019-12-10 19:48:16 +00:00
//StorageID() = delete;
StorageID() = default;
2019-12-27 19:30:22 +00:00
StorageID(const String & database, const String & table, UUID uuid_ = UUID{UInt128(0, 0)})
2019-12-05 11:42:13 +00:00
: database_name(database), table_name(table), uuid(uuid_)
{
}
String getFullTableName() const
{
2019-12-10 19:48:16 +00:00
assert_valid();
2019-12-05 11:42:13 +00:00
return (database_name.empty() ? "" : database_name + ".") + table_name;
}
String getNameForLogs() const
{
2019-12-10 19:48:16 +00:00
assert_valid();
2019-12-27 19:30:22 +00:00
return (database_name.empty() ? "" : backQuoteIfNeed(database_name) + ".") + backQuoteIfNeed(table_name) + (hasUUID() ? "" : " (UUID " + toString(uuid) + ")");
2019-12-05 11:42:13 +00:00
}
bool operator<(const StorageID & rhs) const
{
2019-12-10 19:48:16 +00:00
assert_valid();
/// It's needed for ViewDependencies
2019-12-27 19:30:22 +00:00
if (!hasUUID() && !rhs.hasUUID())
2019-12-10 19:48:16 +00:00
/// If both IDs don't have UUID, compare them like pair of strings
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
2019-12-27 19:30:22 +00:00
else if (hasUUID() && rhs.hasUUID())
2019-12-10 19:48:16 +00:00
/// If both IDs have UUID, compare UUIDs and ignore database and table name
return uuid < rhs.uuid;
else
/// All IDs without UUID are less, then all IDs with UUID
2019-12-27 19:30:22 +00:00
return !hasUUID();
2019-12-10 19:48:16 +00:00
}
bool empty() const
{
2019-12-27 19:30:22 +00:00
return table_name.empty() || (table_name == TABLE_WITH_UUID_NAME_PLACEHOLDER && !hasUUID());
2019-12-05 11:42:13 +00:00
}
2019-12-10 19:48:16 +00:00
void assert_valid() const
2019-12-05 11:42:13 +00:00
{
2019-12-10 19:48:16 +00:00
if (empty())
2019-12-05 11:42:13 +00:00
throw Exception("empty table name", ErrorCodes::LOGICAL_ERROR);
2019-12-27 19:30:22 +00:00
if (table_name == TABLE_WITH_UUID_NAME_PLACEHOLDER && !hasUUID() && !database_name.empty())
2019-12-10 19:48:16 +00:00
throw Exception("unexpected database name", ErrorCodes::LOGICAL_ERROR);
2019-12-05 11:42:13 +00:00
}
2019-12-27 19:30:22 +00:00
bool hasUUID() const
{
return uuid != UUID{UInt128(0, 0)};
}
2019-12-05 11:42:13 +00:00
};
}