DataStore.cpp 2.32 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() { 
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 44
    impl->init(configFile);
    return DataStore(std::move(impl));
45 46
}

47
DataStore DataStore::connect(const std::string& configFile) {
48
    auto impl = std::make_shared<DataStoreImpl>();
49 50
    impl->init(configFile);
    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");
    }
66 67 68
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, buffer);
}

69 70 71 72 73 74 75
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");
    }
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, data, size);
}

76 77
}