ProductCacheImpl.hpp 3.85 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * (C) 2020 The University of Chicago
 *
 * See COPYRIGHT in top-level directory.
 */
#include "hepnos/ProductCache.hpp"
#include "ItemDescriptor.hpp"
#include "DataStoreImpl.hpp"
#include <unordered_map>
#include <thallium.hpp>

namespace hepnos {

namespace tl = thallium;

struct ProductCacheImpl {

    mutable tl::rwlock                           m_lock;
    std::unordered_map<std::string, std::string> m_map;
20
    bool                                         m_erase_on_load = false;
Matthieu Dorier's avatar
Matthieu Dorier committed
21

22 23 24
    bool loadRawProduct(const ProductID& product_id, std::string& data) {
        if(!m_erase_on_load) m_lock.rdlock();
        else                 m_lock.wrlock();
Matthieu Dorier's avatar
Matthieu Dorier committed
25 26 27
        auto it = m_map.find(product_id.m_key);
        auto found = it != m_map.end();
        if(found) {
28 29 30 31 32 33
            if(!m_erase_on_load) {
                data = it->second;
            } else {
                data = std::move(it->second);
                m_map.erase(it);
            }
Matthieu Dorier's avatar
Matthieu Dorier committed
34 35 36 37 38
        }
        m_lock.unlock();
        return found;
    }

39 40 41
    bool loadRawProduct(const ProductID& product_id, char* value, size_t* vsize) {
        if(!m_erase_on_load) m_lock.rdlock();
        else                 m_lock.wrlock();
42 43 44 45 46 47
        auto it = m_map.find(product_id.m_key);
        auto found = it != m_map.end();
        if(found) {
            auto& data = it->second;
            *vsize = data.size();
            if(*vsize) std::memcpy(value, data.data(), *vsize);
48 49
            if(m_erase_on_load)
                m_map.erase(it);
50 51 52 53 54
        }
        m_lock.unlock();
        return found;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
55 56
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
57
                        std::string& data) {
Matthieu Dorier's avatar
Matthieu Dorier committed
58 59 60 61
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        return loadRawProduct(product_id, data);
    }

62 63
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
64
                        char* value, size_t* vsize) {
65 66 67 68
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        return loadRawProduct(product_id, value, vsize);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    bool hasProduct(const ProductID& product_id) const {
        m_lock.rdlock();
        auto it = m_map.find(product_id.m_key);
        auto found = it != m_map.end();
        m_lock.unlock();
        return found;
    }

    bool hasProduct(const ItemDescriptor& id,
                    const std::string& productName) const {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        return hasProduct(product_id);
    }

    void addRawProduct(const ProductID& product_id,
                       const std::string& data) {
        m_lock.wrlock();
        m_map[product_id.m_key] = data;
        m_lock.unlock();
    }

    void addRawProduct(const ItemDescriptor& id,
                       const std::string& productName,
                       const std::string& data) {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        addRawProduct(product_id, data);
    }

    void addRawProduct(const ProductID& product_id,
                       std::string&& data) {
        m_lock.wrlock();
        m_map[product_id.m_key] = std::move(data);
        m_lock.unlock();
    }

    void addRawProduct(const ItemDescriptor& id,
                       const std::string& productName,
                       std::string&& data) {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        addRawProduct(product_id, std::move(data));
    }

    void removeRawProduct(const ProductID& product_id) {
        m_lock.wrlock();
        m_map.erase(product_id.m_key);
        m_lock.unlock();
    }

    void removeRawProduct(const ItemDescriptor& id,
                          const std::string& productName) {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        removeRawProduct(product_id);
    }
};

}