Skip to content

Commit 4d6a64b

Browse files
authored
chore: Updating examples for questdb.io (#24)
* chore: Updating examples for questdb.io * chore: Added building Rust examples to CI.
1 parent ff944a0 commit 4d6a64b

14 files changed

+286
-23
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,23 @@ compile_example(
8484
line_sender_c_example_auth
8585
examples/line_sender_c_example_auth.c)
8686
compile_example(
87-
line_sender_c_example_tls
88-
examples/line_sender_c_example_tls.c)
87+
line_sender_c_example_tls_ca
88+
examples/line_sender_c_example_tls_ca.c)
89+
compile_example(
90+
line_sender_c_example_auth_tls
91+
examples/line_sender_c_example_auth_tls.c)
8992
compile_example(
9093
line_sender_cpp_example
9194
examples/line_sender_cpp_example.cpp)
9295
compile_example(
9396
line_sender_cpp_example_auth
9497
examples/line_sender_cpp_example_auth.cpp)
9598
compile_example(
96-
line_sender_cpp_example_tls
97-
examples/line_sender_cpp_example_tls.cpp)
99+
line_sender_cpp_example_tls_ca
100+
examples/line_sender_cpp_example_tls_ca.cpp)
101+
compile_example(
102+
line_sender_cpp_example_auth_tls
103+
examples/line_sender_cpp_example_auth_tls.cpp)
98104

