diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-10-16 20:11:33 +0100 |
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-10-16 20:11:33 +0100 |
| commit | 28892f3d4cf6d9fb6486de5197dd6d46da706f13 (patch) | |
| tree | 29fe4a4ff5eac49d9868daac3224e00f4f28e674 | |
| parent | b9538c693b34a6382ea9f3b985bb922b9c17f12e (diff) | |
| download | webstat-28892f3d4cf6d9fb6486de5197dd6d46da706f13.tar.bz2 webstat-28892f3d4cf6d9fb6486de5197dd6d46da706f13.tar.xz webstat-28892f3d4cf6d9fb6486de5197dd6d46da706f13.zip | |
Replace that awful magic number heavy mapping function
Now a tuple of mapping functors and we pass each value through its
corresponding converter.
| -rw-r--r-- | src/ingestor.cpp | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 1bf4c99..4f752d7 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -30,32 +30,36 @@ namespace WebStat { static_cast<uInt>(value.length()))); } - Entity - toEntity(const std::string_view value, const EntityType type) - { - return {crc32(value), type, value}; - } + template<EntityType Type> struct ToEntity { + Entity + operator()(const std::string_view value) const + { + return {crc32(value), Type, value}; + } - std::optional<Entity> - toEntityo(const std::optional<std::string_view> value, const EntityType type) - { - return value.transform([type](auto && value) { - return toEntity(value, type); - }); - } + template<typename T> + std::optional<Entity> + operator()(const std::optional<T> & value) const + { + return value.transform([this](auto && value) { + return (*this)(value); + }); + } + }; auto crc32ScanValues(const Ingestor::ScanValues & values) { - return std::apply( - [](auto &&... value) { - return std::make_tuple(toEntity(value...[0], EntityType::VirtualHost), value...[1], value...[2], - value...[3], toEntity(value...[4], EntityType::Path), - toEntityo(value...[5], EntityType::QueryString), value...[6], value...[7], value...[8], - value...[9], toEntityo(value...[10], EntityType::Referrer), - toEntityo(value...[11], EntityType::UserAgent)); - }, - values); + static constexpr std::tuple<ToEntity<EntityType::VirtualHost>, std::identity, std::identity, std::identity, + ToEntity<EntityType::Path>, ToEntity<EntityType::QueryString>, std::identity, std::identity, + std::identity, std::identity, ToEntity<EntityType::Referrer>, ToEntity<EntityType::UserAgent>> + ENTITY_TYPE_MAP; + static constexpr size_t VALUE_COUNT = std::tuple_size_v<Ingestor::ScanValues>; + static_assert(VALUE_COUNT == std::tuple_size_v<decltype(ENTITY_TYPE_MAP)>); + + return [&values]<size_t... N>(std::index_sequence<N...>) { + return std::make_tuple(std::get<N>(ENTITY_TYPE_MAP)(std::get<N>(values))...); + }(std::make_index_sequence<VALUE_COUNT>()); } } @@ -186,7 +190,7 @@ namespace WebStat { } else { linesDiscarded++; - const auto unparsableLine = toEntity(line, EntityType::UnparsableLine); + const auto unparsableLine = ToEntity<EntityType::UnparsableLine> {}(line); rememberNewEntityIds(storeEntities(dbconn, {unparsableLine})); } } |
