Skip to content

Commit b27e80c

Browse files
authored
Merge pull request #1365 from joto/cleanup-persistent-cache
Cleanup persistent cache
2 parents 70fec11 + b54ca63 commit b27e80c

File tree

5 files changed

+30
-44
lines changed

5 files changed

+30
-44
lines changed

src/middle-pgsql.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ class string_id_list
270270

271271
}; // class string_id_list
272272

273-
size_t
274-
middle_query_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
273+
std::size_t middle_query_pgsql_t::get_way_node_locations_db(
274+
osmium::WayNodeList *nodes) const
275275
{
276276
size_t count = 0;
277277
string_id_list id_list;
@@ -330,10 +330,29 @@ void middle_pgsql_t::node_set(osmium::Node const &node)
330330
}
331331
}
332332

333+
std::size_t middle_query_pgsql_t::get_way_node_locations_flatnodes(
334+
osmium::WayNodeList *nodes) const
335+
{
336+
std::size_t count = 0;
337+
338+
for (auto &n : *nodes) {
339+
auto loc = m_cache->get(n.ref());
340+
if (!loc.valid() && n.ref() >= 0) {
341+
loc = m_persistent_cache->get(n.ref());
342+
}
343+
n.set_location(loc);
344+
if (loc.valid()) {
345+
++count;
346+
}
347+
}
348+
349+
return count;
350+
}
351+
333352
size_t middle_query_pgsql_t::nodes_get_list(osmium::WayNodeList *nodes) const
334353
{
335-
return m_persistent_cache ? m_persistent_cache->get_list(nodes)
336-
: local_nodes_get_list(nodes);
354+
return m_persistent_cache ? get_way_node_locations_flatnodes(nodes)
355+
: get_way_node_locations_db(nodes);
337356
}
338357

339358
void middle_pgsql_t::node_delete(osmid_t osm_id)
@@ -756,7 +775,7 @@ middle_pgsql_t::middle_pgsql_t(options_t const *options)
756775
m_db_copy(m_copy_thread)
757776
{
758777
if (options->flat_node_cache_enabled) {
759-
m_persistent_cache.reset(new node_persistent_cache{options, m_cache});
778+
m_persistent_cache.reset(new node_persistent_cache{options});
760779
}
761780

762781
log_debug("Mid: pgsql, cache={}", options->cache);

src/middle-pgsql.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class middle_query_pgsql_t : public middle_query_t
3737
void exec_sql(std::string const &sql_cmd) const;
3838

3939
private:
40-
size_t local_nodes_get_list(osmium::WayNodeList *nodes) const;
40+
std::size_t get_way_node_locations_flatnodes(osmium::WayNodeList *nodes) const;
41+
std::size_t get_way_node_locations_db(osmium::WayNodeList *nodes) const;
4142

4243
pg_conn_t m_sql_conn;
4344
std::shared_ptr<node_ram_cache> m_cache;

src/node-persistent-cache.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,16 @@
66

77
void node_persistent_cache::set(osmid_t id, osmium::Location location)
88
{
9-
if (id < 0) {
10-
throw std::runtime_error{"Flatnode store cannot save negative IDs."};
11-
}
129
m_index->set(static_cast<osmium::unsigned_object_id_type>(id), location);
1310
}
1411

1512
osmium::Location node_persistent_cache::get(osmid_t id) const noexcept
1613
{
17-
if (id < 0) {
18-
return osmium::Location{};
19-
}
20-
2114
return m_index->get_noexcept(
2215
static_cast<osmium::unsigned_object_id_type>(id));
2316
}
2417

25-
std::size_t node_persistent_cache::get_list(osmium::WayNodeList *nodes) const
26-
{
27-
std::size_t count = 0;
28-
29-
for (auto &n : *nodes) {
30-
auto loc = m_ram_cache->get(n.ref());
31-
if (!loc.valid() && n.ref() >= 0) {
32-
loc = m_index->get_noexcept(
33-
static_cast<osmium::unsigned_object_id_type>(n.ref()));
34-
}
35-
n.set_location(loc);
36-
if (loc.valid()) {
37-
++count;
38-
}
39-
}
40-
41-
return count;
42-
}
43-
44-
node_persistent_cache::node_persistent_cache(
45-
const options_t *options, std::shared_ptr<node_ram_cache> ptr)
46-
: m_ram_cache(std::move(ptr))
18+
node_persistent_cache::node_persistent_cache(const options_t *options)
4719
{
4820
if (!options->flat_node_file) {
4921
throw std::runtime_error{"Unable to set up persistent cache: the name "

src/node-persistent-cache.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,24 @@
66
#include <osmium/index/map/dense_file_array.hpp>
77
#include <osmium/osm/location.hpp>
88

9-
#include "node-ram-cache.hpp"
109
#include "osmtypes.hpp"
1110

1211
class options_t;
1312

1413
class node_persistent_cache
1514
{
1615
public:
17-
node_persistent_cache(options_t const *options,
18-
std::shared_ptr<node_ram_cache> ptr);
16+
node_persistent_cache(options_t const *options);
1917
~node_persistent_cache() noexcept;
2018

2119
void set(osmid_t id, osmium::Location location);
2220
osmium::Location get(osmid_t id) const noexcept;
23-
std::size_t get_list(osmium::WayNodeList *nodes) const;
2421

2522
private:
26-
// Dense node cache for unsigned IDs only
2723
using index_t =
2824
osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type,
2925
osmium::Location>;
3026

31-
std::shared_ptr<node_ram_cache> m_ram_cache;
3227
int m_fd = -1;
3328
std::unique_ptr<index_t> m_index;
3429
bool m_remove_file = false;

tests/test-persistent-cache.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ TEST_CASE("Persistent cache", "[NoDB]")
2929
{
3030
options_t const options = testing::opt_t().flatnodes();
3131
testing::cleanup::file_t flatnode_cleaner{options.flat_node_file.get()};
32-
auto ram_cache = std::make_shared<node_ram_cache>(); // dummy cache
3332

3433
// create a new cache
3534
{
36-
node_persistent_cache cache{&options, ram_cache};
35+
node_persistent_cache cache{&options};
3736

3837
// write in order
3938
write_and_read_location(cache, 10, 10.01, -45.3);
@@ -56,7 +55,7 @@ TEST_CASE("Persistent cache", "[NoDB]")
5655

5756
// reopen the cache
5857
{
59-
node_persistent_cache cache{&options, ram_cache};
58+
node_persistent_cache cache{&options};
6059

6160
// read all previously written locations
6261
read_location(cache, 10, 10.01, -45.3);

0 commit comments

Comments
 (0)