2015-01-27 00:52:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-03-24 02:44:52 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2020-09-15 09:16:10 +00:00
|
|
|
#include <Storages/SetSettings.h>
|
2015-01-27 00:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-03-24 02:44:52 +00:00
|
|
|
class IDisk;
|
|
|
|
using DiskPtr = std::shared_ptr<IDisk>;
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
class Set;
|
|
|
|
using SetPtr = std::shared_ptr<Set>;
|
|
|
|
|
2015-01-27 21:24:24 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/** Common part of StorageSet and StorageJoin.
|
2015-01-27 21:24:24 +00:00
|
|
|
*/
|
2017-11-04 03:20:18 +00:00
|
|
|
class StorageSetOrJoinBase : public IStorage
|
2015-01-27 21:24:24 +00:00
|
|
|
{
|
2021-07-23 14:25:35 +00:00
|
|
|
friend class SetOrJoinSink;
|
2015-01-28 00:08:45 +00:00
|
|
|
|
|
|
|
public:
|
2020-04-07 14:05:51 +00:00
|
|
|
void rename(const String & new_path_to_table_data, const StorageID & new_table_id) override;
|
2015-01-28 00:08:45 +00:00
|
|
|
|
2023-06-07 18:33:08 +00:00
|
|
|
SinkToStoragePtr write(const ASTPtr & query, const StorageMetadataPtr & /*metadata_snapshot*/, ContextPtr context, bool async_insert) override;
|
2015-01-28 00:08:45 +00:00
|
|
|
|
2020-11-01 17:38:43 +00:00
|
|
|
bool storesDataOnDisk() const override { return true; }
|
2019-04-04 13:13:59 +00:00
|
|
|
Strings getDataPaths() const override { return {path}; }
|
2018-02-21 19:26:59 +00:00
|
|
|
|
2015-01-27 21:24:24 +00:00
|
|
|
protected:
|
2015-01-28 00:08:45 +00:00
|
|
|
StorageSetOrJoinBase(
|
2020-12-15 16:45:13 +00:00
|
|
|
DiskPtr disk_,
|
2019-10-25 19:07:47 +00:00
|
|
|
const String & relative_path_,
|
2019-12-04 16:06:55 +00:00
|
|
|
const StorageID & table_id_,
|
2019-08-24 21:20:20 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2019-10-25 19:07:47 +00:00
|
|
|
const ConstraintsDescription & constraints_,
|
2021-04-23 12:18:23 +00:00
|
|
|
const String & comment,
|
2020-09-18 12:58:27 +00:00
|
|
|
bool persistent_);
|
2015-01-28 00:08:45 +00:00
|
|
|
|
2020-12-15 16:45:13 +00:00
|
|
|
DiskPtr disk;
|
2015-01-27 21:24:24 +00:00
|
|
|
String path;
|
2020-09-18 12:58:27 +00:00
|
|
|
bool persistent;
|
2015-01-27 21:24:24 +00:00
|
|
|
|
2018-08-08 03:12:35 +00:00
|
|
|
std::atomic<UInt64> increment = 0; /// For the backup file names.
|
2015-01-27 21:24:24 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Restore from backup.
|
2015-01-27 21:24:24 +00:00
|
|
|
void restore();
|
2015-01-28 00:08:45 +00:00
|
|
|
|
|
|
|
private:
|
2015-05-28 03:49:28 +00:00
|
|
|
void restoreFromFile(const String & file_path);
|
2015-01-27 21:24:24 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Insert the block into the state.
|
2021-09-29 17:30:07 +00:00
|
|
|
virtual void insertBlock(const Block & block, ContextPtr context) = 0;
|
2019-11-01 10:58:29 +00:00
|
|
|
/// Call after all blocks were inserted.
|
|
|
|
virtual void finishInsert() = 0;
|
2021-09-29 17:30:07 +00:00
|
|
|
virtual size_t getSize(ContextPtr context) const = 0;
|
2015-01-28 00:08:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/** Lets you save the set for later use on the right side of the IN statement.
|
|
|
|
* When inserted into a table, the data will be inserted into the set,
|
|
|
|
* and also written to a file-backup, for recovery after a restart.
|
|
|
|
* Reading from the table is not possible directly - it is possible to specify only the right part of the IN statement.
|
2015-01-27 00:52:03 +00:00
|
|
|
*/
|
2022-05-03 06:43:28 +00:00
|
|
|
class StorageSet final : public StorageSetOrJoinBase
|
2015-01-27 00:52:03 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-19 20:47:29 +00:00
|
|
|
StorageSet(
|
|
|
|
DiskPtr disk_,
|
|
|
|
const String & relative_path_,
|
|
|
|
const StorageID & table_id_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
|
|
|
const String & comment,
|
|
|
|
bool persistent_);
|
|
|
|
|
2015-01-27 00:52:03 +00:00
|
|
|
String getName() const override { return "Set"; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Access the insides.
|
2023-10-14 00:52:53 +00:00
|
|
|
SetPtr getSet() const;
|
2015-01-27 00:52:03 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void truncate(const ASTPtr &, const StorageMetadataPtr & metadata_snapshot, ContextPtr, TableExclusiveLockHolder &) override;
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2020-11-25 13:47:32 +00:00
|
|
|
std::optional<UInt64> totalRows(const Settings & settings) const override;
|
|
|
|
std::optional<UInt64> totalBytes(const Settings & settings) const override;
|
2020-10-23 18:11:55 +00:00
|
|
|
|
2015-01-27 00:52:03 +00:00
|
|
|
private:
|
2023-10-14 00:52:53 +00:00
|
|
|
/// Allows to concurrently truncate the set and work (read/fill) the existing set.
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
SetPtr set TSA_GUARDED_BY(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-09-29 17:30:07 +00:00
|
|
|
void insertBlock(const Block & block, ContextPtr) override;
|
2019-11-01 10:58:29 +00:00
|
|
|
void finishInsert() override;
|
2021-09-29 17:30:07 +00:00
|
|
|
size_t getSize(ContextPtr) const override;
|
2015-01-27 00:52:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|