libdballe  9.14
batch.h
1 #ifndef DBALLE_DB_V7_BATCH_H
2 #define DBALLE_DB_V7_BATCH_H
3 
4 #include <dballe/core/smallset.h>
5 #include <dballe/db/v7/fwd.h>
6 #include <dballe/db/v7/utils.h>
7 #include <dballe/types.h>
8 #include <memory>
9 #include <tuple>
10 #include <vector>
11 #include <wreport/var.h>
12 
13 namespace dballe {
14 namespace db {
15 namespace v7 {
16 struct Transaction;
17 
18 class Batch
19 {
20 protected:
21  bool write_attrs = true;
22  batch::Station* last_station = nullptr;
23 
24  bool have_station(const std::string& report, const Coords& coords,
25  const Ident& ident);
26  void new_station(Tracer<>& trc, const std::string& report,
27  const Coords& coords, const Ident& ident);
28 
29 public:
30  Transaction& transaction;
31  unsigned count_select_stations = 0;
32  unsigned count_select_station_data = 0;
33  unsigned count_select_data = 0;
34 
35  Batch(Transaction& transaction) : transaction(transaction) {}
36  ~Batch();
37 
38  void set_write_attrs(bool write_attrs);
39 
40  batch::Station* get_station(Tracer<>& trc, const dballe::DBStation& station,
41  bool station_can_add);
42  batch::Station* get_station(Tracer<>& trc, const std::string& report,
43  const Coords& coords, const Ident& ident);
44 
45  void write_pending(Tracer<>& trc);
46  void clear();
47  void dump(FILE* out) const;
48 };
49 
50 namespace batch {
51 
52 enum UpdateMode {
53  UPDATE,
54  IGNORE,
55  ERROR,
56 };
57 
59 {
60  int id = MISSING_INT;
61  const wreport::Var* var;
62 
63  StationDatum(const wreport::Var* var) : var(var) {}
64  StationDatum(int id, const wreport::Var* var) : id(id), var(var) {}
65 
66  void dump(FILE* out) const;
67  bool operator<(const StationDatum& o) const
68  {
69  return var->code() < o.var->code();
70  }
71  bool operator==(const StationDatum& o) const
72  {
73  return var->code() == o.var->code();
74  }
75 };
76 
77 inline const wreport::Varcode& station_data_ids_get_value(const IdVarcode& item)
78 {
79  return item.varcode;
80 }
81 
82 struct StationDataIDs : public core::SmallSet<IdVarcode, wreport::Varcode,
83  station_data_ids_get_value>
84 {
85 };
86 
88 {
89  StationDataIDs ids_by_code;
90  std::vector<StationDatum> to_insert;
91  std::vector<StationDatum> to_update;
92  bool loaded = false;
93 
94  void add(const wreport::Var* var, UpdateMode on_conflict);
95  void write_pending(Tracer<>& trc, Transaction& tr, int station_id,
96  bool with_attrs);
97 };
98 
100 {
101  int id = MISSING_INT;
102  int id_levtr;
103  const wreport::Var* var;
104 
105  MeasuredDatum(int id_levtr, const wreport::Var* var)
106  : id_levtr(id_levtr), var(var)
107  {
108  }
109  MeasuredDatum(int id, int id_levtr, const wreport::Var* var)
110  : id(id), id_levtr(id_levtr), var(var)
111  {
112  }
113 
114  void dump(FILE* out) const;
115  bool operator<(const MeasuredDatum& o) const
116  {
117  return id_levtr < o.id_levtr ||
118  (id_levtr == o.id_levtr && var->code() < o.var->code());
119  }
120  bool operator==(const MeasuredDatum& o) const
121  {
122  return id_levtr == o.id_levtr && var->code() == o.var->code();
123  }
124 };
125 
127 {
128  IdVarcode id_varcode;
129  int id;
130 
131  MeasuredDataID(IdVarcode id_varcode, int id)
132  : id_varcode(id_varcode), id(id)
133  {
134  }
135 };
136 
137 inline const IdVarcode& measured_data_ids_get_value(const MeasuredDataID& item)
138 {
139  return item.id_varcode;
140 }
141 
142 struct MeasuredDataIDs : public core::SmallSet<MeasuredDataID, IdVarcode,
143  measured_data_ids_get_value>
144 {
145 };
146 
148 {
149  Datetime datetime;
150  MeasuredDataIDs ids_on_db;
151  std::vector<MeasuredDatum> to_insert;
152  std::vector<MeasuredDatum> to_update;
153 
154  MeasuredData(Datetime datetime) : datetime(datetime) {}
155 
156  void add(int id_levtr, const wreport::Var* var, UpdateMode on_conflict);
157  void write_pending(Tracer<>& trc, Transaction& tr, int station_id,
158  bool with_attrs);
159 };
160 
161 inline const Datetime& measured_data_vector_get_value(MeasuredData* const& item)
162 {
163  return item->datetime;
164 }
165 
167  : public core::SmallSet<MeasuredData*, Datetime,
168  measured_data_vector_get_value>
169 {
170  MeasuredDataVector() {}
171  MeasuredDataVector(const MeasuredDataVector&) = delete;
174  MeasuredDataVector& operator=(const MeasuredDataVector&) = delete;
175  MeasuredDataVector& operator=(MeasuredDataVector&&) = default;
176 };
177 
178 struct Station : public dballe::DBStation
179 {
180  Batch& batch;
181  bool is_new = true;
182  StationData station_data;
183  MeasuredDataVector measured_data;
184 
185  Station(Batch& batch) : batch(batch) {}
186 
187  StationData& get_station_data(Tracer<>& trc);
188  MeasuredData& get_measured_data(Tracer<>& trc, const Datetime& datetime);
189 
190  void write_pending(Tracer<>& trc, bool with_attrs);
191  void dump(FILE* out) const;
192 };
193 
194 } // namespace batch
195 } // namespace v7
196 } // namespace db
197 } // namespace dballe
198 
199 #endif
Common base types used by most of DB-All.e code.
Coordinates.
Definition: types.h:371
Definition: cmdline.h:18
Definition: batch.h:178
Varcode code() const
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:750
Definition: utils.h:11
Set structure optimized for a small number of items.
Definition: smallset.h:20
Definition: transaction.h:15
Smart pointer for trace::Step objects, which calls done() when going out of scope.
Definition: db/v7/fwd.h:44
Date and time.
Definition: types.h:163
Definition: batch.h:18
Definition: types.h:939