Skip to content

Commit 9a7c692

Browse files
committed
C API extended for #8
1 parent d56133c commit 9a7c692

File tree

8 files changed

+825
-3
lines changed

8 files changed

+825
-3
lines changed

scripts/test.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ BIN_DIRS=(
3232
"${BUILD_DIR}"
3333
)
3434

35-
# Set library path
36-
export LD_LIBRARY_PATH="${BUILD_DIR}:${LD_LIBRARY_PATH:-}"
37-
export DYLD_LIBRARY_PATH="${BUILD_DIR}:${DYLD_LIBRARY_PATH:-}"
35+
# Set library path - prefer bin directories first to pick up latest built dylibs
36+
BIN_RELEASE="${BUILD_DIR}/bin/${BUILD_TYPE}"
37+
BIN_GENERIC="${BUILD_DIR}/bin"
38+
export LD_LIBRARY_PATH="${BIN_RELEASE}:${BIN_GENERIC}:${BUILD_DIR}:${LD_LIBRARY_PATH:-}"
39+
export DYLD_LIBRARY_PATH="${BIN_RELEASE}:${BIN_GENERIC}:${BUILD_DIR}:${DYLD_LIBRARY_PATH:-}"
3840

3941
# Also add build directory to PATH for shared libraries
4042
export PATH="${BUILD_DIR}:${PATH}"

src/influx-c-rest/influx_c_rest_async.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
//
88

99
#include "influx_c_rest_async.h"
10+
#include "influx_c_rest_config.h"
11+
#include "influx_c_rest_lines.h"
1012

1113
#include "../influxdb-cpp-rest/influxdb_simple_api.h"
1214
#include "../influxdb-cpp-rest/influxdb_simple_async_api.h"
1315
#include "../influxdb-cpp-rest/influxdb_line.h"
16+
#include "../influxdb-cpp-rest/influxdb_config.h"
1417

1518
#include <memory>
1619
#include <cassert>
@@ -52,6 +55,37 @@ extern "C" {
5255
return res;
5356
}
5457

58+
extern "C" INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new_config(const char* url, const char* name, influx_c_rest_config_t * config) {
59+
assert(url);
60+
assert(name);
61+
assert(config);
62+
63+
try {
64+
void* config_ptr = influx_c_rest_config_get_internal(config);
65+
influxdb::api::db_config* cpp_config = static_cast<influxdb::api::db_config*>(config_ptr);
66+
influx_c_rest_async_t *res = new influx_c_rest_async_t {
67+
std::make_unique<influxdb::async_api::simple_db>(url, name, *cpp_config)
68+
};
69+
70+
assert(res);
71+
return res;
72+
} catch (std::exception& e) {
73+
std::cerr << e.what() << std::endl;
74+
return nullptr;
75+
}
76+
}
77+
78+
extern "C" INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new_auth_config(const char* url, const char* name, const char* username, const char* password, influx_c_rest_config_t * config) {
79+
assert(username);
80+
assert(password);
81+
assert(config);
82+
auto res = influx_c_rest_async_new_config(url, name, config);
83+
if (res) {
84+
res->asyncdb->with_authentication(username, password);
85+
}
86+
return res;
87+
}
88+
5589
extern "C" INFLUX_C_REST int influx_c_rest_async_drop(influx_c_rest_async_t * self) {
5690
assert(self);
5791
assert(self->asyncdb.get());
@@ -93,4 +127,33 @@ extern "C" {
93127
assert(line);
94128
self->asyncdb->insert(influxdb::api::line(std::string(line), self->timestamp));
95129
}
130+
131+
extern "C" INFLUX_C_REST void influx_c_rest_async_insert_lines(influx_c_rest_async_t * self, influx_c_rest_lines_t * lines) {
132+
assert(self);
133+
assert(self->asyncdb.get());
134+
assert(lines);
135+
void* line_ptr = influx_c_rest_lines_get_internal(lines);
136+
influxdb::api::line* line_obj = static_cast<influxdb::api::line*>(line_ptr);
137+
self->asyncdb->insert(*line_obj);
138+
}
139+
140+
extern "C" INFLUX_C_REST void influx_c_rest_async_insert_lines_default_timestamp(influx_c_rest_async_t * self, influx_c_rest_lines_t * lines) {
141+
assert(self);
142+
assert(self->asyncdb.get());
143+
assert(lines);
144+
void* line_ptr = influx_c_rest_lines_get_internal(lines);
145+
influxdb::api::line* line_obj = static_cast<influxdb::api::line*>(line_ptr);
146+
influxdb::api::line line_with_timestamp(line_obj->get(), self->timestamp);
147+
self->asyncdb->insert(line_with_timestamp);
148+
}
149+
150+
extern "C" INFLUX_C_REST void influx_c_rest_async_wait_quiet_ms(influx_c_rest_async_t * self, unsigned quiet_period_ms) {
151+
assert(self);
152+
assert(self->asyncdb.get());
153+
try {
154+
self->asyncdb->wait_for_submission(std::chrono::milliseconds(quiet_period_ms));
155+
} catch (std::exception& e) {
156+
std::cerr << e.what() << std::endl;
157+
}
158+
}
96159
}

