Event.cpp 4.68 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.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6
#include "hepnos/Event.hpp"
7
#include "hepnos/AsyncEngine.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
8
#include "ItemImpl.hpp"
9 10
#include "DataStoreImpl.hpp"
#include "WriteBatchImpl.hpp"
11
#include "AsyncEngineImpl.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
12 13 14 15

namespace hepnos {

Event::Event()
16
: m_impl(std::make_shared<ItemImpl>(nullptr, UUID(), InvalidRunNumber)) {}
Matthieu Dorier's avatar
Matthieu Dorier committed
17

Matthieu Dorier's avatar
Matthieu Dorier committed
18
Event::Event(std::shared_ptr<ItemImpl>&& impl)
19
: m_impl(std::move(impl)) { }
Matthieu Dorier's avatar
Matthieu Dorier committed
20

Matthieu Dorier's avatar
Matthieu Dorier committed
21
Event::Event(const std::shared_ptr<ItemImpl>& impl)
22
: m_impl(impl) { }
Matthieu Dorier's avatar
Matthieu Dorier committed
23

24
DataStore Event::datastore() const {
25 26 27
    if(!valid()) {
        throw Exception("Calling Event member function on an invalid Event object");
    }
28
    return DataStore(m_impl->m_datastore);
29 30
}

31 32 33 34 35 36 37 38 39 40 41
SubRun Event::subrun() const {
    if(!valid()) {
        throw Exception("Calling Event member function on invalid Event object");
    }
    ItemDescriptor subrun_descriptor(
            m_impl->m_descriptor.dataset,
            m_impl->m_descriptor.run,
            m_impl->m_descriptor.subrun);
    return SubRun(std::make_shared<ItemImpl>(m_impl->m_datastore, subrun_descriptor));
}

Matthieu Dorier's avatar
Matthieu Dorier committed
42 43
Event Event::next() const {
    if(!valid()) return Event();
Matthieu Dorier's avatar
Matthieu Dorier committed
44 45

    std::vector<std::shared_ptr<ItemImpl>> next_events;
46
    size_t s = m_impl->m_datastore->nextItems(ItemType::EVENT, ItemType::SUBRUN, m_impl, next_events, 1);
Matthieu Dorier's avatar
Matthieu Dorier committed
47
    if(s == 0) return Event();
Matthieu Dorier's avatar
Matthieu Dorier committed
48
    return Event(std::move(next_events[0]));   
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53 54 55
}

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

}

56
ProductID Event::storeRawData(const std::string& key, const char* value, size_t vsize) {
Matthieu Dorier's avatar
Matthieu Dorier committed
57
    if(!valid()) {
58
        throw Exception("Calling Event member function on an invalid Event object");
Matthieu Dorier's avatar
Matthieu Dorier committed
59 60
    }
    // forward the call to the datastore's store function
Matthieu Dorier's avatar
Matthieu Dorier committed
61 62
    auto& id = m_impl->m_descriptor;
    return m_impl->m_datastore->storeRawProduct(id, key, value, vsize);
Matthieu Dorier's avatar
Matthieu Dorier committed
63 64
}

65
ProductID Event::storeRawData(WriteBatch& batch, const std::string& key, const char* value, size_t vsize) {
66 67 68
    if(!valid()) {
        throw Exception("Calling Event member function on an invalid Event object");
    }
69
    // forward the call to the batch's store function
Matthieu Dorier's avatar
Matthieu Dorier committed
70
    auto& id = m_impl->m_descriptor;
71 72 73 74
    if(batch.m_impl)
        return batch.m_impl->storeRawProduct(id, key, value, vsize);
    else
        return m_impl->m_datastore->storeRawProduct(id, key, value, vsize);
75 76
}

77 78 79 80 81 82
ProductID Event::storeRawData(AsyncEngine& async, const std::string& key, const char* value, size_t vsize) {
    if(!valid()) {
        throw Exception("Calling Event member function on an invalid Event object");
    }
    // forward the call to the async engine's store function
    auto& id = m_impl->m_descriptor;
83 84 85 86
    if(async.m_impl)
        return async.m_impl->storeRawProduct(id, key, value, vsize);
    else
        return m_impl->m_datastore->storeRawProduct(id, key, value, vsize);
87 88
}

89
bool Event::loadRawData(const std::string& key, std::string& buffer) const {
90 91 92
    if(!valid()) {
        throw Exception("Calling Event member function on an invalid Event object");
    }
93
    // forward the call to the datastore's load function
Matthieu Dorier's avatar
Matthieu Dorier committed
94 95
    auto& id = m_impl->m_descriptor;
    return m_impl->m_datastore->loadRawProduct(id, key, buffer);
96 97
}

98
bool Event::loadRawData(const std::string& key, char* value, size_t* vsize) const {
Matthieu Dorier's avatar
Matthieu Dorier committed
99
    if(!valid()) {
100
        throw Exception("Calling DataSet member function on an invalid DataSet");
Matthieu Dorier's avatar
Matthieu Dorier committed
101 102
    }
    // forward the call to the datastore's load function
Matthieu Dorier's avatar
Matthieu Dorier committed
103 104
    auto& id = m_impl->m_descriptor;
    return m_impl->m_datastore->loadRawProduct(id, key, value, vsize);
Matthieu Dorier's avatar
Matthieu Dorier committed
105 106 107
}

bool Event::operator==(const Event& other) const {
108 109 110 111 112
    bool v1 = valid();
    bool v2 = other.valid();
    if(!v1 && !v2) return true;
    if(!v1 &&  v2) return false;
    if(v1  && !v2) return false;
113
    return (m_impl == other.m_impl) || (*m_impl == *other.m_impl);
Matthieu Dorier's avatar
Matthieu Dorier committed
114 115 116 117 118 119 120
}

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

const EventNumber& Event::number() const {
121 122 123
    if(!valid()) {
        throw Exception("Calling Event member function on an invalid Event object");
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
124
    return m_impl->m_descriptor.event;
Matthieu Dorier's avatar
Matthieu Dorier committed
125 126
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
void Event::toDescriptor(EventDescriptor& descriptor) {
    std::memset(descriptor.data, 0, sizeof(descriptor.data));
    if(!valid()) return;
    std::memcpy(descriptor.data, &(m_impl->m_descriptor), sizeof(descriptor.data));
}

Event Event::fromDescriptor(const DataStore& datastore, const EventDescriptor& descriptor, bool validate) {
    auto itemImpl = std::make_shared<ItemImpl>(datastore.m_impl, UUID(), InvalidRunNumber);
    auto& itemDescriptor = itemImpl->m_descriptor;
    std::memcpy(&itemDescriptor, descriptor.data, sizeof(descriptor.data));
    if((!validate) || datastore.m_impl->itemExists(itemDescriptor))
        return Event(std::move(itemImpl));
    else return Event();
}

Matthieu Dorier's avatar
Matthieu Dorier committed
142
}