Skip to content

Commit 7141b5d

Browse files
committed
Get rid of regexp
1 parent 97e0a03 commit 7141b5d

File tree

4 files changed

+222
-303
lines changed

4 files changed

+222
-303
lines changed

programs/static-files-disk-uploader/static-files-disk-uploader.cpp

+83-44
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
namespace fs = std::filesystem;
1717

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/(.*)"
2019

2120

2221
namespace DB
@@ -32,40 +31,102 @@ namespace ErrorCodes
3231
* If test-mode option is added, files will be put by given url via PUT request.
3332
*/
3433

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)
3735
{
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)
4044
{
41-
auto remote_file_name = uuid + "/" + file_name;
42-
writeText(remote_file_name, metadata_buf);
4345
writeChar('\t', metadata_buf);
4446
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+
};
4668

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);
5069

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+
}
5591

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)
5794
{
5895
if (dir_it->is_directory())
5996
{
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+
60116
fs::directory_iterator files_end;
61117
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();
63122
}
64123
else
65124
{
66-
process_file(dir_it->path().filename(), dir_it->path());
125+
processFile(dir_it->path(), dst_path, test_mode, *root_meta);
67126
}
68127
}
128+
root_meta->next();
129+
root_meta->finalize();
69130
}
70131
}
71132

@@ -94,7 +155,7 @@ try
94155
exit(0);
95156
}
96157

97-
String url, metadata_path;
158+
String metadata_path;
98159

99160
if (options.count("metadata-path"))
100161
metadata_path = options["metadata-path"].as<std::string>();
@@ -108,46 +169,24 @@ try
108169
return 1;
109170
}
110171

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;
117172
String root_path;
118-
119173
auto test_mode = options.contains("test-mode");
120174
if (test_mode)
121175
{
122176
if (options.count("url"))
123-
url = options["url"].as<std::string>();
177+
root_path = options["url"].as<std::string>();
124178
else
125179
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-
};
133180
}
134181
else
135182
{
136183
if (options.count("output-dir"))
137184
root_path = options["output-dir"].as<std::string>();
138185
else
139186
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-
};
146187
}
147188

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);
151190

152191
return 0;
153192
}

0 commit comments

Comments
 (0)