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

namespace hepnos {

5
6
7
DataSet::DataSet()
: m_datastore(nullptr)
, m_level(0)
8
, m_container("")
9
10
, m_name("") {}

11
12
13
14
15
16
17
18
19
20
21
22
23
DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& fullname)
: m_datastore(&ds)
, m_level(level) {
    size_t p = fullname.find_last_of('/');
    if(p == std::string::npos) {
        m_name = fullname;
    } else {
        m_name = fullname.substr(p+1);
        m_container = fullname.substr(0, p);
    }
}

DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& container, const std::string& name) 
24
: m_datastore(&ds)
25
, m_level(level)
26
, m_container(container)
27
28
, m_name(name) {}

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
DataSet DataSet::next() const {
    if(!valid()) return DataSet();
   
    std::vector<std::string> keys; 
    size_t s = m_datastore->nextKeys(m_level, m_container, m_name, keys, 1);
    if(s == 0) return DataSet();
    return DataSet(*m_datastore, m_level, m_container, keys[0]);
}

bool DataSet::valid() const {
    return m_datastore != nullptr;

}

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
    return m_datastore->store(0, fullname(), key, buffer);
}

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
    std::stringstream ss;
    if(m_container.size() != 0)
        ss << m_container << "/";
    ss << m_name;
    return m_datastore->load(0, fullname(), key, buffer);
}

bool DataSet::operator==(const DataSet& other) const {
    return m_datastore == other.m_datastore
        && m_level     == other.m_level
        && m_container == other.m_container
        && m_name      == other.m_name;
}

70
}