DataSet.cpp 6.65 KB
Newer Older
1
#include "hepnos/DataSet.hpp"
2 3 4 5
#include "hepnos/Run.hpp"
#include "hepnos/RunSet.hpp"
#include "private/RunImpl.hpp"
#include "private/DataSetImpl.hpp"
6
#include "private/DataStoreImpl.hpp"
7 8 9

namespace hepnos {

10
DataSet::DataSet()
11
: m_impl(std::make_unique<DataSet::Impl>(this, nullptr, 0, "", "")) {}
12

13
DataSet::DataSet(DataStore* ds, uint8_t level, const std::string& fullname)
14
: m_impl(std::make_unique<DataSet::Impl>(this, ds, level, "", "")) {
15 16
    size_t p = fullname.find_last_of('/');
    if(p == std::string::npos) {
17
        m_impl->m_name = fullname;
18
    } else {
19 20
        m_impl->m_name = fullname.substr(p+1);
        m_impl->m_container = fullname.substr(0, p);
21 22 23
    }
}

24
DataSet::DataSet(DataStore* ds, uint8_t level, const std::string& container, const std::string& name) 
25
: m_impl(std::make_unique<DataSet::Impl>(this, ds, level, container, name)) {}
26 27

DataSet::DataSet(const DataSet& other)
28 29 30 31 32
: m_impl(std::make_unique<DataSet::Impl>(
            this, other.m_impl->m_datastore,
            other.m_impl->m_level,
            other.m_impl->m_container,
            other.m_impl->m_name)) {}
33 34 35 36 37

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

DataSet& DataSet::operator=(const DataSet& other) {
    if(this == &other) return *this;
38 39 40 41 42
    m_impl = std::make_unique<DataSet::Impl>(this, 
            other.m_impl->m_datastore,
            other.m_impl->m_level,
            other.m_impl->m_container,
            other.m_impl->m_name);
43 44 45 46 47 48
    return *this;
}

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

DataSet::~DataSet() {}
49

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

bool DataSet::valid() const {
61
    return m_impl && m_impl->m_datastore; 
62 63 64

}

65
bool DataSet::storeRawData(const std::string& key, const std::vector<char>& buffer) {
66 67 68 69
    if(!valid()) {
        throw Exception("Calling store() on invalid DataSet");
    }
    // forward the call to the datastore's store function
70
    return m_impl->m_datastore->m_impl->store(0, fullname(), key, buffer);
71 72
}

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

bool DataSet::operator==(const DataSet& other) const {
82 83 84 85
    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;
86 87
}

88 89 90 91
bool DataSet::operator!=(const DataSet& other) const {
    return !(*this == other);
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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();
}

108 109 110 111
DataSet DataSet::createDataSet(const std::string& name) {
    if(name.find('/') != std::string::npos) {
        throw Exception("Invalid character '/' in dataset name");
    }
112
    std::string parent = fullname();
113
    m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, name, std::vector<char>());
114
    return DataSet(m_impl->m_datastore, m_impl->m_level+1, parent, name);
115
}
116

117 118 119 120 121 122 123
Run DataSet::createRun(const RunNumber& runNumber) {
    std::string parent = fullname();
    std::string runStr = Run::Impl::makeKeyStringFromRunNumber(runNumber);
    m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, runStr, std::vector<char>());
    return Run(m_impl->m_datastore, m_impl->m_level+1, parent, runNumber);
}

124 125 126 127 128
DataSet DataSet::operator[](const std::string& datasetName) const {
    auto it = find(datasetName);
    return std::move(*it);
}

129 130 131 132 133
Run DataSet::operator()(const RunNumber& runNumber) const {
    auto it = runs().find(runNumber);
    return std::move(*it);
}

134 135 136 137 138 139
DataSet::iterator DataSet::find(const std::string& datasetName) {
    int ret;
    if(datasetName.find('/') != std::string::npos) {
        throw Exception("Invalid character '/' in dataset name");
    }
    std::vector<char> data;
140
    std::string parent = fullname();
141
    bool b = m_impl->m_datastore->m_impl->load(m_impl->m_level+1, parent, datasetName, data);
142 143 144
    if(!b) {
        return m_impl->m_datastore->end();
    }
145
    return iterator(DataSet(m_impl->m_datastore, m_impl->m_level+1, parent, datasetName));
146 147 148 149 150 151 152 153
}

DataSet::const_iterator DataSet::find(const std::string& datasetName) const {
    iterator it = const_cast<DataSet*>(this)->find(datasetName);
    return it;
}

DataSet::iterator DataSet::begin() {
154
    DataSet ds(m_impl->m_datastore, m_impl->m_level+1, fullname(),"");
155
    ds = ds.next();
156
    if(ds.valid()) return iterator(ds);
157 158 159 160 161 162 163
    else return end();
}

DataSet::iterator DataSet::end() {
    return m_impl->m_datastore->end();
}

164 165 166 167 168 169 170 171
DataSet::const_iterator DataSet::begin() const {
    return const_iterator(const_cast<DataSet*>(this)->begin());
}

DataSet::const_iterator DataSet::end() const {
    return m_impl->m_datastore->cend();
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
DataSet::const_iterator DataSet::cbegin() const {
    return const_iterator(const_cast<DataSet*>(this)->begin());
}

DataSet::const_iterator DataSet::cend() const {
    return m_impl->m_datastore->cend();
}

DataSet::iterator DataSet::lower_bound(const std::string& lb) {
    std::string lb2 = lb;
    size_t s = lb2.size();
    lb2[s-1] -= 1; // sdskv_list_keys's start_key is exclusive
    iterator it = find(lb2);
    if(it != end()) {
        // we found something before the specified lower bound
        ++it;
        return it;
    }
190
    DataSet ds(m_impl->m_datastore, m_impl->m_level+1, fullname(), lb2);
191 192
    ds = ds.next();
    if(!ds.valid()) return end();
193
    else return iterator(ds);
194 195 196 197 198 199 200 201
}

DataSet::const_iterator DataSet::lower_bound(const std::string& lb) const {
    iterator it = const_cast<DataSet*>(this)->lower_bound(lb);
    return it;
}

DataSet::iterator DataSet::upper_bound(const std::string& ub) {
202
    DataSet ds(m_impl->m_datastore, m_impl->m_level+1, fullname(), ub);
203 204
    ds = ds.next();
    if(!ds.valid()) return end();
205
    else return iterator(ds);
206 207 208 209 210 211 212
}

DataSet::const_iterator DataSet::upper_bound(const std::string& ub) const {
    iterator it = const_cast<DataSet*>(this)->upper_bound(ub);
    return it;
}

213 214 215 216 217 218 219 220
RunSet& DataSet::runs() {
    return m_impl->m_runset;
}

const RunSet& DataSet::runs() const {
    return const_cast<DataSet*>(this)->runs();
}

221
}