Run.hpp 10.8 KB
Newer Older
1 2 3 4 5
#ifndef __HEPNOS_RUN_H
#define __HEPNOS_RUN_H

#include <memory>
#include <string>
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>
11 12
#include <hepnos/DataStore.hpp>
#include <hepnos/RunNumber.hpp>
13 14
#include <hepnos/SubRun.hpp>
#include <hepnos/Exception.hpp>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

namespace hepnos {

class RunSet;

class Run {

    private:

    friend class RunSet;
    friend class DataSet;

    class Impl;
    std::unique_ptr<Impl> m_impl;

30 31 32 33 34 35 36 37
    /**
     * @brief Constructor.
     *
     * @param datastore Pointer to the DataStore managing the underlying data.
     * @param level Level of nesting.
     * @param container Full name of the dataset containing the run.
     * @param run Run number.
     */
38 39 40 41
    Run(DataStore* datastore, uint8_t level, const std::string& container, const RunNumber& run);

    public:

42 43 44
    /**
     * @brief Default constructor. Creates a Run instance such that run.valid() is false.
     */
45 46
    Run();

47 48 49 50 51 52
    /**
     * @brief Copy constructor.
     *
     * @param other Run to copy.
     */
    Run(const Run& other);
53

54 55 56 57 58 59
    /**
     * @brief Move constructor.
     *
     * @param other Run to move.
     */
    Run(Run&& other);
60

61 62 63 64 65 66 67 68
    /**
     * @brief Copy-assignment operator.
     *
     * @param other Run to assign.
     *
     * @return Reference to this Run.
     */
    Run& operator=(const Run& other);
69

70 71 72 73 74 75 76 77
    /**
     * @brief Move-assignment operator.
     *
     * @param other Run to move from.
     *
     * @return Reference to this Run.
     */
    Run& operator=(Run&& other);
78

79 80 81
    /**
     * @brief Destructor.
     */
82 83
    ~Run();

84 85 86 87 88 89 90
    /**
     * @brief Returns the next Run in the same container,
     * sorted by run number. If no such run exists, a Run instance
     * such that Run::valid() returns false is returned.
     *
     * @return The next Run in the container.
     */
91 92
    Run next() const;

93 94 95 96 97 98 99
    /**
     * @brief Indicates whether this Run instance points to a valid
     * run in the underlying storage service.
     *
     * @return True if the Run instance points to a valid run in the
     * underlying service, false otherwise.
     */
100 101
    bool valid() const;

102 103 104 105 106 107 108 109
    /**
     * @brief Stores raw key/value data in this Run.
     *
     * @param key Key
     * @param buffer Value
     *
     * @return true if the key did not already exist, false otherwise.
     */
110 111
    bool storeRawData(const std::string& key, const std::vector<char>& buffer);

112 113 114 115 116 117 118 119
    /**
     * @brief Loads raw key/value data from this Run.
     *
     * @param key Key
     * @param buffer Buffer used to hold the value.
     *
     * @return true if the key exists, false otherwise.
     */
120 121
    bool loadRawData(const std::string& key, std::vector<char>& buffer) const;

122 123 124 125 126 127 128 129
    /**
     * @brief Compares this Run with another Run. The Runs must point to
     * the same run number within the same container.
     *
     * @param other Run instance to compare against.
     *
     * @return true if the Runs are the same, false otherwise.
     */
130 131
    bool operator==(const Run& other) const;

132 133 134 135 136 137 138
    /**
     * @brief Compares this Run with another Run.
     *
     * @param other Run instance to compare against.
     *
     * @return true if the Runs are different, false otherwise.
     */
139 140
    bool operator!=(const Run& other) const;

141 142 143 144 145 146
    /**
     * @brief Returns the run number of this Run. Note that if
     * the Run is not valid, this function will return 0.
     *
     * @return The run number.
     */
147 148
    const RunNumber& number() const;

149 150 151 152 153 154
    /**
     * @brief Returns the full name of the DataSet containing
     * this Run.
     *
     * @return the full name of the DataSet containing this Run.
     */
155
    const std::string& container() const;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 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 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

