libdballe  9.11
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>
7 #include <dballe/db/v7/repinfo.h>
8 #include <dballe/db/v7/levtr.h>
9 #include <dballe/values.h>
10 #include <memory>
11 #include <deque>
12 
13 namespace dballe {
14 namespace db {
15 namespace v7 {
16 namespace cursor {
17 
18 struct Stations;
19 struct StationData;
20 struct Data;
21 struct Summary;
22 
26 struct StationRow
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;
46  ~StationDataRow() {}
47 
48  void dump(FILE* out) const;
49 };
50 
51 struct 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 
64 struct SummaryRow
65 {
66  dballe::DBStation station;
67  int id_levtr;
68  wreport::Varcode code;
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 
79 template<typename Cursor>
80 struct ImplTraits
81 {
82 };
83 
84 template<>
86 {
88  typedef db::CursorStation Parent;
89  typedef StationRow Row;
90 };
91 
92 template<>
94 {
97  typedef StationDataRow Row;
98 };
99 
100 template<>
102 {
104  typedef db::CursorData Parent;
105  typedef DataRow Row;
106 };
107 
108 template<>
110 {
112  typedef db::CursorSummary Parent;
113  typedef SummaryRow Row;
114 };
115 
116 
121 template<typename Impl>
122 struct 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 override { 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 
180 protected:
181  int get_priority() const { return tr->repinfo().get_priority(results.front().station.report); }
182 };
183 
184 extern template class Base<Stations>;
185 extern template class Base<StationData>;
186 extern template class Base<Data>;
187 extern template class Base<Summary>;
188 
189 
191 struct Stations : public Base<Stations>
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 
199 protected:
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 
220 protected:
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 
226 template<typename Impl>
227 struct LevTrBase : public Base<Impl>
228 {
229 protected:
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 
241 public:
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 
258 struct Data : public LevTrBase<Data>
259 {
260 protected:
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 
272 public:
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 
290 protected:
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 
296 {
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 
307 protected:
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 
314 std::shared_ptr<dballe::CursorStation> run_station_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
315 std::shared_ptr<dballe::CursorStationData> run_station_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
316 std::shared_ptr<dballe::CursorData> run_data_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
317 std::shared_ptr<dballe::CursorSummary> run_summary_query(Tracer<>& trc, std::shared_ptr<v7::Transaction> tr, const core::Query& query, bool explain);
318 void 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
Row resulting from a station query.
Definition: db/v7/cursor.h:26
Definition: qbuilder.h:81
CursorStation implementation.
Definition: db/v7/cursor.h:191
CursorStationData implementation.
Definition: db/v7/cursor.h:207
Definition: qbuilder.h:124
Common base types used by most of DB-All.e code.
Definition: db/db.h:137
Class passed to key-value accessors to set values in an invoker-defined way.
Definition: core/enq.h:17
CursorSummary implementation.
Definition: db/v7/cursor.h:295
Definition: db/db.h:71
unsigned test_iterate(FILE *dump=0) override
Iterate the cursor until the end, returning the number of items.
bool at_start
True if we are at the start of the iteration.
Definition: db/v7/cursor.h:134
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:686
Functions used to connect to DB-All.e and insert, query and delete data.
Cursor iterating over summary entries.
Definition: cursor.h:97
Standard dballe::Query implementation.
Definition: core/query.h:34
Definition: cmdline.h:18
Cursor iterating over data values.
Definition: cursor.h:77
CursorData implementation.
Definition: db/v7/cursor.h:258
Definition: db/v7/cursor.h:80
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:191
Definition: db/v7/cursor.h:227
Definition: db/v7/cursor.h:36
Vertical level or layer.
Definition: types.h:624
Definition: db/db.h:58
Definition: db/db.h:104
uint16_t Varcode
Range of datetimes.
Definition: types.h:294
Structure used to build and execute a query, and to iterate through the results.
Definition: db/v7/cursor.h:122
Definition: cache.h:14
Definition: db/v7/cursor.h:51
Definition: db/v7/cursor.h:64
std::deque< Row > results
Storage for the raw database results.
Definition: db/v7/cursor.h:131
Smart pointer for trace::Step objects, which calls done() when going out of scope.
Definition: db/v7/fwd.h:45
Cursor iterating over station data values.
Definition: cursor.h:66
Date and time.
Definition: types.h:164
std::shared_ptr< v7::Transaction > tr
Database to operate on.
Definition: db/v7/cursor.h:128
Cursor iterating over stations.
Definition: cursor.h:56
Definition: types.h:850
Structures used as input to database insert functions.
Repinfo table management used by the db module.
Container for a wreport::Var pointer, and its database ID.
Definition: value.h:71
Definition: qbuilder.h:91