ProductCacheImpl.hpp 3.46 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 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * (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;

    bool loadRawProduct(const ProductID& product_id, std::string& data) const {
        m_lock.rdlock();
        auto it = m_map.find(product_id.m_key);
        auto found = it != m_map.end();
        if(found) {
            data = it->second;
        }
        m_lock.unlock();
        return found;
    }

32 33 34 35 36 37 38 39 40 41 42 43 44
    bool loadRawProduct(const ProductID& product_id, char* value, size_t* vsize) const {
        m_lock.rdlock();
        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);
        }
        m_lock.unlock();
        return found;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
45 46 47 48 49 50 51
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
                        std::string& data) const {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        return loadRawProduct(product_id, data);
    }

52 53 54 55 56 57 58
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
                        char* value, size_t* vsize) const {
        auto product_id = DataStoreImpl::buildProductID(id, productName);
        return loadRawProduct(product_id, value, vsize);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
59 60 61 62 63 64 65 66 67 68 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
    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);
    }
};

}