DataStore.cpp 9.21 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 12 13 14 15
#include <vector>
#include <functional>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include <sdskv-client.h>
#include <bake-client.h>
#include <ch-placement.h>
#include "hepnos/Exception.hpp"
#include "hepnos/DataStore.hpp"
#include "hepnos/DataSet.hpp"
16
#include "private/DataStoreImpl.hpp"
17 18 19

namespace hepnos {

20 21 22 23
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore implementation
////////////////////////////////////////////////////////////////////////////////////////////

24 25 26 27 28 29 30 31 32
DataStore::DataStore() 
: m_impl(std::make_unique<DataStore::Impl>(this)) {
    char* file = getenv("HEPNOS_CONFIG_FILE");
    if(file == nullptr) 
        throw Exception("HEPNOS_CONFIG_FILE environment variable not set");
    std::string configFile(file);
    m_impl->init(configFile);
}

33
DataStore::DataStore(const std::string& configFile) 
34
: m_impl(std::make_unique<DataStore::Impl>(this)) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    m_impl->init(configFile);
}

DataStore::DataStore(DataStore&& other)
: m_impl(std::move(other.m_impl)) {}

DataStore& DataStore::operator=(DataStore&& other) {
    if(&other == this) return *this;
    if(m_impl) {
        m_impl->cleanup();
    }
    m_impl = std::move(other.m_impl);
}
    
DataStore::~DataStore() {
    if(m_impl) {
        m_impl->cleanup();
    }
}

55
DataStore::iterator DataStore::find(const std::string& datasetPath) {
56
    int ret;
57 58 59

    if(datasetPath.find('%') != std::string::npos) {
        throw Exception("Invalid character ('%') in dataset name");
60
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    size_t slash_count = std::count(datasetPath.begin(), datasetPath.end(), '/');
    size_t level = 1 + slash_count;
    std::string containerName;
    std::string datasetName;

    if(slash_count == 0) {
        datasetName = datasetPath;
        containerName = "";
    } else {
        size_t c = datasetPath.find_last_of('/');
        containerName = datasetPath.substr(0,c);
        datasetName   = datasetPath.substr(c+1);
    }

76
    std::vector<char> data;
77
    bool b = m_impl->load(level, containerName, datasetName, data);
78 79
    if(!b) {
        return m_impl->m_end;
80
    }
81
    return iterator(DataSet(this, level, containerName, datasetName));
82 83
}

Matthieu Dorier's avatar
Matthieu Dorier committed
84 85 86 87 88
DataSet DataStore::operator[](const std::string& datasetName) const {
    auto it = find(datasetName);
    return std::move(*it);
}

89
DataStore::const_iterator DataStore::find(const std::string& datasetName) const {
90 91
    DataStore::iterator it = const_cast<DataStore*>(this)->find(datasetName);
    return it;
92 93 94
}

DataStore::iterator DataStore::begin() {
95
    DataSet ds(this, 1, "", "");
96
    ds = ds.next();
97
    if(ds.valid()) return iterator(std::move(ds));
98
    else return end();
99 100
}

101 102 103 104 105
DataStore::const_iterator DataStore::begin() const {
    iterator it = const_cast<DataStore*>(this)->begin();
    return const_iterator(std::move(it));
}

106 107 108 109
DataStore::iterator DataStore::end() {
    return m_impl->m_end;
}

110 111 112 113
DataStore::const_iterator DataStore::end() const {
    return m_impl->m_end;
}

114
DataStore::const_iterator DataStore::cbegin() const {
115
    return const_cast<DataStore*>(this)->begin();
116 117 118
}

DataStore::const_iterator DataStore::cend() const {
119
    return const_cast<DataStore*>(this)->end();
120 121 122
}

DataStore::iterator DataStore::lower_bound(const std::string& lb) {
123 124 125 126 127 128 129 130 131
    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;
    }
132
    DataSet ds(this, 1, "", lb2);
133 134
    ds = ds.next();
    if(!ds.valid()) return end();
135
    else return iterator(std::move(ds));
136 137 138
}

DataStore::const_iterator DataStore::lower_bound(const std::string& lb) const {
139 140
    iterator it = const_cast<DataStore*>(this)->lower_bound(lb);
    return it;
141 142 143
}

DataStore::iterator DataStore::upper_bound(const std::string& ub) {
144
    DataSet ds(this, 1, "", ub);
145 146
    ds = ds.next();
    if(!ds.valid()) return end();
147
    else return iterator(std::move(ds));
148 149 150
}

DataStore::const_iterator DataStore::upper_bound(const std::string& ub) const {
151 152
    iterator it = const_cast<DataStore*>(this)->upper_bound(ub);
    return it;
153 154 155
}

