DataSet.hpp 13.6 KB
Newer Older
1 2 3
#ifndef __HEPNOS_DATA_SET_H
#define __HEPNOS_DATA_SET_H

4
#include <memory>
5 6 7 8 9 10
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <hepnos/Exception.hpp>
11
#include <hepnos/RunNumber.hpp>
12 13 14 15
#include <hepnos/DataStore.hpp>

namespace hepnos {

16 17 18
class RunSet;
class Run;

19 20 21 22 23 24
/**
 * @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.
 */
25 26 27
class DataSet {

    friend class DataStore;
28
    friend class RunSet;
29 30 31

    private:

32 33 34 35 36 37 38
    /**
     * @brief Constructor.
     *
     * @param ds DataStore to which this DataSet belongs.
     * @param level Level of nesting.
     * @param fullname Full name of the DataSet.
     */
39
    DataSet(DataStore* ds, uint8_t level, const std::string& fullname);
40 41 42 43 44 45 46 47 48

    /**
     * @brief Constructor.
     *
     * @param ds DataStore to which this DataSet belongs.
     * @param level Level of nesting.
     * @param container Full name of the parent DataSet ("" if no parent).
     * @param name Name of the DataSet.
     */
49
    DataSet(DataStore* ds, uint8_t level, const std::string& container, const std::string& name);
50 51 52 53 54 55 56 57


    /**
     * @brief Implementation class (used for the Pimpl idiom).
     */
    class Impl;

    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation. */
58 59 60

    public:

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 93 94 95 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 139 140 141 142 143 144
    /**
     * @brief Default constructor.
     */
    DataSet();

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

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

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

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

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

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

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    /**
     * @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.
     *
     * @return trye if the key did not already exist and the write succeeded,
     *      false otherwise.
     */
    bool storeRawData(const std::string& key, const std::vector<char>& buffer);

    /**
     * @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.
     */
    bool loadRawData(const std::string& key, std::vector<char>& buffer) const;

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    /**
     * @brief Stores a key/value pair into the DataSet.
     * The type of the key should have operator<< available
     * to stream it into a std::stringstream for the purpose
     * of converting it into an std::string. The resulting
     * string must not have the "/" or "#" characters. The
     * type of the value must be serializable using Boost.
     *
     * @tparam K type of the key.
     * @tparam V type of the value.
     * @param key Key to store.
     * @param value Value to store.
     *
     * @return true if the key was found. false otherwise.
     */
    template<typename K, typename V>
    bool store(const K& key, const V& value) {
        std::stringstream ss_value;
        boost::archive::binary_oarchive oa(ss_value);
        try {
            oa << value;
        } catch(...) {
            throw Exception("Exception occured during serialization");
194
        }
195 196
        std::string serialized = ss_value.str();
        std::vector<char> buffer(serialized.begin(), serialized.end());
197
        return storeRawData(key, buffer);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }

    /**
     * @brief Loads a value associated with a key from the DataSet.
     * The type of the key should have operator<< available
     * to stream it into a std::stringstream for the purpose
     * of converting it into an std::string. The resulting
     * string must not have the "/" or "#" characters. The
     * type of the value must be serializable using Boost.
     *
     * @tparam K type of the key.
     * @tparam V type of the value.
     * @param key Key to load.
     * @param value Value to load.
     *
     * @return bool if the 
     */
    template<typename K, typename V>
    bool load(const K& key, V& value) const {
        std::vector<char> buffer;
218
        if(!loadRawData(key, buffer)) {
219
            return false;
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
        try {
            std::string serialized(buffer.begin(), buffer.end());
            std::stringstream ss(serialized);
            boost::archive::binary_iarchive ia(ss);
            ia >> value;
        } catch(...) {
            throw Exception("Exception occured during serialization");
        }
        return true;
    }

    /**
     * @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;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

    /**
     * @brief Creates a dataset with a given name inside the
     * DataSet. This name must not have the '/' and '#' characters.
     * 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);

266 267 268 269 270 271 272 273 274 275 276 277 278
    /**
     * @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);

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
    typedef DataStore::const_iterator const_iterator;
    typedef DataStore::iterator iterator;

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

    /**
     * @brief Searches this DataSet for an DataSet with 
     * the provided name and returns an iterator to it if found,
     * otherwise it returns an iterator to DataStore::end().
     *
     * @param datasetName Name of the DataSet to find.
     *
     * @return an iterator pointing to the DataSet if found,
     * DataSet::end() otherwise.
     */
    iterator find(const std::string& datasetName);

    /**
     * @brief Searches this DataSet for an DataSet with 
     * the provided name and returns a const_iterator to it 
     * if found, otherwise it returns an iterator to DataSet::end().
     *
     * @param datasetName Name of the DataSet to find.
     *
     * @return a const_iterator pointing to the DataSet if found,
     * DataSet::cend() otherwise.
     */
    const_iterator find(const std::string& datasetName) const;


    /**
     * @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,
330
     * `end()->valid()` returns `false`).
331 332 333 334 335
     *
     * @return an iterator referring to the end of the DataSet.
     */
    iterator end();

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    /**
     * @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;

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    /**
     * @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;

420 421 422 423 424 425
    /**
     * @brief Returns a reference to the RunSet associated with this DataSet.
     *
     * @return a reference to the RunSet associated with this DataSet.
     */
    RunSet& runs();
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    /**
     * @brief Returns a reference to the RunSet associated with this DataSet.
     *
     * @return a reference to the RunSet associated with this DataSet.
     */
    const RunSet& runs() const;

    /**
     * @brief Accesses an existing run using the ()
     * 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.
     */
    Run operator()(const RunNumber& runNumber) const;
445 446 447 448 449
};

}

#endif