summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ingestor.cpp57
-rw-r--r--src/ingestor.hpp5
-rw-r--r--src/util.hpp16
3 files changed, 39 insertions, 39 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
diff --git a/src/ingestor.hpp b/src/ingestor.hpp
index 458b941..7141515 100644
--- a/src/ingestor.hpp
+++ b/src/ingestor.hpp
@@ -25,11 +25,6 @@ namespace WebStat {
void ingestLog(std::FILE *);
void ingestLogLine(std::string_view);
- template<typename T> [[nodiscard]] size_t storeEntity(const T &) const;
- // NOLINTNEXTLINE(modernize-use-nodiscard); testing exposition only
- size_t storeEntity(Entity) const;
- // NOLINTNEXTLINE(modernize-use-nodiscard); testing exposition only
- size_t storeEntity(std::optional<Entity>) const;
template<typename... T> void storeLogLine(const std::tuple<T...> &) const;
protected:
diff --git a/src/util.hpp b/src/util.hpp
new file mode 100644
index 0000000..0ed260c
--- /dev/null
+++ b/src/util.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <tuple>
+
+namespace WebStat {
+ template<typename... T>
+ auto
+ visitSum(auto && visitor, const std::tuple<T...> & values)
+ {
+ return std::apply(
+ [&](auto &&... value) {
+ return (visitor(value) + ...);
+ },
+ values);
+ }
+}