forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflex-lua-table.cpp
444 lines (377 loc) · 15.3 KB
/
flex-lua-table.cpp
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-lua-table.hpp"
#include "expire-output.hpp"
#include "flex-lua-expire-output.hpp"
#include "flex-lua-index.hpp"
#include "flex-table.hpp"
#include "lua-utils.hpp"
#include "pgsql-capabilities.hpp"
#include <lua.hpp>
namespace {
void check_tablespace(std::string const &tablespace)
{
if (!has_tablespace(tablespace)) {
throw fmt_error(
"Tablespace '{0}' not available."
" Use 'CREATE TABLESPACE \"{0}\" ...;' to create it.",
tablespace);
}
}
flex_table_t &create_flex_table(lua_State *lua_state,
std::string const &default_schema,
std::vector<flex_table_t> *tables)
{
std::string const table_name =
luaX_get_table_string(lua_state, "name", -1, "The table");
check_identifier(table_name, "table names");
if (util::find_by_name(*tables, table_name)) {
throw fmt_error("Table with name '{}' already exists.", table_name);
}
auto &new_table =
tables->emplace_back(default_schema, table_name, tables->size());
lua_pop(lua_state, 1); // "name"
// optional "schema" field
lua_getfield(lua_state, -1, "schema");
if (lua_isstring(lua_state, -1)) {
std::string const schema = lua_tostring(lua_state, -1);
check_identifier(schema, "schema field");
check_schema(schema);
new_table.set_schema(schema);
}
lua_pop(lua_state, 1);
// optional "cluster" field
lua_getfield(lua_state, -1, "cluster");
int const cluster_type = lua_type(lua_state, -1);
if (cluster_type == LUA_TSTRING) {
std::string const cluster = lua_tostring(lua_state, -1);
if (cluster == "auto") {
new_table.set_cluster_by_geom(true);
} else if (cluster == "no") {
new_table.set_cluster_by_geom(false);
} else {
throw fmt_error("Unknown value '{}' for 'cluster' table option"
" (use 'auto' or 'no').",
cluster);
}
} else if (cluster_type == LUA_TNIL) {
// ignore
} else {
throw std::runtime_error{
"Unknown value for 'cluster' table option: Must be string."};
}
lua_pop(lua_state, 1);
// optional "data_tablespace" field
lua_getfield(lua_state, -1, "data_tablespace");
if (lua_isstring(lua_state, -1)) {
std::string const tablespace = lua_tostring(lua_state, -1);
check_identifier(tablespace, "data_tablespace field");
check_tablespace(tablespace);
new_table.set_data_tablespace(tablespace);
}
lua_pop(lua_state, 1);
// optional "index_tablespace" field
lua_getfield(lua_state, -1, "index_tablespace");
if (lua_isstring(lua_state, -1)) {
std::string const tablespace = lua_tostring(lua_state, -1);
check_identifier(tablespace, "index_tablespace field");
check_tablespace(tablespace);
new_table.set_index_tablespace(tablespace);
}
lua_pop(lua_state, 1);
return new_table;
}
void parse_create_index(lua_State *lua_state, flex_table_t *table)
{
std::string const create_index = luaX_get_table_string(
lua_state, "create_index", -1, "The ids field", "auto");
lua_pop(lua_state, 1); // "create_index"
if (create_index == "always") {
table->set_always_build_id_index();
} else if (create_index == "unique") {
table->set_always_build_id_index();
table->set_build_unique_id_index(false);
} else if (create_index == "primary_key") {
table->set_always_build_id_index();
table->set_build_unique_id_index(true);
} else if (create_index != "auto") {
throw fmt_error("Unknown value '{}' for 'create_index' field of ids",
create_index);
}
}
void setup_flex_table_id_columns(lua_State *lua_state, flex_table_t *table)
{
assert(lua_state);
assert(table);
lua_getfield(lua_state, -1, "ids");
if (lua_type(lua_state, -1) != LUA_TTABLE) {
log_warn("Table '{}' doesn't have an id column. Two-stage"
" processing, updates and expire will not work!",
table->name());
lua_pop(lua_state, 1); // ids
return;
}
std::string const type{
luaX_get_table_string(lua_state, "type", -1, "The ids field")};
lua_pop(lua_state, 1); // "type"
if (type == "node") {
table->set_id_type(flex_table_index_type::node);
} else if (type == "way") {
table->set_id_type(flex_table_index_type::way);
} else if (type == "relation") {
table->set_id_type(flex_table_index_type::relation);
} else if (type == "area") {
table->set_id_type(flex_table_index_type::area);
} else if (type == "any") {
table->set_id_type(flex_table_index_type::any_object);
lua_getfield(lua_state, -1, "type_column");
if (lua_isstring(lua_state, -1)) {
std::string const column_name =
lua_tolstring(lua_state, -1, nullptr);
check_identifier(column_name, "column names");
auto &column = table->add_column(column_name, "id_type", "");
column.set_not_null();
} else if (!lua_isnil(lua_state, -1)) {
throw std::runtime_error{"type_column must be a string or nil."};
}
lua_pop(lua_state, 1); // "type_column"
} else if (type == "tile") {
table->set_id_type(flex_table_index_type::tile);
parse_create_index(lua_state, table);
table->add_column("x", "int", "int").set_not_null();
table->add_column("y", "int", "int").set_not_null();
lua_pop(lua_state, 1); // "ids"
return;
} else {
throw fmt_error("Unknown ids type: {}.", type);
}
std::string const name =
luaX_get_table_string(lua_state, "id_column", -1, "The ids field");
lua_pop(lua_state, 1); // "id_column"
check_identifier(name, "column names");
parse_create_index(lua_state, table);
auto &column = table->add_column(name, "id_num", "");
column.set_not_null();
lua_pop(lua_state, 1); // "ids"
}
std::size_t idx_from_userdata(lua_State *lua_state, int idx,
std::size_t expire_outputs_size)
{
void const *const user_data = lua_touserdata(lua_state, idx);
if (user_data == nullptr || !lua_getmetatable(lua_state, idx)) {
throw std::runtime_error{"Expire output must be of type ExpireOutput."};
}
luaL_getmetatable(lua_state, osm2pgsql_expire_output_name);
if (!lua_rawequal(lua_state, -1, -2)) {
throw std::runtime_error{"Expire output must be of type ExpireOutput."};
}
lua_pop(lua_state, 2); // remove the two metatables
auto const eo = *static_cast<std::size_t const *>(user_data);
if (eo >= expire_outputs_size) {
throw std::runtime_error{"Internal error in expire output code."};
}
return eo;
}
void parse_and_set_expire_options(lua_State *lua_state,
flex_table_column_t *column,
std::size_t expire_outputs_size,
bool append_mode)
{
auto const type = lua_type(lua_state, -1);
if (type == LUA_TNIL) {
return;
}
if (!column->is_geometry_column() || column->srid() != 3857) {
throw std::runtime_error{"Expire only allowed for geometry"
" columns in Web Mercator projection."};
}
if (type == LUA_TUSERDATA) {
auto const eo = idx_from_userdata(lua_state, -1, expire_outputs_size);
// Actually add the expire only if we are in append mode.
if (append_mode) {
expire_config_t const config{eo};
column->add_expire(config);
}
return;
}
if (type != LUA_TTABLE) {
throw std::runtime_error{"Expire field must be a Lua array table"};
}
if (luaX_is_empty_table(lua_state)) {
return;
}
if (!luaX_is_array(lua_state)) {
throw std::runtime_error{"Expire field must be a Lua array table"};
}
luaX_for_each(lua_state, [&]() {
if (!lua_istable(lua_state, -1) || luaX_is_array(lua_state)) {
throw std::runtime_error{"Expire config must be a Lua table"};
}
lua_getfield(lua_state, -1, "output");
auto const eo = idx_from_userdata(lua_state, -1, expire_outputs_size);
lua_pop(lua_state, 1); // "output"
expire_config_t config{eo};
std::string mode;
lua_getfield(lua_state, -1, "mode");
if (lua_isstring(lua_state, -1)) {
mode = lua_tostring(lua_state, -1);
} else if (!lua_isnil(lua_state, -1)) {
throw std::runtime_error{
"Optional expire field 'mode' must contain a string."};
}
lua_pop(lua_state, 1); // "mode"
if (mode.empty() || mode == "full-area") {
config.mode = expire_mode::full_area;
} else if (mode == "boundary-only") {
config.mode = expire_mode::boundary_only;
} else if (mode == "hybrid") {
config.mode = expire_mode::hybrid;
} else {
throw fmt_error("Unknown expire mode '{}'.", mode);
}
lua_getfield(lua_state, -1, "full_area_limit");
if (lua_isnumber(lua_state, -1)) {
if (config.mode != expire_mode::hybrid) {
log_warn("Ignoring 'full_area_limit' setting in expire config,"
" because 'mode' is not set to 'hybrid'.");
}
config.full_area_limit = lua_tonumber(lua_state, -1);
} else if (!lua_isnil(lua_state, -1)) {
throw std::runtime_error{"Optional expire field 'full_area_limit' "
"must contain a number."};
}
lua_pop(lua_state, 1); // "full_area_limit"
lua_getfield(lua_state, -1, "buffer");
if (lua_isnumber(lua_state, -1)) {
config.buffer = lua_tonumber(lua_state, -1);
} else if (!lua_isnil(lua_state, -1)) {
throw std::runtime_error{
"Optional expire field 'buffer' must contain a number."};
}
lua_pop(lua_state, 1); // "buffer"
// Actually add the expire only if we are in append mode.
if (append_mode) {
column->add_expire(config);
}
});
}
void setup_flex_table_columns(lua_State *lua_state, flex_table_t *table,
std::vector<expire_output_t> *expire_outputs,
bool append_mode)
{
assert(lua_state);
assert(table);
lua_getfield(lua_state, -1, "columns");
if (lua_type(lua_state, -1) != LUA_TTABLE) {
throw fmt_error("No 'columns' field (or not an array) in table '{}'.",
table->name());
}
if (!luaX_is_array(lua_state)) {
throw std::runtime_error{"The 'columns' field must contain an array."};
}
std::size_t num_columns = 0;
luaX_for_each(lua_state, [&]() {
if (!lua_istable(lua_state, -1)) {
throw std::runtime_error{
"The entries in the 'columns' array must be tables."};
}
char const *const type = luaX_get_table_string(lua_state, "type", -1,
"Column entry", "text");
char const *const name =
luaX_get_table_string(lua_state, "column", -2, "Column entry");
check_identifier(name, "column names");
char const *const sql_type = luaX_get_table_string(
lua_state, "sql_type", -3, "Column entry", "");
auto &column = table->add_column(name, type, sql_type);
lua_pop(lua_state, 3); // "type", "column", "sql_type"
column.set_not_null(luaX_get_table_bool(lua_state, "not_null", -1,
"Entry 'not_null'", false));
lua_pop(lua_state, 1); // "not_null"
column.set_create_only(luaX_get_table_bool(
lua_state, "create_only", -1, "Entry 'create_only'", false));
lua_pop(lua_state, 1); // "create_only"
lua_getfield(lua_state, -1, "projection");
if (!lua_isnil(lua_state, -1)) {
if (column.is_geometry_column()) {
column.set_projection(lua_tostring(lua_state, -1));
} else {
throw std::runtime_error{"Projection can only be set on "
"geometry columns."};
}
}
lua_pop(lua_state, 1); // "projection"
lua_getfield(lua_state, -1, "expire");
parse_and_set_expire_options(lua_state, &column, expire_outputs->size(),
append_mode);
lua_pop(lua_state, 1); // "expire"
++num_columns;
});
if (num_columns == 0 && !table->has_id_column()) {
throw fmt_error("No columns defined for table '{}'.", table->name());
}
lua_pop(lua_state, 1); // "columns"
}
void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table,
bool updatable)
{
assert(lua_state);
assert(table);
lua_getfield(lua_state, -1, "indexes");
if (lua_type(lua_state, -1) == LUA_TNIL) {
if (table->has_geom_column()) {
auto &index = table->add_index("gist");
index.set_columns(table->geom_column().name());
if (!updatable) {
// If database can not be updated, use fillfactor 100.
index.set_fillfactor(100);
}
index.set_tablespace(table->index_tablespace());
}
lua_pop(lua_state, 1); // "indexes"
return;
}
if (lua_type(lua_state, -1) != LUA_TTABLE) {
throw fmt_error("The 'indexes' field in definition of"
" table '{}' is not an array.",
table->name());
}
if (!luaX_is_array(lua_state)) {
throw std::runtime_error{"The 'indexes' field must contain an array."};
}
luaX_for_each(lua_state, [&]() {
if (!lua_istable(lua_state, -1)) {
throw std::runtime_error{
"The entries in the 'indexes' array must be Lua tables."};
}
flex_lua_setup_index(lua_state, table);
});
lua_pop(lua_state, 1); // "indexes"
}
} // anonymous namespace
int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
std::vector<expire_output_t> *expire_outputs,
std::string const &default_schema, bool updatable,
bool append_mode)
{
if (lua_type(lua_state, 1) != LUA_TTABLE) {
throw std::runtime_error{
"Argument #1 to 'define_table' must be a table."};
}
auto &new_table = create_flex_table(lua_state, default_schema, tables);
setup_flex_table_id_columns(lua_state, &new_table);
setup_flex_table_columns(lua_state, &new_table, expire_outputs,
append_mode);
setup_flex_table_indexes(lua_state, &new_table, updatable);
void *ptr = lua_newuserdata(lua_state, sizeof(std::size_t));
auto *num = new (ptr) std::size_t{};
*num = tables->size() - 1;
luaL_getmetatable(lua_state, osm2pgsql_table_name);
lua_setmetatable(lua_state, -2);
return 1;
}