Skip to content

Commit 7cd079d

Browse files
committed
Replace util::string_id_list_t by util::string_joiner_t
1 parent 6a6f91e commit 7cd079d

File tree

4 files changed

+31
-60
lines changed

4 files changed

+31
-60
lines changed

src/middle-pgsql.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
251251
osmium::WayNodeList *nodes) const
252252
{
253253
size_t count = 0;
254-
util::string_id_list_t id_list;
254+
util::string_joiner_t id_list{',', '\0', '{', '}'};
255255

256256
// get nodes where possible from cache,
257257
// at the same time build a list for querying missing nodes from DB
@@ -261,7 +261,7 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
261261
n.set_location(loc);
262262
++count;
263263
} else {
264-
id_list.add(n.ref());
264+
id_list.add(fmt::to_string(n.ref()));
265265
}
266266
}
267267

@@ -271,7 +271,7 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
271271

272272
// get any remaining nodes from the DB
273273
// Nodes must have been written back at this point.
274-
auto const res = m_sql_conn.exec_prepared("get_node_list", id_list.get());
274+
auto const res = m_sql_conn.exec_prepared("get_node_list", id_list());
275275
std::unordered_map<osmid_t, osmium::Location> locs;
276276
for (int i = 0; i < res.num_tuples(); ++i) {
277277
locs.emplace(
@@ -478,16 +478,16 @@ middle_query_pgsql_t::rel_members_get(osmium::Relation const &rel,
478478
idlist_t wayidspg;
479479
if (types & osmium::osm_entity_bits::way) {
480480
// collect ids from all way members into a list..
481-
util::string_id_list_t way_ids;
481+
util::string_joiner_t way_ids{',', '\0', '{', '}'};
482482
for (auto const &member : rel.members()) {
483483
if (member.type() == osmium::item_type::way) {
484-
way_ids.add(member.ref());
484+
way_ids.add(fmt::to_string(member.ref()));
485485
}
486486
}
487487

488488
// ...and get those ways from database
489489
if (!way_ids.empty()) {
490-
res = m_sql_conn.exec_prepared("get_way_list", way_ids.get());
490+
res = m_sql_conn.exec_prepared("get_way_list", way_ids());
491491
wayidspg = get_ids_from_result(res);
492492
}
493493
}

src/util.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@
2121

2222
namespace util {
2323

24-
void string_id_list_t::add(osmid_t id)
25-
{
26-
fmt::format_to(std::back_inserter(m_list), "{},", id);
27-
}
28-
29-
std::string const &string_id_list_t::get()
30-
{
31-
assert(!empty());
32-
m_list.back() = '}';
33-
return m_list;
34-
}
35-
3624
std::string human_readable_duration(uint64_t seconds)
3725
{
3826
if (seconds < 60) {

src/util.hpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,6 @@ class double_to_buffer
6565
std::array<char, buffer_size> m_buffer{};
6666
};
6767

68-
/**
69-
* Class for building a stringified list of ids in the format "{id1,id2,id3}"
70-
* for use in PostgreSQL queries.
71-
*/
72-
class string_id_list_t
73-
{
74-
public:
75-
void add(osmid_t id);
76-
77-
bool empty() const noexcept { return m_list.size() == 1; }
78-
79-
std::string const &get();
80-
81-
private:
82-
std::string m_list{"{"};
83-
84-
}; // class string_id_list_t
85-
8668
/**
8769
* Helper class for timing with a granularity of microseconds. The timer will
8870
* start on construction or it can be started by calling start(). It is stopped
@@ -191,6 +173,14 @@ class string_joiner_t
191173
*/
192174
void add(std::string const &item);
193175

176+
bool empty() const noexcept
177+
{
178+
if (m_before == '\0') {
179+
return m_result.empty();
180+
}
181+
return m_result.size() == 1;
182+
}
183+
194184
/// Return result (as value!)
195185
std::string operator()();
196186

tests/test-util.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@ TEST_CASE("double_to_buffer 3.141", "[NoDB]")
4646
REQUIRE(std::strcmp(buffer.c_str(), "3.141") == 0);
4747
}
4848

49-
TEST_CASE("string_id_list_t with one element", "[NoDB]")
50-
{
51-
util::string_id_list_t list;
52-
REQUIRE(list.empty());
53-
54-
list.add(17);
55-
56-
REQUIRE_FALSE(list.empty());
57-
REQUIRE(list.get() == "{17}");
58-
}
59-
60-
TEST_CASE("string_id_list_t with several elements", "[NoDB]")
61-
{
62-
util::string_id_list_t list;
63-
REQUIRE(list.empty());
64-
65-
list.add(17);
66-
list.add(3);
67-
list.add(99);
68-
69-
REQUIRE_FALSE(list.empty());
70-
REQUIRE(list.get() == "{17,3,99}");
71-
}
72-
7349
TEST_CASE("human readable time durations", "[NoDB]")
7450
{
7551
REQUIRE(util::human_readable_duration(0) == "0s");
@@ -116,28 +92,45 @@ TEST_CASE("find_by_name()", "[NoDB]")
11692
TEST_CASE("Use string_joiner_t with delim only without items", "[NoDB]")
11793
{
11894
util::string_joiner_t joiner{','};
95+
REQUIRE(joiner.empty());
11996
REQUIRE(joiner().empty());
12097
}
12198

12299
TEST_CASE("Use string_joiner_t with all params without items", "[NoDB]")
123100
{
124101
util::string_joiner_t joiner{',', '"', '(', ')'};
102+
REQUIRE(joiner.empty());
125103
REQUIRE(joiner().empty());
126104
}
127105

128106
TEST_CASE("Use string_joiner_t without quote char", "[NoDB]")
129107
{
130108
util::string_joiner_t joiner{',', '\0', '(', ')'};
109+
REQUIRE(joiner.empty());
131110
joiner.add("foo");
111+
REQUIRE_FALSE(joiner.empty());
132112
joiner.add("bar");
113+
REQUIRE_FALSE(joiner.empty());
133114
REQUIRE(joiner() == "(foo,bar)");
134115
}
135116

117+
TEST_CASE("Use string_joiner_t with quote char", "[NoDB]")
118+
{
119+
util::string_joiner_t joiner{',', '-', '(', ')'};
120+
REQUIRE(joiner.empty());
121+
joiner.add("foo");
122+
REQUIRE_FALSE(joiner.empty());
123+
joiner.add("bar");
124+
REQUIRE_FALSE(joiner.empty());
125+
REQUIRE(joiner() == "(-foo-,-bar-)");
126+
}
127+
136128
TEST_CASE("string_joiner_t without before/after", "[NoDB]")
137129
{
138130
util::string_joiner_t joiner{','};
139131
joiner.add("xxx");
140132
joiner.add("yyy");
133+
REQUIRE_FALSE(joiner.empty());
141134
REQUIRE(joiner() == "xxx,yyy");
142135
}
143136

0 commit comments

Comments
 (0)