DataSet.hpp 17.4 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 8
#ifndef __HEPNOS_DATA_SET_H
#define __HEPNOS_DATA_SET_H

9
#include <memory>
10
#include <mpi.h>
11
#include <hepnos/Exception.hpp>
12
#include <hepnos/RunNumber.hpp>
13
#include <hepnos/DataStore.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
14
#include <hepnos/KeyValueContainer.hpp>
15 16 17

namespace hepnos {

18 19 20
class RunSet;
class Run;

21 22 23 24 25 26
/**
 * @brief The DataSet class represents a handle to a named dataset
 * stored either at the root of an HEPnOS DataStore service, or within
 * another dataset. It provides functionalities to navigate nested
 * datasets and to load/store data products.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
27
class DataSet : public KeyValueContainer {
28 29

    friend class DataStore;
30
    friend class RunSet;
31
    friend class DataSetImpl;
32 33 34

    private:

35
    /**
36
     * @brief Implementation class (used for the Pimpl idiom).
37
     */
38
    std::shared_ptr<DataSetImpl> m_impl; /*!< Pointer to implementation. */
39 40

    /**
41
     * @brief Constructor.
42
     */
43 44
    DataSet(const std::shared_ptr<DataSetImpl>& impl);
    DataSet(std::shared_ptr<DataSetImpl>&& impl);
45 46 47

    public:

48 49
    typedef DataSet value_type;

50 51 52 53 54 55 56 57 58 59
    /**
     * @brief Default constructor.
     */
    DataSet();

    /**
     * @brief Copy-constructor.
     *
     * @param other DataSet to copy.
     */
60
    DataSet(const DataSet& other) = default;
61 62 63 64 65 66

    /**
     * @brief Move-constructor.
     *
     * @param other DataSet to move.
     */
67
    DataSet(DataSet&& other) = default;
68 69 70 71 72 73 74 75

    /**
     * @brief Copy-assignment operator.
     *
     * @param other DataSet to copy.
     *
     * @return this.
     */
76
    DataSet& operator=(const DataSet& other) = default;
77 78 79 80 81 82 83 84

    /**
     * @brief Move-assignment operator.
     *
     * @param other DataSet to move.
     *
     * @return this.
     */
85
    DataSet& operator=(DataSet&& other) = default;
86 87 88 89

    /**
     * @brief Destructor.
     */
90
    ~DataSet() = default;
91

92
    /**
93
     * @brief Overrides datastore from KeyValueContainer class.
94
     */
95
    DataStore datastore() const override;
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /**
     * @brief Name of the DataSet.
     *
     * @return the name of the DataSet.
     */
    const std::string& name() const;

    /**
     * @brief Name of the container of the DataSet.
     *
     * @return the name of the container of the DataSet.
     */
    const std::string& container() const;

    /**
     * @brief Full name of the DataSet
     * (container() + "/" + name() if container() is not empty,
     * name() otherwise)
     *
     * @return the full name of the DataSet.
     */
    std::string fullname() const;

    /**
     * @brief Gets the next DataSet from this DataSet in 
     * alphabetical order within the same container.
     * If no such dataset exists, this function returns
     * a DataSet instance such that valid() == false.
     *
     * @return the next DataSet from this DataSet.
     */
    DataSet next() const;

    /**
     * @brief Check if a DataSet is valid, i.e. if it
     * corresponds to a DataSet that exists in the
     * underlying DataStore.
     *
     * @return true if the DataSet is valid, false otherwise.
     */
    bool valid() const;

139 140 141 142 143 144 145 146
    /**
     * @brief Stores binary data associated with a particular key into this DataSet.
     * This function will return true if the key did not already exist and the
     * write succeeded. It will return false otherwise.
     *
     * @param key Key.
     * @param buffer Binary data to insert.
     *
147 148
     * @return a valid ProductID if the key did not already exist and the write succeeded,
     *      an invalid one otherwise.
149
     */
150 151
    ProductID storeRawData(const std::string& key, const char* value, size_t vsize) override;
    ProductID storeRawData(WriteBatch& batch, const std::string& key, const char* value, size_t vsize) override;
152
    ProductID storeRawData(AsyncEngine& engine, const std::string& key, const char* value, size_t vsize) override;
153 154 155 156 157 158 159 160 161 162 163 164

