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

added EventSet and test for EventSet

parent c2c42bdb
......@@ -369,7 +369,7 @@ class DataSet : public KeyValueContainer {
/**
* @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;
......@@ -384,6 +384,17 @@ class DataSet : public KeyValueContainer {
* @return a Run corresponding to the provided run number.
*/
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 {
......
......@@ -9,6 +9,7 @@
#include <vector>
#include <string>
#include <memory>
#include <hepnos/ItemType.hpp>
namespace hepnos {
......@@ -167,6 +168,16 @@ class DataStore {
*/
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:
/**
......
......@@ -88,41 +88,6 @@ class EventSet {
*/
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
* in this EventSet.
......@@ -174,77 +139,12 @@ class EventSet {
*/
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 {
friend class EventSet;
protected:
/**
......@@ -253,6 +153,8 @@ class EventSet::const_iterator {
class Impl;
std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */
const_iterator(std::unique_ptr<Impl>&& impl);
public:
/**
* @brief Constructor. Creates a const_iterator pointing
......@@ -260,22 +162,6 @@ class EventSet::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 Event value_type;
typedef Event& reference;
......@@ -379,6 +265,12 @@ class EventSet::const_iterator {
class EventSet::iterator : public EventSet::const_iterator {
friend class EventSet;
private:
iterator(std::unique_ptr<EventSet::const_iterator::Impl>&& impl);
public:
/**
......@@ -387,22 +279,6 @@ class EventSet::iterator : public EventSet::const_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 Event value_type;
typedef Event& reference;
......
......@@ -7,7 +7,8 @@ enum class ItemType : uint32_t {
DATASET,
RUN,
SUBRUN,
EVENT
EVENT,
PRODUCT
};
}
......
......@@ -9,6 +9,7 @@
#include "hepnos/AsyncEngine.hpp"
#include "ItemImpl.hpp"
#include "DataSetImpl.hpp"
#include "EventSetImpl.hpp"
#include "DataStoreImpl.hpp"
#include "AsyncEngineImpl.hpp"
#include "WriteBatchImpl.hpp"
......@@ -337,6 +338,18 @@ RunSet DataSet::runs() const {
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
////////////////////////////////////////////////////////////////////////////////////////////
......
......@@ -73,5 +73,12 @@ bool DataStore::loadRawProduct(const ProductID& productID, char* data, size_t* s
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 @@
#include "StringHash.hpp"
#include "DataSetImpl.hpp"
#include "ItemImpl.hpp"
#include "ItemType.hpp"
namespace hepnos {
......@@ -155,6 +154,22 @@ class DataStoreImpl {
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:
static void checkConfig(YAML::Node& config) {
......@@ -477,8 +492,13 @@ class DataStoreImpl {
// 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;
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;
size_t prime = 1099511628211ULL;
hash = id.dataset.hash();
......@@ -508,11 +528,12 @@ class DataStoreImpl {
const ItemType& prefix_type,
const std::shared_ptr<ItemImpl>& current,
std::vector<std::shared_ptr<ItemImpl>>& result,
size_t maxItems) const {
size_t maxItems,
int target=-1) const {
int ret;
result.resize(0);
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
// issue an sdskv_list_keys
std::vector<ItemDescriptor> descriptors(maxItems);
......@@ -543,7 +564,8 @@ class DataStoreImpl {
bool itemExists(const UUID& containerUUID,
const RunNumber& run_number,
const SubRunNumber& subrun_number = InvalidSubRunNumber,
const EventNumber& event_number = InvalidEventNumber) const {
const EventNumber& event_number = InvalidEventNumber,
int target = -1) const {
// build the key
ItemDescriptor k;
k.dataset = containerUUID;
......@@ -557,7 +579,7 @@ class DataStoreImpl {
type = ItemType::EVENT;
}
// find out which DB to access
auto& db = locateItemDb(type, k);
auto& db = locateItemDb(type, k, target);
try {
bool b = db.exists(&k, sizeof(k));
return b;
......
This diff is collapsed.
......@@ -7,46 +7,24 @@
#define __HEPNOS_PRIVATE_EVENTSET_IMPL_H
#include "hepnos/EventSet.hpp"
#include "hepnos/UUID.hpp"
#include "DataSetImpl.hpp"
namespace hepnos {
class DataStoreImpl;
class EventSetImpl {
public:
std::shared_ptr<DataStoreImpl> m_datastore;
uint8_t m_level;
std::shared_ptr<std::string> m_container;
std::string m_name;
UUID m_uuid;
static EventSet::iterator m_end;
EventSetImpl(const std::shared_ptr<DataStoreImpl>& ds,
uint8_t level,
const std::shared_ptr<std::string>& container,
const std::string& name,
const UUID& uuid = UUID())
: m_datastore(ds)
, m_level(level)
, m_container(container)
, m_name(name)
, m_uuid(uuid) {}
EventSetImpl(const std::shared_ptr<DataStoreImpl>& ds,
uint8_t level,
const std::string& fullname,
const UUID& uuid = UUID())
: m_datastore(ds)
, m_level(level)
, m_uuid(uuid) {
size_t p = fullname.find_last_of('/');
m_name = fullname.substr(p+1);
m_container = std::make_shared<std::string>(fullname.substr(0, p));
}
class EventSetImpl : public DataSetImpl {
public:
const int m_target; // target to which to restrict the EventSet, or -1 if no restriction
const int m_num_targets; // number of targets; copy of datastore->numTargets(ItemType::EVENT)
EventSetImpl(const DataSetImpl& dataset, int target=-1, int num_targets=0)
: DataSetImpl(dataset)
, m_target(target)
, m_num_targets(num_targets)
{}
};
}
......
......@@ -11,7 +11,7 @@
#include <memory>
#include "hepnos/Run.hpp"
#include "hepnos/UUID.hpp"
#include "ItemType.hpp"
#include "hepnos/ItemType.hpp"
#include "ItemDescriptor.hpp"
namespace hepnos {
......
......@@ -21,6 +21,9 @@ target_link_libraries(DataSetTest ${CPPUNIT_LIBRARIES} hepnos)
add_executable(RunSetTest RunSetTest.cpp HEPnOSTestMain.cpp)
target_link_libraries(RunSetTest ${CPPUNIT_LIBRARIES} hepnos)
add_executable(EventSetTest EventSetTest.cpp HEPnOSTestMain.cpp)
target_link_libraries(EventSetTest ${CPPUNIT_LIBRARIES} hepnos)
add_executable(RunTest RunTest.cpp HEPnOSTestMain.cpp)
target_link_libraries(RunTest ${CPPUNIT_LIBRARIES} hepnos)
......@@ -57,6 +60,7 @@ target_link_libraries(AsyncWriteBatchTest ${CPPUNIT_LIBRARIES} hepnos ${BOOST_DE
add_test(NAME DataStoreTest COMMAND run-test.sh ./DataStoreTest)
add_test(NAME DataSetTest COMMAND run-test.sh ./DataSetTest)
add_test(NAME RunSetTest COMMAND run-test.sh ./RunSetTest)
add_test(NAME EventSetTest COMMAND run-test.sh ./EventSetTest)
add_test(NAME RunTest COMMAND run-test.sh ./RunTest)
add_test(NAME SubRunTest COMMAND run-test.sh ./SubRunTest)
add_test(NAME EventTest COMMAND run-test.sh ./EventTest)
......
#include "EventSetTest.hpp"
#include "CppUnitAdditionalMacros.hpp"
CPPUNIT_TEST_SUITE_REGISTRATION( EventSetTest );
using namespace hepnos;
void EventSetTest::setUp() {}
void EventSetTest::tearDown() {}
void EventSetTest::testFillDataStore() {
auto root = datastore->root();
auto mds = root.createDataSet("matthieu");
CPPUNIT_ASSERT(mds.valid());
for(unsigned i=3; i < 5; i++) {
auto run = mds.createRun(i);
CPPUNIT_ASSERT(run.valid());
for(unsigned j=6; j < 9; j++) {
auto subrun = run.createSubRun(j);
CPPUNIT_ASSERT(subrun.valid());
for(unsigned k=1; k < 5; k++) {
auto event = subrun.createEvent(k);
CPPUNIT_ASSERT(event.valid());
}
}
}
// dataset in which (0,0,0) exists
auto zero = root.createDataSet("zero");
CPPUNIT_ASSERT(mds.valid());
for(unsigned i=0; i < 2; i++) {
auto run = zero.createRun(i);
CPPUNIT_ASSERT(run.valid());
for(unsigned j=0; j < 3; j++) {
auto subrun = run.createSubRun(j);
CPPUNIT_ASSERT(subrun.valid());
for(unsigned k=0; k < 4; k++) {
auto event = subrun.createEvent(k);
CPPUNIT_ASSERT(event.valid());
}
}
}
// empty dataset
root.createDataSet("empty");
// only runs and subruns
auto mds2 = root.createDataSet("runsandsubruns");
CPPUNIT_ASSERT(mds.valid());
for(unsigned i=3; i < 5; i++) {
auto run = mds.createRun(i);
CPPUNIT_ASSERT(run.valid());
for(unsigned j=6; j < 9; j++) {
auto subrun = run.createSubRun(j);
CPPUNIT_ASSERT(subrun.valid());
}
}
// one single item
auto onevent = root.createDataSet("oneevent");
onevent.createRun(1).createSubRun(3).createEvent(76);
}
void EventSetTest::testBeginEnd() {
auto root = datastore->root();
DataSet mds = root["matthieu"];
CPPUNIT_ASSERT(mds.valid());
DataSet zero = root["zero"];
CPPUNIT_ASSERT(zero.valid());
DataSet empty = root["empty"];
CPPUNIT_ASSERT(empty.valid());
DataSet runsandsubruns = root["runsandsubruns"];
CPPUNIT_ASSERT(runsandsubruns.valid());
DataSet oneevent = root["oneevent"];
CPPUNIT_ASSERT(oneevent.valid());
// empty dataset doesn't have events
auto eventset = empty.events();
for(auto& ev : eventset) {
CPPUNIT_ASSERT_MESSAGE("This statement shouldn't be reached", false);
}
// runsandsubruns dataset doesn't have events
eventset = runsandsubruns.events();
for(auto& ev : eventset) {
CPPUNIT_ASSERT_MESSAGE("This statement shouldn't be reached", false);
}
std::vector<std::tuple<RunNumber, SubRunNumber, EventNumber>> events;
// iteration target by target
unsigned i = 0;
for(int target = 0; target < datastore->numTargets(hepnos::ItemType::EVENT); target++) {
eventset = mds.events(target);
for(auto& ev : eventset) {
CPPUNIT_ASSERT(ev.valid());
i += 1;
}
}
CPPUNIT_ASSERT_EQUAL(2*3*4, (int)i);
// iteration all targets at once
i = 0;
eventset = mds.events();
for(auto& ev : eventset) {
CPPUNIT_ASSERT(ev.valid());
i += 1;
}
CPPUNIT_ASSERT_EQUAL(2*3*4, (int)i);
// iteration target by target when first event is (0,0,0)
i = 0;
for(int target = 0; target < datastore->numTargets(hepnos::ItemType::EVENT); target++) {
eventset = zero.events(target);
for(auto& ev : eventset) {
CPPUNIT_ASSERT(ev.valid());
i += 1;
}
}
CPPUNIT_ASSERT_EQUAL(2*3*4, (int)i);
// iteration all targets at once
i = 0;
eventset = zero.events();
for(auto& ev : eventset) {
CPPUNIT_ASSERT(ev.valid());
i += 1;
}
CPPUNIT_ASSERT_EQUAL(2*3*4, (int)i);
// iteration on single-event dataset
i = 0;
eventset = oneevent.events();
for(auto& ev : eventset) {
CPPUNIT_ASSERT_EQUAL(76, (int)ev.number());
i += 1;
}
CPPUNIT_ASSERT_EQUAL(1,(int)i);
}
#ifndef __HEPNOS_TEST_EVENTSET_H
#define __HEPNOS_TEST_EVENTSET_H
#include <cppunit/extensions/HelperMacros.h>
#include <hepnos.hpp>
extern hepnos::DataStore* datastore;
class EventSetTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( EventSetTest );
CPPUNIT_TEST( testFillDataStore );
CPPUNIT_TEST( testBeginEnd );
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void testFillDataStore();
void testBeginEnd();
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment