ClickHouse/dbms/Common/FileChecker.cpp

144 lines
3.6 KiB
C++
Raw Normal View History

2015-09-29 19:19:54 +00:00
#include <common/JSON.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadHelpers.h>
#include <Common/escapeForFileName.h>
#include <Common/FileChecker.h>
namespace DB
{
2019-12-31 00:32:39 +00:00
FileChecker::FileChecker(DiskPtr disk_, const String & file_info_path_) : disk(std::move(disk_))
{
setPath(file_info_path_);
}
void FileChecker::setPath(const String & file_info_path_)
{
files_info_path = file_info_path_;
tmp_files_info_path = parentPath(files_info_path) + "tmp_" + fileName(files_info_path);
}
void FileChecker::update(const String & file_path)
{
initialize();
updateImpl(file_path);
save();
}
void FileChecker::update(const Strings::const_iterator & begin, const Strings::const_iterator & end)
{
initialize();
for (auto it = begin; it != end; ++it)
updateImpl(*it);
save();
}
2019-07-03 13:17:19 +00:00
CheckResults FileChecker::check() const
{
// Read the files again every time you call `check` - so as not to violate the constancy.
// `check` method is rarely called.
2019-07-03 13:17:19 +00:00
CheckResults results;
Map local_map;
2018-08-26 02:08:35 +00:00
load(local_map, files_info_path);
if (local_map.empty())
2019-07-03 13:17:19 +00:00
return {};
for (const auto & name_size : local_map)
{
const String & name = name_size.first;
String path = parentPath(files_info_path) + name;
if (!disk->exists(path))
{
results.emplace_back(name, false, "File " + path + " doesn't exist");
2019-07-03 13:17:19 +00:00
break;
}
auto real_size = disk->getFileSize(path);
if (real_size != name_size.second)
{
results.emplace_back(name, false, "Size of " + path + " is wrong. Size is " + toString(real_size) + " but should be " + toString(name_size.second));
2019-07-03 13:17:19 +00:00
break;
}
results.emplace_back(name, true, "");
}
2019-07-03 13:17:19 +00:00
return results;
}
void FileChecker::initialize()
{
if (initialized)
return;
2018-08-26 02:08:35 +00:00
load(map, files_info_path);
initialized = true;
}
void FileChecker::updateImpl(const String & file_path)
{
map[fileName(file_path)] = disk->getFileSize(file_path);
}
void FileChecker::save() const
{
{
2019-12-26 14:28:22 +00:00
std::unique_ptr<WriteBuffer> out = disk->writeFile(tmp_files_info_path);
/// So complex JSON structure - for compatibility with the old format.
writeCString("{\"yandex\":{", *out);
auto settings = FormatSettings();
for (auto it = map.begin(); it != map.end(); ++it)
{
if (it != map.begin())
writeString(",", *out);
/// `escapeForFileName` is not really needed. But it is left for compatibility with the old code.
writeJSONString(escapeForFileName(it->first), *out, settings);
2020-03-08 21:04:10 +00:00
writeString(R"(:{"size":")", *out);
writeIntText(it->second, *out);
writeString("\"}", *out);
}
writeCString("}}", *out);
out->next();
}
disk->replaceFile(tmp_files_info_path, files_info_path);
}
void FileChecker::load(Map & local_map, const String & path) const
{
2018-08-26 02:08:35 +00:00
local_map.clear();
if (!disk->exists(path))
return;
2019-12-26 14:28:22 +00:00
std::unique_ptr<ReadBuffer> in = disk->readFile(path);
2017-07-31 21:39:24 +00:00
WriteBufferFromOwnString out;
2017-07-31 21:39:24 +00:00
/// The JSON library does not support whitespace. We delete them. Inefficient.
while (!in->eof())
2017-07-31 21:39:24 +00:00
{
char c;
readChar(c, *in);
2017-07-31 21:39:24 +00:00
if (!isspace(c))
writeChar(c, out);
}
2017-07-31 21:39:24 +00:00
JSON json(out.str());
JSON files = json["yandex"];
2020-03-08 21:04:10 +00:00
for (const JSON file : files) // NOLINT
local_map[unescapeForFileName(file.getName())] = file.getValue()["size"].toUInt();
}
}