Skip to content

Commit 7563213

Browse files
committed
iproto: replace iproto_constant with string arrays
There are no substantial gaps in the remaining IPROTO constant enums so there's no need in iproto_constant struct. Instead we can generate string arrays, as we usually do. This is more flexible because it allows us to look up a name by code. It's also consistent with iproto_type and iproto_key names. The only tricky part here is the iproto_flag enum because it contains bit masks. To generate names for the flags, we add the auxiliary enum iproto_flag_bit that contains bit numbers. Follow-up tarantool#8443 Follow-up commit b3fb883 ("iproto: export IPROTO constants to Lua automatically") NO_DOC=refactoring NO_TEST=refactoring NO_CHANGELOG=refactoring
1 parent 42dc000 commit 7563213

File tree

5 files changed

+85
-102
lines changed

5 files changed

+85
-102
lines changed

src/box/iproto_constants.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const uint64_t iproto_body_key_map[IPROTO_TYPE_STAT_MAX] = {
5252
};
5353
#undef bit
5454

55-
const struct iproto_constant iproto_flag_constants[] = {
56-
IPROTO_FLAGS(IPROTO_CONSTANT_MEMBER)
57-
};
55+
#define IPROTO_FLAG_BIT_STRS_MEMBER(s, ...) \
56+
[IPROTO_FLAG_BIT_ ## s] = #s,
5857

59-
const size_t iproto_flag_constants_size = lengthof(iproto_flag_constants);
58+
const char *iproto_flag_bit_strs[iproto_flag_bit_MAX] = {
59+
IPROTO_FLAGS(IPROTO_FLAG_BIT_STRS_MEMBER)
60+
};
6061

6162
#define IPROTO_KEY_TYPE_MEMBER(s, v, t) \
6263
[IPROTO_ ## s] = t,
@@ -72,33 +73,33 @@ const char *iproto_key_strs[iproto_key_MAX] = {
7273
IPROTO_KEYS(IPROTO_KEY_STRS_MEMBER)
7374
};
7475

75-
const struct iproto_constant iproto_metadata_key_constants[] = {
76-
IPROTO_METADATA_KEYS(IPROTO_CONSTANT_MEMBER)
76+
#define IPROTO_METADATA_KEY_STRS_MEMBER(s, ...) \
77+
[IPROTO_FIELD_ ## s] = #s,
78+
79+
const char *iproto_metadata_key_strs[iproto_metadata_key_MAX] = {
80+
IPROTO_METADATA_KEYS(IPROTO_METADATA_KEY_STRS_MEMBER)
7781
};
7882

79-
const size_t iproto_metadata_key_constants_size =
80-
lengthof(iproto_metadata_key_constants);
83+
#define IPROTO_BALLOT_KEY_STRS_MEMBER(s, ...) \
84+
[IPROTO_BALLOT_ ## s] = #s,
8185

82-
const struct iproto_constant iproto_ballot_key_constants[] = {
83-
IPROTO_BALLOT_KEYS(IPROTO_CONSTANT_MEMBER)
86+
const char *iproto_ballot_key_strs[iproto_ballot_key_MAX] = {
87+
IPROTO_BALLOT_KEYS(IPROTO_BALLOT_KEY_STRS_MEMBER)
8488
};
8589

86-
const size_t iproto_ballot_key_constants_size =
87-
lengthof(iproto_ballot_key_constants);
88-
8990
#define IPROTO_TYPE_STRS_MEMBER(s, ...) \
9091
[IPROTO_ ## s] = #s,
9192

9293
const char *iproto_type_strs[iproto_type_MAX] = {
9394
IPROTO_TYPES(IPROTO_TYPE_STRS_MEMBER)
9495
};
9596

96-
const struct iproto_constant iproto_raft_keys_constants[] = {
97-
IPROTO_RAFT_KEYS(IPROTO_CONSTANT_MEMBER)
98-
};
97+
#define IPROTO_RAFT_KEY_STRS_MEMBER(s, ...) \
98+
[IPROTO_RAFT_ ## s] = #s,
9999

100-
const size_t iproto_raft_keys_constants_size =
101-
lengthof(iproto_raft_keys_constants);
100+
const char *iproto_raft_key_strs[iproto_raft_key_MAX] = {
101+
IPROTO_RAFT_KEYS(IPROTO_RAFT_KEY_STRS_MEMBER)
102+
};
102103

103104
#define VY_RUN_INFO_KEY_STRS_MEMBER(s, ...) \
104105
[VY_RUN_INFO_ ## s] = #s,

src/box/iproto_constants.h

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@
4040
extern "C" {
4141
#endif
4242

43-
/** Named IPROTO constant. */
44-
struct iproto_constant {
45-
/** NULL-terminated name of constant. */
46-
const char *name;
47-
/** Value of constant. */
48-
int value;
49-
};
50-
51-
#define IPROTO_CONSTANT_MEMBER(s, v, ...) {.name = #s, .value = v},
52-
5343
enum {
5444
/** Maximal iproto package body length (2GiB) */
5545
IPROTO_BODY_LEN_MAX = 2147483648UL,
@@ -62,23 +52,27 @@ enum {
6252
/** IPROTO_FLAGS bitfield constants. */
6353
#define IPROTO_FLAGS(_) \
6454
/** Set for the last xrow in a transaction. */ \
65-
_(COMMIT, 0x01) \
55+
_(COMMIT, 0) \
6656
/** Set for the last row of a tx residing in limbo. */ \
67-
_(WAIT_SYNC, 0x02) \
57+
_(WAIT_SYNC, 1) \
6858
/** Set for the last row of a synchronous tx. */ \
69-
_(WAIT_ACK, 0x04) \
59+
_(WAIT_ACK, 2) \
7060

71-
#define IPROTO_FLAG_MEMBER(s, v) IPROTO_FLAG_ ## s = v,
61+
#define IPROTO_FLAG_MEMBER(s, v) IPROTO_FLAG_ ## s = 1ULL << (v),
7262

7363
enum iproto_flag {
7464
IPROTO_FLAGS(IPROTO_FLAG_MEMBER)
7565
};
7666

77-
/** Constants generated from IPROTO_FLAGS. */
78-
extern const struct iproto_constant iproto_flag_constants[];
67+
#define IPROTO_FLAG_BIT_MEMBER(s, v) IPROTO_FLAG_BIT_ ## s = v,
68+
69+
enum iproto_flag_bit {
70+
IPROTO_FLAGS(IPROTO_FLAG_BIT_MEMBER)
71+
iproto_flag_bit_MAX,
72+
};
7973

80-
/** Size of iproto_flag_constants. */
81-
extern const size_t iproto_flag_constants_size;
74+
/** IPROTO flag name by bit number. */
75+
extern const char *iproto_flag_bit_strs[];
8276

8377
/**
8478
* IPROTO key name, code, and MsgPack value type.
@@ -253,13 +247,11 @@ extern const unsigned char iproto_key_type[];
253247

254248
enum iproto_metadata_key {
255249
IPROTO_METADATA_KEYS(IPROTO_METADATA_KEY_MEMBER)
250+
iproto_metadata_key_MAX
256251
};
257252

258-
/** Constants generated from IPROTO_METADATA_KEYS. */
259-
extern const struct iproto_constant iproto_metadata_key_constants[];
260-
261-
/** Size of iproto_metadata_key_constants. */
262-
extern const size_t iproto_metadata_key_constants_size;
253+
/** IPROTO metadata key name by code */
254+
extern const char *iproto_metadata_key_strs[];
263255

264256
#define IPROTO_BALLOT_KEYS(_) \
265257
_(IS_RO_CFG, 0x01) \
@@ -276,13 +268,11 @@ extern const size_t iproto_metadata_key_constants_size;
276268

277269
enum iproto_ballot_key {
278270
IPROTO_BALLOT_KEYS(IPROTO_BALLOT_KEY_MEMBER)
271+
iproto_ballot_key_MAX
279272
};
280273

281-
/** Constants generated from IPROTO_BALLOT_KEYS. */
282-
extern const struct iproto_constant iproto_ballot_key_constants[];
283-
284-
/** Size of iproto_ballot_key_constants. */
285-
extern const size_t iproto_ballot_key_constants_size;
274+
/** IPROTO ballot key name by code */
275+
extern const char *iproto_ballot_key_strs[];
286276

287277
static inline uint64_t
288278
iproto_key_bit(unsigned char key)
@@ -432,13 +422,11 @@ extern const char *iproto_type_strs[];
432422

433423
enum iproto_raft_key {
434424
IPROTO_RAFT_KEYS(IPROTO_RAFT_KEY_MEMBER)
425+
iproto_raft_key_MAX
435426
};
436427

437-
/** Constants generated from IPROTO_RAFT_KEYS. */
438-
extern const struct iproto_constant iproto_raft_keys_constants[];
439-
440-
/** Size of iproto_raft_keys_constants. */
441-
extern const size_t iproto_raft_keys_constants_size;
428+
/** IPROTO raft key name by code */
429+
extern const char *iproto_raft_key_strs[];
442430

443431
/**
444432
* Returns IPROTO type name by @a type code.

src/box/iproto_features.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
#include "msgpuck.h"
1111

12+
#define IPROTO_FEATURE_ID_STRS_MEMBER(s, ...) \
13+
[IPROTO_FEATURE_ ## s] = #s,
14+
15+
const char *iproto_feature_id_strs[iproto_feature_id_MAX] = {
16+
IPROTO_FEATURES(IPROTO_FEATURE_ID_STRS_MEMBER)
17+
};
18+
1219
struct iproto_features IPROTO_CURRENT_FEATURES;
1320

1421
uint32_t
@@ -54,13 +61,6 @@ mp_decode_iproto_features(const char **data, struct iproto_features *features)
5461
return 0;
5562
}
5663

57-
const struct iproto_constant iproto_feature_id_constants[] = {
58-
IPROTO_FEATURES(IPROTO_CONSTANT_MEMBER)
59-
};
60-
61-
const size_t iproto_feature_id_constants_size =
62-
lengthof(iproto_feature_id_constants);
63-
6464
void
6565
iproto_features_init(void)
6666
{

src/box/iproto_features.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <string.h>
1111

1212
#include "bit/bit.h"
13-
#include "iproto_constants.h"
1413

1514
#if defined(__cplusplus)
1615
extern "C" {
@@ -66,11 +65,8 @@ enum iproto_feature_id {
6665
iproto_feature_id_MAX
6766
};
6867

69-
/** Constants generated from IPROTO_FEATURES. */
70-
extern const struct iproto_constant iproto_feature_id_constants[];
71-
72-
/** Size of iproto_feature_id_constants. */
73-
extern const size_t iproto_feature_id_constants_size;
68+
/** IPROTO feature name by id. */
69+
extern const char *iproto_feature_id_strs[];
7470

7571
/**
7672
* IPROTO protocol feature bit map.

src/box/lua/iproto.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,18 @@
2828

2929
struct mh_strnu32_t *iproto_key_translation;
3030

31-
/**
32-
* Pushes an array of IPROTO constants onto Lua stack.
33-
*/
34-
static void
35-
push_iproto_constant_subnamespace(struct lua_State *L, const char *subnamespace,
36-
const struct iproto_constant *constants,
37-
int constants_len)
38-
{
39-
lua_createtable(L, 0, constants_len);
40-
for (int i = 0; i < constants_len; ++i) {
41-
const char *name = constants[i].name;
42-
int value = constants[i].value;
43-
lua_pushinteger(L, value);
44-
lua_setfield(L, -2, name);
45-
}
46-
lua_setfield(L, -2, subnamespace);
47-
}
48-
4931
/**
5032
* Pushes IPROTO constants generated from `IPROTO_FLAGS` onto Lua stack.
5133
*/
5234
static void
53-
push_iproto_flag_constants(struct lua_State *L)
35+
push_iproto_flag_enum(struct lua_State *L)
5436
{
55-
push_iproto_constant_subnamespace(L, "flag", iproto_flag_constants,
56-
iproto_flag_constants_size);
37+
lua_newtable(L);
38+
for (int i = 0; i < iproto_flag_bit_MAX; i++) {
39+
lua_pushinteger(L, 1ULL << i);
40+
lua_setfield(L, -2, iproto_flag_bit_strs[i]);
41+
}
42+
lua_setfield(L, -2, "flag");
5743
}
5844

5945
/**
@@ -93,9 +79,13 @@ push_iproto_key_enum(struct lua_State *L)
9379
static void
9480
push_iproto_metadata_key_enum(struct lua_State *L)
9581
{
96-
push_iproto_constant_subnamespace(L, "metadata_key",
97-
iproto_metadata_key_constants,
98-
iproto_metadata_key_constants_size);
82+
lua_newtable(L);
83+
for (int i = 0; i < iproto_metadata_key_MAX; i++) {
84+
const char *name = iproto_metadata_key_strs[i];
85+
lua_pushinteger(L, i);
86+
lua_setfield(L, -2, name);
87+
}
88+
lua_setfield(L, -2, "metadata_key");
9989
}
10090

10191
/**
@@ -104,9 +94,15 @@ push_iproto_metadata_key_enum(struct lua_State *L)
10494
static void
10595
push_iproto_ballot_key_enum(struct lua_State *L)
10696
{
107-
push_iproto_constant_subnamespace(L, "ballot_key",
108-
iproto_ballot_key_constants,
109-
iproto_ballot_key_constants_size);
97+
lua_newtable(L);
98+
for (int i = 0; i < iproto_ballot_key_MAX; i++) {
99+
const char *name = iproto_ballot_key_strs[i];
100+
if (name == NULL)
101+
continue;
102+
lua_pushinteger(L, i);
103+
lua_setfield(L, -2, name);
104+
}
105+
lua_setfield(L, -2, "ballot_key");
110106
}
111107

112108
/**
@@ -136,9 +132,13 @@ push_iproto_type_enum(struct lua_State *L)
136132
static void
137133
push_iproto_raft_keys_enum(struct lua_State *L)
138134
{
139-
push_iproto_constant_subnamespace(L, "raft_key",
140-
iproto_raft_keys_constants,
141-
iproto_raft_keys_constants_size);
135+
lua_newtable(L);
136+
for (int i = 0; i < iproto_raft_key_MAX; i++) {
137+
const char *name = iproto_raft_key_strs[i];
138+
lua_pushinteger(L, i);
139+
lua_setfield(L, -2, name);
140+
}
141+
lua_setfield(L, -2, "raft_key");
142142
}
143143

144144
/**
@@ -147,7 +147,7 @@ push_iproto_raft_keys_enum(struct lua_State *L)
147147
static void
148148
push_iproto_constants(struct lua_State *L)
149149
{
150-
push_iproto_flag_constants(L);
150+
push_iproto_flag_enum(L);
151151
push_iproto_key_enum(L);
152152
push_iproto_metadata_key_enum(L);
153153
push_iproto_ballot_key_enum(L);
@@ -164,15 +164,13 @@ push_iproto_protocol_features(struct lua_State *L)
164164
lua_pushinteger(L, IPROTO_CURRENT_VERSION);
165165
lua_setfield(L, -2, "protocol_version");
166166

167-
for (size_t i = 0; i < 2; ++i)
168-
lua_createtable(L, 0, iproto_feature_id_constants_size);
169-
for (size_t i = 0; i < iproto_feature_id_constants_size; ++i) {
170-
struct iproto_constant constant =
171-
iproto_feature_id_constants[i];
172-
char *name = strtolowerdup(constant.name);
167+
for (int i = 0; i < 2; i++)
168+
lua_newtable(L);
169+
for (int i = 0; i < iproto_feature_id_MAX; i++) {
170+
char *name = strtolowerdup(iproto_feature_id_strs[i]);
173171
lua_pushboolean(L, true);
174172
lua_setfield(L, -2, name);
175-
lua_pushinteger(L, iproto_feature_id_constants[i].value);
173+
lua_pushinteger(L, i);
176174
lua_setfield(L, -3, name);
177175
free(name);
178176
}

0 commit comments

Comments
 (0)