15
15
16
16
namespace fs = std::filesystem;
17
17
18
- #define UUID_PATTERN " [\\ w]{8}-[\\ w]{4}-[\\ w]{4}-[\\ w]{4}-[\\ w]{12}"
19
- #define EXTRACT_UUID_PATTERN fmt::format (" .*\\ /({})\\ /.*" , UUID_PATTERN)
18
+ #define EXTRACT_PATH_PATTERN " .*\\ /store/(.*)"
20
19
21
20
22
21
namespace DB
@@ -32,40 +31,102 @@ namespace ErrorCodes
32
31
* If test-mode option is added, files will be put by given url via PUT request.
33
32
*/
34
33
35
- void processTableFiles (const fs::path & path, String root_path, String uuid,
36
- WriteBuffer & metadata_buf, std::function<std::shared_ptr<WriteBuffer>(const String &)> create_dst_buf)
34
+ void processFile (const fs::path & file_path, const fs::path & dst_path, bool test_mode, WriteBuffer & metadata_buf)
37
35
{
38
- fs::directory_iterator dir_end;
39
- auto process_file = [&](const String & file_name, const String & file_path)
36
+ String remote_path;
37
+ RE2::FullMatch (file_path.string (), EXTRACT_PATH_PATTERN, &remote_path);
38
+ bool is_directory = fs::is_directory (file_path);
39
+
40
+ writeText (file_path.filename (), metadata_buf);
41
+ writeChar (' \t ' , metadata_buf);
42
+ writeBoolText (is_directory, metadata_buf);
43
+ if (!is_directory)
40
44
{
41
- auto remote_file_name = uuid + " /" + file_name;
42
- writeText (remote_file_name, metadata_buf);
43
45
writeChar (' \t ' , metadata_buf);
44
46
writeIntText (fs::file_size (file_path), metadata_buf);
45
- writeChar (' \n ' , metadata_buf);
47
+ }
48
+ writeChar (' \n ' , metadata_buf);
49
+
50
+ if (is_directory)
51
+ return ;
52
+
53
+ auto dst_file_path = fs::path (dst_path) / remote_path;
54
+
55
+ auto src_buf = createReadBufferFromFileBase (file_path, {}, fs::file_size (file_path));
56
+ std::shared_ptr<WriteBuffer> dst_buf;
57
+
58
+ // / test mode for integration tests.
59
+ if (test_mode)
60
+ dst_buf = std::make_shared<WriteBufferFromHTTP>(Poco::URI (dst_file_path), Poco::Net::HTTPRequest::HTTP_PUT);
61
+ else
62
+ dst_buf = std::make_shared<WriteBufferFromFile>(dst_file_path);
63
+
64
+ copyData (*src_buf, *dst_buf);
65
+ dst_buf->next ();
66
+ dst_buf->finalize ();
67
+ };
46
68
47
- auto src_buf = createReadBufferFromFileBase (file_path, {}, fs::file_size (file_path));
48
- fs::create_directories ((fs::path (root_path) / remote_file_name).parent_path ());
49
- auto dst_buf = create_dst_buf (remote_file_name);
50
69
51
- copyData (*src_buf, *dst_buf);
52
- dst_buf->next ();
53
- dst_buf->finalize ();
54
- };
70
+ void processTableFiles (const fs::path & data_path, fs::path dst_path, bool test_mode)
71
+ {
72
+ std::cerr << " Data path: " << data_path << " , destination path: " << dst_path << std::endl;
73
+
74
+ String prefix;
75
+ RE2::FullMatch (data_path.string (), EXTRACT_PATH_PATTERN, &prefix);
76
+
77
+ std::shared_ptr<WriteBuffer> root_meta;
78
+ if (test_mode)
79
+ {
80
+ dst_path /= " store" ;
81
+ auto files_root = dst_path / prefix;
82
+ root_meta = std::make_shared<WriteBufferFromHTTP>(Poco::URI (files_root / " .index" ), Poco::Net::HTTPRequest::HTTP_PUT);
83
+ }
84
+ else
85
+ {
86
+ dst_path = fs::canonical (dst_path);
87
+ auto files_root = dst_path / prefix;
88
+ fs::create_directories (files_root);
89
+ root_meta = std::make_shared<WriteBufferFromFile>(files_root / " .index" );
90
+ }
55
91
56
- for (fs::directory_iterator dir_it (path); dir_it != dir_end; ++dir_it)
92
+ fs::directory_iterator dir_end;
93
+ for (fs::directory_iterator dir_it (data_path); dir_it != dir_end; ++dir_it)
57
94
{
58
95
if (dir_it->is_directory ())
59
96
{
97
+ processFile (dir_it->path (), dst_path, test_mode, *root_meta);
98
+
99
+ String directory_prefix;
100
+ RE2::FullMatch (dir_it->path ().string (), EXTRACT_PATH_PATTERN, &directory_prefix);
101
+
102
+ std::shared_ptr<WriteBuffer> directory_meta;
103
+ if (test_mode)
104
+ {
105
+ auto files_root = dst_path / prefix;
106
+ directory_meta = std::make_shared<WriteBufferFromHTTP>(Poco::URI (dst_path / directory_prefix / " .index" ), Poco::Net::HTTPRequest::HTTP_PUT);
107
+ }
108
+ else
109
+ {
110
+ dst_path = fs::canonical (dst_path);
111
+ auto files_root = dst_path / prefix;
112
+ fs::create_directories (dst_path / directory_prefix);
113
+ directory_meta = std::make_shared<WriteBufferFromFile>(dst_path / directory_prefix / " .index" );
114
+ }
115
+
60
116
fs::directory_iterator files_end;
61
117
for (fs::directory_iterator file_it (dir_it->path ()); file_it != files_end; ++file_it)
62
- process_file (dir_it->path ().filename () / file_it->path ().filename (), file_it->path ());
118
+ processFile (file_it->path (), dst_path, test_mode, *directory_meta);
119
+
120
+ directory_meta->next ();
121
+ directory_meta->finalize ();
63
122
}
64
123
else
65
124
{
66
- process_file (dir_it->path (). filename (), dir_it-> path () );
125
+ processFile (dir_it->path (), dst_path, test_mode, *root_meta );
67
126
}
68
127
}
128
+ root_meta->next ();
129
+ root_meta->finalize ();
69
130
}
70
131
}
71
132
94
155
exit (0 );
95
156
}
96
157
97
- String url, metadata_path;
158
+ String metadata_path;
98
159
99
160
if (options.count (" metadata-path" ))
100
161
metadata_path = options[" metadata-path" ].as <std::string>();
@@ -108,46 +169,24 @@ try
108
169
return 1 ;
109
170
}
110
171
111
- String uuid;
112
- if (!RE2::Extract (metadata_path, EXTRACT_UUID_PATTERN, " \\ 1" , &uuid))
113
- throw Exception (ErrorCodes::BAD_ARGUMENTS, " Cannot extract uuid for: {}" , metadata_path);
114
-
115
- std::shared_ptr<WriteBuffer> metadata_buf;
116
- std::function<std::shared_ptr<WriteBuffer>(const String &)> create_dst_buf;
117
172
String root_path;
118
-
119
173
auto test_mode = options.contains (" test-mode" );
120
174
if (test_mode)
121
175
{
122
176
if (options.count (" url" ))
123
- url = options[" url" ].as <std::string>();
177
+ root_path = options[" url" ].as <std::string>();
124
178
else
125
179
throw Exception (ErrorCodes::BAD_ARGUMENTS, " No url option passed for test mode" );
126
-
127
- metadata_buf = std::make_shared<WriteBufferFromHTTP>(Poco::URI (fs::path (url) / (" .index-" + uuid)), Poco::Net::HTTPRequest::HTTP_PUT);
128
-
129
- create_dst_buf = [&](const String & remote_file_name)
130
- {
131
- return std::make_shared<WriteBufferFromHTTP>(Poco::URI (fs::path (url) / remote_file_name), Poco::Net::HTTPRequest::HTTP_PUT);
132
- };
133
180
}
134
181
else
135
182
{
136
183
if (options.count (" output-dir" ))
137
184
root_path = options[" output-dir" ].as <std::string>();
138
185
else
139
186
root_path = fs::current_path ();
140
-
141
- metadata_buf = std::make_shared<WriteBufferFromFile>(fs::path (root_path) / (" .index-" + uuid));
142
- create_dst_buf = [&](const String & remote_file_name)
143
- {
144
- return std::make_shared<WriteBufferFromFile>(fs::path (root_path) / remote_file_name);
145
- };
146
187
}
147
188
148
- processTableFiles (fs_path, root_path, uuid, *metadata_buf, create_dst_buf);
149
- metadata_buf->next ();
150
- metadata_buf->finalize ();
189
+ processTableFiles (fs_path, root_path, test_mode);
151
190
152
191
return 0 ;
153
192
}
0 commit comments