1
- =====================================================================
2
- C
3
- =====================================================================
1
+ C
2
+ =
4
3
5
4
Here follow two examples of using Tarantool's high-level C API.
6
5
7
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8
- Example 1
9
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
+ Example 1
7
+ ---------
10
8
11
9
Here is a complete C program that inserts :code: `[99999,'B'] ` into
12
10
space :code: `examples ` via the high-level C API.
13
11
14
- .. code-block :: c
12
+ .. code-block :: c
15
13
16
14
#include <stdio.h>
17
15
#include <stdlib.h>
@@ -44,7 +42,7 @@ space :code:`examples` via the high-level C API.
44
42
Paste the code into a file named :file: `example.c ` and install ``tarantool-c ``.
45
43
One way to install ``tarantool-c `` (using Ubuntu) is:
46
44
47
- .. code-block :: console
45
+ .. code-block :: console
48
46
49
47
$ git clone git://github.com/tarantool/tarantool-c.git ~/tarantool-c
50
48
$ cd ~/tarantool-c
@@ -54,9 +52,9 @@ One way to install ``tarantool-c`` (using Ubuntu) is:
54
52
$ make
55
53
$ make install
56
54
57
- To compile and link the program, say :
55
+ To compile and link the program, run :
58
56
59
- .. code-block :: console
57
+ .. code-block :: console
60
58
61
59
$ # sometimes this is necessary:
62
60
$ export LD_LIBRARY_PATH=/usr/local/lib
@@ -72,11 +70,14 @@ If Tarantool is not running on localhost with listen address = 3301, the program
72
70
will print “Connection refused”.
73
71
If the insert fails, the program will print "Insert failed" and an error number
74
72
(see all error codes in the source file
75
- `/src/box/errcode.h <https://github.com/tarantool/tarantool/blob/2.1/src/box/errcode.h >`_ ).
73
+ `/src/box/errcode.h <https://github.com/tarantool/tarantool/blob/2.1/src/box/errcode.h >`__ ).
76
74
77
75
Here are notes corresponding to comments in the example program.
78
76
79
- **SETUP: ** The setup begins by creating a stream.
77
+ SETUP
78
+ ~~~~~
79
+
80
+ The setup begins by creating a stream.
80
81
81
82
.. code-block :: c
82
83
@@ -96,28 +97,34 @@ Function description:
96
97
struct tnt_stream *tnt_net(struct tnt_stream *s)
97
98
int tnt_set(struct tnt_stream *s, int option, variant option-value)
98
99
99
- **CONNECT: ** Now that the stream named ``tnt `` exists and is associated with a
100
+ CONNECT
101
+ ~~~~~~~
102
+
103
+ Now that the stream named ``tnt `` exists and is associated with a
100
104
URI, this example program can connect to a server instance.
101
105
102
- .. code-block :: c
106
+ .. code-block :: c
103
107
104
108
if (tnt_connect(tnt) < 0)
105
109
{ printf("Connection refused\n"); exit(-1); }
106
110
107
111
Function description:
108
112
109
- .. code-block :: text
113
+ .. code-block :: text
110
114
111
115
int tnt_connect(struct tnt_stream *s)
112
116
113
117
The connection might fail for a variety of reasons, such as:
114
118
the server is not running, or the URI contains an invalid :ref: `password<authentication-passwords> `.
115
119
If the connection fails, the return value will be -1.
116
120
117
- **MAKE REQUEST: ** Most requests require passing a structured value, such as
121
+ MAKE REQUEST
122
+ ~~~~~~~~~~~~
123
+
124
+ Most requests require passing a structured value, such as
118
125
the contents of a tuple.
119
126
120
- .. code-block :: c
127
+ .. code-block :: c
121
128
122
129
struct tnt_stream *tuple = tnt_object(NULL);
123
130
tnt_object_format(tuple, "[%d%s]", 99999, "B");
@@ -133,14 +140,17 @@ then the integer value, then a pointer to the string value.
133
140
134
141
Function description:
135
142
136
- .. code-block :: text
143
+ .. code-block :: text
137
144
138
145
ssize_t tnt_object_format(struct tnt_stream *s, const char *fmt, ...)
139
146
140
- **SEND REQUEST: ** The database-manipulation requests are analogous to the
147
+ SEND REQUEST
148
+ ~~~~~~~~~~~~
149
+
150
+ The database-manipulation requests are analogous to the
141
151
requests in the box library.
142
152
143
- .. code-block :: c
153
+ .. code-block :: c
144
154
145
155
tnt_insert(tnt, 999, tuple);
146
156
tnt_flush(tnt);
@@ -152,7 +162,7 @@ the program passes the ``tnt_stream`` that was used for connection
152
162
153
163
Function description:
154
164
155
- .. code-block :: text
165
+ .. code-block :: text
156
166
157
167
ssize_t tnt_insert(struct tnt_stream *s, uint32_t space, struct tnt_stream *tuple)
158
168
ssize_t tnt_replace(struct tnt_stream *s, uint32_t space, struct tnt_stream *tuple)
@@ -162,10 +172,13 @@ Function description:
162
172
ssize_t tnt_update(struct tnt_stream *s, uint32_t space, uint32_t index,
163
173
struct tnt_stream *key, struct tnt_stream *ops)
164
174
165
- **GET REPLY: ** For most requests, the client will receive a reply containing some
175
+ GET REPLY
176
+ ~~~~~~~~~
177
+
178
+ For most requests, the client will receive a reply containing some
166
179
indication whether the result was successful, and a set of tuples.
167
180
168
- .. code-block :: c
181
+ .. code-block :: c
169
182
170
183
struct tnt_reply reply; tnt_reply_init(&reply);
171
184
tnt->read_reply(tnt, &reply);
@@ -176,40 +189,42 @@ This program checks for success but does not decode the rest of the reply.
176
189
177
190
Function description:
178
191
179
- .. code-block :: text
192
+ .. code-block :: text
180
193
181
194
struct tnt_reply *tnt_reply_init(struct tnt_reply *r)
182
195
tnt->read_reply(struct tnt_stream *s, struct tnt_reply *r)
183
196
void tnt_reply_free(struct tnt_reply *r)
184
197
185
- **TEARDOWN: ** When a session ends, the connection that was made with
198
+ TEARDOWN
199
+ ~~~~~~~~
200
+
201
+ When a session ends, the connection that was made with
186
202
:c:func: `tarantoolc:tnt_connect() ` should be closed, and the objects that were
187
203
made in the setup should be destroyed.
188
204
189
- .. code-block :: c
205
+ .. code-block :: c
190
206
191
207
tnt_close(tnt);
192
208
tnt_stream_free(tuple);
193
209
tnt_stream_free(tnt);
194
210
195
211
Function description:
196
212
197
- .. code-block :: text
213
+ .. code-block :: text
198
214
199
215
void tnt_close(struct tnt_stream *s)
200
216
void tnt_stream_free(struct tnt_stream *s)
201
217
202
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203
- Example 2
204
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218
+ Example 2
219
+ ---------
205
220
206
221
Here is a complete C program that selects, using index key ``[99999] ``, from
207
222
space ``examples `` via the high-level C API.
208
223
To display the results, the program uses functions in the
209
- `MsgPuck <https://github.com/tarantool/msgpuck >`_ library which allow decoding of
210
- `MessagePack <https://en.wikipedia.org/wiki/MessagePack >`_ arrays.
224
+ `MsgPuck <https://github.com/tarantool/msgpuck >`__ library which allow decoding of
225
+ `MessagePack <https://en.wikipedia.org/wiki/MessagePack >`__ arrays.
211
226
212
- .. code-block :: c
227
+ .. code-block :: c
213
228
214
229
#include <stdio.h>
215
230
#include <stdlib.h>
@@ -281,13 +296,13 @@ Similarly to the first example, paste the code into a file named
281
296
282
297
To compile and link the program, say:
283
298
284
- .. code-block :: console
299
+ .. code-block :: console
285
300
286
301
$ gcc -o example2 example2.c -ltarantool
287
302
288
303
To run the program, say :samp: `./example2 `.
289
304
290
305
The two example programs only show a few requests and do not show all that's
291
306
necessary for good practice. See more in the
292
- `tarantool-c documentation at GitHub <http://github.com/tarantool/tarantool-c >`_ .
307
+ `tarantool-c documentation at GitHub <http://github.com/tarantool/tarantool-c >`__ .
293
308
0 commit comments