<commit_before>/**
   This class is responsible to store global states for each requester ip. In particular 
   it is supposed:
   - Memory allocation.
   - Take care of locking and unlocking.
   - Keeping track of memory limit and garbage collection.

   AUTHORS:
   - Vmon: Oct 2013: Initial version
 */
#ifndef IP_DATABASE_H
#define IP_DATABASE_H

#include <unordered_map> //IP DB is a hash table
#include <utility>
#include <ts/ts.h> //for locking business
#include <vector>
#include <boost/optional.hpp>

#include "filter_list.h"

const int NO_OF_NON_FILTER_STATE_KEEPER = 1; //swabber interface
const FilterIDType SWABBER_INTERFACE_ID = TOTAL_NO_OF_FILTERS; //swabber interface isn't a filter but it needs use IP database.


//list of filter with db storage requirement (state keepers
const FilterIDType column_to_filter[] = {
  REGEX_BANNER_FILTER_ID,
  CHALLENGER_FILTER_ID,
  SWABBER_INTERFACE_ID
};

/* const unsigned  filters_to_column[] = { */
/*   0 */
/* }; */
const unsigned int NUMBER_OF_STATE_KEEPER_FILTERS = sizeof(column_to_filter) / sizeof(int);

const unsigned int NUMBER_OF_STATE_WORDS = 2;

typedef long long FilterStateUnit;
typedef std::vector<FilterStateUnit> FilterState;
  
struct IPState
{
  std::vector<FilterState> state_array;

  IPState():
  state_array(NUMBER_OF_STATE_KEEPER_FILTERS){};
};

typedef std::unordered_map<std::string, IPState> IPHashTable;

class IPDatabase
{
protected:
  IPHashTable _ip_db;
  TSMutex db_mutex;

  size_t ip_state_array_size;
  size_t filter_to_column[TOTAL_NO_OF_FILTERS + NO_OF_NON_FILTER_STATE_KEEPER]; //+1 because swabber_interface
  
public:
  /**<commit_msg>Move some code to protected IPDatabase class scope<commit_after>/**
   This class is responsible to store global states for each requester ip. In particular 
   it is supposed:
   - Memory allocation.
   - Take care of locking and unlocking.
   - Keeping track of memory limit and garbage collection.

   AUTHORS:
   - Vmon: Oct 2013: Initial version
 */
#ifndef IP_DATABASE_H
#define IP_DATABASE_H

#include <unordered_map> //IP DB is a hash table
#include <utility>
#include <ts/ts.h> //for locking business
#include <vector>
#include <boost/optional.hpp>

#include "filter_list.h"

const FilterIDType SWABBER_INTERFACE_ID = TOTAL_NO_OF_FILTERS; //swabber interface isn't a filter but it needs use IP database.


//list of filter with db storage requirement (state keepers
const FilterIDType column_to_filter[] = {
  REGEX_BANNER_FILTER_ID,
  CHALLENGER_FILTER_ID,
  SWABBER_INTERFACE_ID
};

typedef long long FilterStateUnit;
typedef std::vector<FilterStateUnit> FilterState;

class IPDatabase
{
protected:
  static constexpr size_t NUMBER_OF_STATE_KEEPER_FILTERS = sizeof(column_to_filter) / sizeof(int);
  static constexpr size_t NO_OF_NON_FILTER_STATE_KEEPER = 1; //swabber interface

  struct IPState {
    std::vector<FilterState> state_array;
    IPState(): state_array(NUMBER_OF_STATE_KEEPER_FILTERS){};
  };

  using IPHashTable = std::unordered_map<std::string, IPState>;

  IPHashTable _ip_db;
  TSMutex db_mutex;

  size_t ip_state_array_size;
  size_t filter_to_column[TOTAL_NO_OF_FILTERS + NO_OF_NON_FILTER_STATE_KEEPER]; //+1 because swabber_interface
  
public:
  /**
{'fraction_non_alphanumeric': 0.08299658066521604, 'fraction_numerical': 0.004351880634131178, 'mean_word_length': 4.893772893772894, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '870887', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1079, 'n_words': 297}