File: function1.c

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (291 lines) | stat: -rw-r--r-- 7,653 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdbool.h>
#include "module.h"

#include <stdio.h>
#include <msgpuck.h>

int
function1(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	say_info("-- function1 -  called --");
	printf("ok - function1\n");
	return 0;
}

int
multireturn(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	char tuple_buf[512];
	char *d = tuple_buf;
	d = mp_encode_array(d, 1);
	d = mp_encode_uint(d, 1);
	assert(d <= tuple_buf + sizeof(tuple_buf));

	box_tuple_format_t *fmt = box_tuple_format_default();
	box_tuple_t *tuple_a = box_tuple_new(fmt, tuple_buf, d);
	if (tuple_a == NULL)
		return -1;
	int rc = box_return_tuple(ctx, tuple_a);
	if (rc != 0)
		return rc;
	return box_return_tuple(ctx, tuple_a);
}

int
args(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	uint32_t arg_count = mp_decode_array(&args);
	if (arg_count < 1) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s",
			"invalid argument count");
	}

	if (mp_typeof(*args) != MP_UINT) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s",
			"first tuple field must be uint");
	}

	uint32_t num = mp_decode_uint(&args);

	char tuple_buf[512];
	char *d = tuple_buf;
	d = mp_encode_array(d, 2);
	d = mp_encode_uint(d, num);
	d = mp_encode_str(d, "hello", strlen("hello"));
	assert(d <= tuple_buf + sizeof(tuple_buf));

	box_tuple_format_t *fmt = box_tuple_format_default();
	box_tuple_t *tuple = box_tuple_new(fmt, tuple_buf, d);
	if (tuple == NULL)
		return -1;
	return box_return_tuple(ctx, tuple);
}

int
divide(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	uint32_t arg_count = mp_decode_array(&args);
	if (arg_count < 2)
		goto error;

	if (mp_typeof(*args) != MP_UINT)
		goto error;
	double a = mp_decode_uint(&args);
	if (mp_typeof(*args) != MP_UINT)
		goto error;
	double b = mp_decode_uint(&args);
	if (b == 0)
		goto error;

	char tuple_buf[512];
	char *d = tuple_buf;
	d = mp_encode_array(d, 1);
	d = mp_encode_double(d, a / b);
	assert(d <= tuple_buf + sizeof(tuple_buf));

	box_tuple_format_t *fmt = box_tuple_format_default();
	box_tuple_t *tuple = box_tuple_new(fmt, tuple_buf, d);
	if (tuple == NULL)
		return -1;
	return box_return_tuple(ctx, tuple);
error:
	return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s",
			     "invalid argument");
}


/*
 * For each UINT key in arguments create or increment counter in
 * box.space.test space.
 */
int
multi_inc(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	(void )ITER_ALL;
	static const char *SPACE_NAME = "test";
	static const char *INDEX_NAME = "primary";

	uint32_t space_id = box_space_id_by_name(SPACE_NAME, strlen(SPACE_NAME));
	uint32_t index_id = box_index_id_by_name(space_id, INDEX_NAME,
		strlen(INDEX_NAME));
	if (space_id == BOX_ID_NIL || index_id == BOX_ID_NIL) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
			"Can't find index %s in space %s",
			INDEX_NAME, SPACE_NAME);
	}
	say_debug("space_id = %u, index_id = %u", space_id, index_id);

	uint32_t arg_count = mp_decode_array(&args);
	assert(!box_txn());
	box_txn_begin();
	assert(box_txn());
	for (uint32_t i = 0; i < arg_count; i++) {
		/* Decode next argument */
		if (mp_typeof(*args) != MP_UINT)
			return box_error_set(__FILE__, __LINE__,
					       ER_PROC_C, "Expected uint keys");
		uint32_t key = mp_decode_uint(&args);
		(void) key;

		/* Prepare MsgPack key for search */
		char key_buf[16];
		char *key_end = key_buf;
		key_end = mp_encode_array(key_end, 1);
		key_end = mp_encode_uint(key_end, key);
		assert(key_end < key_buf + sizeof(key_buf));

		/* Get current value from space */
		uint64_t counter = 0;
		box_tuple_t *tuple;
		if (box_index_get(space_id, index_id, key_buf, key_end,
				  &tuple) != 0) {
			return -1; /* error */
		} else if (tuple != NULL) {
			const char *field = box_tuple_field(tuple, 1);
			if (field == NULL || mp_typeof(*field) != MP_UINT)
				return box_error_set(__FILE__, __LINE__,
						       ER_PROC_LUA,
						       "Invalid tuple");
			counter = mp_decode_uint(&field) + 1;
		}

		/* Replace value */
		char tuple_buf[16];
		char *tuple_end = tuple_buf;
		tuple_end = mp_encode_array(tuple_end, 2);
		tuple_end = mp_encode_uint(tuple_end, key); /* key */
		tuple_end = mp_encode_uint(tuple_end, counter); /* counter */
		assert(tuple_end <= tuple_buf + sizeof(tuple_buf));

		if (box_replace(space_id, tuple_buf, tuple_end, NULL) != 0)
			return -1;
	}
	box_txn_commit();
	assert(!box_txn());
	return 0;
}

