<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * Authors: <NAME><<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#pragma once

#include <memory>
#include <unordered_map>
#include <vector>

#include "compat/Mutex.h"
#include "SqliteTools.h"

namespace medialibrary
{

namespace cachepolicy
{

template <typename T>
struct Cached
{
private:
    using Lock = std::unique_lock<compat::Mutex>;
    static std::unordered_map<int64_t, std::shared_ptr<T>> Store;
    static compat::Mutex Mutex;

public:
    static Lock lock()
    {
        return Lock{ Mutex };
    }

    static void insert( int64_t key, std::shared_ptr<T> value )
    {
        assert( Store.find( key ) == end( Store ) );
        save( key, std::move( value ) );
    }

    static void save( int64_t key, std::shared_ptr<T> value )
    {
        Store[key] = std::move( value );
    }

    static void remove( int64_t key )
    {
        Lock l{ Mutex };
        auto it = Store.find( key );
        if ( it != end( Store ) )
            Store.erase( it );
    }

    static void clear()
    {
        Lock l{ Mutex };
        Store.clear();
    }

    static std::shared_ptr<T> load( int64_t key )
    {
        auto it = Store.find( key );
        if ( it == Store.end() )
            return nullptr;
        return it->second;
    }
};

template <typename T>
std::unordered_map<int64_t, std::shared_ptr<T>>
Cached<T>::Store;

template <typename T>
compat::Mutex Cached<T>::Mutex;

template <typename T>
struct Uncached
{
private:
    struct FakeLock
    {
        FakeLock() = default;
        ~FakeLock() = default;
    };

public:
    static FakeLock lock() { return FakeLock{}; }
    static void insert( int64_t, std::shared_ptr<T> ) {}
    static void save( int64_t, std::shared_ptr<T> ) {}
    static void remove( int64_t ) {}
    static void clear() {}
    static std::shared_ptr<T> load( int64_t ) { return nullptr; }
};

}

template <typename IMPL, typename TABLEPOLICY, typename CACHEPOLICY = cachepolicy::Cached<IMPL>>
class DatabaseHelpers
{
    public:
        template <typename... Args>
        static std::shared_ptr<IMPL> fetch( MediaLibraryPtr ml, const std::string& req, Args&&... args )
        {
            return sqlite::Tools::fetchOne<IMPL>( ml, req, std::forward<Args>( args )... );
        }

        static std::shared_ptr<IMPL> fetch( MediaLibraryPtr ml, int64_t pkValue )
        {
            static std::string req = "SELECT * FROM " + TABLEPOLICY::Name + " WHERE " +
                    TABLEPOLICY::PrimaryKeyColumn + " = ?";
            return sqlite::Tools::fetchOne<IMPL>( ml, req, pkValue );
        }

        /*
         * Will fetch all elements from the database & cache them.
         */
        template <typename INTF = IMPL>
        static std::vector<std::shared_ptr<INTF>> fetchAll( MediaLibraryPtr ml )
        {
            static const std::string req = "SELECT * FROM " + TABLEPOLICY::Name;
            return sqlite::Tools::fetchAll<IMPL, INTF>( ml, req );
        }

        template <typename INTF, typename... Args>
        static std::vector<std::shared_ptr<INTF>> fetchAll( MediaLibraryPtr ml, const std::string &req, Args&&... args )
        {
            return sqlite::Tools::fetchAll<IMPL, INTF>( ml, req, std::forward<Args>( args )... );
        }

        static std::shared_ptr<IMPL> load( MediaLibraryPtr ml, sqlite::Row& row )
        {
            auto l = CACHEPOLICY::lock();

            auto key = row.load<int64_t>( 0 );
            auto res = CACHEPOLICY::load( key );
            if ( res != nullptr )
                return res;
            res = std::make_shared<IMPL>( ml, row );
            CACHEPOLICY::save( key, res );
            return res;
        }

        static bool destroy( MediaLibraryPtr ml, int64_t pkValue )
        {
            static const std::string req = "DELETE FROM " + TABLEPOLICY::Name + " WHERE "
                    + TABLEPOLICY::PrimaryKeyColumn + " = ?";
            return sqlite::Tools::executeDelete( ml->getConn(), req, pkValue );
        }

        /**
         * @warning removeFromCache is only meant to be called from an SQLite hook
         */
        static void removeFromCache( int64_t pkValue )
        {
            CACHEPOLICY::remove( pkValue );
        }

        static void clear()
        {
            CACHEPOLICY::clear();
        }

    protected:
        /*
         * Create a new instance of the cache class.
         */
        template <typename... Args>
        static bool insert( MediaLibraryPtr ml, std::shared_ptr<IMPL> self, const std::string& req, Args&&... args )
        {
            int64_t pKey = sqlite::Tools::executeInsert( ml->getConn(), req, std::forward<Args>( args )... );
            if ( pKey == 0 )
                return false;
            (self.get())->*TABLEPOLICY::PrimaryKey = pKey;
            auto l = CACHEPOLICY::lock();
            CACHEPOLICY::insert( pKey, self );
            return true;
        }
};

}<commit_msg>DatabaseHelper: Lock cache store from outside the storage policy<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * Authors: <NAME><<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#pragma once

#include <memory>
#include <unordered_map>
#include <vector>

