DataStore.cpp 11.7 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
#include <vector>
#include <functional>
#include <iostream>
#include "hepnos/Exception.hpp"
#include "hepnos/DataStore.hpp"
#include "hepnos/DataSet.hpp"
12
#include "hepnos/WriteBatch.hpp"
13
#include "DataSetImpl.hpp"
14 15
#include "DataStoreImpl.hpp"
#include "WriteBatchImpl.hpp"
16 17 18

namespace hepnos {

19 20 21 22
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore implementation
////////////////////////////////////////////////////////////////////////////////////////////

23
DataStore::DataStore(std::shared_ptr<DataStoreImpl>&& impl)
24 25
: m_impl(std::move(impl)) {}

26
DataStore::DataStore(const std::shared_ptr<DataStoreImpl>& impl)
27 28 29 30 31 32 33
: m_impl(impl) {}

bool DataStore::valid() const {
    return m_impl != nullptr;
}

DataStore DataStore::connect() { 
34 35 36 37
    char* file = getenv("HEPNOS_CONFIG_FILE");
    if(file == nullptr) 
        throw Exception("HEPNOS_CONFIG_FILE environment variable not set");
    std::string configFile(file);
38
    auto impl = std::make_shared<DataStoreImpl>();
39 40
    impl->init(configFile);
    return DataStore(std::move(impl));
41 42
}

43
DataStore DataStore::connect(const std::string& configFile) {
44
    auto impl = std::make_shared<DataStoreImpl>();
45 46
    impl->init(configFile);
    return DataStore(std::move(impl));
47 48
}

49
DataStore::iterator DataStore::find(const std::string& datasetPath) {
50 51 52 53
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }

54
    int ret;
55 56 57

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

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

74
    bool b = m_impl->dataSetExists(level, containerName, datasetName);
75 76
    if(!b) {
        return m_impl->m_end;
77
    }
78 79
    return iterator(
            DataSet(
80
                std::make_shared<DataSetImpl>(
81
                    m_impl, level, std::make_shared<std::string>(containerName), datasetName)));
82 83
}

Matthieu Dorier's avatar
Matthieu Dorier committed
84 85
DataSet DataStore::operator[](const std::string& datasetName) const {
    auto it = find(datasetName);
86 87
    if(!it->valid())
        throw Exception("Requested DataSet does not exist");
Matthieu Dorier's avatar
Matthieu Dorier committed
88 89 90
    return std::move(*it);
}

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

DataStore::iterator DataStore::begin() {
97 98 99
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
100
    DataSet ds(
101
            std::make_shared<DataSetImpl>(
102
                m_impl, 1, std::make_shared<std::string>(""), ""));
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(
146
            std::make_shared<DataSetImpl>(
147
                m_impl, 1, std::make_shared<std::string>(""), lb2));
148 149
    ds = ds.next();
    if(!ds.valid()) return end();
150
    else return iterator(std::move(ds));
151 152 153
}

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

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

DataStore::const_iterator DataStore::upper_bound(const std::string& ub) const {
171 172
    iterator it = const_cast<DataStore*>(this)->upper_bound(ub);
    return it;
173 174 175
}

DataSet DataStore::createDataSet(const std::string& name) {
176 177 178
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
179 180 181
    if(name.find('/') != std::string::npos
    || name.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
182
    }
183
    m_impl->createDataSet(1, "", name);
184
    return DataSet(
185
            std::make_shared<DataSetImpl>(
186
                m_impl, 1, std::make_shared<std::string>(""), name));
187 188 189 190 191 192 193 194 195 196
}

DataSet DataStore::createDataSet(WriteBatch& batch, const std::string& name) {
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
    if(name.find('/') != std::string::npos
    || name.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
    }
197
    batch.m_impl->store(1, "", name);
198
    return DataSet(
199
            std::make_shared<DataSetImpl>(
200
                m_impl, 1, std::make_shared<std::string>(""), name));
201 202
}

