DataStore.cpp 2.53 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8 9 10 11
#include <vector>
#include <functional>
#include <iostream>
#include "hepnos/Exception.hpp"
#include "hepnos/DataStore.hpp"
#include "hepnos/DataSet.hpp"
12
#include "hepnos/WriteBatch.hpp"
13
#include "DataSetImpl.hpp"
14 15
#include "DataStoreImpl.hpp"
#include "WriteBatchImpl.hpp"
16 17 18

namespace hepnos {

19 20 21 22
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore implementation
////////////////////////////////////////////////////////////////////////////////////////////

23
DataStore::DataStore(std::shared_ptr<DataStoreImpl>&& impl)
24 25
: m_impl(std::move(impl)) {}

26
DataStore::DataStore(const std::shared_ptr<DataStoreImpl>& impl)
27 28 29 30 31 32
: m_impl(impl) {}

bool DataStore::valid() const {
    return m_impl != nullptr;
}

33 34 35 36
DataSet DataStore::root() const {
    return DataSet(std::make_shared<DataSetImpl>(m_impl, 0, std::make_shared<std::string>(""), ""));
}

37
DataStore DataStore::connect(bool use_progress_thread) { 
38 39 40 41
    char* file = getenv("HEPNOS_CONFIG_FILE");
    if(file == nullptr) 
        throw Exception("HEPNOS_CONFIG_FILE environment variable not set");
    std::string configFile(file);
42
    auto impl = std::make_shared<DataStoreImpl>();
43
    impl->init(configFile, use_progress_thread);
44
    return DataStore(std::move(impl));
45 46
}

47
DataStore DataStore::connect(const std::string& configFile, bool use_progress_thread) {
48
    auto impl = std::make_shared<DataStoreImpl>();
49
    impl->init(configFile, use_progress_thread);
50
    return DataStore(std::move(impl));
51 52
}

53
void DataStore::shutdown() {
54 55 56
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
57
    for(auto addr : m_impl->m_addrs) {
Matthieu Dorier's avatar
Matthieu Dorier committed
58
        margo_shutdown_remote_instance(m_impl->m_mid, addr.second);
59 60 61
    }
}

62
bool DataStore::loadRawProduct(const ProductID& productID, std::string& buffer) {
63 64 65
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
66
    return m_impl->loadRawProduct(productID, buffer);
67 68
}

69 70 71 72
bool DataStore::loadRawProduct(const ProductID& productID, char* data, size_t* size) {
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
73
    return m_impl->loadRawProduct(productID, data, size);
74 75
}

76 77 78 79 80 81 82
size_t DataStore::numTargets(const ItemType& type) const {
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
    return m_impl->numTargets(type);
}

83 84
}