ClickHouse/dbms/src/Storages/StorageID.h

104 lines
2.7 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-30 18:20:43 +00:00
UUID uuid = UUID{UInt128(0, 0)};
2019-12-05 11:42:13 +00:00
2019-12-10 19:48:16 +00:00
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_)
{
2019-12-30 18:20:43 +00:00
assertNotEmpty();
}
String getDatabaseName() const
{
assertNotEmpty();
return database_name;
}
String getTableName() const
{
assertNotEmpty();
return table_name;
2019-12-05 11:42:13 +00:00
}
String getFullTableName() const
{
2019-12-30 18:20:43 +00:00
assertNotEmpty();
2019-12-05 11:42:13 +00:00
return (database_name.empty() ? "" : database_name + ".") + table_name;
}
String getNameForLogs() const
{
2019-12-30 18:20:43 +00:00
assertNotEmpty();
return (database_name.empty() ? "" : backQuoteIfNeed(database_name) + ".") + backQuoteIfNeed(table_name)
2020-01-09 16:01:44 +00:00
+ (hasUUID() ? " (UUID " + toString(uuid) + ")" : "");
2019-12-30 18:20:43 +00:00
}
2020-02-12 18:14:12 +00:00
explicit operator bool () const
{
return !empty();
}
2019-12-30 18:20:43 +00:00
bool empty() const
{
return table_name.empty() && !hasUUID();
}
bool hasUUID() const
{
return uuid != UUID{UInt128(0, 0)};
2019-12-05 11:42:13 +00:00
}
bool operator<(const StorageID & rhs) const
{
2019-12-30 18:20:43 +00:00
assertNotEmpty();
2019-12-10 19:48:16 +00:00
/// 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
}
2019-12-30 18:20:43 +00:00
void assertNotEmpty() const
2019-12-05 11:42:13 +00:00
{
2019-12-10 19:48:16 +00:00
if (empty())
2019-12-30 18:20:43 +00:00
throw Exception("Both table name and UUID are empty", ErrorCodes::LOGICAL_ERROR);
if (table_name == TABLE_WITH_UUID_NAME_PLACEHOLDER && !hasUUID())
throw Exception("Table name was replaced with placeholder, but UUID is Nil", ErrorCodes::LOGICAL_ERROR);
if (table_name.empty() && !database_name.empty())
throw Exception("Table name is empty, but database name is not", ErrorCodes::LOGICAL_ERROR);
2019-12-05 11:42:13 +00:00
}
2019-12-27 19:30:22 +00:00
2019-12-30 18:20:43 +00:00
/// Avoid implicit construction of empty StorageID. However, it's needed for deferred initialization.
static StorageID createEmpty() { return {}; }
private:
StorageID() = default;
2019-12-05 11:42:13 +00:00
};
}