Skip to content

Commit f32af37

Browse files
authored
fix: C++ API improvements. (#29)
* Fixed linkage issue (some inline keywords missing). * Removed allocation from line_sender_buffer's constructor for better interoperability with APIs of concurrent queues.
1 parent 2cd4e7f commit f32af37

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

cpp_test/test_line_sender.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ TEST_CASE("Buffer move and copy ctor testing")
380380
CHECK(buffer1.peek() == "buffer3");
381381
CHECK(buffer3.peek() == "");
382382
CHECK(buffer3.size() == 0);
383-
CHECK(buffer3.capacity() == 3 * init_capacity);
383+
CHECK(buffer3.capacity() == 0);
384384
CHECK(buffer3.peek() == "");
385385

386386

@@ -649,4 +649,42 @@ TEST_CASE("Test Marker")
649649
buffer.clear();
650650
CHECK(buffer.peek() == "");
651651
CHECK_THROWS_AS(buffer.rewind_to_marker(), questdb::ilp::line_sender_error);
652+
}
653+
654+
TEST_CASE("Moved View") {
655+
auto v1 = "abc"_tn;
656+
CHECK(v1.size() == 3);
657+
questdb::ilp::table_name_view v2{std::move(v1)};
658+
CHECK(v2.size() == 3);
659+
CHECK(v1.size() == 3);
660+
CHECK(v1.data() == v2.data());
661+
}
662+
663+
TEST_CASE("Empty Buffer") {
664+
questdb::ilp::line_sender_buffer b1;
665+
CHECK(b1.size() == 0);
666+
questdb::ilp::line_sender_buffer b2{std::move(b1)};
667+
CHECK(b1.size() == 0);
668+
CHECK(b2.size() == 0);
669+
questdb::ilp::line_sender_buffer b3;
670+
b3 = std::move(b2);
671+
CHECK(b2.size() == 0);
672+
CHECK(b3.size() == 0);
673+
questdb::ilp::line_sender_buffer b4;
674+
b4.table("test").symbol("a", "b").at_now();
675+
questdb::ilp::line_sender_buffer b5;
676+
b5 = std::move(b4);
677+
CHECK(b4.size() == 0);
678+
CHECK(b5.size() == 9);
679+
680+
questdb::ilp::test::mock_server server;
681+
questdb::ilp::line_sender sender{"localhost", server.port()};
682+
CHECK_THROWS_WITH_AS(
683+
sender.flush(b1),
684+
"State error: Bad call to `flush`, should have called `table` instead.",
685+
questdb::ilp::line_sender_error);
686+
CHECK_THROWS_WITH_AS(
687+
sender.flush_and_keep(b1),
688+
"State error: Bad call to `flush`, should have called `table` instead.",
689+
questdb::ilp::line_sender_error);
652690
}

include/questdb/ilp/line_sender.hpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ namespace questdb::ilp
195195
* auto validated = "A UTF-8 encoded string"_utf8;
196196
* @endcode
197197
*/
198-
utf8_view operator "" _utf8(const char* buf, size_t len)
198+
inline utf8_view operator "" _utf8(const char* buf, size_t len)
199199
{
200200
return utf8_view{buf, len};
201201
}
@@ -206,7 +206,7 @@ namespace questdb::ilp
206206
* auto table_name = "events"_tn;
207207
* @endcode
208208
*/
209-
table_name_view operator "" _tn(const char* buf, size_t len)
209+
inline table_name_view operator "" _tn(const char* buf, size_t len)
210210
{
211211
return table_name_view{buf, len};
212212
}
@@ -217,7 +217,7 @@ namespace questdb::ilp
217217
* auto column_name = "events"_cn;
218218
* @endcode
219219
*/
220-
column_name_view operator "" _cn(const char* buf, size_t len)
220+
inline column_name_view operator "" _cn(const char* buf, size_t len)
221221
{
222222
return column_name_view{buf, len};
223223
}
@@ -271,11 +271,10 @@ namespace questdb::ilp
271271
line_sender_buffer(
272272
size_t init_capacity,
273273
size_t max_name_len) noexcept
274-
: _impl{::line_sender_buffer_with_max_name_len(max_name_len)}
274+
: _impl{nullptr}
275275
, _init_capacity{init_capacity}
276276
, _max_name_len{max_name_len}
277277
{
278-
::line_sender_buffer_reserve(_impl, init_capacity);
279278
}
280279

281280
line_sender_buffer(const line_sender_buffer& other) noexcept
@@ -297,7 +296,10 @@ namespace questdb::ilp
297296
if (this != &other)
298297
{
299298
::line_sender_buffer_free(_impl);
300-
_impl = ::line_sender_buffer_clone(other._impl);
299+
if (other._impl)
300+
_impl = ::line_sender_buffer_clone(other._impl);
301+
else
302+
_impl = nullptr;
301303
_init_capacity = other._init_capacity;
302304
_max_name_len = other._max_name_len;
303305
}
@@ -333,13 +335,9 @@ namespace questdb::ilp
333335
size_t capacity() const noexcept
334336
{
335337
if (_impl)
336-
{
337338
return ::line_sender_buffer_capacity(_impl);
338-
}
339339
else
340-
{
341-
return _init_capacity;
342-
}
340+
return 0;
343341
}
344342

345343
size_t size() const noexcept
@@ -381,17 +379,13 @@ namespace questdb::ilp
381379
void clear_marker() noexcept
382380
{
383381
if (_impl)
384-
{
385382
::line_sender_buffer_clear_marker(_impl);
386-
}
387383
}
388384

389385
void clear() noexcept
390386
{
391387
if (_impl)
392-
{
393388
::line_sender_buffer_clear(_impl);
394-
}
395389
}
396390

397391
/**
@@ -566,7 +560,7 @@ namespace questdb::ilp
566560
::line_sender_buffer_free(_impl);
567561
}
568562
private:
569-
void may_init()
563+
inline void may_init()
570564
{
571565
if (!_impl)
572566
{
@@ -788,6 +782,7 @@ namespace questdb::ilp
788782
*/
789783
void flush(line_sender_buffer& buffer)
790784
{
785+
buffer.may_init();
791786
ensure_impl();
792787
line_sender_error::wrapped_call(
793788
::line_sender_flush,
@@ -803,11 +798,23 @@ namespace questdb::ilp
803798
*/
804799
void flush_and_keep(const line_sender_buffer& buffer)
805800
{
806-
ensure_impl();
807-
line_sender_error::wrapped_call(
808-
::line_sender_flush_and_keep,
809-
_impl,
810-
buffer._impl);
801+
if (buffer._impl)
802+
{
803+
ensure_impl();
804+
line_sender_error::wrapped_call(
805+
::line_sender_flush_and_keep,
806+
_impl,
807+
buffer._impl);
808+
}
809+
else
810+
{
811+
line_sender_buffer buffer2{0};
812+
buffer2.may_init();
813+
line_sender_error::wrapped_call(
814+
::line_sender_flush_and_keep,
815+
_impl,
816+
buffer2._impl);
817+
}
811818
}
812819

813820
/**

0 commit comments

Comments
 (0)