DataSet.hpp 11.2 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 32 33

    private:

34
    /**
35
     * @brief Implementation class (used for the Pimpl idiom).
36
     */
37
    class Impl;
38

39
    std::shared_ptr<Impl> m_impl; /*!< Pointer to implementation. */
40 41

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

    public:

49 50
    typedef DataSet value_type;

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

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

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

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

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

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

93
    /**
94
     * @brief Overrides datastore from KeyValueContainer class.
95
     */
96
    DataStore datastore() const override;
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
    /**
     * @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;

140 141 142 143 144 145 146 147
    /**
     * @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.
     *
148 149
     * @return a valid ProductID if the key did not already exist and the write succeeded,
     *      an invalid one otherwise.
150
     */
151 152
    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;
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);
Matthieu Dorier's avatar
Matthieu Dorier committed
214
    Run createRun(WriteBatch& batch, const RunNumber& runNumber);
215

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    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;

    /**
232 233
     * @brief Searches this DataSet for a DataSet with 
     * the provided path and returns an iterator to it if found,
234 235
     * otherwise it returns an iterator to DataStore::end().
     *
236
     * @param datasetPath Path of the DataSet to find.
237 238 239 240
     *
     * @return an iterator pointing to the DataSet if found,
     * DataSet::end() otherwise.
     */
241
    iterator find(const std::string& datasetPath);
242 243 244

    /**
     * @brief Searches this DataSet for an DataSet with 
245
     * the provided path and returns a const_iterator to it 
246 247
     * if found, otherwise it returns an iterator to DataSet::end().
     *
248
     * @param datasetPath Path of the DataSet to find.
249 250 251 252
     *
     * @return a const_iterator pointing to the DataSet if found,
     * DataSet::cend() otherwise.
     */
253
    const_iterator find(const std::string& datasetPath) const;
254 255 256 257 258 259 260 261 262 263 264 265 266


    /**
     * @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,
267
     * `end()->valid()` returns `false`).
268 269 270 271 272
     *
     * @return an iterator referring to the end of the DataSet.
     */
    iterator end();

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    /**
     * @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;

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

357
    /**
358
     * @brief Returns the RunSet associated with this DataSet.
359 360 361
     *
     * @return a reference to the RunSet associated with this DataSet.
     */
362
    RunSet runs() const;
363 364

    /**
365
     * @brief Accesses an existing run using the []
366 367 368 369 370 371 372 373
     * 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.
     */
374
    Run operator[](const RunNumber& runNumber) const;
375 376 377 378 379
};

}

#endif