DataSet DataStore::createDataSet(const std::string& name) {
156 157 158
    if(name.find('/') != std::string::npos
    || name.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
159
    }
160
    m_impl->store(1, "", name, std::vector<char>());
161
    return DataSet(this, 1, "", name);
162 163
}

164 165
void DataStore::shutdown() {
    for(auto addr : m_impl->m_addrs) {
Matthieu Dorier's avatar
Matthieu Dorier committed
166
        margo_shutdown_remote_instance(m_impl->m_mid, addr.second);
167 168 169
    }
}

170 171 172 173
bool DataStore::loadRawProduct(const ProductID& productID, std::vector<char>& buffer) {
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, buffer);
}

174 175 176 177
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

178 179 180 181
class DataStore::const_iterator::Impl {
    public:
        DataSet    m_current_dataset;

182 183
        Impl()
        : m_current_dataset()
184
        {}
185

186 187 188 189 190 191
        Impl(const DataSet& dataset)
        : m_current_dataset(dataset)
        {}

        Impl(DataSet&& dataset)
        : m_current_dataset(std::move(dataset))
192
        {}
193 194

        Impl(const Impl& other)
195
        : m_current_dataset(other.m_current_dataset) 
196 197 198
        {}

        bool operator==(const Impl& other) const {
199
            return m_current_dataset == other.m_current_dataset;
200
        }
201 202
};

203 204 205
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////
206

207 208
DataStore::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}
209

210 211 212 213 214
DataStore::const_iterator::const_iterator(const DataSet& dataset)
: m_impl(std::make_unique<Impl>(dataset)) {}

DataStore::const_iterator::const_iterator(DataSet&& dataset)
: m_impl(std::make_unique<Impl>(std::move(dataset))) {}
215

216
DataStore::const_iterator::~const_iterator() {}
217 218

DataStore::const_iterator::const_iterator(const DataStore::const_iterator& other) 
219
: m_impl(std::make_unique<Impl>(*other.m_impl)) {}
220 221

DataStore::const_iterator::const_iterator(DataStore::const_iterator&& other) 
222
: m_impl(std::move(other.m_impl)) {}
223

224 225 226 227
DataStore::const_iterator& DataStore::const_iterator::operator=(const DataStore::const_iterator& other) {
    if(&other == this) return *this;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    return *this;
228 229
}

230 231 232 233
DataStore::const_iterator& DataStore::const_iterator::operator=(DataStore::const_iterator&& other) {
    if(&other == this) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
234 235 236
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++() {
237 238 239 240 241
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_dataset = m_impl->m_current_dataset.next();
    return *this;
242 243 244
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++(int) {
245 246 247
    const_iterator copy = *this;
    ++(*this);
    return copy;
248 249 250
}

const DataStore::const_iterator::reference DataStore::const_iterator::operator*() {
251 252 253 254
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_dataset;
255 256 257
}

const DataStore::const_iterator::pointer DataStore::const_iterator::operator->() {
258 259
    if(!m_impl) return nullptr;
    return &(m_impl->m_current_dataset);
260 261 262
}

bool DataStore::const_iterator::operator==(const self_type& rhs) const {
263 264 265 266
    if(!m_impl && !rhs.m_impl)  return true;
    if(m_impl  && !rhs.m_impl)  return false;
    if(!m_impl && rhs.m_impl)   return false;
    return *m_impl == *(rhs.m_impl);
267 268 269
}

bool DataStore::const_iterator::operator!=(const self_type& rhs) const {
270
    return !(*this == rhs);
271 272
}

273 274 275 276
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

277 278 279 280 281
DataStore::iterator::iterator(const DataSet& current)
: const_iterator(current) {}

DataStore::iterator::iterator(DataSet&& current)
: const_iterator(std::move(current)) {}
282

283 284
DataStore::iterator::iterator()
: const_iterator() {}
285

286
DataStore::iterator::~iterator() {}
287 288

DataStore::iterator::iterator(const DataStore::iterator& other)
289
: const_iterator(other) {}
290 291

DataStore::iterator::iterator(DataStore::iterator&& other) 
292
: const_iterator(std::move(other)) {}
293 294

DataStore::iterator& DataStore::iterator::operator=(const DataStore::iterator& other) {
295 296 297
    if(this == &other) return *this;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    return *this;
298 299 300
}

DataStore::iterator& DataStore::iterator::operator=(DataStore::iterator&& other) {
301 302 303
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
304 305 306
}

DataStore::iterator::reference DataStore::iterator::operator*() {
307
    return const_cast<reference>(const_iterator::operator*());
308 309 310
}

DataStore::iterator::pointer DataStore::iterator::operator->() {
311
    return const_cast<pointer>(const_iterator::operator->());
312 313 314 315
}

}