int
errors(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", "Proc error");

	const box_error_t *error = box_error_last();
	assert(strcmp(box_error_type(error), "ClientError") == 0);
	assert(box_error_code(error) == ER_PROC_C);
	assert(strcmp(box_error_message(error), "Proc error") == 0);
	(void) error;

	/* Backwards compatibility */
	box_error_raise(ER_PROC_C, "hello %s", "world");
	assert(box_error_last() != NULL);
	error = box_error_last();
	assert(box_error_code(error) == ER_PROC_C);
	assert(strcmp(box_error_message(error), "hello world") == 0);

	/* Backwards compatibility */
	box_error_raise(ER_PROC_C, "hello, lalala");
	assert(box_error_last() != NULL);
	error = box_error_last();
	assert(box_error_code(error) == ER_PROC_C);
	assert(strcmp(box_error_message(error), "hello, lalala") == 0);

	box_error_clear();
	assert(box_error_last() == NULL);

	return -1; /* raises "Unknown procedure error" */
}

int
test_yield(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	static const char *SPACE_NAME = "test_yield";

	uint32_t space_id = box_space_id_by_name(SPACE_NAME, strlen(SPACE_NAME));
	if (space_id == BOX_ID_NIL) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
			"Can't find space %s", SPACE_NAME);
	}

	assert(!box_txn());
	box_txn_begin();
	assert(box_txn());

	/* Replace value */
	char tuple_buf[16];
	char *tuple_end = tuple_buf;
	tuple_end = mp_encode_array(tuple_end, 2);
	tuple_end = mp_encode_uint(tuple_end, 1);
	tuple_end = mp_encode_uint(tuple_end, 2); /* counter */
	assert(tuple_end <= tuple_buf + sizeof(tuple_buf));

	if (box_replace(space_id, tuple_buf, tuple_end, NULL) != 0)
		return -1;

	box_txn_commit();
	assert(!box_txn());
	say_info("-- yield -  called --");
	fiber_sleep(0.001);
	printf("ok - yield\n");
	return 0;
}

int
test_sleep(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	(void) ctx;
	(void) args;
	(void) args_end;
	/*
	 * Sleep until a cancellation. Purpose of this function -
	 * test module unloading prevention while at least one of
	 * its functions is being executed.
	 */
	while (!fiber_is_cancelled())
		fiber_sleep(0);
	return 0;
}

int
test_push(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	(void)ctx;
	return box_session_push(args, args_end);
}

int
test_return_mp(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	(void) args;
	(void) args_end;
	char buf[512];
	char *pos = mp_encode_uint(buf, 1);
	int rc = box_return_mp(ctx, buf, pos);
	if (rc != 0)
		return rc;

	pos = mp_encode_int(buf, -1);
	rc = box_return_mp(ctx, buf, pos);
	if (rc != 0)
		return rc;

	pos = mp_encode_uint(buf, UINT64_MAX);
	rc = box_return_mp(ctx, buf, pos);
	if (rc != 0)
		return rc;

	const char *str = "123456789101112131415";
	pos = mp_encode_str(buf, str, strlen(str));
	rc = box_return_mp(ctx, buf, pos);
	if (rc != 0)
		return rc;

	pos = mp_encode_array(buf, 1);
	pos = mp_encode_uint(pos, 2);
	box_tuple_t *tuple = box_tuple_new(box_tuple_format_default(),
					   buf, pos);
	if (tuple == NULL)
		return -1;
	rc = box_return_tuple(ctx, tuple);
	return rc;
}