    /**
     * @brief Loads binary data associated with a particular key from the DataSet.
     * This function will return true if the key exists and the read succeeded.
     * It will return false otherwise.
     * 
     * @param key Key.
     * @param buffer Buffer in which to put the binary data.
     *
     * @return true if the key exists and the read succeeded,
     *      false otherwise.
     */
165
    bool loadRawData(const std::string& key, std::string& value) const override;
166
    bool loadRawData(const std::string& key, char* value, size_t* vsize) const override;
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    /**
     * @brief Comparison operator.
     *
     * @param other DataSet to compare with.
     *
     * @return true of both DataSets point to the same
     * entry in the HEPnOS service, false otherwise.
     */
    bool operator==(const DataSet& other) const;

    /**
     * @brief Comparison operator.
     *
     * @param other DataSet to compare with.
     *
     * @return false of both DataSets point to the same
     * entry in the HEPnOS service, true otherwise.
     */
    bool operator!=(const DataSet& other) const;
187 188 189

    /**
     * @brief Creates a dataset with a given name inside the
190
     * DataSet. This name must not have the '/' and '%' characters.
191 192 193 194 195 196 197 198 199 200 201
     * A DataSet object pointing to the created dataset is returned.
     * If a dataset with this name already exists in the DataStore, 
     * it is not created, but a DataSet object pointing to the 
     * existing one is returned instead.
     *
     * @param name Name of DataSet.
     *
     * @return A DataSet instance pointing to the created dataset.
     */
    DataSet createDataSet(const std::string& name);

202 203 204 205 206 207 208 209 210 211 212 213
    /**
     * @brief Creates a run with a given run number inside the DataSet.
     * A Run object pointing to the created run is returned.
     * If a run with the same number exists in this DataSet, the run
     * is not created by a Run object pointing to the existing one is
     * returned instead.
     *
     * @param runNumber Run number of the run to create.
     *
     * @return A Run instance pointing to the created run.
     */
    Run createRun(const RunNumber& runNumber);
214
    Run createRun(AsyncEngine& async, const RunNumber& runNumber);
Matthieu Dorier's avatar
Matthieu Dorier committed
215
    Run createRun(WriteBatch& batch, const RunNumber& runNumber);
216

217 218 219 220 221 222 223 224 225 226 227 228
    /**
     * @brief Accesses an existing DataSet using the []
     * operator. If no DataSet correspond to the provided name,
     * the function returns a DataSet instance d such that
     * d.valid() is false.
     *
     * @param datasetName Name of the DataSet to retrieve.
     *
     * @return a DataSet corresponding to the provided name.
     */
    DataSet operator[](const std::string& datasetName) const;

229 230 231 232 233 234 235 236 237 238 239 240 241 242
    /**
     * @brief iterator class to navigate DataSets.
     * This iterator is a forward iterator. DataSets are sorted
     * alphabetically inside the DataStore.
     */
    class iterator;

    /**
     * @brief const_iterator class to navigate DataSets.
     * This iterator is a forward iterator. DataSets are sorted
     * alphabetically inside the DataStore.
     */
    class const_iterator;

243
    /**
244 245
     * @brief Searches this DataSet for a DataSet with 
     * the provided path and returns an iterator to it if found,
246 247
     * otherwise it returns an iterator to DataStore::end().
     *
248
     * @param datasetPath Path of the DataSet to find.
249 250 251 252
     *
     * @return an iterator pointing to the DataSet if found,
     * DataSet::end() otherwise.
     */
253
    iterator find(const std::string& datasetPath);
254 255 256

    /**
     * @brief Searches this DataSet for an DataSet with 
257
     * the provided path and returns a const_iterator to it 
258 259
     * if found, otherwise it returns an iterator to DataSet::end().
     *
260
     * @param datasetPath Path of the DataSet to find.
261 262 263 264
     *
     * @return a const_iterator pointing to the DataSet if found,
     * DataSet::cend() otherwise.
     */
265
    const_iterator find(const std::string& datasetPath) const;
266 267 268 269 270 271 272 273 274 275 276 277 278


    /**
     * @brief Returns an iterator referring to the first DataSet
     * in this DataSet.
     *
     * @return an iterator referring to the first DataSet in this DataSet.
     */
    iterator begin();

