aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/FileChecker.h
blob: bb0383e4b563e7a0281f2040ee93841ed01b7b2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once

#include <Storages/CheckResults.h>
#include <map>
#include <base/types.h>

namespace Poco { class Logger; }

namespace DB
{
class IDisk;
using DiskPtr = std::shared_ptr<IDisk>;


/// Stores the sizes of all columns, and can check whether the columns are corrupted.
class FileChecker
{
public:
    FileChecker(const String & file_info_path_);
    FileChecker(DiskPtr disk_, const String & file_info_path_);

    void setPath(const String & file_info_path_);
    String getPath() const;

    void update(const String & full_file_path);
    void setEmpty(const String & full_file_path);
    void save() const;
    bool empty() const { return map.empty(); }

    /// Check the files whose parameters are specified in sizes.json
    CheckResults check() const;

    /// Truncate files that have excessive size to the expected size.
    /// Throw exception if the file size is less than expected.
    /// The purpose of this function is to rollback a group of unfinished writes.
    void repair();

    /// Returns stored file size.
    size_t getFileSize(const String & full_file_path) const;

    /// Returns total size of all files.
    size_t getTotalSize() const;

private:
    void load();

    bool fileReallyExists(const String & path_) const;
    size_t getRealFileSize(const String & path_) const;

    const DiskPtr disk;
    const Poco::Logger * log;

    String files_info_path;
    std::map<String, size_t> map;
};

}