Run.cpp 9.87 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 10 11 12 13
#include "private/DataStoreImpl.hpp"

namespace hepnos {

Run::Run()
14
: m_impl(std::make_unique<Run::Impl>(nullptr, 0, "", InvalidRunNumber)) {} 
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

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

Run::Run(const Run& other)
: m_impl(std::make_unique<Run::Impl>(*other.m_impl)) {}

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

Run& Run::operator=(const Run& other) {
    if(this == &other) return *this;
    m_impl = std::make_unique<Run::Impl>(*other.m_impl);
    return *this;
}

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

Run::~Run() = default; 

Run Run::next() const {
    if(!valid()) return Run();
   
    std::vector<std::string> keys;
    size_t s = m_impl->m_datastore->m_impl->nextKeys(
            m_impl->m_level, m_impl->m_container, 
            m_impl->makeKeyStringFromRunNumber(), keys, 1);
    if(s == 0) return Run();
    size_t i = m_impl->m_container.size()+1;
    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; 

}

bool Run::storeRawData(const std::string& key, const std::vector<char>& buffer) {
    if(!valid()) {
        throw Exception("Calling store() on invalid Run");
    }
    // forward the call to the datastore's store function
    return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer);
}

bool Run::loadRawData(const std::string& key, std::vector<char>& buffer) const {
    if(!valid()) {
        throw Exception("Calling load() on invalid Run");
    }
    // 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 {
    return m_impl->m_datastore == other.m_impl->m_datastore
        && m_impl->m_level     == other.m_impl->m_level
        && m_impl->m_container == other.m_impl->m_container
        && 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 {
    return m_impl->m_run_nr;
}

const std::string& Run::container() const {
    return m_impl->m_container;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
93 94 95 96 97
SubRun Run::createSubRun(const SubRunNumber& subRunNumber) {
    std::string parent = m_impl->fullpath();
    std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
    m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, subRunStr, std::vector<char>());
    return SubRun(m_impl->m_datastore, m_impl->m_level+1, parent, subRunNumber);
98 99
}

Matthieu Dorier's avatar
Matthieu Dorier committed
100 101
SubRun Run::operator()(const SubRunNumber& subRunNumber) const {
    auto it = find(subRunNumber);
102 103 104
    return std::move(*it);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
105
Run::iterator Run::find(const SubRunNumber& subRunNumber) {
106 107
    int ret;
    std::vector<char> data;
Matthieu Dorier's avatar
Matthieu Dorier committed
108 109 110
    std::string parent = m_impl->fullpath();
    std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
    bool b = m_impl->m_datastore->m_impl->load(m_impl->m_level+1, parent, subRunStr, data);
111
    if(!b) {
Matthieu Dorier's avatar
Matthieu Dorier committed
112
        return m_impl->m_end;
113
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
114
    return iterator(SubRun(m_impl->m_datastore, m_impl->m_level+1, parent, subRunNumber));
115 116
}

Matthieu Dorier's avatar
Matthieu Dorier committed
117 118
Run::const_iterator Run::find(const SubRunNumber& subRunNumber) const {
    iterator it = const_cast<Run*>(this)->find(subRunNumber);
119 120 121
    return it;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
122 123 124 125 126 127 128 129 130 131 132
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();
    SubRun subrun(datastore, level+1, container, 0);
    subrun = subrun.next();

    if(subrun.valid()) return iterator(subrun);
133 134 135
    else return end();
}

Matthieu Dorier's avatar
Matthieu Dorier committed
136 137 138 139 140 141 142 143 144 145
Run::iterator Run::end() {
    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 {
    return m_impl->m_end;
146 147
}

Matthieu Dorier's avatar
Matthieu Dorier committed
148 149
Run::const_iterator Run::cbegin() const {
    return const_iterator(const_cast<Run*>(this)->begin());
150 151
}

Matthieu Dorier's avatar
Matthieu Dorier committed
152 153
Run::const_iterator Run::cend() const {
    return m_impl->m_end;
154 155
}

Matthieu Dorier's avatar
Matthieu Dorier committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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,
                    m_impl->fullpath(), 0);
            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,
                m_impl->fullpath(), lb-1);
        subrun = subrun.next();
        if(!subrun.valid()) return end();
        else return iterator(subrun);
181 182 183
    }
}

Matthieu Dorier's avatar
Matthieu Dorier committed
184 185
Run::const_iterator Run::lower_bound(const SubRunNumber& lb) const {
    iterator it = const_cast<Run*>(this)->lower_bound(lb);
186 187 188
    return it;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
189 190 191 192 193 194 195
Run::iterator Run::upper_bound(const SubRunNumber& ub) {
    SubRun subrun(m_impl->m_datastore, 
            m_impl->m_level+1, 
            m_impl->fullpath(), ub);
    subrun = subrun.next();
    if(!subrun.valid()) return end();
    else return iterator(subrun);
196 197
}

Matthieu Dorier's avatar
Matthieu Dorier committed
198 199
Run::const_iterator Run::upper_bound(const SubRunNumber& ub) const {
    iterator it = const_cast<Run*>(this)->upper_bound(ub);
200 201
    return it;
}
Matthieu Dorier's avatar
Matthieu Dorier committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

////////////////////////////////////////////////////////////////////////////////////////////
// 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() {}

Run::const_iterator::const_iterator(const Run::const_iterator& other)
: m_impl(std::make_unique<Impl>(*other.m_impl)) {}

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;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    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 {
        return !(*this == rhs);
}

////////////////////////////////////////////////////////////////////////////////////////////
// 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;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    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->());
}

343
}