Commit 070d5c58 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added EventSet and test for EventSet

parent c2c42bdb
...@@ -369,7 +369,7 @@ class DataSet : public KeyValueContainer { ...@@ -369,7 +369,7 @@ class DataSet : public KeyValueContainer {
/** /**
* @brief Returns the RunSet associated with this DataSet. * @brief Returns the RunSet associated with this DataSet.
* *
* @return a reference to the RunSet associated with this DataSet. * @return the RunSet associated with this DataSet.
*/ */
RunSet runs() const; RunSet runs() const;
...@@ -384,6 +384,17 @@ class DataSet : public KeyValueContainer { ...@@ -384,6 +384,17 @@ class DataSet : public KeyValueContainer {
* @return a Run corresponding to the provided run number. * @return a Run corresponding to the provided run number.
*/ */
Run operator[](const RunNumber& runNumber) const; Run operator[](const RunNumber& runNumber) const;
/**
* @brief Returns an EventSet pointing to Events in
* the specified target. If target is -1, the EventSet
* will also iterate over targets.
*
* @param target Target index in which to find events.
*
* @return an EventSet associated with the DataSet.
*/
EventSet events(int target=-1) const;
}; };
class DataSet::const_iterator { class DataSet::const_iterator {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory> #include <memory>
#include <hepnos/ItemType.hpp>
namespace hepnos { namespace hepnos {
...@@ -167,6 +168,16 @@ class DataStore { ...@@ -167,6 +168,16 @@ class DataStore {
*/ */
void shutdown(); void shutdown();
/**
* @brief Returns the number of underlying targets for the
* specified item type.
*
* @param type Item type.
*
* @return The number of targets for the item type.
*/
size_t numTargets(const ItemType& type) const;
private: private:
/** /**
......
...@@ -88,41 +88,6 @@ class EventSet { ...@@ -88,41 +88,6 @@ class EventSet {
*/ */
DataStore datastore() const; DataStore datastore() const;
/**
* @brief Searches this EventSet for an Event with
* the provided run, subrun, and event number and returns
* an iterator to it if found, otherwise it returns an iterator
* pointing to EventSet::end().
*
* @param runNumber Run number of the Event to find.
* @param subrunNumber SubRun number of the Event to find.
* @param eventNumber Event number of the Event to find.
*
* @return an iterator pointing to the Run if found,
* EventSet::end() otherwise.
*/
iterator find(const RunNumber& runNumber,
const SubRunNumber& subrunNumber,
const EventNumber& eventNumber);
/**
* @brief Searches this EventSet for an Event with
* the provided run, subrun, and event number and returns a
* const_iterator to it if found, otherwise it returns an iterator
* pointing to EventSet::cend().
*
* @param runNumber Run number of the Event to find.
* @param subrunNumber SubRun number of the Event to find.
* @param eventNumber Event number of the Event to find.
*
* @return a const_iterator pointing to the Event if found,
* EventSet::cend() otherwise.
*/
const_iterator find(const RunNumber& runNumber,
const SubRunNumber& subrunNumber,
const EventNumber& eventNumber) const;
/** /**
* @brief Returns an iterator referring to the first Event * @brief Returns an iterator referring to the first Event
* in this EventSet. * in this EventSet.
...@@ -174,77 +139,12 @@ class EventSet { ...@@ -174,77 +139,12 @@ class EventSet {
*/ */
const_iterator cend() const; const_iterator cend() const;
/**
* @brief Returns an iterator pointing to the first Event in this
* EventSet, whose run, subrun, end event numbers is equal or greater
* than the lower bounds.
*
* @param lb_run Lower bound Run number.
* @param lb_subrun Lower bound SubRun number.
* @param lb_event Lower bound Event number.
*
* @return An iterator to the first Event in this EventSet
* whose number is equal or greater than lower bounds or EventSet::end()
* if such an Event does not exist.
*/
iterator lower_bound(const RunNumber& lb_run,
const SubRunNumber& lb_subrun,
const EventNumber& lb_event);
/**
* @brief Returns a const_iterator pointing to the first Event in this
* EventSet, whose run, subrun, end event numbers is equal or greater
* than the lower bounds.
*
* @param lb_run Lower bound Run number.
* @param lb_subrun Lower bound SubRun number.
* @param lb_event Lower bound Event number.
*
* @return An iterator to the first Event in this EventSet
* whose number is equal or greater than lower bounds or EventSet::end()
* if such an Event does not exist.
*/
const_iterator lower_bound(const RunNumber& lb_run,
const SubRunNumber& lb_subrun,
const EventNumber& lb_event) const;
/**
* @brief Returns an iterator pointing to the first Event in the
* EventSet whose run, subrun, and event number are strictly greater than
* the provided upper bounds.
*
* @param ub_run Upper bound Run number.
* @param ub_subrun Upper bound SubRun number.
* @param ub_event Upper bound Event number.
*
* @return An iterator to the the first Event in this EventSet,
* whose number is stricly greater than provided upper bound, or
* EventSet::end() if no such an Event exist.
*/
iterator upper_bound(const RunNumber& ub_run,
const SubRunNumber& ub_subrun,
const EventNumber& ub_event);
/**
* @brief Returns a const_iterator pointing to the first Event in the
* EventSet whose run, subrun, and event number are strictly greater than
* the provided upper bounds.
*
* @param ub_run Upper bound Run number.
* @param ub_subrun Upper bound SubRun number.
* @param ub_event Upper bound Event number.
*
* @return An iterator to the the first Event in this EventSet,
* whose number is stricly greater than provided upper bound, or
* EventSet::end() if no such an Event exist.
*/
const_iterator upper_bound(const RunNumber& ub_run,
const SubRunNumber& ub_subrun,
const EventNumber& ub_event) const;
}; };
class EventSet::const_iterator { class EventSet::const_iterator {
friend class EventSet;
protected: protected:
/** /**
...@@ -253,6 +153,8 @@ class EventSet::const_iterator { ...@@ -253,6 +153,8 @@ class EventSet::const_iterator {
class Impl; class Impl;
std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */ std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */
const_iterator(std::unique_ptr<Impl>&& impl);
public: public:
/** /**
* @brief Constructor. Creates a const_iterator pointing * @brief Constructor. Creates a const_iterator pointing
...@@ -260,22 +162,6 @@ class EventSet::const_iterator { ...@@ -260,22 +162,6 @@ class EventSet::const_iterator {
*/ */
const_iterator(); const_iterator();
/**
* @brief Constructor. Creates a const_iterator pointing
* to a given Run. The Run may or may not be valid.
*
* @param current Run to make the const_iterator point to.
*/
const_iterator(const Event& current);
/**
* @brief Constructor. Creates a const_iterator pointing
* to a given Event. The Event may or may not be valid.
*
* @param current Event to make the const_iterator point to.
*/
const_iterator(Event&& current);
typedef const_iterator self_type; typedef const_iterator self_type;
typedef Event value_type; typedef Event value_type;
typedef Event& reference; typedef Event& reference;
...@@ -379,6 +265,12 @@ class EventSet::const_iterator { ...@@ -379,6 +265,12 @@ class EventSet::const_iterator {
class EventSet::iterator : public EventSet::const_iterator { class EventSet::iterator : public EventSet::const_iterator {
friend class EventSet;
private:
iterator(std::unique_ptr<EventSet::const_iterator::Impl>&& impl);
public: public:
/** /**
...@@ -387,22 +279,6 @@ class EventSet::iterator : public EventSet::const_iterator { ...@@ -387,22 +279,6 @@ class EventSet::iterator : public EventSet::const_iterator {
*/ */
iterator(); iterator();
/**
* @brief Constructor. Builds an iterator pointing to
* an existing Event. The Event may or may not be valid.
*
* @param current Event to point to.
*/
iterator(const Event& current);
/**
* @brief Constructor. Builds an iterator pointing to
* an existing Event. The Run may or may not be valid.
*
* @param current DataSet to point to.
*/
iterator(Event&& current);
typedef iterator self_type; typedef iterator self_type;
typedef Event value_type; typedef Event value_type;
typedef Event& reference; typedef Event& reference;
......
...@@ -7,7 +7,8 @@ enum class ItemType : uint32_t { ...@@ -7,7 +7,8 @@ enum class ItemType : uint32_t {
DATASET, DATASET,
RUN, RUN,
SUBRUN, SUBRUN,
EVENT EVENT,
PRODUCT
}; };
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "hepnos/AsyncEngine.hpp" #include "hepnos/AsyncEngine.hpp"
#include "ItemImpl.hpp" #include "ItemImpl.hpp"
#include "DataSetImpl.hpp" #include "DataSetImpl.hpp"
#include "EventSetImpl.hpp"
#include "DataStoreImpl.hpp" #include "DataStoreImpl.hpp"
#include "AsyncEngineImpl.hpp" #include "AsyncEngineImpl.hpp"
#include "WriteBatchImpl.hpp" #include "WriteBatchImpl.hpp"
...@@ -337,6 +338,18 @@ RunSet DataSet::runs() const { ...@@ -337,6 +338,18 @@ RunSet DataSet::runs() const {
return RunSet(m_impl); return RunSet(m_impl);
} }
EventSet DataSet::events(int target) const {
if(!valid()) {
throw Exception("Calling DataSet member function on an invalid DataSet");
}
if(target >= 0)
return EventSet(std::make_shared<EventSetImpl>(*m_impl, target));
else {
auto numTargets = m_impl->m_datastore->numTargets(ItemType::EVENT);
return EventSet(std::make_shared<EventSetImpl>(*m_impl, 0, numTargets));
}
}
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
// DataSet::const_iterator::Impl implementation // DataSet::const_iterator::Impl implementation
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
......
...@@ -73,5 +73,12 @@ bool DataStore::loadRawProduct(const ProductID& productID, char* data, size_t* s ...@@ -73,5 +73,12 @@ bool DataStore::loadRawProduct(const ProductID& productID, char* data, size_t* s
return m_impl->loadRawProduct(productID, data, size); return m_impl->loadRawProduct(productID, data, size);
} }
size_t DataStore::numTargets(const ItemType& type) const {
if(!m_impl) {
throw Exception("Calling DataStore member function on an invalid DataStore object");
}
return m_impl->numTargets(type);
}
} }
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "StringHash.hpp" #include "StringHash.hpp"
#include "DataSetImpl.hpp" #include "DataSetImpl.hpp"
#include "ItemImpl.hpp" #include "ItemImpl.hpp"
#include "ItemType.hpp"
namespace hepnos { namespace hepnos {
...@@ -155,6 +154,22 @@ class DataStoreImpl { ...@@ -155,6 +154,22 @@ class DataStoreImpl {
if(m_mid) margo_finalize(m_mid); if(m_mid) margo_finalize(m_mid);
} }
size_t numTargets(const ItemType& type) const {
switch(type) {
case ItemType::DATASET:
return m_dataset_dbs.dbs.size();
case ItemType::RUN:
return m_run_dbs.dbs.size();
case ItemType::SUBRUN:
return m_subrun_dbs.dbs.size();
case ItemType::EVENT:
return m_event_dbs.dbs.size();
case ItemType::PRODUCT:
return m_product_dbs.dbs.size();
}
return 0;
}
private: private:
static void checkConfig(YAML::Node& config) { static void checkConfig(YAML::Node& config) {
...@@ -477,8 +492,13 @@ class DataStoreImpl { ...@@ -477,8 +492,13 @@ class DataStoreImpl {
// Access functions for numbered items (Runs, SubRuns, and Events) // Access functions for numbered items (Runs, SubRuns, and Events)
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
const sdskv::database& locateItemDb(const ItemType& type, const ItemDescriptor& id) const { const sdskv::database& locateItemDb(const ItemType& type, const ItemDescriptor& id, int target=-1) const {
long unsigned db_idx = 0; long unsigned db_idx = 0;
if(target >= 0) {
if(type == ItemType::RUN) return m_run_dbs.dbs[target];
if(type == ItemType::SUBRUN) return m_subrun_dbs.dbs[target];
if(type == ItemType::EVENT) return m_event_dbs.dbs[target];
}
uint64_t hash; uint64_t hash;
size_t prime = 1099511628211ULL; size_t prime = 1099511628211ULL;
hash = id.dataset.hash(); hash = id.dataset.hash();
...@@ -508,11 +528,12 @@ class DataStoreImpl { ...@@ -508,11 +528,12 @@ class DataStoreImpl {
const ItemType& prefix_type, const ItemType& prefix_type,
const std::shared_ptr<ItemImpl>& current, const std::shared_ptr<ItemImpl>& current,
std::vector<std::shared_ptr<ItemImpl>>& result, std::vector<std::shared_ptr<ItemImpl>>& result,
size_t maxItems) const { size_t maxItems,
int target=-1) const {
int ret; int ret;
result.resize(0); result.resize(0);
const ItemDescriptor& start_key = current->m_descriptor; const ItemDescriptor& start_key = current->m_descriptor;
auto& db = locateItemDb(item_type, start_key); auto& db = locateItemDb(item_type, start_key, target);
// ignore keys that don't have the same uuid // ignore keys that don't have the same uuid
// issue an sdskv_list_keys // issue an sdskv_list_keys
std::vector<ItemDescriptor> descriptors(maxItems); std::vector<ItemDescriptor> descriptors(maxItems);
...@@ -543,7 +564,8 @@ class DataStoreImpl { ...@@ -543,7 +564,8 @@ class DataStoreImpl {
bool itemExists(const UUID& containerUUID, bool itemExists(const UUID& containerUUID,
const RunNumber& run_number, const RunNumber& run_number,
const SubRunNumber& subrun_number = InvalidSubRunNumber, const SubRunNumber& subrun_number = InvalidSubRunNumber,
const EventNumber& event_number = InvalidEventNumber) const { const EventNumber& event_number = InvalidEventNumber,
int target = -1) const {
// build the key // build the key
ItemDescriptor k; ItemDescriptor k;
k.dataset = containerUUID; k.dataset = containerUUID;
...@@ -557,7 +579,7 @@ class DataStoreImpl { ...@@ -557,7 +579,7 @@ class DataStoreImpl {
type = ItemType::EVENT; type = ItemType::EVENT;
} }
// find out which DB to access // find out which DB to access
auto& db = locateItemDb(type, k); auto& db = locateItemDb(type, k, target);
try { try {
bool b = db.exists(&k, sizeof(k)); bool b = db.exists(&k, sizeof(k));
return b; return b;
......
...@@ -14,158 +14,50 @@ ...@@ -14,158 +14,50 @@
namespace hepnos { namespace hepnos {
////////////////////////////////////////////////////////////////////////////////////////////
// EventSet implementation
////////////////////////////////////////////////////////////////////////////////////////////
static EventSet::iterator EventSet_end;
EventSet::EventSet(const std::shared_ptr<EventSetImpl>& impl)
: m_impl(impl) {}
EventSet::EventSet(std::shared_ptr<EventSetImpl>&& impl)
: m_impl(std::move(impl)) {}
DataStore EventSet::datastore() const {
return DataStore(m_impl->m_datastore);
}
EventSet::iterator EventSet::find(const RunNumber& runNumber,
const SubRunNumber& subrunNumber,
const EventNumber& eventNumber) {
int ret;
auto& datastore = m_impl->m_datastore;
bool b = datastore->itemExists(m_impl->m_uuid, runNumber, subrunNumber, eventNumber);
if(!b) return end();
return iterator(
std::make_shared<ItemImpl>(
datastore,
m_impl->m_uuid,
runNumber));
}
EventSet::const_iterator EventSet::find(const RunNumber& runNumber,
const SubRunNumber& subrunNumber,
const EventNumber& eventNumber) const {
iterator it = const_cast<EventSet*>(this)->find(runNumber, subrunNumber, eventNumber);
return it;
}
EventSet::iterator EventSet::begin() {
auto it = find(0,0,0);
if(it != end()) return *it;
auto ds_level = m_impl->m_level;
auto datastore = m_impl->m_datastore;
auto new_event_impl = std::make_shared<ItemImpl>(datastore, m_impl->m_uuid, 0, 0, 0);
Event event(std::move(new_event_impl));
event = event.next();
if(event.valid()) return iterator(event);
else return end();
}
EventSet::iterator EventSet::end() {
return EventSet_end;
}
EventSet::const_iterator EventSet::cbegin() const {
return const_iterator(const_cast<EventSet*>(this)->begin());
}
EventSet::const_iterator EventSet::cend() const {
return EventSet_end;
}
EventSet::const_iterator EventSet::begin() const {
return const_iterator(const_cast<EventSet*>(this)->begin());
}
EventSet::const_iterator EventSet::end() const {
return EventSet_end;
}
EventSet::iterator EventSet::lower_bound(const RunNumber& lb_run,
const SubRunNumber& lb_subrun,
const EventNumber& lb_event) {
if(lb_run == 0 && lb_subrun == 0 && lb_event == 0) {
auto it = find(0,0,0);
if(it != end()) {
return it;
} else {
Event event(std::make_shared<ItemImpl>(
m_impl->m_datastore,
m_impl->m_uuid, 0, 0, 0));
event = event.next();
if(!event.valid()) return end();
else return iterator(event);
}
} else {
// XXX the bellow is actually not correct
auto it = find(lb_run, lb_subrun, lb_event-1);
if(it != end()) {
++it;
return it;
}
Event event(std::make_shared<ItemImpl>(
m_impl->m_datastore,
m_impl->m_uuid, lb_event-1));
event = event.next();
if(!event.valid()) return end();
else return iterator(event);
}
}
EventSet::const_iterator EventSet::lower_bound(const RunNumber& lb_run,
const SubRunNumber& lb_subrun,
const EventNumber& lb_event) const {
iterator it = const_cast<EventSet*>(this)->lower_bound(lb_run, lb_subrun, lb_event);
return it;
}
EventSet::iterator EventSet::upper_bound(const RunNumber& ub_run,
const SubRunNumber& ub_subrun,
const EventNumber& ub_event) {
Event event(std::make_shared<ItemImpl>(m_impl->m_datastore,
m_impl->m_uuid, ub_run, ub_subrun, ub_event));
event = event.next();
if(!event.valid()) return end();
else return iterator(event);
}
EventSet::const_iterator EventSet::upper_bound(const RunNumber& ub_run,
const SubRunNumber& ub_subrun,
const EventNumber& ub_event) const {
iterator it = const_cast<EventSet*>(this)->upper_bound(ub_run, ub_subrun, ub_event);
return it;
}
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
// EventSet::const_iterator::Impl implementation // EventSet::const_iterator::Impl implementation
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
class EventSet::const_iterator::Impl { class EventSet::const_iterator::Impl {
friend class EventSet;
public: public:
Event m_current_event; Event m_current_event;
int m_target = 0;
int m_num_targets = 0;
Impl() Impl()
: m_current_event() : m_current_event()
{}