From b9538c693b34a6382ea9f3b985bb922b9c17f12e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 15 Oct 2025 23:24:32 +0100 Subject: Refactor handling of new entity insert Replaces weird select with one thing with a function pointer stored in the type definition array. --- src/ingestor.cpp | 37 +++++++++++++++++++++++-------------- src/ingestor.hpp | 1 + 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/ingestor.cpp b/src/ingestor.cpp index cb758cd..1bf4c99 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -283,32 +283,41 @@ namespace WebStat { Ingestor::NewEntityIds Ingestor::storeEntities(DB::Connection * dbconn, const std::span> values) const { - static constexpr std::array ENTITY_TYPE_VALUES { - "host", "virtual_host", "path", "query_string", "referrer", "user_agent", "unparsable_line"}; + static constexpr std::array, 7> + ENTITY_TYPE_VALUES {{ + {"host", nullptr}, + {"virtual_host", nullptr}, + {"path", nullptr}, + {"query_string", nullptr}, + {"referrer", nullptr}, + {"user_agent", &Ingestor::onNewUserAgent}, + {"unparsable_line", nullptr}, + }}; auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS); NewEntityIds ids; std::ranges::transform(values | std::views::take_while(&std::optional::has_value), ids.begin(), [this, &insert](auto && entity) { const auto & [entityId, type, value] = *entity; - bindMany(insert, 0, entityId, ENTITY_TYPE_VALUES[std::to_underlying(type)], value); - if (insert->execute() > 0) { - switch (type) { - case EntityType::UserAgent: { - auto curlOp = curlGetUserAgentDetail(entityId, value, settings.userAgentAPI.c_str()); - auto added = curlOperations.emplace(curlOp->hnd.get(), std::move(curlOp)); - curl_multi_add_handle(curl.get(), added.first->first); - break; - } - default: - break; - } + const auto & [typeName, onInsert] = ENTITY_TYPE_VALUES[std::to_underlying(type)]; + bindMany(insert, 0, entityId, typeName, value); + if (insert->execute() > 0 && onInsert) { + std::invoke(onInsert, this, *entity); } return std::get<0>(*entity); }); return ids; } + void + Ingestor::onNewUserAgent(const Entity & entity) const + { + const auto & [entityId, type, value] = entity; + auto curlOp = curlGetUserAgentDetail(entityId, value, settings.userAgentAPI.c_str()); + auto added = curlOperations.emplace(curlOp->hnd.get(), std::move(curlOp)); + curl_multi_add_handle(curl.get(), added.first->first); + } + template void Ingestor::storeLogLine(DB::Connection * dbconn, const std::tuple & values) const diff --git a/src/ingestor.hpp b/src/ingestor.hpp index 8be7360..3e25938 100644 --- a/src/ingestor.hpp +++ b/src/ingestor.hpp @@ -71,6 +71,7 @@ namespace WebStat { NewEntityIds storeEntities(DB::Connection *, std::span>) const; using NewEntities = std::array, MAX_NEW_ENTITIES>; template NewEntities newEntities(const std::tuple &) const; + void onNewUserAgent(const Entity &) const; void handleCurlOperations(); void jobIngestParkedLine(const std::filesystem::directory_iterator &); -- cgit v1.2.3