203
void DataStore::shutdown() {
204 205 206
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
207
    for(auto addr : m_impl->m_addrs) {
Matthieu Dorier's avatar
Matthieu Dorier committed
208
        margo_shutdown_remote_instance(m_impl->m_mid, addr.second);
209 210 211
    }
}

212
bool DataStore::loadRawProduct(const ProductID& productID, std::string& buffer) {
213 214 215
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
216 217 218
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, buffer);
}

219 220 221 222 223 224 225
bool DataStore::loadRawProduct(const ProductID& productID, char* data, size_t* size) {
    if(!m_impl) {
        throw Exception("Calling DataStore member function on an invalid DataStore object");
    }
    return m_impl->load(productID.m_level, productID.m_containerName, productID.m_objectName, data, size);
}

226 227 228 229
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

230 231 232 233
class DataStore::const_iterator::Impl {
    public:
        DataSet    m_current_dataset;

234 235
        Impl()
        : m_current_dataset()
236
        {}
237

238 239 240 241 242 243
        Impl(const DataSet& dataset)
        : m_current_dataset(dataset)
        {}

        Impl(DataSet&& dataset)
        : m_current_dataset(std::move(dataset))
244
        {}
245 246

        Impl(const Impl& other)
247
        : m_current_dataset(other.m_current_dataset) 
248 249 250
        {}

        bool operator==(const Impl& other) const {
251
            return m_current_dataset == other.m_current_dataset;
252
        }
253 254
};

255 256 257
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////
258

259 260
DataStore::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}
261

262 263 264 265 266
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))) {}
267

268
DataStore::const_iterator::~const_iterator() {}
269

270 271 272 273 274
DataStore::const_iterator::const_iterator(const DataStore::const_iterator& other) {
    if(other.m_impl) {
        m_impl = std::make_unique<Impl>(*other.m_impl);
    }
}
275 276

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

279 280
DataStore::const_iterator& DataStore::const_iterator::operator=(const DataStore::const_iterator& other) {
    if(&other == this) return *this;
281 282 283 284
    if(other.m_impl)
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
285
    return *this;
286 287
}

288 289 290 291
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;
292 293 294
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++() {
295 296 297 298 299
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_dataset = m_impl->m_current_dataset.next();
    return *this;
300 301 302
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++(int) {
303 304 305
    const_iterator copy = *this;
    ++(*this);
    return copy;
306 307 308
}

const DataStore::const_iterator::reference DataStore::const_iterator::operator*() {
309 310 311 312
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_dataset;
313 314 315
}

const DataStore::const_iterator::pointer DataStore::const_iterator::operator->() {
316 317 318
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
319
    return &(m_impl->m_current_dataset);
320 321 322
}

bool DataStore::const_iterator::operator==(const self_type& rhs) const {
323 324 325 326
    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);
327 328 329
}

bool DataStore::const_iterator::operator!=(const self_type& rhs) const {
330
    return !(*this == rhs);
331 332
}

333 334 335 336
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

337 338 339 340 341
DataStore::iterator::iterator(const DataSet& current)
: const_iterator(current) {}

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

343 344
DataStore::iterator::iterator()
: const_iterator() {}
345

346
DataStore::iterator::~iterator() {}
347 348

DataStore::iterator::iterator(const DataStore::iterator& other)
349
: const_iterator(other) {}
350 351

DataStore::iterator::iterator(DataStore::iterator&& other) 
352
: const_iterator(std::move(other)) {}
353 354

DataStore::iterator& DataStore::iterator::operator=(const DataStore::iterator& other) {
355
    if(this == &other) return *this;
356 357 358 359
    if(other.m_impl)
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
360
    return *this;
361 362 363
}

DataStore::iterator& DataStore::iterator::operator=(DataStore::iterator&& other) {
364 365 366
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
367 368 369
}

DataStore::iterator::reference DataStore::iterator::operator*() {
370
    return const_cast<reference>(const_iterator::operator*());
371 372 373
}

DataStore::iterator::pointer DataStore::iterator::operator->() {
374
    return const_cast<pointer>(const_iterator::operator->());
375 376 377 378
}

}