    /**
     * @brief Returns an iterator referring to the end of the DataSet.
     * The DataSet pointed to by this iterator is not valid (that is,
279
     * `end()->valid()` returns `false`).
280 281 282 283 284
     *
     * @return an iterator referring to the end of the DataSet.
     */
    iterator end();

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    /**
     * @brief Returns a const_iterator referring to the first DataSet
     * in this DataSet.
     *
     * @return a const_iterator referring to the first DataSet in this DataSet.
     */
    const_iterator begin() const;

    /**
     * @brief Returns a const_iterator referring to the end of the DataSet.
     * The DataSet pointed to by this iterator is not valid (that is,
     * `end()->valid()` returns `false`).
     *
     * @return a const_iterator referring to the end of the DataSet.
     */
    const_iterator end() const;

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 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
    /**
     * @brief Returns a const_iterator referring to the first DataSet
     * in this DataSet.
     *
     * @return a const_iterator referring to the first DataSet in this DataSet.
     */
    const_iterator cbegin() const;

    /**
     * @brief Returns a const_iterator referring to the end of the DataSet.
     * The DataSet pointed to by this iterator is not valid (that is,
     * `cend()->valid()` return `false`).
     *
     * @return a const_iterator referring to the end of the DataStore.
     */
    const_iterator cend() const;

    /**
     * @brief Returns an iterator pointing to the first DataSet in this
     * DataSet, whose name is not considered to go before lb 
     * (i.e., either it is equal or goes after, alphabetically).
     *
     * @param lb DataSet name to search for.
     *
     * @return An iterator to the the first DataSet in this DataSet 
     * whose name is not considered to go before lb, or DataStore::end() 
     * if all keys are considered to go before it.
     */
    iterator lower_bound(const std::string& lb);

    /**
     * @brief Returns a const_iterator pointing to the first DataSet in this
     * DataSet whose name is not considered to go before lb 
     * (i.e., either it is equal or goes after, alphabetically).
     *
     * @param lb DataSet name to search for.
     *
     * @return A const_iterator to the the first DataSet in the DataSet 
     * whose name is not considered to go before lb, or DataSet::cend() 
     * if all DataSet names are considered to go before it.
     */
    const_iterator lower_bound(const std::string& lb) const;

    /**
     * @brief Returns an iterator pointing to the first DataSet in the 
     * DataStore whose key is considered to go after ub.
     *
     * @param ub DataSet name to search for.
     *
     * @return An iterator to the the first DataSet in this DataSet,
     * whose name is considered to go after ub, or DataSet::end() if 
     * no DataSet names are considered to go after it.
     */
    iterator upper_bound(const std::string& ub);

    /**
     * @brief Returns a const_iterator pointing to the first DataSet in this
     * DataSet whose key is considered to go after ub.
     *
     * @param ub DataSet name to search for.
     *
     * @return A const_iterator to the the first DataSet in this DataSet 
     * whose name is considered to go after ub, or DataSet::end() if 
     * no DataSet names are considered to go after it.
     */
    const_iterator upper_bound(const std::string& ub) const;

369
    /**
370
     * @brief Returns the RunSet associated with this DataSet.
371
     *
372
     * @return the RunSet associated with this DataSet.
373
     */
374
    RunSet runs() const;
375 376

    /**
377
     * @brief Accesses an existing run using the []
378 379 380 381 382 383 384 385
     * operator. If no run corresponds to the provided run number,
     * the function returns a Run instance d such that
     * r.valid() is false.
     *
     * @param runNumber Number of the run to retrieve.
     *
     * @return a Run corresponding to the provided run number.
     */
386
    Run operator[](const RunNumber& runNumber) const;
387 388 389 390 391 392 393 394 395 396 397

    /**
     * @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;
398 399
};

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
class DataSet::const_iterator {

    protected:

    /**
     * @brief Implementation of the class (using Pimpl idiom)
     */
    class Impl;
    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */

    public:
    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to an invalid DataSet.
     */
    const_iterator();

    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to a given DataSet. The DataSet may or may not be valid. 
     *
     * @param current DataSet to make the const_iterator point to.
     */
    const_iterator(const DataSet& current);

    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to a given DataSet. The DataSet may or may not be valid.
     *
     * @param current DataSet to make the const_iterator point to.
     */
    const_iterator(DataSet&& current);

