Run.cpp 12.9 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
#include "hepnos/Run.hpp"
#include "private/RunImpl.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
8
#include "private/SubRunImpl.hpp"
9
#include "private/DataStoreImpl.hpp"
10
#include "private/WriteBatchImpl.hpp"
11 12 13 14

namespace hepnos {

Run::Run()
15
: m_impl(std::make_unique<Run::Impl>(nullptr, 0, std::make_shared<std::string>(""), InvalidRunNumber)) {} 
16

17
Run::Run(DataStore* ds, uint8_t level, const std::shared_ptr<std::string>& container, const RunNumber& rn)
18 19
: m_impl(std::make_unique<Run::Impl>(ds, level, container, rn)) { }

20 21 22 23
Run::Run(const Run& other) {
    if(other.m_impl)
        m_impl = std::make_unique<Run::Impl>(*other.m_impl);
}
24 25 26 27 28

Run::Run(Run&&) = default;

Run& Run::operator=(const Run& other) {
    if(this == &other) return *this;
29 30 31
    if(other.m_impl) {
        m_impl = std::make_unique<Run::Impl>(*other.m_impl);
    }
32 33 34 35 36 37 38
    return *this;
}

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

Run::~Run() = default; 

39
DataStore* Run::getDataStore() const {
40 41 42
    if(!valid()) { 
        throw Exception("Calling Run member function on an invalid Run object");
    }
43 44 45
    return m_impl->m_datastore;
}

46 47 48 49 50
Run Run::next() const {
    if(!valid()) return Run();
   
    std::vector<std::string> keys;
    size_t s = m_impl->m_datastore->m_impl->nextKeys(
51
            m_impl->m_level, *m_impl->m_container, 
52 53
            m_impl->makeKeyStringFromRunNumber(), keys, 1);
    if(s == 0) return Run();
54
    size_t i = m_impl->m_container->size()+1;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    if(keys[0].size() <= i) return Run();
    if(keys[0][i] != '%') return Run();
    std::stringstream strRunNumber;
    strRunNumber << std::hex << keys[0].substr(i+1);
    RunNumber rn;
    strRunNumber >> rn;

    return Run(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, rn);
}

bool Run::valid() const {
    return m_impl && m_impl->m_datastore; 

}

70
ProductID Run::storeRawData(const std::string& key, const std::string& buffer) {
71
    if(!valid()) {
72
        throw Exception("Calling Run member function on an invalid Run object");
73 74 75 76 77
    }
    // forward the call to the datastore's store function
    return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer);
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
ProductID Run::storeRawData(std::string&& key, std::string&& buffer) {
    return storeRawData(key, buffer); // calls the above function
}

ProductID Run::storeRawData(WriteBatch& batch, const std::string& key, const std::string& buffer) {
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
    // forward the call to the datastore's store function
    return batch.m_impl->store(0, m_impl->fullpath(), key, buffer);
}

ProductID Run::storeRawData(WriteBatch& batch, std::string&& key, std::string&& buffer) {
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
    // forward the call to the datastore's store function
    return batch.m_impl->store(0, m_impl->fullpath(), std::move(key), std::move(buffer));
}

bool Run::loadRawData(const std::string& key, std::string& buffer) const {
99
    if(!valid()) {
100
        throw Exception("Calling Run member function on an invalid Run object");
101 102 103 104 105 106
    }
    // forward the call to the datastore's load function
    return m_impl->m_datastore->m_impl->load(0, m_impl->fullpath(), key, buffer);
}

bool Run::operator==(const Run& other) const {
107 108 109 110 111
    bool v1 = valid();
    bool v2 = other.valid();
    if(!v1 && !v2) return true;
    if(v1 && !v2)  return false;
    if(!v1 && v2)  return false;
112 113
    return m_impl->m_datastore == other.m_impl->m_datastore
        && m_impl->m_level     == other.m_impl->m_level
114
        && *m_impl->m_container == *other.m_impl->m_container
115 116 117 118 119 120 121 122
        && m_impl->m_run_nr    == other.m_impl->m_run_nr;
}

bool Run::operator!=(const Run& other) const {
    return !(*this == other);
}

const RunNumber& Run::number() const {
123 124 125
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
126 127 128 129
    return m_impl->m_run_nr;
}

const std::string& Run::container() const {
130 131 132
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
133
    return *m_impl->m_container;
134 135
}

Matthieu Dorier's avatar
Matthieu Dorier committed
136
SubRun Run::createSubRun(const SubRunNumber& subRunNumber) {
137 138 139
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
140 141
    std::string parent = m_impl->fullpath();
    std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
142
    m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, subRunStr, std::string());
143 144
    return SubRun(m_impl->m_datastore, m_impl->m_level+1, 
            std::make_shared<std::string>(parent), subRunNumber);
145 146
}