    /**
     * @brief Stores a key/value pair into the Run.
     * 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");
        }
        std::string serialized = ss_value.str();
        std::vector<char> buffer(serialized.begin(), serialized.end());
        return storeRawData(key, buffer);
    }

    /**
     * @brief Loads a value associated with a key from the Run.
     * 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 true if the key exists and was loaded. False otherwise.
     */
    template<typename K, typename V>
    bool load(const K& key, V& value) const {
        std::vector<char> buffer;
        if(!loadRawData(key, buffer)) {
            return false;
        }
        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;
    }

    class const_iterator;
    class iterator;

    /**
     * @brief Searches this Run for an SubRun with 
     * the provided number and returns an iterator to it if found,
     * otherwise it returns an iterator to Run::end().
     *
     * @param srn SubRun number of the SubRun to find.
     *
     * @return an iterator pointing to the SubRun if found,
     * Run::end() otherwise.
     */
    iterator find(const SubRunNumber& srn);

    /**
     * @brief Searches this Run for a SubRun with 
     * the provided number and returns a const_iterator to it 
     * if found, otherwise it returns an iterator to Run::end().
     *
     * @param srn SubRunNumber of the SubRun to find.
     *
     * @return a const_iterator pointing to the SubRun if found,
     * Run::cend() otherwise.
     */
    const_iterator find(const SubRunNumber&) const;

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

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

    /**
     * @brief Returns a const_iterator referring to the first SubRun
     * in this Run.
     *
     * @return a const_iterator referring to the first SubRun in this Run.
     */
    const_iterator begin() const;

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

    /**
     * @brief Returns a const_iterator referring to the first SubRun
     * in this Run.
     *
     * @return a const_iterator referring to the first SubRun in this Run.
     */
    const_iterator cbegin() const;

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

    /**
     * @brief Returns an iterator pointing to the first SubRun in this
     * Run, whose SubRunNumber is not lower than lb.
     *
     * @param lb SubRunNumber lower bound to search for.
     *
     * @return An iterator to the first SubRun in this Run
     * whose whose SubRunNumber is not lower than lb, or Run::end() 
     * if all subrun numbers are lower.
     */
    iterator lower_bound(const SubRunNumber&);

    /**
     * @brief Returns a const_iterator pointing to the first SubRun in this
     * Run, whose SubRunNumber is not lower than lb.
     *
     * @param lb SubRunNumber lower bound to search for.
     *
     * @return A const_iterator to the first SubRun in this Run
     * whose whose SubRunNumber is not lower than lb, or Run::cend() 
     * if all subrun numbers are lower.
     */
    const_iterator lower_bound(const SubRunNumber&) const;

    /**
     * @brief Returns an iterator pointing to the first SubRun in the 
     * Run whose SubRunNumber is greater than ub.
     *
     * @param ub SubRunNumber upper bound to search for.
     *
     * @return An iterator to the first SubRun in this Run,
     * whose SubRunNumber is greater than ub, or Run::end() if 
     * no such SubRun exists.
     */
    iterator upper_bound(const SubRunNumber&);

    /**
     * @brief Returns a const_iterator pointing to the first SubRun in the 
     * Run whose SubRunNumber is greater than ub.
     *
     * @param ub SubRunNumber upper bound to search for.
     *
     * @return An const_iterator to the first SubRun in this Run,
     * whose SubRunNumber is greater than ub, or Run::cend() if 
     * no such SubRun exists.
     */
    const_iterator upper_bound(const SubRunNumber&) const;

    /**
     * @brief Accesses an existing ubun using the ()
     * operator. If no run corresponds to the provided subrun number,
     * the function returns a SubRun instance r such that
     * r.valid() is false.
     *
     * @param subRunNumber Number of the subrun to retrieve.
     *
     * @return a SubRun corresponding to the provided subrun number.
     */
    SubRun operator()(const SubRunNumber& subRunNumber) const;

    /**
     * @brief Creates a SubRun within this Run, with the provided
     * SubRunNumber. If a SubRun with the same SubRunNumber already
     * exists, this method does not create a new SubRun but returns
     * a handle to the existing one instead.
     *
     * @param subRunNumber SubRunNumber of the SubRun to create.
     *
     * @return a handle to the created or existing SubRun.
     */
    SubRun createSubRun(const SubRunNumber& subRunNumber);
367 368 369 370 371
};

}

#endif