File: reload1.c

package info (click to toggle)
tarantool 1.9.1.26.g63eb81e3c-1.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 38,724 kB
  • sloc: ansic: 247,425; cpp: 24,952; sh: 17,809; python: 10,699; makefile: 2,682
file content (83 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download | duplicates (4)
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
#include "module.h"

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

int
foo(box_function_ctx_t *ctx, const char *args, const char *args_end)
{
	static const char *SPACE_TEST_NAME = "test";
	static const char *INDEX_NAME = "primary";
	uint32_t space_test_id = box_space_id_by_name(SPACE_TEST_NAME,
			strlen(SPACE_TEST_NAME));
	uint32_t index_id = box_index_id_by_name(space_test_id, INDEX_NAME,
		strlen(INDEX_NAME));
	if (space_test_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_TEST_NAME);
	}
	mp_decode_array(&args);
	uint32_t num = mp_decode_uint(&args);

	char buf[16];
	char *end = buf;
	end = mp_encode_array(end, 1);
	end = mp_encode_uint(end, num);
	if (box_insert(space_test_id, buf, end, NULL) < 0) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
			"Can't insert in space %s", SPACE_TEST_NAME);
	}
	end = buf;
	end = mp_encode_array(end, 1);
	end = mp_encode_uint(end, 0);
	while (box_index_count(space_test_id, index_id, ITER_EQ, buf, end) <= 0) {
		fiber_sleep(0.001);
	}
	end = buf;
	end = mp_encode_array(end, 1);
	end = mp_encode_int(end, -((int)num));
	if (box_insert(space_test_id, buf, end, NULL) < 0) {
		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
			"Can't insert in space %s", SPACE_TEST_NAME);
	}
	return 0;
}

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

	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());

	fiber_sleep(0.001);

	tuple_end = tuple_buf;
	tuple_end = mp_encode_array(tuple_end, 1);
	tuple_end = mp_encode_uint(tuple_end, 1);
	struct tuple *tuple = box_tuple_new(box_tuple_format_default(), tuple_buf, tuple_end);
	return box_return_tuple(ctx, tuple);
}