<commit_before>/* 
 * File:   DBCache.h
 * Author: thomas
 *
 * Created on 5. Januar 2016, 17:17
 */

#ifndef DBCACHE_H
#define DBCACHE_H

#include "types.h"
#include "db.h"
#include <map>
#include <memory>

namespace annis {

  struct DBCacheKey {
    std::string corpus;
    bool forceFallback;
    std::map<Component, std::string> overrideImpl;
  };
}

namespace std {

  template<>
  struct less<annis::DBCacheKey> {

    bool operator()(const struct annis::DBCacheKey &a, const struct annis::DBCacheKey &b) const {
      ANNIS_STRUCT_COMPARE(a.corpus, b.corpus);
      ANNIS_STRUCT_COMPARE(a.forceFallback, b.forceFallback);
      const auto& mapA = a.overrideImpl;
      const auto& mapB = b.overrideImpl;
      ANNIS_STRUCT_COMPARE(mapA.size(), mapB.size());
      // more expensive checks using the actual entries
      auto itA = mapA.begin();
      auto itB = mapB.begin();
      while(itA != mapA.end() && itB != mapB.end()) {
        
        // compare key
        std::less<annis::Component> lessCmp;
        const auto& compA = itA->first;
        const auto& compB = itB->first;        
        {if(lessCmp(compA, compB)) {return true;} else if(lessCmp(compB, compA)) {return false;}}
        
        // compare value
        ANNIS_STRUCT_COMPARE(itA->second, itB->second);
        
        itA++;
        itB++;
      }
      
      // they are equal
      return false;
    }
  };
}

namespace annis {

  class DBCache {
  public:
    DBCache();
    DBCache(const DBCache& orig) = delete;

    DB& get(const std::string& corpus, bool forceFallback = false,
            std::map<Component, std::string> overrideImpl = std::map<Component, std::string>()) {
      DBCacheKey key = {corpus, forceFallback, overrideImpl};
      auto it = cache.find(key);
      if (it == cache.end()) {
        // create a new one
        cache[key] = initDB(key);
        return *cache[key];
      }
      return *(it->second);
    }

    void close(const std::string& corpus, bool forceFallback = false,
            std::map<Component, std::string> overrideImpl = std::map<Component, std::string>()) {
      cache.erase({corpus, forceFallback, overrideImpl});
    }

    std::unique_ptr<DB> initDB(const DBCacheKey& key) {
      //std::cerr << "INIT DB " << corpus << " in " << (forceFallback ? "fallback" : "default") << " mode" <<  std::endl;
      std::unique_ptr<DB> result = std::unique_ptr<DB>(new DB());
<commit_msg>rename<commit_after>/* 
 * File:   DBCache.h
 * Author: thomas
 *
 * Created on 5. Januar 2016, 17:17
 */

#ifndef DBCACHE_H
#define DBCACHE_H

#include "types.h"
#include "db.h"
#include <map>
#include <memory>

namespace annis {

  struct DBCacheKey {
    std::string corpus;
    bool forceFallback;
    std::map<Component, std::string> overrideImpl;
  };
}

namespace std {

  template<>
  struct less<annis::DBCacheKey> {

    bool operator()(const struct annis::DBCacheKey &a, const struct annis::DBCacheKey &b) const {
      ANNIS_STRUCT_COMPARE(a.corpus, b.corpus);
      ANNIS_STRUCT_COMPARE(a.forceFallback, b.forceFallback);
      const auto& mapA = a.overrideImpl;
      const auto& mapB = b.overrideImpl;
      ANNIS_STRUCT_COMPARE(mapA.size(), mapB.size());
      // more expensive checks using the actual entries
      auto itA = mapA.begin();
      auto itB = mapB.begin();
      while(itA != mapA.end() && itB != mapB.end()) {
        
        // compare key
        std::less<annis::Component> lessCmp;
        const auto& compA = itA->first;
        const auto& compB = itB->first;        
        {if(lessCmp(compA, compB)) {return true;} else if(lessCmp(compB, compA)) {return false;}}
        
        // compare value
        ANNIS_STRUCT_COMPARE(itA->second, itB->second);
        
        itA++;
        itB++;
      }
      
      // they are equal
      return false;
    }
  };
}

namespace annis {

  class DBCache {
  public:
    DBCache();
    DBCache(const DBCache& orig) = delete;

    DB& get(const std::string& corpus, bool forceFallback = false,
            std::map<Component, std::string> overrideImpl = std::map<Component, std::string>()) {
      DBCacheKey key = {corpus, forceFallback, overrideImpl};
      auto it = cache.find(key);
      if (it == cache.end()) {
        // create a new one
        cache[key] = initDB(key);
        return *cache[key];
      }
      return *(it->second);
    }

    void release(const std::string& corpus, bool forceFallback = false,
            std::map<Component, std::string> overrideImpl = std::map<Component, std::string>()) {
      cache.erase({corpus, forceFallback, overrideImpl});
    }

    std::unique_ptr<DB> initDB(const DBCacheKey& key) {
      //std::cerr << "INIT DB " << corpus << " in " << (forceFallback ? "fallback" : "default") << " mode" <<  std::endl;
      std::unique_ptr<DB> result = std::unique_ptr<DB>(new DB());

{'fraction_non_alphanumeric': 0.14672452986154164, 'fraction_numerical': 0.0037197768133911966, 'mean_word_length': 2.7031369548584543, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 39, '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': '4778371', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1621, 'n_words': 376}