ClickHouse/src/Databases/DatabaseAtomic.h

87 lines
3.4 KiB
C++
Raw Normal View History

2019-10-23 13:46:38 +00:00
#pragma once
#include <Databases/DatabasesCommon.h>
2020-01-22 11:30:11 +00:00
#include <Core/BackgroundSchedulePool.h>
2019-10-23 13:46:38 +00:00
#include <Databases/DatabaseOrdinary.h>
namespace DB
{
2020-04-22 20:43:10 +00:00
/// All tables in DatabaseAtomic have persistent UUID and store data in
/// /clickhouse_path/store/xxx/xxxyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/
/// where xxxyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy is UUID of the table.
/// RENAMEs are performed without changing UUID and moving table data.
/// Tables in Atomic databases can be accessed by UUID through DatabaseCatalog.
/// On DROP TABLE no data is removed, DatabaseAtomic just marks table as dropped
/// by moving metadata to /clickhouse_path/metadata_dropped/ and notifies DatabaseCatalog.
/// Running queries still may use dropped table. Table will be actually removed when it's not in use.
/// Allows to execute RENAME and DROP without IStorage-level RWLocks
2019-11-11 11:34:03 +00:00
class DatabaseAtomic : public DatabaseOrdinary
2019-10-23 13:46:38 +00:00
{
public:
2020-07-02 20:39:31 +00:00
DatabaseAtomic(String name_, String metadata_path_, UUID uuid, Context & context_);
2019-10-23 13:46:38 +00:00
String getEngineName() const override { return "Atomic"; }
2020-07-02 20:39:31 +00:00
UUID getUUID() const override { return db_uuid; }
2019-10-23 13:46:38 +00:00
2020-07-07 12:11:58 +00:00
void renameDatabase(const String & new_name) override;
2019-11-11 14:28:28 +00:00
void renameTable(
const Context & context,
const String & table_name,
IDatabase & to_database,
2020-03-31 20:38:05 +00:00
const String & to_table_name,
2020-07-16 21:41:26 +00:00
bool exchange,
bool dictionary) override;
2019-11-11 14:28:28 +00:00
2020-03-20 00:07:52 +00:00
void dropTable(const Context & context, const String & table_name, bool no_delay) override;
2020-01-22 11:30:11 +00:00
2020-03-20 12:45:06 +00:00
void attachTable(const String & name, const StoragePtr & table, const String & relative_table_path) override;
2019-11-11 11:34:03 +00:00
StoragePtr detachTable(const String & name) override;
String getTableDataPath(const String & table_name) const override;
String getTableDataPath(const ASTCreateQuery & query) const override;
2019-11-11 11:34:03 +00:00
void drop(const Context & /*context*/) override;
2020-05-28 20:10:45 +00:00
DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name) override;
2020-04-01 22:41:29 +00:00
void loadStoredObjects(Context & context, bool has_force_restore_data_flag, bool force_attach) override;
2020-04-10 00:08:43 +00:00
2020-04-23 18:00:43 +00:00
/// Atomic database cannot be detached if there is detached table which still in use
2020-09-29 22:30:34 +00:00
void assertCanBeDetached(bool cleanup);
2020-04-10 23:02:15 +00:00
2020-04-13 14:09:56 +00:00
UUID tryGetTableUUID(const String & table_name) const override;
void tryCreateSymlink(const String & table_name, const String & actual_data_path);
void tryRemoveSymlink(const String & table_name);
void waitDetachedTableNotInUse(const UUID & uuid);
2019-11-11 11:34:03 +00:00
private:
2020-03-23 00:12:13 +00:00
void commitAlterTable(const StorageID & table_id, const String & table_metadata_tmp_path, const String & table_metadata_path) override;
void commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table,
const String & table_metadata_tmp_path, const String & table_metadata_path) override;
2020-01-27 20:31:39 +00:00
void assertDetachedTableNotInUse(const UUID & uuid);
2020-04-10 00:08:43 +00:00
typedef std::unordered_map<UUID, StoragePtr> DetachedTables;
[[nodiscard]] DetachedTables cleanupDetachedTables();
2020-07-07 12:11:58 +00:00
void tryCreateMetadataSymlink();
2020-07-16 14:25:39 +00:00
void renameDictionaryInMemoryUnlocked(const StorageID & old_name, const StorageID & new_name);
2020-01-27 20:31:39 +00:00
//TODO store path in DatabaseWithOwnTables::tables
2020-04-10 00:08:43 +00:00
typedef std::unordered_map<String, String> NameToPathMap;
NameToPathMap table_name_to_path;
2020-01-27 20:31:39 +00:00
2020-04-06 23:22:44 +00:00
DetachedTables detached_tables;
2020-07-07 12:11:58 +00:00
String path_to_table_symlinks;
String path_to_metadata_symlink;
2020-07-02 20:39:31 +00:00
const UUID db_uuid;
2019-10-23 13:46:38 +00:00
};
}