Skip to content

Commit 7d25cd3

Browse files
authored
Merge pull request #1922 from joto/move-expire-tiles-output-funcs1
Move functions for expire tiles output into expire_output_t class
2 parents 4e5bb28 + 1f9147b commit 7d25cd3

File tree

8 files changed

+127
-138
lines changed

8 files changed

+127
-138
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ target_sources(osm2pgsql_lib PRIVATE
55
db-copy.cpp
66
dependency-manager.cpp
77
expire-tiles.cpp
8+
expire-output.cpp
89
gazetteer-style.cpp
910
geom.cpp
1011
geom-box.cpp
@@ -47,7 +48,6 @@ if (WITH_LUA)
4748
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/init.lua" LUA_INIT_CODE)
4849
configure_file(lua-init.cpp.in lua-init.cpp @ONLY)
4950
target_sources(osm2pgsql_lib PRIVATE
50-
expire-output.cpp
5151
flex-index.cpp
5252
flex-table.cpp
5353
flex-table-column.cpp

src/expire-output.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,63 @@
99

1010
#include "expire-output.hpp"
1111

12-
#include "expire-tiles.hpp"
12+
#include "format.hpp"
13+
#include "logging.hpp"
14+
#include "pgsql.hpp"
1315
#include "tile.hpp"
1416

1517
std::size_t expire_output_t::output(quadkey_list_t const &tile_list,
1618
std::string const &conninfo) const
1719
{
1820
std::size_t num = 0;
1921
if (!m_filename.empty()) {
20-
num = output_tiles_to_file(tile_list, m_minzoom, m_maxzoom,
21-
m_filename.c_str());
22+
num = output_tiles_to_file(tile_list);
2223
}
2324
if (!m_table.empty()) {
24-
num = output_tiles_to_table(tile_list, m_minzoom, m_maxzoom, conninfo,
25-
m_schema, m_table);
25+
num = output_tiles_to_table(tile_list, conninfo);
2626
}
2727
return num;
2828
}
29+
30+
std::size_t expire_output_t::output_tiles_to_file(
31+
quadkey_list_t const &tiles_at_maxzoom) const
32+
{
33+
FILE *outfile = std::fopen(m_filename.data(), "a");
34+
if (outfile == nullptr) {
35+
log_warn("Failed to open expired tiles file ({}). Tile expiry "
36+
"list will not be written!",
37+
std::strerror(errno));
38+
return 0;
39+
}
40+
41+
auto const count = for_each_tile(
42+
tiles_at_maxzoom, m_minzoom, m_maxzoom, [&](tile_t const &tile) {
43+
fmt::print(outfile, "{}/{}/{}\n", tile.zoom(), tile.x(), tile.y());
44+
});
45+
46+
(void)std::fclose(outfile);
47+
48+
return count;
49+
}
50+
51+
std::size_t
52+
expire_output_t::output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
53+
std::string const &conninfo) const
54+
{
55+
auto const qn = qualified_name(m_schema, m_table);
56+
57+
pg_conn_t connection{conninfo};
58+
59+
connection.exec("PREPARE insert_tiles(int4, int4, int4) AS"
60+
" INSERT INTO {} (zoom, x, y) VALUES ($1, $2, $3)"
61+
" ON CONFLICT DO NOTHING",
62+
qn);
63+
64+
auto const count = for_each_tile(
65+
tiles_at_maxzoom, m_minzoom, m_maxzoom, [&](tile_t const &tile) {
66+
connection.exec_prepared("insert_tiles", tile.zoom(), tile.x(),
67+
tile.y());
68+
});
69+
70+
return count;
71+
}

src/expire-output.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class expire_output_t
2222
{
2323
public:
24+
expire_output_t() = default;
25+
2426
explicit expire_output_t(std::string name) : m_name(std::move(name)) {}
2527

2628
std::string const &name() const noexcept { return m_name; }
@@ -51,6 +53,23 @@ class expire_output_t
5153
std::size_t output(quadkey_list_t const &tile_list,
5254
std::string const &conninfo) const;
5355

56+
/**
57+
* Write the list of tiles to a file.
58+
*
59+
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
60+
*/
61+
std::size_t
62+
output_tiles_to_file(quadkey_list_t const &tiles_at_maxzoom) const;
63+
64+
/**
65+
* Write the list of tiles to a database table.
66+
*
67+
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
68+
* \param conninfo database connection info
69+
*/
70+
std::size_t output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
71+
std::string const &conninfo) const;
72+
5473
private:
5574
/// The internal (unique) name of the output
5675
std::string m_name;

src/expire-tiles.cpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "expire-tiles.hpp"
2828
#include "format.hpp"
2929
#include "geom-functions.hpp"
30-
#include "logging.hpp"
3130
#include "options.hpp"
3231
#include "reprojection.hpp"
3332
#include "table.hpp"
@@ -292,52 +291,6 @@ void expire_tiles::merge_and_destroy(expire_tiles *other)
292291
}
293292
}
294293

