libdballe 9.6
db/v7/cursor.h
1#ifndef DBA_DB_V7_CURSOR_H
2#define DBA_DB_V7_CURSOR_H
3
4#include <dballe/types.h>
5#include <dballe/db/db.h>
6#include <dballe/db/v7/transaction.h>
8#include <dballe/db/v7/levtr.h>
9#include <dballe/values.h>
10#include <memory>
11#include <deque>
12
13namespace dballe {
14namespace db {
15namespace v7 {
16namespace cursor {
17
18struct Stations;
19struct StationData;
20struct Data;
21struct Summary;
22
27{
28 dballe::DBStation station;
29 mutable std::unique_ptr<DBValues> values;
30
31 StationRow(const dballe::DBStation& station) : station(station) {}
32
33 void dump(FILE* out) const;
34};
35
37{
38 dballe::DBStation station;
39 DBValue value;
40
41 StationDataRow(const dballe::DBStation& station, int id_data, std::unique_ptr<wreport::Var> var) : station(station), value(id_data, std::move(var)) {}
42 StationDataRow(const StationDataRow&) = delete;
43 StationDataRow(StationDataRow&& o) = default;
44 StationDataRow& operator=(const StationDataRow&) = delete;
45 StationDataRow& operator=(StationDataRow&& o) = default;
47
48 void dump(FILE* out) const;
49};
50
51struct DataRow : public StationDataRow
52{
53 int id_levtr;
54 Datetime datetime;
55
56 using StationDataRow::StationDataRow;
57
58 DataRow(const dballe::DBStation& station, int id_levtr, const Datetime& datetime, int id_data, std::unique_ptr<wreport::Var> var)
59 : StationDataRow(station, id_data, std::move(var)), id_levtr(id_levtr), datetime(datetime) {}
60
61 void dump(FILE* out) const;
62};
63
65{
66 dballe::DBStation station;
67 int id_levtr;
69 DatetimeRange dtrange;
70 size_t count = 0;
71
72 SummaryRow(const dballe::DBStation& station, int id_levtr, wreport::Varcode code, const DatetimeRange& dtrange, size_t count)
73 : station(station), id_levtr(id_levtr), code(code), dtrange(dtrange), count(count) {}
74
75 void dump(FILE* out) const;
76};
77
78
79template<typename Cursor>
81{
82};
83
84template<>
86{
89 typedef StationRow Row;
90};
91
92template<>
94{
97 typedef StationDataRow Row;
98};
99
100template<>
102{
104 typedef db::CursorData Parent;
105 typedef DataRow Row;
106};
107
108template<>
110{
113 typedef SummaryRow Row;
114};
115
116
121template<typename Impl>
122struct Base : public ImplTraits<Impl>::Parent
123{
124 typedef typename ImplTraits<Impl>::Row Row;
125 typedef typename ImplTraits<Impl>::Interface Interface;
126
128 std::shared_ptr<v7::Transaction> tr;
129
131 std::deque<Row> results;
132
134 bool at_start = true;
135
136 Base(std::shared_ptr<v7::Transaction> tr)
137 : tr(tr)
138 {
139 }
140
141 virtual ~Base() {}
142
143 int remaining() const override;
144 bool has_value() const { return !at_start && !results.empty(); }
145 bool next() override
146 {
147 if (at_start)
148 at_start = false;
149 else if (!results.empty())
150 results.pop_front();
151 return !results.empty();
152 }
153
154 void discard() override
155 {
156 at_start = false;
157 results.clear();
158 tr.reset();
159 }
160
161 dballe::DBStation get_station() const override { return row().station; }
162
168 unsigned test_iterate(FILE* dump=0) override;
169
170 inline static std::shared_ptr<Impl> downcast(std::shared_ptr<Interface> c)
171 {
172 auto res = std::dynamic_pointer_cast<Impl>(c);
173 if (!res)
174 throw std::runtime_error("Attempted to downcast the wrong kind of cursor");
175 return res;
176 }
177
178 const Row& row() const { return results.front(); }
179
180protected:
181 int get_priority() const { return tr->repinfo().get_priority(results.front().station.report); }
182};
183
184extern template class Base<Stations>;
185extern template class Base<StationData>;
186extern template class Base<Data>;
187extern template class Base<Summary>;
188
189
192{
193 using Base::Base;
194 DBValues get_values() const override;
195
196 void remove() override;
197 void enq(impl::Enq& enq) const override;
198
199protected:
200 const DBValues& values() const;
201 void load(Tracer<>& trc, const StationQueryBuilder& qb);
202
203 friend std::shared_ptr<dballe::CursorStation> run_station_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
204};
205
208{
209 bool with_attributes;
210
211 StationData(DataQueryBuilder& qb, bool with_attributes);
212 std::shared_ptr<dballe::db::Transaction> get_transaction() const override { return tr; }
213 wreport::Varcode get_varcode() const override { return row().value.code(); }
214 wreport::Var get_var() const override { return *row().value; }
215 int attr_reference_id() const override { return row().value.data_id; }
216 void query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest, bool force_read) override;
217 void remove() override;
218 void enq(impl::Enq& enq) const override;
219
220protected:
221 void load(Tracer<>& trc, const DataQueryBuilder& qb);
222
223 friend std::shared_ptr<dballe::CursorStationData> run_station_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
224};
225
226template<typename Impl>
227struct LevTrBase : public Base<Impl>
228{
229protected:
230 // Cached levtr for the current row
231 mutable const LevTrEntry* levtr = nullptr;
232
233 const LevTrEntry& get_levtr() const
234 {
235 if (levtr == nullptr)
236 // We prefetch levtr info for all IDs, so we do not need to hit the database here
237 levtr = &(this->tr->levtr().lookup_cache(this->results.front().id_levtr));
238 return *levtr;
239 }
240
241public:
242 using Base<Impl>::Base;
243
244 bool next() override
245 {
246 levtr = nullptr;
247 return Base<Impl>::next();
248 }
249
250 void discard() override
251 {
252 levtr = nullptr;
254 }
255};
256
258struct Data : public LevTrBase<Data>
259{
260protected:
261 int insert_cur_prio;
262
264 bool add_to_best_results(const dballe::DBStation& station, int id_levtr, const Datetime& datetime, int id_data, std::unique_ptr<wreport::Var> var);
266 bool add_to_last_results(const dballe::DBStation& station, int id_levtr, const Datetime& datetime, int id_data, std::unique_ptr<wreport::Var> var);
267
268 void load(Tracer<>& trc, const DataQueryBuilder& qb);
269 void load_best(Tracer<>& trc, const DataQueryBuilder& qb);
270 void load_last(Tracer<>& trc, const DataQueryBuilder& qb);
271
272public:
273 bool with_attributes;
274
275 Data(DataQueryBuilder& qb, bool with_attributes);
276
277 std::shared_ptr<dballe::db::Transaction> get_transaction() const override { return tr; }
278
279 Datetime get_datetime() const override { return row().datetime; }
280 wreport::Varcode get_varcode() const override { return row().value.code(); }
281 wreport::Var get_var() const override { return *row().value; }
282 int attr_reference_id() const override { return row().value.data_id; }
283 Level get_level() const override { return get_levtr().level; }
284 Trange get_trange() const override { return get_levtr().trange; }
285
286 void query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest, bool force_read) override;
287 void remove() override;
288 void enq(impl::Enq& enq) const override;
289
290protected:
291 friend std::shared_ptr<dballe::CursorData> run_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
292};
293
295struct Summary : public LevTrBase<Summary>
296{
297 using LevTrBase<Summary>::LevTrBase;
298
299 DatetimeRange get_datetimerange() const override { return row().dtrange; }
300 Level get_level() const override { return get_levtr().level; }
301 Trange get_trange() const override { return get_levtr().trange; }
302 wreport::Varcode get_varcode() const override { return row().code; }
303 size_t get_count() const override { return row().count; }
304 void remove() override;
305 void enq(impl::Enq& enq) const override;
306
307protected:
308 void load(Tracer<>& trc, const SummaryQueryBuilder& qb);
309
310 friend std::shared_ptr<dballe::CursorSummary> run_summary_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
311};
312
313
314std::shared_ptr<dballe::CursorStation> run_station_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
315std::shared_ptr<dballe::CursorStationData> run_station_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
316std::shared_ptr<dballe::CursorData> run_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
317std::shared_ptr<dballe::CursorSummary> run_summary_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
318void run_delete_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool station_vars, bool explain);
319
320}
321}
322}
323}
324#endif
Cursor iterating over data values.
Definition: cursor.h:78
Cursor iterating over station data values.
Definition: cursor.h:67
Cursor iterating over stations.
Definition: cursor.h:57
Cursor iterating over summary entries.
Definition: cursor.h:98
Smart pointer for trace::Step objects, which calls done() when going out of scope.
Definition: db/v7/fwd.h:46
Functions used to connect to DB-All.e and insert, query and delete data.
uint16_t Varcode
Repinfo table management used by the db module.
Definition: types.h:851
Container for a wreport::Var pointer, and its database ID.
Definition: value.h:72
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:192
Range of datetimes.
Definition: types.h:295
Date and time.
Definition: types.h:165
Vertical level or layer.
Definition: types.h:625
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:687
Standard dballe::Query implementation.
Definition: core/query.h:35
Definition: db/db.h:105
Definition: db/db.h:72
Definition: db/db.h:59
Definition: db/db.h:138
Definition: qbuilder.h:92
Definition: cache.h:15
Trange trange
Time range.
Definition: cache.h:23
Level level
Vertical level or layer.
Definition: cache.h:20
Definition: qbuilder.h:82
Definition: qbuilder.h:125
Structure used to build and execute a query, and to iterate through the results.
Definition: db/v7/cursor.h:123
std::deque< Row > results
Storage for the raw database results.
Definition: db/v7/cursor.h:131
unsigned test_iterate(FILE *dump=0) override
Iterate the cursor until the end, returning the number of items.
std::shared_ptr< v7::Transaction > tr
Database to operate on.
Definition: db/v7/cursor.h:128
bool at_start
True if we are at the start of the iteration.
Definition: db/v7/cursor.h:134
Definition: db/v7/cursor.h:52
CursorData implementation.
Definition: db/v7/cursor.h:259
bool add_to_last_results(const dballe::DBStation &station, int id_levtr, const Datetime &datetime, int id_data, std::unique_ptr< wreport::Var > var)
Append or replace the last result according to datetime. Returns false if the value has been ignored.
bool add_to_best_results(const dballe::DBStation &station, int id_levtr, const Datetime &datetime, int id_data, std::unique_ptr< wreport::Var > var)
Append or replace the last result according to priority. Returns false if the value has been ignored.
Definition: db/v7/cursor.h:81
Definition: db/v7/cursor.h:228
Definition: db/v7/cursor.h:37
CursorStationData implementation.
Definition: db/v7/cursor.h:208
Row resulting from a station query.
Definition: db/v7/cursor.h:27
CursorStation implementation.
Definition: db/v7/cursor.h:192
Definition: db/v7/cursor.h:65
CursorSummary implementation.
Definition: db/v7/cursor.h:296
Class passed to key-value accessors to set values in an invoker-defined way.
Definition: core/enq.h:18
Common base types used by most of DB-All.e code.
Structures used as input to database insert functions.