WriteBatch.hpp 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * (C) 2019 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#ifndef __HEPNOS_WRITE_BATCH_H
#define __HEPNOS_WRITE_BATCH_H

#include <memory>
#include <string>
#include <vector>

#include <hepnos/KeyValueContainer.hpp>
#include <hepnos/ProductID.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/Exception.hpp>
17
#include <hepnos/Statistics.hpp>
18
19
20
21
22
23
24
25

namespace hepnos {

class DataStore;
class DataSet;
class Run;
class SubRun;
class Event;
Matthieu Dorier's avatar
Matthieu Dorier committed
26
class WriteBatchImpl;
Matthieu Dorier's avatar
Matthieu Dorier committed
27
class AsyncEngine;
28

29
30
31
32
33
34
struct WriteBatchStatistics {
    Statistics<size_t> batch_sizes;
    Statistics<size_t> key_sizes;
    Statistics<size_t> value_sizes; // only non-empty values are accounted for
};

Matthieu Dorier's avatar
Matthieu Dorier committed
35
36
37
38
39
/**
 * @brief The WriteBatch oject can be used to batch
 * operations such as creating Runs, SubRuns, and Events,
 * as well as storing products into the underlying DataStore.
 */
40
41
42
43
44
45
46
47
48
49
50
class WriteBatch {

    friend class DataStore;
    friend class DataSet;
    friend class Run;
    friend class SubRun;
    friend class Event;
    friend class KeyValueContainer;

    private:

Matthieu Dorier's avatar
Matthieu Dorier committed
51
    std::unique_ptr<WriteBatchImpl> m_impl;
52
53
54

    public:

55
56
57
58
59
    /**
     * @brief Default constructor.
     */
    WriteBatch() = default;

Matthieu Dorier's avatar
Matthieu Dorier committed
60
61
62
63
64
65
66
67
68
69
    /**
     * @brief Constructor.
     *
     * Note: the max_batch_size is not the maximum capacity of the WriteBatch.
     * The WriteBatch has virtually unlimited capacity and will only flush its
     * content when instructed to (i.e. calling flush()) or when it is destroyed.
     *
     * @param ds DataStore in which to write.
     * @param max_batch_size Maximum batch size when issuing an operation.
     */
70
    WriteBatch(DataStore& ds, unsigned max_batch_size=128);
Matthieu Dorier's avatar
Matthieu Dorier committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    /**
     * @brief Constructor using and AsyncEngine to write the batches
     * asynchronously.
     *
     * Note: when using an AsyncEngine, the AsyncEngine will continuously
     * try to write the content of the WriteBatch into storage, issuing
     * variable-sized batches of up to max_batch_size.
     *
     * @param async AsyncEngine to use to write asynchronously.
     * @param max_batch_size Maximum batch size when issuing an operation.
     */
    WriteBatch(AsyncEngine& async, unsigned max_batch_size=128);
    
    /**
     * @brief Destructor.
     */
88
    ~WriteBatch();
Matthieu Dorier's avatar
Matthieu Dorier committed
89
90
91
92
    
    /**
     * @brief Deleted copy constructor.
     */
93
    WriteBatch(const WriteBatch&) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
94
95
96
97
    
    /**
     * @brief Deleted move constructor.
     */
98
    WriteBatch& operator=(const WriteBatch&) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
99
100
101
102
    
    /**
     * @brief Deleted copy-assignment operator.
     */
103
    WriteBatch(WriteBatch&&) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
104
105
106
107
    
    /**
     * @brief Deleted move-assignment operator.
     */
108
    WriteBatch& operator=(WriteBatch&&) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
109
110
111
112
113
114
    
    /**
     * @brief Flush the content of the WriteBatch, blocking until
     * everything is flushed.
     */
    void flush();
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    /**
     * @brief Activate statistics collection.
     *
     * @param activate Whether to activate statistics.
     */
    void activateStatistics(bool activate=true);

    /**
     * @brief Collects the usage statistics.
     *
     * @param stats WriteBatchStatistics object to fill.
     */
    void collectStatistics(WriteBatchStatistics& stats) const;
129
130
131
132
133
};

}

#endif