Skip to content

Commit b54ca63

Browse files
committed
Move way node lookup out of persistent cache
This code doesn't belong in the node_persistent_cache class. And without it the node_persistent_cache doesn't have to know about the node_ram_cache any more.
1 parent 33dab8e commit b54ca63

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

src/middle-pgsql.cpp

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

272272
}; // class string_id_list
273273

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

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

340359
void middle_pgsql_t::node_delete(osmid_t osm_id)
@@ -757,7 +776,7 @@ middle_pgsql_t::middle_pgsql_t(options_t const *options)
757776
m_db_copy(m_copy_thread)
758777
{
759778
if (options->flat_node_cache_enabled) {
760-
m_persistent_cache.reset(new node_persistent_cache{options, m_cache});
779+
m_persistent_cache.reset(new node_persistent_cache{options});
761780
}
762781

763782
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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,7 @@ osmium::Location node_persistent_cache::get(osmid_t id) const noexcept
1515
static_cast<osmium::unsigned_object_id_type>(id));
1616
}
1717

18-
std::size_t node_persistent_cache::get_list(osmium::WayNodeList *nodes) const
19-
{
20-
std::size_t count = 0;
21-
22-
for (auto &n : *nodes) {
23-
auto loc = m_ram_cache->get(n.ref());
24-
if (!loc.valid() && n.ref() >= 0) {
25-
loc = m_index->get_noexcept(
26-
static_cast<osmium::unsigned_object_id_type>(n.ref()));
27-
}
28-
n.set_location(loc);
29-
if (loc.valid()) {
30-
++count;
31-
}
32-
}
33-
34-
return count;
35-
}
36-
37-
node_persistent_cache::node_persistent_cache(
38-
const options_t *options, std::shared_ptr<node_ram_cache> ptr)
39-
: m_ram_cache(std::move(ptr))
18+
node_persistent_cache::node_persistent_cache(const options_t *options)
4019
{
4120
if (!options->flat_node_file) {
4221
throw std::runtime_error{"Unable to set up persistent cache: the name "

src/node-persistent-cache.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +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:
2623
using index_t =
2724
osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type,
2825
osmium::Location>;
2926

30-
std::shared_ptr<node_ram_cache> m_ram_cache;
3127
int m_fd = -1;
3228
std::unique_ptr<index_t> m_index;
3329
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)