Matthieu Dorier's avatar
Matthieu Dorier committed
147 148 149 150 151 152 153
SubRun Run::createSubRun(WriteBatch& batch, const SubRunNumber& subRunNumber) {
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
    std::string parent = m_impl->fullpath();
    std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
    batch.m_impl->store(m_impl->m_level+1, parent, subRunStr, std::string());
154 155
    return SubRun(m_impl->m_datastore, m_impl->m_level+1,
            std::make_shared<std::string>(parent), subRunNumber);
Matthieu Dorier's avatar
Matthieu Dorier committed
156 157
}

158
SubRun Run::operator[](const SubRunNumber& subRunNumber) const {
Matthieu Dorier's avatar
Matthieu Dorier committed
159
    auto it = find(subRunNumber);
160 161
    if(!it->valid())
        throw Exception("Requested SubRun does not exist");
162 163 164
    return std::move(*it);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
165
Run::iterator Run::find(const SubRunNumber& subRunNumber) {
166 167 168
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
169
    int ret;
Matthieu Dorier's avatar
Matthieu Dorier committed
170 171
    std::string parent = m_impl->fullpath();
    std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
172
    bool b = m_impl->m_datastore->m_impl->exists(m_impl->m_level+1, parent, subRunStr);
173
    if(!b) {
Matthieu Dorier's avatar
Matthieu Dorier committed
174
        return m_impl->m_end;
175
    }
176 177
    return iterator(SubRun(m_impl->m_datastore, m_impl->m_level+1,
                std::make_shared<std::string>(parent), subRunNumber));
178 179
}

Matthieu Dorier's avatar
Matthieu Dorier committed
180 181
Run::const_iterator Run::find(const SubRunNumber& subRunNumber) const {
    iterator it = const_cast<Run*>(this)->find(subRunNumber);
182 183 184
    return it;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
185 186 187 188 189 190 191
Run::iterator Run::begin() {
    auto it = find(0);
    if(it != end()) return *it;

    auto level = m_impl->m_level;
    auto datastore = m_impl->m_datastore;
    std::string container = m_impl->fullpath();
192
    SubRun subrun(datastore, level+1, std::make_shared<std::string>(container), 0);
Matthieu Dorier's avatar
Matthieu Dorier committed
193 194 195
    subrun = subrun.next();

    if(subrun.valid()) return iterator(subrun);
196 197 198
    else return end();
}

Matthieu Dorier's avatar
Matthieu Dorier committed
199
Run::iterator Run::end() {
200 201 202
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
203 204 205 206 207 208 209 210
    return m_impl->m_end;
}

Run::const_iterator Run::begin() const {
    return const_iterator(const_cast<Run*>(this)->begin());
}

Run::const_iterator Run::end() const {
211 212 213
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
214
    return m_impl->m_end;
215 216
}

Matthieu Dorier's avatar
Matthieu Dorier committed
217 218
Run::const_iterator Run::cbegin() const {
    return const_iterator(const_cast<Run*>(this)->begin());
219 220
}

Matthieu Dorier's avatar
Matthieu Dorier committed
221
Run::const_iterator Run::cend() const {
222 223 224
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
225
    return m_impl->m_end;
226 227
}

Matthieu Dorier's avatar
Matthieu Dorier committed
228 229 230 231 232 233 234 235
Run::iterator Run::lower_bound(const SubRunNumber& lb) {
    if(lb == 0) {
        auto it = find(0);
        if(it != end()) {
            return it;
        } else {
            SubRun subrun(m_impl->m_datastore, 
                    m_impl->m_level+1,
236
                    std::make_shared<std::string>(m_impl->fullpath()), 0);
Matthieu Dorier's avatar
Matthieu Dorier committed
237 238 239 240 241 242 243 244 245 246 247 248
            subrun = subrun.next();
            if(!subrun.valid()) return end();
            else return iterator(subrun);
        }
    } else {
        auto it = find(lb-1);
        if(it != end()) {
            ++it;
            return it;
        }
        SubRun subrun(m_impl->m_datastore,
                m_impl->m_level+1,
249
                std::make_shared<std::string>(m_impl->fullpath()), lb-1);
Matthieu Dorier's avatar
Matthieu Dorier committed
250 251 252
        subrun = subrun.next();
        if(!subrun.valid()) return end();
        else return iterator(subrun);
253 254 255
    }
}

Matthieu Dorier's avatar
Matthieu Dorier committed
256 257
Run::const_iterator Run::lower_bound(const SubRunNumber& lb) const {
    iterator it = const_cast<Run*>(this)->lower_bound(lb);
258 259 260
    return it;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
261
Run::iterator Run::upper_bound(const SubRunNumber& ub) {
262 263 264
    if(!valid()) {
        throw Exception("Calling Run member function on an invalid Run object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
265 266
    SubRun subrun(m_impl->m_datastore, 
            m_impl->m_level+1, 
267
            std::make_shared<std::string>(m_impl->fullpath()), ub);
Matthieu Dorier's avatar
Matthieu Dorier committed
268 269 270
    subrun = subrun.next();
    if(!subrun.valid()) return end();
    else return iterator(subrun);
271 272
}

Matthieu Dorier's avatar
Matthieu Dorier committed
273 274
Run::const_iterator Run::upper_bound(const SubRunNumber& ub) const {
    iterator it = const_cast<Run*>(this)->upper_bound(ub);
275 276
    return it;
}
Matthieu Dorier's avatar
Matthieu Dorier committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

////////////////////////////////////////////////////////////////////////////////////////////
// Run::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

class Run::const_iterator::Impl {
    public:
        SubRun m_current_subrun;

        Impl()
        : m_current_subrun()
        {}

        Impl(const SubRun& subrun)
        : m_current_subrun(subrun)
        {}

        Impl(SubRun&& subrun)
            : m_current_subrun(std::move(subrun))
        {}

        Impl(const Impl& other)
            : m_current_subrun(other.m_current_subrun)
        {}

        bool operator==(const Impl& other) const {
            return m_current_subrun == other.m_current_subrun;
        }
};

////////////////////////////////////////////////////////////////////////////////////////////
// Run::const_iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

Run::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}

Run::const_iterator::const_iterator(const SubRun& subrun)
: m_impl(std::make_unique<Impl>(subrun)) {}

Run::const_iterator::const_iterator(SubRun&& subrun)
: m_impl(std::make_unique<Impl>(std::move(subrun))) {}

Run::const_iterator::~const_iterator() {}

322 323 324 325
Run::const_iterator::const_iterator(const Run::const_iterator& other) {
    if(other.m_impl) 
        m_impl = std::make_unique<Impl>(*other.m_impl);
}
Matthieu Dorier's avatar
Matthieu Dorier committed
326 327 328 329 330 331

Run::const_iterator::const_iterator(Run::const_iterator&& other)
: m_impl(std::move(other.m_impl)) {}

Run::const_iterator& Run::const_iterator::operator=(const Run::const_iterator& other) {
    if(&other == this) return *this;
332 333 334 335
    if(other.m_impl)
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
Matthieu Dorier's avatar
Matthieu Dorier committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    return *this;
}

Run::const_iterator& Run::const_iterator::operator=(Run::const_iterator&& other) {
    if(&other == this) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
}

Run::const_iterator::self_type Run::const_iterator::operator++() {
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_subrun = m_impl->m_current_subrun.next();
    return *this;
}

Run::const_iterator::self_type Run::const_iterator::operator++(int) {
    const_iterator copy = *this;
    ++(*this);
    return copy;
}

const Run::const_iterator::reference Run::const_iterator::operator*() {
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_subrun;
}

const Run::const_iterator::pointer Run::const_iterator::operator->() {
    if(!m_impl) return nullptr;
    return &(m_impl->m_current_subrun);
}

bool Run::const_iterator::operator==(const self_type& rhs) const {
    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);
}

bool Run::const_iterator::operator!=(const self_type& rhs) const {
379
    return !(*this == rhs);
Matthieu Dorier's avatar
Matthieu Dorier committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
}

////////////////////////////////////////////////////////////////////////////////////////////
// Run::iterator implementation
////////////////////////////////////////////////////////////////////////////////////////////

Run::iterator::iterator(const SubRun& current)
: const_iterator(current) {}

Run::iterator::iterator(SubRun&& current)
: const_iterator(std::move(current)) {}

Run::iterator::iterator()
: const_iterator() {}

Run::iterator::~iterator() {}

Run::iterator::iterator(const Run::iterator& other)
: const_iterator(other) {}

Run::iterator::iterator(Run::iterator&& other)
: const_iterator(std::move(other)) {}

Run::iterator& Run::iterator::operator=(const Run::iterator& other) {
    if(this == &other) return *this;
405 406 407 408
    if(other.m_impl) 
        m_impl = std::make_unique<Impl>(*other.m_impl);
    else
        m_impl.reset();
Matthieu Dorier's avatar
Matthieu Dorier committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    return *this;
}

Run::iterator& Run::iterator::operator=(Run::iterator&& other) {
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
}

Run::iterator::reference Run::iterator::operator*() {
    return const_cast<reference>(const_iterator::operator*());
}

Run::iterator::pointer Run::iterator::operator->() {
    return const_cast<pointer>(const_iterator::operator->());
}

426
}