Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit d88b84c

Browse files
committed
fix buffer bug
1 parent 74f7916 commit d88b84c

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/influxdb-cpp-rest/deflate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace web::http::compression::builtin;
1313

1414
namespace influxdb {
1515
namespace utility {
16-
void compress(std::shared_ptr<fmt::MemoryWriter> w, std::vector<uint8_t> &compression_buffer) {
16+
int compress(std::shared_ptr<fmt::MemoryWriter> w, std::vector<uint8_t> &compression_buffer) {
1717
auto c = make_compressor(algorithm::GZIP);
1818
const int rawdata_size = w->size();
1919
const uint8_t* rawdata_ptr =
@@ -49,6 +49,7 @@ namespace influxdb {
4949
rawdata_cursor += used;
5050
}
5151
compression_buffer.resize(compressed_size);
52+
return compressed_size;
5253
}
5354
}
5455
}

src/influxdb-cpp-rest/deflate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace influxdb {
77
namespace utility {
8-
void compress(std::shared_ptr<fmt::MemoryWriter> w, std::vector<uint8_t> &compression_buffer);
8+
int compress(std::shared_ptr<fmt::MemoryWriter> w, std::vector<uint8_t> &compression_buffer);
99
}
1010
}
1111

src/influxdb-cpp-rest/influxdb_raw_db.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ namespace {
3030

3131
inline http_request request_from(
3232
uri const& uri_with_db,
33-
std::shared_ptr<fmt::MemoryWriter> lines,
3433
std::string const& username,
3534
std::string const& password,
3635
bool deflate,
@@ -63,16 +62,6 @@ namespace {
6362
)
6463
;
6564
}
66-
67-
if (lines->size() > 0 && deflate) {
68-
std::vector<uint8_t> buffer;
69-
influxdb::utility::compress(lines, buffer);
70-
request.headers().add(header_names::content_encoding, algorithm::GZIP);
71-
request.set_body(concurrency::streams::rawptr_stream<uint8_t>::open_istream(
72-
buffer.data(), buffer.size()));
73-
} else {
74-
request.set_body(lines->str());
75-
}
7665

7766
return request;
7867
}
@@ -94,11 +83,10 @@ void influxdb::raw::db::post(string_t const & query)
9483

9584
builder.append_query(U("q"), query);
9685

97-
auto w = std::make_shared<fmt::MemoryWriter>();
9886
// synchronous for now
99-
auto response = client.request(
100-
request_from(builder.to_string(), w, username, password, deflate, retention_policy)
101-
);
87+
auto request = request_from(builder.to_string(), username, password, deflate, retention_policy);
88+
request.set_body("");
89+
auto response = client.request(request);
10290

10391
try {
10492
response.wait();
@@ -115,12 +103,11 @@ string_t influxdb::raw::db::get(string_t const & query)
115103
uri_builder builder(U("/query"));
116104

117105
builder.append_query(U("q"), query);
118-
auto w = std::make_shared<fmt::MemoryWriter>();
119106

107+
auto request = request_from(builder.to_string(), username, password, deflate, retention_policy);
108+
request.set_body("");
120109
// synchronous for now
121-
auto response = client.request(
122-
request_from(builder.to_string(), w, username, password, deflate, retention_policy)
123-
);
110+
auto response = client.request(request);
124111

125112
try {
126113
response.wait();
@@ -140,7 +127,17 @@ string_t influxdb::raw::db::get(string_t const & query)
140127

141128
void influxdb::raw::db::insert(std::shared_ptr<fmt::MemoryWriter> lines)
142129
{
143-
auto response = client.request(request_from(uri_with_db, lines, username, password, deflate, retention_policy));
130+
auto request = request_from(uri_with_db, username, password, deflate, retention_policy);
131+
std::vector<uint8_t> buffer; // needs to live until sending the request
132+
if (lines->size() > 0 && deflate) {
133+
int size = influxdb::utility::compress(lines, buffer);
134+
request.headers().add(header_names::content_encoding, algorithm::GZIP);
135+
request.set_body(concurrency::streams::rawptr_stream<uint8_t>::open_istream(
136+
buffer.data(), size));
137+
} else {
138+
request.set_body(lines->str());
139+
}
140+
auto response = client.request(request);
144141

145142
try {
146143
response.wait();

0 commit comments

Comments
 (0)