summaryrefslogtreecommitdiff
path: root/src/buildtool/storage/compactification_task.hpp
blob: e0f7e492a876122424db8e11f9f1cb2f7ac70f11 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_BUILDTOOL_STORAGE_COMPACTIFICATION_TASK_HPP
#define INCLUDED_SRC_BUILDTOOL_STORAGE_COMPACTIFICATION_TASK_HPP

#include <filesystem>
#include <functional>
#include <string>

#include "fmt/core.h"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/local_cas.hpp"

/// \brief Set of data that defines a compactification task.
/// \param cas          CAS to be scanned.
/// \param large        True if large storages need to be scanned.
/// \param f_task       A handler for ObjectType::File entries.
/// \param x_task       A handler for ObjectType::Executable entries.
/// It is never called during scanning of large storages.
/// \param t_task       A handler for ObjectType::Tree entries.
/// \param logger       A callback for logging.
/// \param filter       A callback that defines which files must be processed.
struct CompactificationTask final {
    using Logger = std::function<void(LogLevel, std::string const&)>;
    using Filter = std::function<bool(std::filesystem::path const&)>;
    using ObjectTask = std::function<bool(CompactificationTask const&,
                                          std::filesystem::path const&)>;

    static inline const Logger kLoggerDefault = [](LogLevel level,
                                                   std::string const& message) {
        ::Logger::Log(level, "{}", message);
    };
    static inline const Filter kFilterDefault =
        [](std::filesystem::path const& /*unused*/) { return false; };
    static inline const ObjectTask kObjectTaskDefault =
        [](CompactificationTask const& /*unused*/,
           std::filesystem::path const& /*unused*/) {
            kLoggerDefault(LogLevel::Error, "Default ObjectTask was called");
            return false;
        };

    LocalCAS<false> const& cas;
    bool large = false;
    Logger logger = kLoggerDefault;
    Filter filter = kFilterDefault;
    ObjectTask f_task = kObjectTaskDefault;
    ObjectTask x_task = kObjectTaskDefault;
    ObjectTask t_task = kObjectTaskDefault;

    /// \brief Log a formatted error.
    template <typename... Args>
    void Log(LogLevel level,
             std::string const& message,
             Args&&... args) const noexcept;
};

/// \brief Execute the comapctification task using multiple threads.
/// Execution of the CompactificationTask-defined logic begins only after the
/// entire storage has been scanned, otherwise the storage may be invalidated.
/// Example: casf is scanned while another thread puts new split chunks there.
/// \return         True if execution was successful.
[[nodiscard]] auto CompactifyConcurrently(
    CompactificationTask const& task) noexcept -> bool;

template <typename... Args>
void CompactificationTask::Log(LogLevel level,
                               std::string const& message,
                               Args&&... args) const noexcept {
    if (not logger) {
        ::Logger::Log(LogLevel::Error, "Logger is missing.");
        return;
    }
    auto msg = fmt::vformat(message, fmt::make_format_args(args...));
    try {
        std::invoke(logger, level, msg);
    } catch (...) {
        ::Logger::Log(LogLevel::Error, "Failed to invoke a logger");
        ::Logger::Log(level, "{}", msg);
    }
}

#endif  // INCLUDED_SRC_BUILDTOOL_STORAGE_COMPACTIFICATION_TASK_HPP