forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-persistent-cache.cpp
More file actions
60 lines (50 loc) · 1.62 KB
/
Copy pathnode-persistent-cache.cpp
File metadata and controls
60 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2021 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "logging.hpp"
#include "node-persistent-cache.hpp"
#include <cassert>
#include <cstring>
#include <stdexcept>
void node_persistent_cache::set(osmid_t id, osmium::Location location)
{
m_index->set(static_cast<osmium::unsigned_object_id_type>(id), location);
}
osmium::Location node_persistent_cache::get(osmid_t id) const noexcept
{
return m_index->get_noexcept(
static_cast<osmium::unsigned_object_id_type>(id));
}
node_persistent_cache::node_persistent_cache(std::string file_name,
bool remove_file)
: m_file_name(std::move(file_name)), m_remove_file(remove_file)
{
assert(!m_file_name.empty());
log_debug("Loading persistent node cache from '{}'.", m_file_name);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
m_fd = open(m_file_name.c_str(), O_RDWR | O_CREAT, 0644);
if (m_fd < 0) {
throw std::runtime_error{"Unable to open flatnode file '{}': {}"_format(
m_file_name, std::strerror(errno))};
}
m_index = std::make_unique<index_t>(m_fd);
}
node_persistent_cache::~node_persistent_cache() noexcept
{
m_index.reset();
if (m_fd >= 0) {
close(m_fd);
}
if (m_remove_file) {
try {
log_debug("Removing persistent node cache at '{}'.", m_file_name);
} catch (...) {
}
unlink(m_file_name.c_str());
}
}