diff options
Diffstat (limited to 'src/ingestor.cpp')
| -rw-r--r-- | src/ingestor.cpp | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 954f872..16638eb 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -5,7 +5,9 @@ #include <connection.h> #include <csignal> #include <dbTypes.h> +#include <fstream> #include <modifycommand.h> +#include <print> #include <ranges> #include <scn/scan.h> #include <selectcommand.h> @@ -26,19 +28,13 @@ namespace DB { namespace WebStat { namespace { - using ByteArrayView = std::span<const uint8_t>; - - auto - bytesToHexRange(const ByteArrayView bytes) - { + constexpr auto TO_HEX_RANGE = std::views::transform([](auto byte) { constexpr auto HEXN = 16ZU; - return bytes | std::views::transform([](auto byte) { - return std::array {byte / HEXN, byte % HEXN}; - }) | std::views::join - | std::views::transform([](auto nibble) { - return "0123456789abcdef"[nibble]; - }); - } + return std::array {byte / HEXN, byte % HEXN}; + }) | std::views::join + | std::views::transform([](auto nibble) { + return "0123456789abcdef"[nibble]; + }); EntityHash makeHash(const std::string_view value) @@ -457,18 +453,18 @@ namespace WebStat { if (lines.empty()) { return std::unexpected(0); } - const std::filesystem::path path { - settings.fallbackDir / std::format("parked-{:s}.short", bytesToHexRange(makeHash(lines.front())))}; - if (auto parked = FilePtr(fopen(path.c_str(), "w"))) { - fprintf(parked.get(), "%zu\n", lines.size()); + const std::filesystem::path path + = settings.fallbackDir / std::format("parked-{:s}.short", makeHash(lines.front()) | TO_HEX_RANGE); + if (std::ofstream parked {path}; parked.good()) { + std::println(parked, "{}", lines.size()); for (const auto & line : lines) { - fprintf(parked.get(), "%.*s\n", static_cast<int>(line.length()), line.data()); + std::println(parked, "{}", line); } - if (fflush(parked.get()) == 0) { + if (parked.flush().good()) { lines.clear(); - auto finalPath = std::filesystem::path {path}.replace_extension(".log"); - parked.reset(); - if (rename(path.c_str(), finalPath.c_str()) == 0) { + auto finalPath = auto {path}.replace_extension(".log"); + parked.close(); + if (!renameNoExcept(path, finalPath)) { return finalPath; } } @@ -590,12 +586,11 @@ namespace WebStat { Ingestor::jobPurgeOldLogs() { auto dbconn = dbpool->get(); - const auto stopAt = Job::LastRunTime::clock::now() + settings.purgeDeleteMaxTime; const auto purge = dbconn->modify(SQL::ACCESS_LOG_PURGE_OLD, SQL::ACCESS_LOG_PURGE_OLD_OPTS); purge->bindParam(0, std::format("{} days", settings.purgeDaysToKeep)); purge->bindParam(1, settings.purgeDeleteMax); unsigned int purgedTotal {}; - while (!terminated && stopAt > Job::LastRunTime::clock::now()) { + while (!terminated) { const auto purged = purge->execute(); purgedTotal += purged; if (purged < settings.purgeDeleteMax) { |
