DataStore.cpp 8.19 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
DataStore::DataStore(const std::string& configFile) 
25
: m_impl(std::make_unique<DataStore::Impl>(this)) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    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();
    }
}

DataStore::iterator DataStore::find(const std::string& datasetName) {
47
    int ret;
48 49 50
    if(datasetName.find('/') != std::string::npos
    || datasetName.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
51
    }
52
    std::vector<char> data;
53
    bool b = m_impl->load(1, "", datasetName, data);
54 55
    if(!b) {
        return m_impl->m_end;
56
    }
57
    return iterator(DataSet(this, 1, datasetName));
58 59
}

Matthieu Dorier's avatar
Matthieu Dorier committed
60 61 62 63 64
DataSet DataStore::operator[](const std::string& datasetName) const {
    auto it = find(datasetName);
    return std::move(*it);
}

65
DataStore::const_iterator DataStore::find(const std::string& datasetName) const {
66 67
    DataStore::iterator it = const_cast<DataStore*>(this)->find(datasetName);
    return it;
68 69 70
}

DataStore::iterator DataStore::begin() {
71
    DataSet ds(this, 1, "", "");
72
    ds = ds.next();
73
    if(ds.valid()) return iterator(std::move(ds));
74
    else return end();
75 76
}

77 78 79 80 81
DataStore::const_iterator DataStore::begin() const {
    iterator it = const_cast<DataStore*>(this)->begin();
    return const_iterator(std::move(it));
}

82 83 84 85
DataStore::iterator DataStore::end() {
    return m_impl->m_end;
}

86 87 88 89
DataStore::const_iterator DataStore::end() const {
    return m_impl->m_end;
}

90
DataStore::const_iterator DataStore::cbegin() const {
91
    return const_cast<DataStore*>(this)->begin();
92 93 94
}

DataStore::const_iterator DataStore::cend() const {
95
    return const_cast<DataStore*>(this)->end();
96 97 98
}

DataStore::iterator DataStore::lower_bound(const std::string& lb) {
99 100 101 102 103 104 105 106 107
    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;
    }
108
    DataSet ds(this, 1, "", lb2);
109 110
    ds = ds.next();
    if(!ds.valid()) return end();
111
    else return iterator(std::move(ds));
112 113 114
}

DataStore::const_iterator DataStore::lower_bound(const std::string& lb) const {
115 116
    iterator it = const_cast<DataStore*>(this)->lower_bound(lb);
    return it;
117 118 119
}

DataStore::iterator DataStore::upper_bound(const std::string& ub) {
120
    DataSet ds(this, 1, "", ub);
121 122
    ds = ds.next();
    if(!ds.valid()) return end();
123
    else return iterator(std::move(ds));
124 125 126
}

DataStore::const_iterator DataStore::upper_bound(const std::string& ub) const {
127 128
    iterator it = const_cast<DataStore*>(this)->upper_bound(ub);
    return it;
129 130 131
}

DataSet DataStore::createDataSet(const std::string& name) {
132 133 134
    if(name.find('/') != std::string::npos
    || name.find('%') != std::string::npos) {
        throw Exception("Invalid character ('/' or '%') in dataset name");
135
    }
136
    m_impl->store(1, "", name, std::vector<char>());
137
    return DataSet(this, 1, "", name);
138 139 140 141 142 143
}

////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

144 145 146 147
class DataStore::const_iterator::Impl {
    public:
        DataSet    m_current_dataset;

148 149
        Impl()
        : m_current_dataset()
150
        {}
151

152 153 154 155 156 157
        Impl(const DataSet& dataset)
        : m_current_dataset(dataset)
        {}

        Impl(DataSet&& dataset)
        : m_current_dataset(std::move(dataset))
158
        {}
159 160

        Impl(const Impl& other)
161
        : m_current_dataset(other.m_current_dataset) 
162 163 164
        {}

        bool operator==(const Impl& other) const {
165
            return m_current_dataset == other.m_current_dataset;
166
        }
167 168
};

169 170 171
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////
172

173 174
DataStore::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}
175

176 177 178 179 180
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))) {}
181

182
DataStore::const_iterator::~const_iterator() {}
183 184

DataStore::const_iterator::const_iterator(const DataStore::const_iterator& other) 
185
: m_impl(std::make_unique<Impl>(*other.m_impl)) {}
186 187

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

190 191 192 193
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;
194 195
}

196 197 198 199
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;
200 201 202
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++() {
203 204 205 206 207
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_dataset = m_impl->m_current_dataset.next();
    return *this;
208 209 210
}

DataStore::const_iterator::self_type DataStore::const_iterator::operator++(int) {
211 212 213
    const_iterator copy = *this;
    ++(*this);
    return copy;
214 215 216
}

const DataStore::const_iterator::reference DataStore::const_iterator::operator*() {
217 218 219 220
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_dataset;
221 222 223
}

const DataStore::const_iterator::pointer DataStore::const_iterator::operator->() {
224 225
    if(!m_impl) return nullptr;
    return &(m_impl->m_current_dataset);
226 227 228
}

bool DataStore::const_iterator::operator==(const self_type& rhs) const {
229 230 231 232
    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);
233 234 235
}

bool DataStore::const_iterator::operator!=(const self_type& rhs) const {
236
    return !(*this == rhs);
237 238
}

239 240 241 242
////////////////////////////////////////////////////////////////////////////////////////////
// DataStore::iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

243 244 245 246 247
DataStore::iterator::iterator(const DataSet& current)
: const_iterator(current) {}

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

249 250
DataStore::iterator::iterator()
: const_iterator() {}
251

252
DataStore::iterator::~iterator() {}
253 254

DataStore::iterator::iterator(const DataStore::iterator& other)
255
: const_iterator(other) {}
256 257

DataStore::iterator::iterator(DataStore::iterator&& other) 
258
: const_iterator(std::move(other)) {}
259 260

DataStore::iterator& DataStore::iterator::operator=(const DataStore::iterator& other) {
261 262 263
    if(this == &other) return *this;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    return *this;
264 265 266
}

DataStore::iterator& DataStore::iterator::operator=(DataStore::iterator&& other) {
267 268 269
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
270 271 272
}

DataStore::iterator::reference DataStore::iterator::operator*() {
273
    return const_cast<reference>(const_iterator::operator*());
274 275 276
}

DataStore::iterator::pointer DataStore::iterator::operator->() {
277
    return const_cast<pointer>(const_iterator::operator->());
278 279 280 281
}

}