libdballe  7.21
state.h
1 #ifndef DBALLE_DB_V7_STATE_H
2 #define DBALLE_DB_V7_STATE_H
3 
4 #include <wreport/var.h>
5 #include <dballe/core/defs.h>
6 #include <map>
7 #include <unordered_set>
8 
9 namespace dballe {
10 struct Record;
11 
12 namespace db {
13 namespace v7 {
14 
15 struct ItemState
16 {
17  // Database ID
18  int id;
19 
20  // True if the item has just been inserted
21  bool is_new;
22 
23  ItemState() {}
24  ItemState(int id, bool is_new) : id(id), is_new(is_new) {}
25  ItemState(const ItemState&) = default;
26  ItemState(ItemState&&) = default;
27  ItemState& operator=(const ItemState&) = default;
28  ItemState& operator=(ItemState&&) = default;
29 };
30 
31 
33 {
34  int rep;
35  Coords coords;
36  Ident ident;
37 
38  int compare(const StationDesc&) const;
39  bool operator<(const StationDesc& o) const { return compare(o) < 0; }
40 
47  void to_record(Record& rec) const;
48 };
49 
50 struct StationState : public ItemState
51 {
52  using ItemState::ItemState;
53 };
54 
55 typedef std::map<StationDesc, StationState> stations_t;
56 
57 
58 struct LevTrDesc
59 {
62 
65 
66  LevTrDesc() = default;
67  LevTrDesc(const Level& level, const Trange& trange) : level(level), trange(trange) {}
68  LevTrDesc(const LevTrDesc&) = default;
69  LevTrDesc(LevTrDesc&&) = default;
70  LevTrDesc& operator=(const LevTrDesc&) = default;
71  LevTrDesc& operator=(LevTrDesc&&) = default;
72 
73  int compare(const LevTrDesc&) const;
74  bool operator<(const LevTrDesc& o) const { return compare(o) < 0; }
75 };
76 
77 struct LevTrState : public ItemState
78 {
79  using ItemState::ItemState;
80 };
81 
82 typedef std::map<LevTrDesc, LevTrState> levtrs_t;
83 typedef std::map<int, levtrs_t::iterator> levtr_id_t;
84 
85 
87 {
88  stations_t::iterator station;
89  wreport::Varcode varcode;
90 
91  StationValueDesc() {}
92  StationValueDesc(const StationValueDesc&) = default;
93  StationValueDesc(stations_t::iterator station, wreport::Varcode varcode)
94  : station(station), varcode(varcode) {}
95  StationValueDesc& operator=(const StationValueDesc&) = default;
96 
97  int compare(const StationValueDesc&) const;
98  bool operator<(const StationValueDesc& o) const { return compare(o) < 0; }
99 };
100 
102 {
103  using ItemState::ItemState;
104 };
105 
106 typedef std::map<StationValueDesc, StationValueState> stationvalues_t;
107 
108 
109 struct ValueDesc
110 {
111  stations_t::iterator station;
112  int levtr;
115  wreport::Varcode varcode;
116 
117  ValueDesc() {}
118  ValueDesc(const ValueDesc&) = default;
119  ValueDesc(stations_t::iterator station, int levtr, const Datetime& datetime, wreport::Varcode varcode)
120  : station(station), levtr(levtr), datetime(datetime), varcode(varcode) {}
121  ValueDesc& operator=(const ValueDesc&) = default;
122 
123  int compare(const ValueDesc&) const;
124  bool operator<(const ValueDesc& o) const { return compare(o) < 0; }
125 };
126 
127 struct ValueState : public ItemState
128 {
129  using ItemState::ItemState;
130 };
131 
132 typedef std::map<ValueDesc, ValueState> values_t;
133 
134 
140 struct State
141 {
142  stations_t stations;
143  levtrs_t levtrs;
144  levtr_id_t levtr_ids;
145  stationvalues_t stationvalues;
146  values_t values;
147  std::unordered_set<int> stationvalues_new;
148  std::unordered_set<int> values_new;
149 
150  stations_t::iterator add_station(const StationDesc& desc, const StationState& state);
151  levtrs_t::iterator add_levtr(const LevTrDesc& desc, const LevTrState& state);
152  stationvalues_t::iterator add_stationvalue(const StationValueDesc& desc, const StationValueState& state);
153  values_t::iterator add_value(const ValueDesc& desc, const ValueState& state);
154 
156  void clear();
157 };
158 
159 }
160 }
161 }
162 #endif
Definition: state.h:32
Cache intermediate results during a database transaction, to avoid hitting the database multiple time...
Definition: state.h:140
Coordinates.
Definition: types.h:337
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:587
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
Definition: state.h:50
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: state.h:86
Vertical level or layer.
Definition: types.h:532
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: core/defs.h:19
Definition: state.h:15
uint16_t Varcode
Definition: state.h:101
Definition: state.h:127
Definition: state.h:77
Trange trange
Time range.
Definition: state.h:64
Date and time.
Definition: types.h:158
Level level
Vertical level or layer.
Definition: state.h:61
Datetime datetime
Date and time at which the value was measured or forecast.
Definition: state.h:114
Definition: state.h:109
Common definitions.
Definition: state.h:58