src/influx-c-rest/influx_c_rest_async.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@ extern "C" {
1111
#endif
1212

1313
typedef struct _influx_c_rest_async_t influx_c_rest_async_t;
14+
typedef struct _influx_c_rest_config_t influx_c_rest_config_t;
15+
typedef struct _influx_c_rest_lines_t influx_c_rest_lines_t;
1416

1517
/* lifetime */
1618
INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new(const char* url, const char* name);
1719
INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new_auth(const char* url, const char* name, const char* username, const char* password);
20+
INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new_config(const char* url, const char* name, influx_c_rest_config_t * config);
21+
INFLUX_C_REST influx_c_rest_async_t *influx_c_rest_async_new_auth_config(const char* url, const char* name, const char* username, const char* password, influx_c_rest_config_t * config);
1822
INFLUX_C_REST void influx_c_rest_async_destroy(influx_c_rest_async_t * self);
1923

2024
/* behavior */
2125
INFLUX_C_REST int influx_c_rest_async_drop(influx_c_rest_async_t * self);
2226
INFLUX_C_REST int influx_c_rest_async_create(influx_c_rest_async_t * self);
2327
INFLUX_C_REST void influx_c_rest_async_insert(influx_c_rest_async_t * self, const char* line);
2428
INFLUX_C_REST void influx_c_rest_async_insert_default_timestamp(influx_c_rest_async_t * self, const char* line);
29+
INFLUX_C_REST void influx_c_rest_async_insert_lines(influx_c_rest_async_t * self, influx_c_rest_lines_t * lines);
30+
INFLUX_C_REST void influx_c_rest_async_insert_lines_default_timestamp(influx_c_rest_async_t * self, influx_c_rest_lines_t * lines);
31+
32+
/* synchronization */
33+
INFLUX_C_REST void influx_c_rest_async_wait_quiet_ms(influx_c_rest_async_t * self, unsigned quiet_period_ms);
2534

2635
#if defined(__cplusplus)
2736
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
2+
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
//
8+
9+
#include "influx_c_rest_config.h"
10+
11+
#include "../influxdb-cpp-rest/influxdb_config.h"
12+
13+
#include <memory>
14+
#include <cassert>
15+
#include <iostream>
16+
17+
extern "C" {
18+
19+
struct _influx_c_rest_config_t {
20+
influxdb::api::db_config config;
21+
};
22+
23+
INFLUX_C_REST influx_c_rest_config_t *influx_c_rest_config_new(void) {
24+
try {
25+
influx_c_rest_config_t *res = new influx_c_rest_config_t();
26+
assert(res);
27+
return res;
28+
} catch (std::exception& e) {
29+
std::cerr << e.what() << std::endl;
30+
return nullptr;
31+
}
32+
}
33+
34+
INFLUX_C_REST void influx_c_rest_config_destroy(influx_c_rest_config_t * self) {
35+
assert(self);
36+
delete self;
37+
}
38+
39+
INFLUX_C_REST void influx_c_rest_config_set_batch_max_lines(influx_c_rest_config_t * self, unsigned max_lines) {
40+
assert(self);
41+
self->config.batch.max_lines = max_lines;
42+
}
43+
44+
INFLUX_C_REST void influx_c_rest_config_set_batch_max_time_ms(influx_c_rest_config_t * self, unsigned max_time_ms) {
45+
assert(self);
46+
self->config.batch.max_time_ms = max_time_ms;
47+
}
48+
49+
INFLUX_C_REST void influx_c_rest_config_set_http_keepalive(influx_c_rest_config_t * self, int keepalive) {
50+
assert(self);
51+
self->config.http.keepalive = (keepalive != 0);
52+
}
53+
54+
INFLUX_C_REST void influx_c_rest_config_set_http_timeout_ms(influx_c_rest_config_t * self, unsigned timeout_ms) {
55+
assert(self);
56+
self->config.http.timeout_ms = timeout_ms;
57+
}
58+
59+
INFLUX_C_REST void influx_c_rest_config_set_http_max_connections_per_host(influx_c_rest_config_t * self, unsigned max_connections) {
60+
assert(self);
61+
self->config.http.max_connections_per_host = max_connections;
62+
}
63+
64+
INFLUX_C_REST void* influx_c_rest_config_get_internal(influx_c_rest_config_t * self) {
65+
assert(self);
66+
return &self->config;
67+
}
68+
69+
}
70+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* * This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
*/
5+
#pragma once
6+
7+
#include "influx_c_rest_api.h"
8+
9+
#if defined(__cplusplus)
10+
extern "C" {
11+
#endif
12+
13+
typedef struct _influx_c_rest_config_t influx_c_rest_config_t;
14+
15+
/* lifetime */
16+
INFLUX_C_REST influx_c_rest_config_t *influx_c_rest_config_new(void);
17+
INFLUX_C_REST void influx_c_rest_config_destroy(influx_c_rest_config_t * self);
18+
19+
/* batch configuration */
20+
INFLUX_C_REST void influx_c_rest_config_set_batch_max_lines(influx_c_rest_config_t * self, unsigned max_lines);
21+
INFLUX_C_REST void influx_c_rest_config_set_batch_max_time_ms(influx_c_rest_config_t * self, unsigned max_time_ms);
22+
23+
/* http configuration */
24+
INFLUX_C_REST void influx_c_rest_config_set_http_keepalive(influx_c_rest_config_t * self, int keepalive);
25+
INFLUX_C_REST void influx_c_rest_config_set_http_timeout_ms(influx_c_rest_config_t * self, unsigned timeout_ms);
26+
INFLUX_C_REST void influx_c_rest_config_set_http_max_connections_per_host(influx_c_rest_config_t * self, unsigned max_connections);
27+
28+
/* internal access - returns pointer to internal config structure */
29+
INFLUX_C_REST void* influx_c_rest_config_get_internal(influx_c_rest_config_t * self);
30+
31+
#if defined(__cplusplus)
32+
}
33+
#endif
34+

0 commit comments

Comments
 (0)