DataStore.cpp 10.3 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 57 58 59
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }

60
    int ret;
61 62 63

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

    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);
    }

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

Matthieu Dorier's avatar
Matthieu Dorier committed
88 89 90 91 92
DataSet DataStore::operator[](const std::string& datasetName) const {
    auto it = find(datasetName);
    return std::move(*it);
}

93
DataStore::const_iterator DataStore::find(const std::string& datasetName) const {
94 95
    DataStore::iterator it = const_cast<DataStore*>(this)->find(datasetName);
    return it;
96 97 98
}

DataStore::iterator DataStore::begin() {
99 100 101
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
102
    DataSet ds(this, 1, "", "");
103
    ds = ds.next();
104
    if(ds.valid()) return iterator(std::move(ds));
105
    else return end();
106 107
}

108 109 110 111 112
DataStore::const_iterator DataStore::begin() const {
    iterator it = const_cast<DataStore*>(this)->begin();
    return const_iterator(std::move(it));
}

113
DataStore::iterator DataStore::end() {
114 115 116
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
117 118 119
    return m_impl->m_end;
}

120
DataStore::const_iterator DataStore::end() const {
121 122 123
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
124 125 126
    return m_impl->m_end;
}

127
DataStore::const_iterator DataStore::cbegin() const {
128
    return const_cast<DataStore*>(this)->begin();
129 130 131
}

DataStore::const_iterator DataStore::cend() const {
132
    return const_cast<DataStore*>(this)->end();
133 134 135
}

DataStore::iterator DataStore::lower_bound(const std::string& lb) {
136 137 138 139 140 141 142 143 144
    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;
    }
145
    DataSet ds(this, 1, "", lb2);
146 147
    ds = ds.next();
    if(!ds.valid()) return end();
148
    else return iterator(std::move(ds));
149 150 151
}

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

DataStore::iterator DataStore::upper_bound(const std::string& ub) {
157 158 159
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
160
    DataSet ds(this, 1, "", ub);
161 162
    ds = ds.next();
    if(!ds.valid()) return end();
163
    else return iterator(std::move(ds));
164 165 166
}

DataStore::const_iterator DataStore::upper_bound(const std::string& ub) const {
167 168
    iterator it = const_cast<DataStore*>(this)->upper_bound(ub);
    return it;
169 170 171
}

DataSet DataStore::createDataSet(const std::string& name) {
172 173 174
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
175 176 177
    if(name.find('/') != std::string::npos
    || name.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
178
    }
179
    m_impl->store(1, "", name, std::vector<char>());
180
    return DataSet(this, 1, "", name);
181 182
}

183
void DataStore::shutdown() {
184 185 186
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
187
    for(auto addr : m_impl->m_addrs) {
Matthieu Dorier's avatar
Matthieu Dorier committed
188
        margo_shutdown_remote_instance(m_impl->m_mid, addr.second);
189 190 191
    }
}

192
bool DataStore::loadRawProduct(const ProductID& productID, std::vector<char>& buffer) {
193 194 195
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
196 197 198
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, buffer);
}

199 200 201 202
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

203 204 205 206
class DataStore::const_iterator::Impl {
    public:
        DataSet    m_current_dataset;

207 208
        Impl()
        : m_current_dataset()
209
        {}
210

211 212 213 214 215 216
        Impl(const DataSet& dataset)
        : m_current_dataset(dataset)
        {}

        Impl(DataSet&& dataset)
        : m_current_dataset(std::move(dataset))
217
        {}
218 219

        Impl(const Impl& other)
220
        : m_current_dataset(other.m_current_dataset) 
221 222 223
        {}

        bool operator==(const Impl& other) const {
224
            return m_current_dataset == other.m_current_dataset;
225
        }
226 227
};

228 229 230
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////
231

232 233
DataStore::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}
234

235 236 237 238 239
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))) {}
240

241
DataStore::const_iterator::~const_iterator() {}
242

243 244 245 246 247
DataStore::const_iterator::const_iterator(const DataStore::const_iterator& other) {
    if(other.m_impl) {
        m_impl = std::make_unique<Impl>(*other.m_impl);
    }
}
248 249

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

252 253
DataStore::const_iterator& DataStore::const_iterator::operator=(const DataStore::const_iterator& other) {
    if(&other == this) return *this;
254 255 256 257
    if(other.m_impl)
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
258
    return *this;
259 260
}

261 262 263 264
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;
265 266 267
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++() {
268 269 270 271 272
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_dataset = m_impl->m_current_dataset.next();
    return *this;
273 274 275
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++(int) {
276 277 278
    const_iterator copy = *this;
    ++(*this);
    return copy;
279 280 281
}

const DataStore::const_iterator::reference DataStore::const_iterator::operator*() {
282 283 284 285
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_dataset;
286 287 288
}

const DataStore::const_iterator::pointer DataStore::const_iterator::operator->() {
289 290 291
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
292
    return &(m_impl->m_current_dataset);
293 294 295
}

bool DataStore::const_iterator::operator==(const self_type& rhs) const {
296 297 298 299
    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);
300 301 302
}

bool DataStore::const_iterator::operator!=(const self_type& rhs) const {
303
    return !(*this == rhs);
304 305
}

306 307 308 309
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

310 311 312 313 314
DataStore::iterator::iterator(const DataSet& current)
: const_iterator(current) {}

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

316 317
DataStore::iterator::iterator()
: const_iterator() {}
318

319
DataStore::iterator::~iterator() {}
320 321

DataStore::iterator::iterator(const DataStore::iterator& other)
322
: const_iterator(other) {}
323 324

DataStore::iterator::iterator(DataStore::iterator&& other) 
325
: const_iterator(std::move(other)) {}
326 327

DataStore::iterator& DataStore::iterator::operator=(const DataStore::iterator& other) {
328
    if(this == &other) return *this;
329 330 331 332
    if(other.m_impl)
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
333
    return *this;
334 335 336
}

DataStore::iterator& DataStore::iterator::operator=(DataStore::iterator&& other) {
337 338 339
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
340 341 342
}

DataStore::iterator::reference DataStore::iterator::operator*() {
343
    return const_cast<reference>(const_iterator::operator*());
344 345 346
}

DataStore::iterator::pointer DataStore::iterator::operator->() {
347
    return const_cast<pointer>(const_iterator::operator->());
348 349 350 351
}

}