forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflex-table.hpp
More file actions
369 lines (279 loc) · 9.51 KB
/
flex-table.hpp
File metadata and controls
369 lines (279 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef OSM2PGSQL_FLEX_TABLE_HPP
#define OSM2PGSQL_FLEX_TABLE_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "db-copy-mgr.hpp"
#include "flex-index.hpp"
#include "flex-table-column.hpp"
#include "pgsql.hpp"
#include "projection.hpp"
#include "reprojection.hpp"
#include "thread-pool.hpp"
#include "util.hpp"
#include <osmium/osm/item_type.hpp>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <future>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
/**
* This defines the type of "primary key" for the tables generated in the flex
* output. This is not a real primary key, because the values are not
* necessarily unique.
*/
enum class flex_table_index_type : uint8_t
{
no_index,
node, // index by node id
way, // index by way id
relation, // index by relation id
area, // index by way (positive) or relation (negative) id
any_object, // any OSM object, two columns for type and id
tile // index by tile with x and y columns (used for generalized data)
};
/**
* An output table (in the SQL sense) for the flex backend.
*/
class flex_table_t
{
public:
/**
* Table creation type: interim tables are created as UNLOGGED and with
* autovacuum disabled.
*/
enum class table_type : uint8_t
{
interim,
permanent
};
flex_table_t(std::string schema, std::string name, std::size_t num)
: m_schema(std::move(schema)), m_name(std::move(name)), m_table_num(num)
{
}
std::string const &name() const noexcept { return m_name; }
std::string const &schema() const noexcept { return m_schema; }
bool cluster_by_geom() const noexcept
{
return has_geom_column() && m_cluster_by_geom;
}
std::string const &data_tablespace() const noexcept
{
return m_data_tablespace;
}
std::string const &index_tablespace() const noexcept
{
return m_index_tablespace;
}
void set_schema(std::string schema) noexcept
{
m_schema = std::move(schema);
}
void set_cluster_by_geom(bool cluster) noexcept
{
m_cluster_by_geom = cluster;
}
void set_data_tablespace(std::string tablespace) noexcept
{
m_data_tablespace = std::move(tablespace);
}
void set_index_tablespace(std::string tablespace) noexcept
{
m_index_tablespace = std::move(tablespace);
}
flex_table_index_type id_type() const noexcept { return m_id_type; }
void set_id_type(flex_table_index_type type) noexcept { m_id_type = type; }
bool has_id_column() const noexcept;
std::size_t num_columns() const noexcept { return m_columns.size(); }
std::vector<flex_table_column_t> const &columns() const noexcept
{
return m_columns;
}
flex_table_column_t *find_column_by_name(std::string const &name)
{
return util::find_by_name(m_columns, name);
}
bool has_geom_column() const noexcept
{
return m_geom_column != std::numeric_limits<std::size_t>::max();
}
/// Get the (first, if there are multiple) geometry column.
flex_table_column_t const &geom_column() const noexcept
{
assert(has_geom_column());
return m_columns[m_geom_column];
}
flex_table_column_t &geom_column() noexcept
{
assert(has_geom_column());
return m_columns[m_geom_column];
}
int srid() const noexcept
{
return has_geom_column() ? geom_column().srid() : PROJ_LATLONG;
}
std::string build_sql_prepare_get_wkb() const;
std::string build_sql_create_table(table_type ttype,
std::string const &table_name) const;
std::string build_sql_column_list() const;
std::string build_sql_create_id_index() const;
/// Does this table take objects of the specified type?
bool matches_type(osmium::item_type type) const noexcept;
/// Map way/node/relation ID to id value used in database table column
osmid_t map_id(osmium::item_type type, osmid_t id) const noexcept;
flex_table_column_t &add_column(std::string const &name,
std::string const &type,
std::string const &sql_type);
bool has_multicolumn_id_index() const noexcept;
std::string id_column_names() const;
std::string full_name() const;
std::string full_tmp_name() const;
bool has_multiple_geom_columns() const noexcept
{
return m_has_multiple_geom_columns;
}
std::vector<flex_index_t> const &indexes() const noexcept
{
return m_indexes;
}
flex_index_t &add_index(std::string method);
void set_always_build_id_index() noexcept
{
m_always_build_id_index = true;
}
bool always_build_id_index() const noexcept
{
return m_always_build_id_index;
}
void set_build_unique_id_index(bool as_primary_key) noexcept
{
m_build_unique_id_index = true;
m_primary_key_index = as_primary_key;
}
bool build_unique_id_index() const noexcept
{
return m_build_unique_id_index;
}
bool has_columns_with_expire() const noexcept;
std::size_t num() const noexcept { return m_table_num; }
void prepare(pg_conn_t const &db_connection) const;
void analyze(pg_conn_t const &db_connection) const;
void enable_id_cache() noexcept;
bool with_id_cache() const noexcept;
private:
/// The schema this table is in
std::string m_schema;
/// The name of the table
std::string m_name;
/// The table space used for this table (empty for default tablespace)
std::string m_data_tablespace;
/**
* The table space used for indexes on this table (empty for default
* tablespace)
*/
std::string m_index_tablespace;
/**
* The columns in this table (The first zero, one or two columns are always
* the id columns).
*/
std::vector<flex_table_column_t> m_columns;
/**
* The indexes defined on this table. Does not include the id index.
*/
std::vector<flex_index_t> m_indexes;
/**
* Index of the (first) geometry column in m_columns. Default means no
* geometry column.
*/
std::size_t m_geom_column = std::numeric_limits<std::size_t>::max();
/// Unique number for each table.
std::size_t m_table_num;
/**
* Type of id stored in this table.
*/
flex_table_index_type m_id_type = flex_table_index_type::no_index;
/// Cluster the table by geometry.
bool m_cluster_by_geom = true;
/// Does this table have more than one geometry column?
bool m_has_multiple_geom_columns = false;
/// Always build the id index, not only when it is needed for updates?
bool m_always_build_id_index = false;
/// Build the index as a unique index.
bool m_build_unique_id_index = false;
/// Index should be a primary key.
bool m_primary_key_index = false;
/// Do we want an ID cache for this table?
bool m_with_id_cache = false;
}; // class flex_table_t
class table_connection_t
{
public:
table_connection_t(flex_table_t *table,
std::shared_ptr<db_copy_thread_t> const ©_thread)
: m_proj(reprojection_t::create_projection(table->srid())), m_table(table),
m_target(std::make_shared<db_target_descr_t>(
table->schema(), table->name(), table->id_column_names(),
table->build_sql_column_list())),
m_copy_mgr(copy_thread)
{
}
void start(pg_conn_t const &db_connection, bool append) const;
void stop(pg_conn_t const &db_connection, bool updateable, bool append);
flex_table_t const &table() const noexcept { return *m_table; }
void create_id_index(pg_conn_t const &db_connection);
/**
* Get all geometries that have at least one expire config defined
* from the database and return the result set.
*/
pg_result_t get_geoms_by_id(pg_conn_t const &db_connection,
osmium::item_type type, osmid_t id) const;
void flush() { m_copy_mgr.flush(); }
void sync() { m_copy_mgr.sync(); }
void new_line() { m_copy_mgr.new_line(m_target); }
db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr() noexcept
{
return &m_copy_mgr;
}
void delete_rows_with(osmium::item_type type, osmid_t id);
reprojection_t const &proj() const noexcept
{
assert(m_proj);
return *m_proj;
}
void task_set(std::future<std::chrono::microseconds> &&future)
{
m_task_result.set(std::move(future));
}
void task_wait();
void increment_insert_counter() noexcept { ++m_count_insert; }
void increment_not_null_error_counter() noexcept
{
++m_count_not_null_error;
}
private:
std::shared_ptr<reprojection_t> m_proj;
flex_table_t *m_table;
std::shared_ptr<db_target_descr_t> m_target;
/**
* The copy manager responsible for sending data through the COPY mechanism
* to the database server.
*/
db_copy_mgr_t<db_deleter_by_type_and_id_t> m_copy_mgr;
task_result_t m_task_result;
std::size_t m_count_insert = 0;
std::size_t m_count_not_null_error = 0;
/// Has the Id index already been created?
bool m_id_index_created = false;
}; // class table_connection_t
char const *type_to_char(osmium::item_type type) noexcept;
#endif // OSM2PGSQL_FLEX_TABLE_HPP