#pragma once #include #include #include #include #include #include class QueryAnalytics { public: struct QWithTimestamp { std::string query; uint64_t timestamp; QWithTimestamp(const std::string& query, uint64_t timestamp) : query(query), timestamp(timestamp) {} }; static const size_t QUERY_FINALIZATION_INTERVAL_MICROS = 4 * 1000 * 1000; private: size_t k; const size_t max_size; const size_t max_query_length = 1024; bool expand_query = false; bool auto_aggregation_enabled; // counts aggregated within the current node tsl::htrie_map local_counts; std::shared_mutex lmutex; std::unordered_map> user_prefix_queries; std::shared_mutex umutex; public: QueryAnalytics(size_t k, bool enable_auto_aggregation = true); void add(const std::string& value, const std::string& expanded_key, const bool live_query, const std::string& user_id, uint64_t now_ts_us = 0); void compact_user_queries(uint64_t now_ts_us); void serialize_as_docs(std::string& docs); void reset_local_counts(); size_t get_k(); std::unordered_map> get_user_prefix_queries(); tsl::htrie_map get_local_counts(); void set_expand_query(bool expand_query); bool is_auto_aggregation_enabled() const; };