DataSet.cpp 3.12 KB
Newer Older
1
2
3
4
#include "hepnos/DataSet.hpp"

namespace hepnos {

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class DataSet::Impl {

    public:

        DataStore*   m_datastore;
        uint8_t      m_level;
        std::string  m_container;
        std::string  m_name;

        Impl(DataStore* ds, uint8_t level, const std::string& container, const std::string& name)
        : m_datastore(ds)
        , m_level(level)
        , m_container(container)
        , m_name(name) {}
};

21
DataSet::DataSet()
22
: m_impl(std::make_unique<DataSet::Impl>(nullptr, 0, "", "")) {}
23

24
DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& fullname)
25
: m_impl(std::make_unique<DataSet::Impl>(&ds, level, "", "")) {
26
27
    size_t p = fullname.find_last_of('/');
    if(p == std::string::npos) {
28
        m_impl->m_name = fullname;
29
    } else {
30
31
        m_impl->m_name = fullname.substr(p+1);
        m_impl->m_container = fullname.substr(0, p);
32
33
34
35
    }
}

DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& container, const std::string& name) 
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
: m_impl(std::make_unique<DataSet::Impl>(&ds, level, container, name)) {}

DataSet::DataSet(const DataSet& other)
: m_impl(std::make_unique<DataSet::Impl>(*other.m_impl)) {}

DataSet::DataSet(DataSet&&) = default;

DataSet& DataSet::operator=(const DataSet& other) {
    if(this == &other) return *this;
    m_impl = std::make_unique<DataSet::Impl>(*other.m_impl);
    return *this;
}

DataSet& DataSet::operator=(DataSet&&) = default;

DataSet::~DataSet() {}
52

53
54
55
56
DataSet DataSet::next() const {
    if(!valid()) return DataSet();
   
    std::vector<std::string> keys; 
57
58
    size_t s = m_impl->m_datastore->nextKeys(
            m_impl->m_level, m_impl->m_container, m_impl->m_name, keys, 1);
59
    if(s == 0) return DataSet();
60
    return DataSet(*(m_impl->m_datastore), m_impl->m_level, m_impl->m_container, keys[0]);
61
62
63
}

bool DataSet::valid() const {
64
    return m_impl && m_impl->m_datastore; 
65
66
67
68
69
70
71
72

}

bool DataSet::storeBuffer(const std::string& key, const std::vector<char>& buffer) {
    if(!valid()) {
        throw Exception("Calling store() on invalid DataSet");
    }
    // forward the call to the datastore's store function
73
    return m_impl->m_datastore->store(0, fullname(), key, buffer);
74
75
76
77
78
79
80
}

bool DataSet::loadBuffer(const std::string& key, std::vector<char>& buffer) const {
    if(!valid()) {
        throw Exception("Calling load() on invalid DataSet");
    }
    // forward the call to the datastore's load function
81
    return m_impl->m_datastore->load(0, fullname(), key, buffer);
82
83
84
}

bool DataSet::operator==(const DataSet& other) const {
85
86
87
88
    return m_impl->m_datastore == other.m_impl->m_datastore
        && m_impl->m_level     == other.m_impl->m_level
        && m_impl->m_container == other.m_impl->m_container
        && m_impl->m_name      == other.m_impl->m_name;
89
90
}

91
92
93
94
bool DataSet::operator!=(const DataSet& other) const {
    return !(*this == other);
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const std::string& DataSet::name() const {
    return m_impl->m_name;
}

const std::string& DataSet::container() const {
    return m_impl->m_container;
}

std::string DataSet::fullname() const {
    std::stringstream ss;
    if(container().size() != 0)
        ss << container() << "/";
    ss << name();
    return ss.str();
}



113
}