    typedef const_iterator self_type;
    typedef DataSet value_type;
    typedef DataSet& reference;
    typedef DataSet* pointer;
    typedef int difference_type;
    typedef std::forward_iterator_tag iterator_category;

    /**
     * @brief Destructor. This destructor is virtual because
     * the iterator class inherits from const_iterator.
     */
    virtual ~const_iterator();

    /**
     * @brief Copy-constructor.
     *
     * @param other const_iterator to copy.
     */
    const_iterator(const const_iterator& other);

    /**
     * @brief Move-constructor.
     *
     * @param other const_iterator to move.
     */
    const_iterator(const_iterator&& other);

    /**
     * @brief Copy-assignment operator.
     *
     * @param other const_iterator to copy.
     *
     * @return this.
     */
    const_iterator& operator=(const const_iterator&);

    /**
     * @brief Move-assignment operator.
     *
     * @param other const_iterator to move.
     *
     * @return this.
     */
    const_iterator& operator=(const_iterator&&);

    /**
     * @brief Increments the const_iterator, returning
     * a copy of the iterator after incrementation.
     *
     * @return a copy of the iterator after incrementation.
     */
    self_type operator++();

    /**
     * @brief Increments the const_iterator, returning
     * a copy of the iterator before incrementation.
     *
     * @return a copy of the iterator after incrementation.
     */
    self_type operator++(int);

    /**
     * @brief Dereference operator. Returns a const reference
     * to the DataSet this const_iterator points to.
     *
     * @return a const reference to the DataSet this
     *      const_iterator points to.
     */
    const reference operator*();

    /**
     * @brief Returns a const pointer to the DataSet this
     * const_iterator points to.
     *
     * @return a const pointer to the DataSet this
     *      const_iterator points to.
     */
    const pointer operator->();

    /**
     * @brief Compares two const_iterators. The two const_iterators
     * are equal if they point to the same DataSet or if both
     * correspond to DataStore::cend().
     *
     * @param rhs const_iterator to compare with.
     *
     * @return true if the two const_iterators are equal, false otherwise.
     */
    bool operator==(const self_type& rhs) const;

    /**
     * @brief Compares two const_iterators.
     *
     * @param rhs const_iterator to compare with.
     *
     * @return true if the two const_iterators are different, false otherwise.
     */
    bool operator!=(const self_type& rhs) const;
};

class DataSet::iterator : public DataSet::const_iterator {

    public:

    /**
     * @brief Constructor. Builds an iterator pointing to an
     * invalid DataSet.
     */
    iterator();

    /**
     * @brief Constructor. Builds an iterator pointing to
     * an existing DataSet. The DataSet may or may not be
     * valid.
     *
     * @param current DataSet to point to.
     */
    iterator(const DataSet& current);

    /**
     * @brief Constructor. Builds an iterator pointing to
     * an existing DataSet. The DataSet may or may not be
     * valid.
     *
     * @param current DataSet to point to.
     */
    iterator(DataSet&& current);

    typedef iterator self_type;
    typedef DataSet value_type;
    typedef DataSet& reference;
    typedef DataSet* pointer;
    typedef int difference_type;
    typedef std::forward_iterator_tag iterator_category;

    /**
     * @brief Destructor.
     */
    ~iterator();

    /**
     * @brief Copy constructor.
     *
     * @param other iterator to copy.
     */
    iterator(const iterator& other);

    /**
     * @brief Move constructor.
     *
     * @param other iterator to move.
     */
    iterator(iterator&& other);

    /**
     * @brief Copy-assignment operator.
     *
     * @param other iterator to copy.
     *
     * @return this.
     */
    iterator& operator=(const iterator& other);

    /**
     * @brief Move-assignment operator.
     *
     * @param other iterator to move.
     *
     * @return this.
     */
    iterator& operator=(iterator&& other);

    /**
     * @brief Dereference operator. Returns a reference
     * to the DataSet this iterator points to.
     *
     * @return A reference to the DataSet this iterator
     *      points to.
     */
    reference operator*();

    /**
     * @brief Returns a pointer to the DataSet this iterator
     * points to.
     *
     * @return A pointer to the DataSet this iterator points to.
     */
    pointer operator->();
};

623 624 625
}

#endif