#include "compat/Mutex.h"
#include "SqliteTools.h"

namespace medialibrary
{

namespace cachepolicy
{

template <typename T>
struct Cached
{
private:
    using Lock = std::unique_lock<compat::Mutex>;
    static std::unordered_map<int64_t, std::shared_ptr<T>> Store;
    static compat::Mutex Mutex;

public:
    static Lock lock()
    {
        return Lock{ Mutex };
    }

    static void insert( int64_t key, std::shared_ptr<T> value )
    {
        assert( Store.find( key ) == end( Store ) );
        save( key, std::move( value ) );
    }

    static void save( int64_t key, std::shared_ptr<T> value )
    {
        Store[key] = std::move( value );
    }

    static void remove( int64_t key )
    {
        auto it = Store.find( key );
        if ( it != end( Store ) )
            Store.erase( it );
    }

    static void clear()
    {
        Store.clear();
    }

    static std::shared_ptr<T> load( int64_t key )
    {
        auto it = Store.find( key );
        if ( it == Store.end() )
            return nullptr;
        return it->second;
    }
};

template <typename T>
std::unordered_map<int64_t, std::shared_ptr<T>>
Cached<T>::Store;

template <typename T>
compat::Mutex Cached<T>::Mutex;

template <typename T>
struct Uncached
{
private:
    struct FakeLock
    {
        FakeLock() = default;
        ~FakeLock() = default;
    };

public:
    static FakeLock lock() { return FakeLock{}; }
    static void insert( int64_t, std::shared_ptr<T> ) {}
    static void save( int64_t, std::shared_ptr<T> ) {}
    static void remove( int64_t ) {}
    static void clear() {}
    static std::shared_ptr<T> load( int64_t ) { return nullptr; }
};

}

template <typename IMPL, typename TABLEPOLICY, typename CACHEPOLICY = cachepolicy::Cached<IMPL>>
class DatabaseHelpers
{
    public:
        template <typename... Args>
        static std::shared_ptr<IMPL> fetch( MediaLibraryPtr ml, const std::string& req, Args&&... args )
        {
            return sqlite::Tools::fetchOne<IMPL>( ml, req, std::forward<Args>( args )... );
        }

        static std::shared_ptr<IMPL> fetch( MediaLibraryPtr ml, int64_t pkValue )
        {
            static std::string req = "SELECT * FROM " + TABLEPOLICY::Name + " WHERE " +
                    TABLEPOLICY::PrimaryKeyColumn + " = ?";
            return sqlite::Tools::fetchOne<IMPL>( ml, req, pkValue );
        }

        /*
         * Will fetch all elements from the database & cache them.
         */
        template <typename INTF = IMPL>
        static std::vector<std::shared_ptr<INTF>> fetchAll( MediaLibraryPtr ml )
        {
            static const std::string req = "SELECT * FROM " + TABLEPOLICY::Name;
            return sqlite::Tools::fetchAll<IMPL, INTF>( ml, req );
        }

        template <typename INTF, typename... Args>
        static std::vector<std::shared_ptr<INTF>> fetchAll( MediaLibraryPtr ml, const std::string &req, Args&&... args )
        {
            return sqlite::Tools::fetchAll<IMPL, INTF>( ml, req, std::forward<Args>( args )... );
        }

        static std::shared_ptr<IMPL> load( MediaLibraryPtr ml, sqlite::Row& row )
        {
            auto l = CACHEPOLICY::lock();

            auto key = row.load<int64_t>( 0 );
            auto res = CACHEPOLICY::load( key );
            if ( res != nullptr )
                return res;
            res = std::make_shared<IMPL>( ml, row );
            CACHEPOLICY::save( key, res );
            return res;
        }

        static bool destroy( MediaLibraryPtr ml, int64_t pkValue )
        {
            static const std::string req = "DELETE FROM " + TABLEPOLICY::Name + " WHERE "
                    + TABLEPOLICY::PrimaryKeyColumn + " = ?";
            return sqlite::Tools::executeDelete( ml->getConn(), req, pkValue );
        }

        /**
         * @warning removeFromCache is only meant to be called from an SQLite hook
         */
        static void removeFromCache( int64_t pkValue )
        {
            auto l = CACHEPOLICY::lock();

            CACHEPOLICY::remove( pkValue );
        }

        static void clear()
        {
            auto l = CACHEPOLICY::lock();

            CACHEPOLICY::clear();
        }

    protected:
        /*
         * Create a new instance of the cache class.
         */
        template <typename... Args>
        static bool insert( MediaLibraryPtr ml, std::shared_ptr<IMPL> self, const std::string& req, Args&&... args )
        {
            int64_t pKey = sqlite::Tools::executeInsert( ml->getConn(), req, std::forward<Args>( args )... );
            if ( pKey == 0 )
                return false;
            (self.get())->*TABLEPOLICY::PrimaryKey = pKey;
            auto l = CACHEPOLICY::lock();
            CACHEPOLICY::insert( pKey, self );
            return true;
        }
};

}
{'fraction_non_alphanumeric': 0.15489654016922175, 'fraction_numerical': 0.008209767948395744, 'mean_word_length': 2.4275050244042493, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 105, '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': '1759601', 'n_tokens_mistral': 3531, 'n_tokens_neox': 3401, 'n_words': 1024}