Skip to content

Commit f3241df

Browse files
committed
Reference expire outputs as Lua objects, not as strings with their names
Expire outputs are now directly referenced from table column definitions as if they are Lua objects, not by using their name. In fact they don't even have a name any more.
1 parent cdcaa99 commit f3241df

File tree

10 files changed

+98
-191
lines changed

10 files changed

+98
-191
lines changed

flex-config/expire.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
local expire_outputs = {}
66

77
expire_outputs.pois = osm2pgsql.define_expire_output({
8-
-- Every expire output must have a name. The name is independent of the
9-
-- data table names although this example file uses the same name for
10-
-- simplicity.
11-
name = 'pois',
128
-- The zoom level at which we calculate the tiles. This must always be set.
139
maxzoom = 14,
1410
-- The filename where tile list should be written to.
1511
filename = 'pois.tiles'
1612
})
1713

1814
expire_outputs.lines = osm2pgsql.define_expire_output({
19-
name = 'lines',
2015
maxzoom = 14,
2116
-- Instead of writing the tile list to a file, it can be written to a table.
2217
-- The table will be created if it isn't there already.
@@ -25,7 +20,6 @@ expire_outputs.lines = osm2pgsql.define_expire_output({
2520
})
2621

2722
expire_outputs.polygons = osm2pgsql.define_expire_output({
28-
name = 'polygons',
2923
-- You can also set a minimum zoom level in addition to the maximum zoom
3024
-- level. Tiles in all zoom levels between those two will be written out.
3125
minzoom = 10,
@@ -36,13 +30,11 @@ expire_outputs.polygons = osm2pgsql.define_expire_output({
3630
print("Expire outputs:(")
3731
for name, eo in pairs(expire_outputs) do
3832
print(" " .. name
39-
.. ": name=".. eo:name()
40-
.. " minzoom=" .. eo:minzoom()
33+
.. ": minzoom=" .. eo:minzoom()
4134
.. " maxzoom=" .. eo:maxzoom()
4235
.. " filename=" .. eo:filename()
4336
.. " schema=" .. eo:schema()
44-
.. " table=" .. eo:table()
45-
.. " (" .. tostring(eo) .. ")")
37+
.. " table=" .. eo:table())
4638
end
4739
print(")")
4840

@@ -53,14 +45,14 @@ tables.pois = osm2pgsql.define_node_table('pois', {
5345
-- Zero, one or more expire outputs are referenced in an `expire` field in
5446
-- the definition of any geometry column using the Web Mercator (3857)
5547
-- projection.
56-
{ column = 'geom', type = 'point', not_null = true, expire = { { output = 'pois' } } },
48+
{ column = 'geom', type = 'point', not_null = true, expire = { { output = expire_outputs.pois } } },
5749
})
5850

5951
tables.lines = osm2pgsql.define_way_table('lines', {
6052
{ column = 'tags', type = 'jsonb' },
6153
-- If you only have a single expire output and with the default
6254
-- parameters, you can specify it directly.
63-
{ column = 'geom', type = 'linestring', not_null = true, expire = 'lines' },
55+
{ column = 'geom', type = 'linestring', not_null = true, expire = expire_outputs.lines },
6456
})
6557

6658
tables.polygons = osm2pgsql.define_area_table('polygons', {
@@ -72,7 +64,7 @@ tables.polygons = osm2pgsql.define_area_table('polygons', {
7264
-- limit the full area is expired, for larger polygons only the boundary.
7365
-- This setting doesn't have any effect on point or linestring geometries.
7466
{ column = 'geom', type = 'geometry', not_null = true, expire = {
75-
{ output = 'polygons', mode = 'boundary-only' }
67+
{ output = expire_outputs.polygons, mode = 'boundary-only' }
7668
}}
7769
})
7870

src/debug-output.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ void write_expire_output_list_to_debug_log(
1919
}
2020

2121
log_debug("ExpireOutputs:");
22+
std::size_t n = 0;
2223
for (auto const &expire_output : expire_outputs) {
23-
log_debug("- ExpireOutput {}", expire_output.name());
24+
log_debug("- ExpireOutput [{}]", n++);
2425
if (expire_output.minzoom() == expire_output.maxzoom()) {
2526
log_debug(" - zoom: {}", expire_output.maxzoom());
2627
} else {
@@ -32,14 +33,12 @@ void write_expire_output_list_to_debug_log(
3233
}
3334
if (!expire_output.table().empty()) {
3435
log_debug(" - table: {}", qualified_name(expire_output.schema(),
35-
expire_output.name()));
36+
expire_output.table()));
3637
}
3738
}
3839
}
3940

40-
void write_table_list_to_debug_log(
41-
std::vector<flex_table_t> const &tables,
42-
std::vector<expire_output_t> const &expire_outputs)
41+
void write_table_list_to_debug_log(std::vector<flex_table_t> const &tables)
4342
{
4443
if (!get_logger().debug_enabled()) {
4544
return;
@@ -54,8 +53,7 @@ void write_table_list_to_debug_log(
5453
column.name(), column.type_name(), column.sql_type_name(),
5554
column.not_null(), column.create_only());
5655
for (auto const &ec : column.expire_configs()) {
57-
auto const &config = expire_outputs[ec.expire_output];
58-
log_debug(" - expire: output={}", config.name());
56+
log_debug(" - expire: [{}]", ec.expire_output);
5957
}
6058
}
6159
log_debug(" - data_tablespace={}", table.data_tablespace());

src/debug-output.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
void write_expire_output_list_to_debug_log(
1717
std::vector<expire_output_t> const &expire_outputs);
1818

19-
void write_table_list_to_debug_log(
20-
std::vector<flex_table_t> const &tables,
21-
std::vector<expire_output_t> const &expire_outputs);
19+
void write_table_list_to_debug_log(std::vector<flex_table_t> const &tables);
2220

2321
#endif // OSM2PGSQL_DEBUG_OUTPUT_HPP

src/expire-output.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ class expire_output_t
2323
public:
2424
expire_output_t() = default;
2525

26-
explicit expire_output_t(std::string name) : m_name(std::move(name)) {}
27-
28-
std::string const &name() const noexcept { return m_name; }
29-
3026
std::string filename() const noexcept { return m_filename; }
3127

3228
void set_filename(std::string filename)
@@ -71,9 +67,6 @@ class expire_output_t
7167
std::string const &conninfo) const;
7268

7369
private:
74-
/// The internal (unique) name of the output
75-
std::string m_name;
76-
7770
/// The filename (if any) for output
7871
std::string m_filename;
7972

src/flex-lua-expire-output.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,7 @@ static expire_output_t &
2121
create_expire_output(lua_State *lua_state,
2222
std::vector<expire_output_t> *expire_outputs)
2323
{
24-
std::string const expire_output_name =
25-
luaX_get_table_string(lua_state, "name", -1, "The expire output");
26-
27-
if (expire_output_name.empty()) {
28-
throw std::runtime_error{"The expire output name can not be empty."};
29-
}
30-
31-
check_identifier(expire_output_name, "expire output names");
32-
33-
if (util::find_by_name(*expire_outputs, expire_output_name)) {
34-
throw fmt_error("Expire output with name '{}' already exists.",
35-
expire_output_name);
36-
}
37-
38-
auto &new_expire_output = expire_outputs->emplace_back(expire_output_name);
39-
40-
lua_pop(lua_state, 1); // "name"
24+
auto &new_expire_output = expire_outputs->emplace_back();
4125

4226
// optional "filename" field
4327
auto const *filename = luaX_get_table_string(lua_state, "filename", -1,
@@ -57,9 +41,8 @@ create_expire_output(lua_State *lua_state,
5741

5842
if (new_expire_output.filename().empty() &&
5943
new_expire_output.table().empty()) {
60-
throw fmt_error(
61-
"Must set 'filename' and/or 'table' on expire output '{}'.",
62-
new_expire_output.name());
44+
throw std::runtime_error{
45+
"Must set 'filename' and/or 'table' on expire output."};
6346
}
6447

6548
// required "maxzoom" field

src/flex-lua-table.cpp

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flex-lua-table.hpp"
1111

1212
#include "expire-output.hpp"
13+
#include "flex-lua-expire-output.hpp"
1314
#include "flex-lua-index.hpp"
1415
#include "flex-table.hpp"
1516
#include "lua-utils.hpp"
@@ -166,35 +167,46 @@ static void setup_flex_table_id_columns(lua_State *lua_state,
166167
lua_pop(lua_state, 1); // "ids"
167168
}
168169

169-
static std::size_t
170-
find_expire_output(std::vector<expire_output_t> const &expire_outputs,
171-
std::string_view name)
170+
static std::size_t idx_from_userdata(lua_State *lua_state, int idx,
171+
std::size_t expire_outputs_size)
172172
{
173-
std::size_t n = 0;
174-
for (auto const &eo : expire_outputs) {
175-
if (eo.name() == name) {
176-
return n;
177-
}
178-
++n;
173+
void const *const user_data = lua_touserdata(lua_state, idx);
174+
175+
if (user_data == nullptr || !lua_getmetatable(lua_state, idx)) {
176+
throw std::runtime_error{"Expire output must be of type ExpireOutput."};
177+
}
178+
179+
luaL_getmetatable(lua_state, osm2pgsql_expire_output_name);
180+
if (!lua_rawequal(lua_state, -1, -2)) {
181+
throw std::runtime_error{"Expire output must be of type ExpireOutput."};
179182
}
183+
lua_pop(lua_state, 2); // remove the two metatables
180184

181-
throw fmt_error("Unknown ExpireOutput '{}'.", name);
185+
auto const eo = *static_cast<std::size_t const *>(user_data);
186+
if (eo >= expire_outputs_size) {
187+
throw std::runtime_error{"Internal error in expire output code."};
188+
}
189+
return eo;
182190
}
183191

184-
static void
185-
parse_and_set_expire_options(lua_State *lua_state, flex_table_column_t *column,
186-
std::vector<expire_output_t> *expire_outputs,
187-
bool append_mode)
192+
static void parse_and_set_expire_options(lua_State *lua_state,
193+
flex_table_column_t *column,
194+
std::size_t expire_outputs_size,
195+
bool append_mode)
188196
{
189197
auto const type = lua_type(lua_state, -1);
190198

191199
if (type == LUA_TNIL) {
192200
return;
193201
}
194202

195-
if (type == LUA_TSTRING) {
196-
auto eo =
197-
find_expire_output(*expire_outputs, lua_tostring(lua_state, -1));
203+
if (!column->is_geometry_column() || column->srid() != 3857) {
204+
throw std::runtime_error{"Expire only allowed for geometry"
205+
" columns in Web Mercator projection."};
206+
}
207+
208+
if (type == LUA_TUSERDATA) {
209+
auto const eo = idx_from_userdata(lua_state, -1, expire_outputs_size);
198210
expire_config_t config{eo};
199211
// Actually add the expire only if we are in append mode.
200212
if (append_mode) {
@@ -215,19 +227,13 @@ parse_and_set_expire_options(lua_State *lua_state, flex_table_column_t *column,
215227
throw std::runtime_error{"Expire field must be a Lua array table"};
216228
}
217229

218-
if (!column->is_geometry_column() || column->srid() != 3857) {
219-
throw std::runtime_error{"Expire only allowed for geometry"
220-
" columns in Web Mercator projection."};
221-
}
222-
223230
luaX_for_each(lua_state, [&]() {
224231
if (!lua_istable(lua_state, -1) || luaX_is_array(lua_state)) {
225232
throw std::runtime_error{"Expire config must be a Lua table"};
226233
}
227234

228-
auto const *name =
229-
luaX_get_table_string(lua_state, "output", -1, "Entry 'output'");
230-
auto eo = find_expire_output(*expire_outputs, name);
235+
lua_getfield(lua_state, -1, "output");
236+
auto const eo = idx_from_userdata(lua_state, -1, expire_outputs_size);
231237
lua_pop(lua_state, 1); // "output"
232238

233239
expire_config_t config{eo};
@@ -337,7 +343,7 @@ setup_flex_table_columns(lua_State *lua_state, flex_table_t *table,
337343
lua_pop(lua_state, 1); // "projection"
338344

339345
lua_getfield(lua_state, -1, "expire");
340-
parse_and_set_expire_options(lua_state, &column, expire_outputs,
346+
parse_and_set_expire_options(lua_state, &column, expire_outputs->size(),
341347
append_mode);
342348
lua_pop(lua_state, 1); // "expire"
343349

src/output-flex.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ TRAMPOLINE(table_insert, insert)
9898
TRAMPOLINE(table_columns, columns)
9999
TRAMPOLINE(table_tostring, __tostring)
100100

101-
TRAMPOLINE(expire_output_name, name)
102101
TRAMPOLINE(expire_output_minzoom, minzoom)
103102
TRAMPOLINE(expire_output_maxzoom, maxzoom)
104103
TRAMPOLINE(expire_output_filename, filename)
@@ -893,21 +892,17 @@ int output_flex_t::expire_output_tostring()
893892
{
894893
auto const &expire_output = get_expire_output_from_param();
895894

896-
std::string const str{
897-
fmt::format("osm2pgsql.ExpireOutput[{}]", expire_output.name())};
895+
std::string const str =
896+
fmt::format("osm2pgsql.ExpireOutput[minzoom={},maxzoom={},filename={},"
897+
"schema={},table={}]",
898+
expire_output.minzoom(), expire_output.maxzoom(),
899+
expire_output.filename(), expire_output.schema(),
900+
expire_output.table());
898901
lua_pushstring(lua_state(), str.c_str());
899902

900903
return 1;
901904
}
902905

903-
int output_flex_t::expire_output_name()
904-
{
905-
auto const &expire_output = get_expire_output_from_param();
906-
907-
lua_pushstring(lua_state(), expire_output.name().c_str());
908-
return 1;
909-
}
910-
911906
int output_flex_t::expire_output_minzoom()
912907
{
913908
auto const &expire_output = get_expire_output_from_param();
@@ -1133,8 +1128,7 @@ void output_flex_t::stop()
11331128
std::size_t const count = eo.output(m_expire_tiles[i].get_tiles(),
11341129
get_options()->conninfo);
11351130

1136-
log_info("Wrote {} entries to expired output '{}'.", count,
1137-
eo.name());
1131+
log_info("Wrote {} entries to expire output [{}].", count, i);
11381132
}
11391133
}
11401134
}
@@ -1337,8 +1331,8 @@ output_flex_t::output_flex_t(std::shared_ptr<middle_query_t> const &mid,
13371331
"No tables defined in Lua config. Nothing to do!"};
13381332
}
13391333

1340-
// For backwards compatibility we add a "default" expire output (with
1341-
// empty name) when the relevant command line options are used.
1334+
// For backwards compatibility we add a "default" expire output to all
1335+
// tables when the relevant command line options are used.
13421336
if (options.append && options.expire_tiles_zoom) {
13431337
auto &eo = m_expire_outputs->emplace_back();
13441338
eo.set_filename(options.expire_tiles_filename);
@@ -1359,7 +1353,7 @@ output_flex_t::output_flex_t(std::shared_ptr<middle_query_t> const &mid,
13591353
}
13601354

13611355
write_expire_output_list_to_debug_log(*m_expire_outputs);
1362-
write_table_list_to_debug_log(*m_tables, *m_expire_outputs);
1356+
write_table_list_to_debug_log(*m_tables);
13631357

13641358
for (auto &table : *m_tables) {
13651359
m_table_connections.emplace_back(&table, m_copy_thread);
@@ -1420,7 +1414,6 @@ static void init_expire_output_class(lua_State *lua_state)
14201414
lua_setfield(lua_state, -2, "__index");
14211415
luaX_add_table_func(lua_state, "__tostring",
14221416
lua_trampoline_expire_output_tostring);
1423-
luaX_add_table_func(lua_state, "name", lua_trampoline_expire_output_name);
14241417
luaX_add_table_func(lua_state, "minzoom",
14251418
lua_trampoline_expire_output_minzoom);
14261419
luaX_add_table_func(lua_state, "maxzoom",

0 commit comments

Comments
 (0)