77//
88
99#include " influxdb_raw_db.h"
10+ #include " deflate.h"
1011
12+ #include < fmt/ostream.h>
1113#include < cpprest/streams.h>
1214#include < cpprest/http_client.h>
15+ #include < cpprest/rawptrstream.h>
1316
1417using namespace utility ;
1518using namespace web ;
1619using namespace web ::http;
20+ using namespace web ::http::compression::builtin;
1721
1822namespace {
1923 inline void throw_response (http_response const & response) {
@@ -26,9 +30,9 @@ namespace {
2630
2731 inline http_request request_from (
2832 uri const & uri_with_db,
29- std::string const & lines,
3033 std::string const & username,
3134 std::string const & password,
35+ bool deflate,
3236 std::string const & retention_policy = " " ,
3337 web::http::method const & m = methods::POST
3438 ) {
@@ -58,16 +62,14 @@ namespace {
5862 )
5963 ;
6064 }
61-
62- request.set_body (lines);
63-
65+
6466 return request;
6567 }
6668}
6769
68- influxdb::raw::db::db (string_t const & url, string_t const & name)
70+ influxdb::raw::db::db (string_t const & url, string_t const & name, bool deflate )
6971 :
70- client(url)
72+ client(url), deflate(deflate)
7173{
7274 uri_builder builder (client.base_uri ());
7375 builder.append (U (" /write" ));
@@ -82,9 +84,9 @@ void influxdb::raw::db::post(string_t const & query)
8284 builder.append_query (U (" q" ), query);
8385
8486 // synchronous for now
85- auto response = client. request (
86- request_from (builder. to_string (), " " , username, password, retention_policy)
87- );
87+ auto request = request_from (builder. to_string (), username, password, deflate, retention_policy);
88+ request. set_body ( " " );
89+ auto response = client. request (request );
8890
8991 try {
9092 response.wait ();
@@ -102,10 +104,10 @@ string_t influxdb::raw::db::get(string_t const & query)
102104
103105 builder.append_query (U (" q" ), query);
104106
107+ auto request = request_from (builder.to_string (), username, password, deflate, retention_policy);
108+ request.set_body (" " );
105109 // synchronous for now
106- auto response = client.request (
107- request_from (builder.to_string (), " " , username, password, retention_policy)
108- );
110+ auto response = client.request (request);
109111
110112 try {
111113 response.wait ();
@@ -123,9 +125,19 @@ string_t influxdb::raw::db::get(string_t const & query)
123125 }
124126}
125127
126- void influxdb::raw::db::insert (std::string const & lines)
128+ void influxdb::raw::db::insert (std::shared_ptr<fmt::MemoryWriter> const & lines)
127129{
128- auto response = client.request (request_from (uri_with_db, lines, username, password, 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);
129141
130142 try {
131143 response.wait ();
@@ -138,7 +150,7 @@ void influxdb::raw::db::insert(std::string const & lines)
138150}
139151
140152// synchronous for now
141- void influxdb::raw::db::insert_async (std::string const & lines)
153+ void influxdb::raw::db::insert_async (std::shared_ptr<fmt::MemoryWriter> const & lines)
142154{
143155 insert (lines);
144156}
0 commit comments