From 7301fe6484dc1b1d652425ad005ccfd214002a87 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 Aug 2025 12:00:10 +0100 Subject: Merge storeEntities into a single thing Add visitSum for tuples and makes storeEntities into a single lambda. --- src/ingestor.cpp | 57 +++++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 34 deletions(-) (limited to 'src/ingestor.cpp') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 8547745..1b54a64 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -1,5 +1,6 @@ #include "ingestor.hpp" #include "sql.hpp" +#include "util.hpp" #include #include #include @@ -54,7 +55,7 @@ namespace WebStat { Ingestor::Ingestor(const std::string_view hostname, DB::ConnectionPtr dbconn) : hostnameId {crc32(hostname)}, dbconn {std::move(dbconn)} { - storeEntity({hostnameId, hostname}); + storeEntities(std::make_tuple(std::make_pair(hostnameId, hostname))); } Ingestor::ScanResult @@ -104,43 +105,31 @@ namespace WebStat { size_t Ingestor::storeEntities(const std::tuple & values) const { - return std::apply( - [this](auto &&... value) { - return (this->storeEntity(value) + ...); + return visitSum( + [this](const X & entity) -> size_t { + auto insertIfReqd = [this](auto && entity) -> size_t { + if (existingEntities.contains(entity.first)) { + return 0; + } + auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS); + insert->bindParamI(0, entity.first); + insert->bindParamS(1, entity.second); + insert->execute(); + existingEntities.emplace(entity.first); + return 1; + }; + + if constexpr (std::is_same_v) { + return insertIfReqd(entity); + } + else if constexpr (std::is_same_v>) { + entity.transform(insertIfReqd).value_or(0); + } + return 0; }, values); } - template - size_t - Ingestor::storeEntity(const T &) const - { - return 0; - } - - size_t - Ingestor::storeEntity(const Entity entity) const - { - if (existingEntities.contains(entity.first)) { - return 0; - } - auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS); - insert->bindParamI(0, entity.first); - insert->bindParamS(1, entity.second); - insert->execute(); - existingEntities.emplace(entity.first); - return 1; - } - - size_t - Ingestor::storeEntity(const std::optional entity) const - { - if (entity) { - return storeEntity(*entity); - } - return 0; - } - template void Ingestor::storeLogLine(const std::tuple & values) const -- cgit v1.2.3