<commit_before>/* vim: set ai et ts=4 sw=4: */

#include <stdexcept>
#include <pthread.h>
#include <defer.h>
#include <InMemoryStorage.h>

InMemoryStorage::InMemoryStorage() {
    if(pthread_rwlock_init(&_rwlock, NULL) != 0)
        throw std::runtime_error("InMemoryStorage::InMemoryStorage() - pthread_rwlock_init() failed");
}

InMemoryStorage::~InMemoryStorage() {
    if(pthread_rwlock_destroy(&_rwlock) != 0) {
        // suppress 'throw will always call terminate() [-Wterminate]' warning
        // see https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
        #pragma GCC diagnostic push
        #pragma GCC diagnostic ignored "-Wterminate"
        throw std::runtime_error("InMemoryStorage::~InMemoryStorage() - pthread_rwlock_destroy() failed");
        #pragma GCC diagnostic pop
    }
}

void InMemoryStorage::set(const std::string& key, const std::string& value) {
    pthread_rwlock_wrlock(&_rwlock);
    defer( pthread_rwlock_unlock(&_rwlock) );

    _kv[key] = value; /* insert or update */
}

std::string InMemoryStorage::get(const std::string& key, bool* found) {
    pthread_rwlock_rdlock(&_rwlock);<commit_msg>Get rid of GCC-specific pragmas for better portability<commit_after>/* vim: set ai et ts=4 sw=4: */

#include <stdexcept>
#include <pthread.h>
#include <defer.h>
#include <InMemoryStorage.h>

InMemoryStorage::InMemoryStorage() {
    if(pthread_rwlock_init(&_rwlock, NULL) != 0)
        throw std::runtime_error("InMemoryStorage::InMemoryStorage() - pthread_rwlock_init() failed");
}

InMemoryStorage::~InMemoryStorage() {
    pthread_rwlock_destroy(&_rwlock);
}

void InMemoryStorage::set(const std::string& key, const std::string& value) {
    pthread_rwlock_wrlock(&_rwlock);
    defer( pthread_rwlock_unlock(&_rwlock) );

    _kv[key] = value; /* insert or update */
}

std::string InMemoryStorage::get(const std::string& key, bool* found) {
    pthread_rwlock_rdlock(&_rwlock);
{'fraction_non_alphanumeric': 0.1415929203539823, 'fraction_numerical': 0.003643935450286309, 'mean_word_length': 4.95046439628483, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '203152', 'n_tokens_mistral': 653, 'n_tokens_neox': 639, 'n_words': 128}