99105
# Include Rust tests as part of the tests run
100106
add_test(

ci/run_tests_pipeline.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ stages:
6565
rustup default $(toolchain)
6666
condition: ne(variables['toolchain'], '')
6767
displayName: "Update and set Rust toolchain"
68+
- script: |
69+
cd questdb-rs
70+
cargo build --example basic
71+
cargo build --example auth_tls
72+
displayName: Build Rust examples.
6873
- script: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
6974
env:
7075
JAVA_HOME: $(JAVA_HOME_11_X64)

doc/C.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
* [Basic example in C](../examples/line_sender_c_example.c).
1313
* [With authentication](../examples/line_sender_c_example_auth.c).
14-
* [With authentication and TLS](../examples/line_sender_c_example_tls.c).
14+
* [With authentication and TLS](../examples/line_sender_c_example_auth_tls.c).
15+
* [Custom certificate authority file](../examples/line_sender_c_example_tls_ca.c).
1516

1617
## API Overview
1718

doc/CPP.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
* [Basic example in C++](../examples/line_sender_cpp_example.cpp).
1313
* [With authentication](../examples/line_sender_cpp_example_auth.cpp).
14-
* [With authentication and TLS](../examples/line_sender_cpp_example_tls.cpp).
14+
* [With authentication and TLS](../examples/line_sender_cpp_example_auth_tls.cpp).
15+
* [Custom certificate authority file](../examples/line_sender_c_example_tls_ca.c).
1516

1617
## API Overview
1718

examples.manifest.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,31 @@
2020
# ILP Auth + TLS Examples
2121
- name: ilp-auth-tls
2222
lang: c
23-
path: examples/line_sender_c_example_tls.c
23+
path: examples/line_sender_c_example_auth_tls.c
2424
header: |-
2525
[C client library docs](https://github.com/questdb/c-questdb-client/blob/main/doc/C.md)
2626
auth:
2727
kid: testUser1
2828
d: 5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48
2929
x: fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU
3030
y: Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac
31+
addr:
32+
host: localhost
33+
port: 9009
3134

3235
- name: ilp-auth-tls
3336
lang: cpp
34-
path: examples/line_sender_cpp_example_tls.cpp
37+
path: examples/line_sender_cpp_example_auth_tls.cpp
3538
header: |-
3639
[C++ client library docs](https://github.com/questdb/c-questdb-client/blob/main/doc/CPP.md)
3740
auth:
3841
kid: testUser1
3942
d: 5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48
4043
x: fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU
4144
y: Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac
45+
addr:
46+
host: localhost
47+
port: 9009
4248

4349
- name: ilp-auth-tls
4450
lang: rust
@@ -50,3 +56,6 @@
5056
d: 5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48
5157
x: fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU
5258
y: Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac
59+
addr:
60+
host: localhost
61+
port: 9009

examples/line_sender_c_example.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ static bool displayed_help(int argc, const char* argv[])
9797
{
9898
fprintf(stderr, "Usage:\n");
9999
fprintf(stderr, "line_sender_c_example: [HOST [PORT]]\n");
100-
fprintf(stderr," HOST: ILP host (defaults to \"localhost\".\n");
101-
fprintf(stderr," PORT: ILP port (defaults to \"9009\".\n");
100+
fprintf(stderr, " HOST: ILP host (defaults to \"localhost\").\n");
101+
fprintf(stderr, " PORT: ILP port (defaults to \"9009\").\n");
102102
return true;
103103
}
104104
}

examples/line_sender_c_example_auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ static bool displayed_help(int argc, const char* argv[])
113113
{
114114
fprintf(stderr, "Usage:\n");
115115
fprintf(stderr, "line_sender_c_example_auth: [HOST [PORT]]\n");
116-
fprintf(stderr," HOST: ILP host (defaults to \"localhost\".\n");
117-
fprintf(stderr," PORT: ILP port (defaults to \"9009\".\n");
116+
fprintf(stderr, " HOST: ILP host (defaults to \"localhost\").\n");
117+
fprintf(stderr, " PORT: ILP port (defaults to \"9009\").\n");
118118
return true;
119119
}
120120
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include <questdb/ilp/line_sender.h>
2+
#include <stdio.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
static bool example(const char* host, const char* port)
7+
{
8+
line_sender_error* err = NULL;
9+
line_sender_opts* opts = NULL;
10+
line_sender* sender = NULL;
11+
line_sender_buffer* buffer = NULL;
12+
13+
line_sender_utf8 host_utf8 = { 0, NULL };
14+
if (!line_sender_utf8_init(&host_utf8, strlen(host), host, &err))
15+
goto on_error;
16+
17+
line_sender_utf8 port_utf8 = { 0, NULL };
18+
if (!line_sender_utf8_init(&port_utf8, strlen(port), port, &err))
19+
goto on_error;
20+
21+
// Call `line_sender_opts_new` if instead you have an integer port.
22+
opts = line_sender_opts_new_service(host_utf8, port_utf8);
23+
24+
// Enable TLS to accept connections using common trusted CAs.
25+
line_sender_opts_tls(opts);
26+
27+
// Use `QDB_UTF_8_FROM_STR_OR` to init from `const char*`.
28+
line_sender_utf8 key_id = QDB_UTF8_LITERAL("testUser1");
29+
line_sender_utf8 priv_key = QDB_UTF8_LITERAL(
30+
"5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48");
31+
line_sender_utf8 pub_key_x = QDB_UTF8_LITERAL(
32+
"fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU");
33+
line_sender_utf8 pub_key_y = QDB_UTF8_LITERAL(
34+
"Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac");
35+
36+
line_sender_opts_auth(
37+
opts,
38+
key_id, // kid
39+
priv_key, // d
40+
pub_key_x, // x
41+
pub_key_y); // y
42+
sender = line_sender_connect(opts, &err);
43+
line_sender_opts_free(opts);
44+
opts = NULL;
45+
if (!sender)
46+
goto on_error;
47+
48+
buffer = line_sender_buffer_new();
49+
line_sender_buffer_reserve(buffer, 64 * 1024); // 64KB buffer initial size.
50+
51+
// We prepare all our table names and column names in advance.
52+
// If we're inserting multiple rows, this allows us to avoid
53+
// re-validating the same strings over and over again.
54+
line_sender_table_name table_name = QDB_TABLE_NAME_LITERAL("c_cars_tls");
55+
line_sender_column_name id_name = QDB_COLUMN_NAME_LITERAL("id");
56+
line_sender_column_name x_name = QDB_COLUMN_NAME_LITERAL("x");
57+
line_sender_column_name y_name = QDB_COLUMN_NAME_LITERAL("y");
58+
line_sender_column_name booked_name = QDB_COLUMN_NAME_LITERAL("booked");
59+
line_sender_column_name passengers_name = QDB_COLUMN_NAME_LITERAL(
60+
"passengers");
61+
line_sender_column_name driver_name = QDB_COLUMN_NAME_LITERAL("driver");
62+
63+
if (!line_sender_buffer_table(buffer, table_name, &err))
64+
goto on_error;
65+
66+
line_sender_utf8 id_value = QDB_UTF8_LITERAL(
67+
"d6e5fe92-d19f-482a-a97a-c105f547f721");
68+
if (!line_sender_buffer_symbol(buffer, id_name, id_value, &err))
69+
goto on_error;
70+
71+
if (!line_sender_buffer_column_f64(buffer, x_name, 30.5, &err))
72+
goto on_error;
73+
74+
if (!line_sender_buffer_column_f64(buffer, y_name, -150.25, &err))
75+
goto on_error;
76+
77+
if (!line_sender_buffer_column_bool(buffer, booked_name, true, &err))
78+
goto on_error;
79+
80+
if (!line_sender_buffer_column_i64(buffer, passengers_name, 3, &err))
81+
goto on_error;
82+
83+
line_sender_utf8 driver_value = QDB_UTF8_LITERAL("John Doe");
84+
if (!line_sender_buffer_column_str(buffer, driver_name, driver_value, &err))
85+
goto on_error;
86+
87+
if (!line_sender_buffer_at_now(buffer, &err))
88+
goto on_error;
89+
90+
// To insert more records, call `line_sender_buffer_table(..)...` again.
91+
92+
if (!line_sender_flush(sender, buffer, &err))
93+
goto on_error;
94+
95+
line_sender_close(sender);
96+
97+
return true;
98+
99+
on_error: ;
100+
line_sender_opts_free(opts);
101+
size_t err_len = 0;
102+
const char* err_msg = line_sender_error_msg(err, &err_len);
103+
fprintf(stderr, "Error running example: %.*s\n", (int)err_len, err_msg);
104+
line_sender_error_free(err);
105+
line_sender_buffer_free(buffer);
106+
line_sender_close(sender);
107+
return false;
108+
}
109+
110+
static bool displayed_help(int argc, const char* argv[])
111+
{
112+
for (int index = 1; index < argc; ++index)
113+
{
114+
const char* arg = argv[index];
115+
if ((strncmp(arg, "-h", 2) == 0) || (strncmp(arg, "--help", 6) == 0))
116+
{
117+
fprintf(stderr, "Usage:\n");
118+
fprintf(stderr, "line_sender_c_example_auth_tls: [HOST [PORT]]\n");
119+
fprintf(stderr, " HOST: ILP host (defaults to \"localhost\").\n");
120+
fprintf(stderr, " PORT: ILP port (defaults to \"9009\").\n");
121+
return true;
122+
}
123+
}
124+
return false;
125+
}
126+
127+
int main(int argc, const char* argv[])
128+
{
129+
if (displayed_help(argc, argv))
130+
return 0;
131+
132+
const char* host = "localhost";
133+
if (argc >= 2)
134+
host = argv[1];
135+
const char* port = "9009";
136+
if (argc >= 3)
137+
port = argv[2];
138+
139+
return !example(host, port);
140+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ static bool displayed_help(int argc, const char* argv[])
120120
if ((strncmp(arg, "-h", 2) == 0) || (strncmp(arg, "--help", 6) == 0))
121121
{
122122
fprintf(stderr, "Usage:\n");
123-
fprintf(stderr, "line_sender_c_example_auth: [HOST [PORT]]\n");
124-
fprintf(stderr," HOST: ILP host (defaults to \"localhost\".\n");
125-
fprintf(stderr," PORT: ILP port (defaults to \"9009\".\n");
123+
fprintf(stderr, "line_sender_c_example_tls_ca: CA_PATH [HOST [PORT]]\n");
124+
fprintf(stderr, " CA_PATH: Certificate authority pem file.\n");
125+
fprintf(stderr, " HOST: ILP host (defaults to \"localhost\").\n");
126+
fprintf(stderr, " PORT: ILP port (defaults to \"9009\").\n");
126127
return true;
127128
}
128129
}

examples/line_sender_cpp_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ static bool displayed_help(int argc, const char* argv[])
6060
std::cerr
6161
<< "Usage:\n"
6262
<< "line_sender_c_example: [HOST [PORT]]\n"
63-
<< " HOST: ILP host (defaults to \"localhost\".\n"
64-
<< " PORT: ILP port (defaults to \"9009\"."
63+
<< " HOST: ILP host (defaults to \"localhost\").\n"
64+
<< " PORT: ILP port (defaults to \"9009\")."
6565
<< std::endl;
6666
return true;
6767
}

0 commit comments

Comments
 (0)