295-
std::size_t output_tiles_to_file(quadkey_list_t const &tiles_at_maxzoom,
296-
uint32_t minzoom, uint32_t maxzoom,
297-
std::string_view filename)
298-
{
299-
FILE *outfile = std::fopen(filename.data(), "a");
300-
if (outfile == nullptr) {
301-
log_warn("Failed to open expired tiles file ({}). Tile expiry "
302-
"list will not be written!",
303-
std::strerror(errno));
304-
return 0;
305-
}
306-
307-
auto const count = for_each_tile(
308-
tiles_at_maxzoom, minzoom, maxzoom, [&](tile_t const &tile) {
309-
fmt::print(outfile, "{}/{}/{}\n", tile.zoom(), tile.x(), tile.y());
310-
});
311-
312-
(void)std::fclose(outfile);
313-
314-
return count;
315-
}
316-
317-
std::size_t output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
318-
uint32_t minzoom, uint32_t maxzoom,
319-
std::string const &conninfo,
320-
std::string const &schema,
321-
std::string const &table)
322-
{
323-
auto const qn = qualified_name(schema, table);
324-
325-
pg_conn_t connection{conninfo};
326-
327-
connection.exec("PREPARE insert_tiles(int4, int4, int4) AS"
328-
" INSERT INTO {} (zoom, x, y) VALUES ($1, $2, $3)"
329-
" ON CONFLICT DO NOTHING",
330-
qn);
331-
332-
auto const count = for_each_tile(
333-
tiles_at_maxzoom, minzoom, maxzoom, [&](tile_t const &tile) {
334-
connection.exec_prepared("insert_tiles", tile.zoom(), tile.x(),
335-
tile.y());
336-
});
337-
338-
return count;
339-
}
340-
341294
int expire_from_result(expire_tiles *expire, pg_result_t const &result,
342295
expire_config_t const &expire_config)
343296
{

src/expire-tiles.hpp

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -134,84 +134,4 @@ class expire_tiles
134134
int expire_from_result(expire_tiles *expire, pg_result_t const &result,
135135
expire_config_t const &expire_config);
136136

137-
/**
138-
* Iterate over tiles and call output function for each tile on all requested
139-
* zoom levels.
140-
*
141-
* \tparam OUTPUT Class with operator() taking a tile_t argument
142-
*
143-
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
144-
* \param minzoom Minimum zoom level
145-
* \param maxzoom Maximum zoom level
146-
* \param output Output function
147-
*/
148-
template <class OUTPUT>
149-
std::size_t for_each_tile(quadkey_list_t const &tiles_at_maxzoom,
150-
uint32_t minzoom, uint32_t maxzoom, OUTPUT &&output)
151-
{
152-
assert(minzoom <= maxzoom);
153-
154-
if (minzoom == maxzoom) {
155-
for (auto const quadkey : tiles_at_maxzoom) {
156-
std::forward<OUTPUT>(output)(
157-
tile_t::from_quadkey(quadkey, maxzoom));
158-
}
159-
return tiles_at_maxzoom.size();
160-
}
161-
162-
/**
163-
* Loop over all requested zoom levels (from maximum down to the minimum
164-
* zoom level).
165-
*/
166-
quadkey_t last_quadkey{};
167-
std::size_t count = 0;
168-
for (auto const quadkey : tiles_at_maxzoom) {
169-
for (uint32_t dz = 0; dz <= maxzoom - minzoom; ++dz) {
170-
auto const qt_current = quadkey.down(dz);
171-
/**
172-
* If dz > 0, there are probably multiple elements whose quadkey
173-
* is equal because they are all sub-tiles of the same tile at the
174-
* current zoom level. We skip all of them after we have written
175-
* the first sibling.
176-
*/
177-
if (qt_current != last_quadkey.down(dz)) {
178-
std::forward<OUTPUT>(output)(
179-
tile_t::from_quadkey(qt_current, maxzoom - dz));
180-
++count;
181-
}
182-
}
183-
last_quadkey = quadkey;
184-
}
185-
return count;
186-
}
187-
188-
/**
189-
* Write the list of tiles to a file.
190-
*
191-
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
192-
* \param minzoom Minimum zoom level
193-
* \param maxzoom Maximum zoom level
194-
* \param filename Name of the file
195-
*/
196-
std::size_t output_tiles_to_file(quadkey_list_t const &tiles_at_maxzoom,
197-
uint32_t minzoom, uint32_t maxzoom,
198-
std::string_view filename);
199-
200-
/**
201-
* Write the list of tiles to a database table. The table will be created
202-
* if it doesn't exist already.
203-
*
204-
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
205-
* \param minzoom Minimum zoom level
206-
* \param maxzoom Maximum zoom level
207-
* \param conninfo database connection info
208-
* \param schema The schema the table is in (empty for public schema)
209-
* \param table The table name
210-
*/
211-
std::size_t output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
212-
uint32_t minzoom, uint32_t maxzoom,
213-
std::string const &conninfo,
214-
std::string const &schema,
215-
std::string const &table);
216-
217137
#endif // OSM2PGSQL_EXPIRE_TILES_HPP

