Skip to content

Commit 523c176

Browse files
authored
Merge pull request #2316 from joto/flat-node-no-create
Do not create flat node files in append mode
2 parents 1264345 + b245ed6 commit 523c176

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

src/middle-pgsql.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ middle_pgsql_t::middle_pgsql_t(std::shared_ptr<thread_pool_t> thread_pool,
12481248
} else {
12491249
m_store_options.use_flat_node_file = true;
12501250
m_persistent_cache = std::make_shared<node_persistent_cache>(
1251-
options->flat_node_file, options->droptemp);
1251+
options->flat_node_file, !options->append, options->droptemp);
12521252
}
12531253

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

src/middle-ram.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ middle_ram_t::middle_ram_t(std::shared_ptr<thread_pool_t> thread_pool,
8080

8181
if (!options->flat_node_file.empty()) {
8282
m_persistent_cache = std::make_shared<node_persistent_cache>(
83-
options->flat_node_file, options->droptemp);
83+
options->flat_node_file, !options->append, options->droptemp);
8484
}
8585
}
8686

src/node-persistent-cache.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@ osmium::Location node_persistent_cache::get(osmid_t id) const noexcept
2929
}
3030

3131
node_persistent_cache::node_persistent_cache(std::string file_name,
32-
bool remove_file)
32+
bool create_file, bool remove_file)
3333
: m_file_name(std::move(file_name)), m_remove_file(remove_file)
3434
{
3535
assert(!m_file_name.empty());
3636

3737
log_debug("Loading persistent node cache from '{}'.", m_file_name);
3838

39-
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
40-
m_fd = open(m_file_name.c_str(), O_RDWR | O_CREAT, 0644);
39+
int flags = O_RDWR; // NOLINT(hicpp-signed-bitwise)
40+
if (create_file) {
41+
flags |= O_CREAT; // NOLINT(hicpp-signed-bitwise)
42+
}
43+
44+
#ifdef _WIN32
45+
flags |= O_BINARY; // NOLINT(hicpp-signed-bitwise)
46+
#endif
47+
48+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
49+
m_fd = open(m_file_name.c_str(), flags, 0644);
4150
if (m_fd < 0) {
4251
throw std::system_error{
4352
errno, std::system_category(),

src/node-persistent-cache.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
class node_persistent_cache
2222
{
2323
public:
24-
node_persistent_cache(std::string file_name, bool remove_file);
24+
node_persistent_cache(std::string file_name, bool create_file,
25+
bool remove_file);
2526
~node_persistent_cache() noexcept;
2627

2728
node_persistent_cache(node_persistent_cache const &) = delete;

tests/bdd/regression/properties.feature

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,25 @@ Feature: Updates to the test database with properties check
6262
| | | Not using flat node file (same as on import). |
6363
| --flat-nodes=x | | Using flat node file |
6464
| --flat-nodes=x | --flat-nodes=x | Using flat node file |
65-
| --flat-nodes=x | --flat-nodes=y | Using the flat node file you specified |
6665
| --prefix=abc | | Using prefix 'abc' (same as on import). |
6766

6867

68+
Scenario: Create, then append with non-existent flat node file
69+
When running osm2pgsql pgsql with parameters
70+
| --slim |
71+
| --flat-nodes=x |
72+
73+
Given the input file '000466354.osc.gz'
74+
Then running osm2pgsql pgsql with parameters fails
75+
| -a |
76+
| --slim |
77+
| --flat-nodes=y |
78+
And the error output contains
79+
"""
80+
Unable to open flatnode file
81+
"""
82+
83+
6984
Scenario: Create with different output than append
7085
When running osm2pgsql pgsql with parameters
7186
| --slim |

tests/test-persistent-cache.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEST_CASE("Persistent cache", "[NoDB]")
4343

4444
// create a new cache
4545
{
46-
node_persistent_cache cache{flat_node_file, false};
46+
node_persistent_cache cache{flat_node_file, true, false};
4747

4848
// write in order
4949
write_and_read_location(&cache, 10, 10.01, -45.3);
@@ -66,7 +66,7 @@ TEST_CASE("Persistent cache", "[NoDB]")
6666

6767
// reopen the cache
6868
{
69-
node_persistent_cache cache{flat_node_file, false};
69+
node_persistent_cache cache{flat_node_file, false, false};
7070

7171
// read all previously written locations
7272
read_location(cache, 10, 10.01, -45.3);
@@ -107,3 +107,12 @@ TEST_CASE("Persistent cache", "[NoDB]")
107107
read_location(cache, 9934, -179.999, 89.1);
108108
}
109109
}
110+
111+
TEST_CASE("Opening non-existent persistent cache should fail in append mode", "[NoDB]")
112+
{
113+
std::string const flat_node_file =
114+
"test_middle_flat.nonexistent.flat.nodes.bin";
115+
testing::cleanup::file_t const flatnode_cleaner{flat_node_file};
116+
117+
REQUIRE_THROWS(node_persistent_cache(flat_node_file, false, false));
118+
}

0 commit comments

Comments
 (0)