summaryrefslogtreecommitdiff
path: root/src/ingestor.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-08-24 12:00:10 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2025-08-25 16:02:46 +0100
commit7301fe6484dc1b1d652425ad005ccfd214002a87 (patch)
treef12bf0ce0ed7adc3f0aea06cf85858aef8c1fdcb /src/ingestor.cpp
parente30cbf9fef8922abef4cc44ad8628d4eef5d28a9 (diff)
downloadwebstat-7301fe6484dc1b1d652425ad005ccfd214002a87.tar.bz2
webstat-7301fe6484dc1b1d652425ad005ccfd214002a87.tar.xz
webstat-7301fe6484dc1b1d652425ad005ccfd214002a87.zip
Merge storeEntities into a single thing
Add visitSum for tuples and makes storeEntities into a single lambda.
Diffstat (limited to 'src/ingestor.cpp')
-rw-r--r--src/ingestor.cpp57
1 files changed, 23 insertions, 34 deletions
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 <connection.h>
#include <dbTypes.h>
#include <modifycommand.h>
@@ -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<T...> & values) const
{
- return std::apply(
- [this](auto &&... value) {
- return (this->storeEntity(value) + ...);
+ return visitSum(
+ [this]<typename X>(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<X, Entity>) {
+ return insertIfReqd(entity);
+ }
+ else if constexpr (std::is_same_v<X, std::optional<Entity>>) {
+ entity.transform(insertIfReqd).value_or(0);
+ }
+ return 0;
},
values);
}
- template<typename T>
- 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> entity) const
- {
- if (entity) {
- return storeEntity(*entity);
- }
- return 0;
- }
-
template<typename... T>
void
Ingestor::storeLogLine(const std::tuple<T...> & values) const