src/output-flex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ output_flex_t::output_flex_t(std::shared_ptr<middle_query_t> const &mid,
13391339
// For backwards compatibility we add a "default" expire output (with
13401340
// empty name) when the relevant command line options are used.
13411341
if (options.append && options.expire_tiles_zoom) {
1342-
auto &eo = m_expire_outputs->emplace_back("");
1342+
auto &eo = m_expire_outputs->emplace_back();
13431343
eo.set_filename(options.expire_tiles_filename);
13441344
eo.set_minzoom(options.expire_tiles_zoom_min);
13451345
eo.set_maxzoom(options.expire_tiles_zoom);

src/output-pgsql.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <cstdlib>
2929
#include <cstring>
3030

31+
#include "expire-output.hpp"
3132
#include "expire-tiles.hpp"
3233
#include "geom-from-osm.hpp"
3334
#include "geom-functions.hpp"
@@ -146,10 +147,12 @@ void output_pgsql_t::stop()
146147
}
147148

148149
if (get_options()->expire_tiles_zoom_min > 0) {
149-
auto const count = output_tiles_to_file(
150-
m_expire.get_tiles(), get_options()->expire_tiles_zoom_min,
151-
get_options()->expire_tiles_zoom,
152-
get_options()->expire_tiles_filename);
150+
expire_output_t expire_out;
151+
expire_out.set_filename(get_options()->expire_tiles_filename);
152+
expire_out.set_minzoom(get_options()->expire_tiles_zoom_min);
153+
expire_out.set_maxzoom(get_options()->expire_tiles_zoom);
154+
auto const count =
155+
expire_out.output_tiles_to_file(m_expire.get_tiles());
153156
log_info("Wrote {} entries to expired tiles list", count);
154157
}
155158
}

src/tile.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,55 @@ class tile_t
215215
uint32_t m_zoom = invalid_zoom;
216216
}; // class tile_t
217217

218+
/**
219+
* Iterate over tiles and call output function for each tile on all requested
220+
* zoom levels.
221+
*
222+
* \tparam OUTPUT Class with operator() taking a tile_t argument
223+
*
224+
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
225+
* \param minzoom Minimum zoom level
226+
* \param maxzoom Maximum zoom level
227+
* \param output Output function
228+
*/
229+
template <class OUTPUT>
230+
std::size_t for_each_tile(quadkey_list_t const &tiles_at_maxzoom,
231+
uint32_t minzoom, uint32_t maxzoom, OUTPUT &&output)
232+
{
233+
assert(minzoom <= maxzoom);
234+
235+
if (minzoom == maxzoom) {
236+
for (auto const quadkey : tiles_at_maxzoom) {
237+
std::forward<OUTPUT>(output)(
238+
tile_t::from_quadkey(quadkey, maxzoom));
239+
}
240+
return tiles_at_maxzoom.size();
241+
}
242+
243+
/**
244+
* Loop over all requested zoom levels (from maximum down to the minimum
245+
* zoom level).
246+
*/
247+
quadkey_t last_quadkey{};
248+
std::size_t count = 0;
249+
for (auto const quadkey : tiles_at_maxzoom) {
250+
for (uint32_t dz = 0; dz <= maxzoom - minzoom; ++dz) {
251+
auto const qt_current = quadkey.down(dz);
252+
/**
253+
* If dz > 0, there are probably multiple elements whose quadkey
254+
* is equal because they are all sub-tiles of the same tile at the
255+
* current zoom level. We skip all of them after we have written
256+
* the first sibling.
257+
*/
258+
if (qt_current != last_quadkey.down(dz)) {
259+
std::forward<OUTPUT>(output)(
260+
tile_t::from_quadkey(qt_current, maxzoom - dz));
261+
++count;
262+
}
263+
}
264+
last_quadkey = quadkey;
265+
}
266+
return count;
267+
}
268+
218269
#endif // OSM2PGSQL_TILE_HPP

0 commit comments

Comments
 (0)