ClickHouse/src/Storages/FileLog/StorageFileLog.h

116 lines
2.7 KiB
C++
Raw Normal View History

2021-06-09 02:03:36 +00:00
#pragma once
#include <Storages/FileLog/Buffer_fwd.h>
2021-09-04 17:04:35 +00:00
#include <Storages/FileLog/FileLogSettings.h>
2021-06-09 02:03:36 +00:00
#include <Core/BackgroundSchedulePool.h>
#include <Storages/IStorage.h>
#include <Common/SettingsChanges.h>
#include <Poco/File.h>
#include <Poco/Semaphore.h>
2021-07-04 08:52:05 +00:00
#include <common/shared_ptr_helper.h>
2021-06-09 02:03:36 +00:00
#include <mutex>
#include <atomic>
2021-09-04 17:04:35 +00:00
#include <fstream>
2021-06-09 02:03:36 +00:00
namespace DB
{
2021-07-04 08:52:05 +00:00
class StorageFileLog final : public shared_ptr_helper<StorageFileLog>, public IStorage, WithContext
2021-06-09 02:03:36 +00:00
{
2021-07-04 08:52:05 +00:00
friend struct shared_ptr_helper<StorageFileLog>;
2021-06-09 02:03:36 +00:00
public:
2021-09-05 06:32:32 +00:00
enum class FileStatus
{
BEGIN,
NO_CHANGE,
UPDATED,
REMOVED
};
2021-06-09 02:03:36 +00:00
using Files = std::vector<String>;
std::string getName() const override { return "FileLog"; }
bool noPushingToViews() const override { return true; }
void startup() override;
void shutdown() override;
Pipe read(
const Names & column_names,
const StorageMetadataPtr & /*metadata_snapshot*/,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
unsigned num_streams) override;
const auto & getFormatName() const { return format_name; }
2021-09-05 06:32:32 +00:00
auto & getFileNames() { return file_names; }
auto & getFileStatus() { return file_status; }
2021-06-09 02:03:36 +00:00
protected:
StorageFileLog(
const StorageID & table_id_,
ContextPtr context_,
const ColumnsDescription & columns_,
const String & path_,
2021-09-04 17:04:35 +00:00
const String & format_name_,
std::unique_ptr<FileLogSettings> settings);
2021-06-09 02:03:36 +00:00
private:
2021-09-04 17:04:35 +00:00
std::unique_ptr<FileLogSettings> filelog_settings;
2021-06-09 02:03:36 +00:00
const String path;
2021-09-04 17:04:35 +00:00
bool path_is_directory = false;
2021-06-09 02:03:36 +00:00
const String format_name;
Poco::Logger * log;
2021-09-04 17:04:35 +00:00
struct FileContext
{
FileStatus status = FileStatus::BEGIN;
std::ifstream reader;
};
using NameToFile = std::unordered_map<String, FileContext>;
NameToFile file_status;
std::vector<String> file_names;
std::mutex status_mutex;
2021-06-09 02:03:36 +00:00
// Stream thread
struct TaskContext
{
BackgroundSchedulePool::TaskHolder holder;
std::atomic<bool> stream_cancelled {false};
explicit TaskContext(BackgroundSchedulePool::TaskHolder&& task_) : holder(std::move(task_))
{
}
};
std::shared_ptr<TaskContext> task;
2021-09-04 17:04:35 +00:00
using TaskThread = BackgroundSchedulePool::TaskHolder;
TaskThread watch_task;
2021-06-09 02:03:36 +00:00
void threadFunc();
2021-09-04 17:04:35 +00:00
void clearInvalidFiles();
2021-06-09 02:03:36 +00:00
size_t getPollMaxBatchSize() const;
size_t getMaxBlockSize() const;
size_t getPollTimeoutMillisecond() const;
bool streamToViews();
bool checkDependencies(const StorageID & table_id);
2021-09-04 17:04:35 +00:00
[[noreturn]] void watchFunc();
2021-06-09 02:03:36 +00:00
};
}