1#pragma once
2
3#include <Storages/IStorage.h>
4
5#include <Poco/File.h>
6#include <Poco/Path.h>
7
8#include <common/logger_useful.h>
9
10#include <atomic>
11#include <shared_mutex>
12#include <ext/shared_ptr_helper.h>
13
14
15namespace DB
16{
17
18class StorageFileBlockInputStream;
19class StorageFileBlockOutputStream;
20
21class StorageFile : public ext::shared_ptr_helper<StorageFile>, public IStorage
22{
23 friend struct ext::shared_ptr_helper<StorageFile>;
24public:
25 std::string getName() const override { return "File"; }
26 std::string getTableName() const override { return table_name; }
27 std::string getDatabaseName() const override { return database_name; }
28
29 BlockInputStreams read(
30 const Names & column_names,
31 const SelectQueryInfo & query_info,
32 const Context & context,
33 QueryProcessingStage::Enum processed_stage,
34 size_t max_block_size,
35 unsigned num_streams) override;
36
37 BlockOutputStreamPtr write(
38 const ASTPtr & query,
39 const Context & context) override;
40
41 void rename(const String & new_path_to_table_data, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override;
42
43 Strings getDataPaths() const override;
44
45 struct CommonArguments
46 {
47 const std::string & database_name;
48 const std::string & table_name;
49 const std::string & format_name;
50 const std::string & compression_method;
51 const ColumnsDescription & columns;
52 const ConstraintsDescription & constraints;
53 const Context & context;
54 };
55
56protected:
57 friend class StorageFileBlockInputStream;
58 friend class StorageFileBlockOutputStream;
59
60 /// From file descriptor
61 StorageFile(int table_fd_, CommonArguments args);
62
63 /// From user's file
64 StorageFile(const std::string & table_path_, const std::string & user_files_absolute_path, CommonArguments args);
65
66 /// From table in database
67 StorageFile(const std::string & relative_table_dir_path, CommonArguments args);
68
69private:
70 explicit StorageFile(CommonArguments args);
71
72 std::string table_name;
73 std::string database_name;
74 std::string format_name;
75
76 int table_fd = -1;
77 String compression_method;
78
79 std::string base_path;
80 std::vector<std::string> paths;
81
82 bool is_db_table = true; /// Table is stored in real database, not user's file
83 bool use_table_fd = false; /// Use table_fd instead of path
84 std::atomic<bool> table_fd_was_used{false}; /// To detect repeating reads from stdin
85 off_t table_fd_init_offset = -1; /// Initial position of fd, used for repeating reads
86
87 mutable std::shared_mutex rwlock;
88
89 Logger * log = &Logger::get("StorageFile");
90};
91
92}
93