From a795f8e1ece4209e587e237f5b084ec92df21a90 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 27 Aug 2025 20:49:18 +0100 Subject: Use a DB connection pool rather than a single connection --- src/ingestor.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/ingestor.cpp') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index f824c80..5c47ab7 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -53,12 +53,13 @@ namespace WebStat { } } - Ingestor::Ingestor(const std::string_view hostname, DB::ConnectionPtr dbconn) : - hostnameId {crc32(hostname)}, dbconn {std::move(dbconn)} + Ingestor::Ingestor(const std::string_view hostname, DB::ConnectionPoolPtr dbpl) : + hostnameId {crc32(hostname)}, dbpool {std::move(dbpl)} { - storeEntities({ - std::make_pair(hostnameId, hostname), - }); + storeEntities(dbpool->get().get(), + { + std::make_pair(hostnameId, hostname), + }); } Ingestor::ScanResult @@ -85,12 +86,12 @@ namespace WebStat { { while (auto line = scn::scan(input, "{:[^\n]}\n")) { linesRead++; - ingestLogLine(line->value()); + ingestLogLine(dbpool->get().get(), line->value()); } } void - Ingestor::ingestLogLine(const std::string_view line) + Ingestor::ingestLogLine(DB::Connection * dbconn, const std::string_view line) { if (auto result = scanLogLine(line)) { linesParsed++; @@ -98,9 +99,9 @@ namespace WebStat { std::optional dbtx; if (const auto newEnts = newEntities(values); newEnts.front()) { dbtx.emplace(*dbconn); - storeEntities(newEnts); + storeEntities(dbconn, newEnts); } - storeLogLine(values); + storeLogLine(dbconn, values); } else { syslog(LOG_WARNING, "Discarded line: [%.*s]", static_cast(line.length()), line.data()); @@ -135,10 +136,10 @@ namespace WebStat { } void - Ingestor::storeEntities(const std::span> values) const + Ingestor::storeEntities(DB::Connection * dbconn, const std::span> values) const { std::ranges::for_each( - values | std::views::take_while(&std::optional::has_value), [this](auto && entity) { + values | std::views::take_while(&std::optional::has_value), [this, dbconn](auto && entity) { auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS); insert->bindParamI(0, entity->first); insert->bindParamS(1, entity->second); @@ -149,7 +150,7 @@ namespace WebStat { template void - Ingestor::storeLogLine(const std::tuple & values) const + Ingestor::storeLogLine(DB::Connection * dbconn, const std::tuple & values) const { auto insert = dbconn->modify(SQL::ACCESS_LOG_INSERT, SQL::ACCESS_LOG_INSERT_OPTS); -- cgit v1.2.3