From 97f090b06165cf5f1bc153e304a09616f16ac188 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Thu, 3 May 2012 16:07:09 +0400
Subject: [PATCH] Don't use word 'cardinality' for things it doesn't mean.

---
 mod/box/box.h       |   9 +++-
 mod/box/box.lua     |  34 +++++++-------
 mod/box/box.m       | 112 ++++++++++++++++++++++----------------------
 mod/box/box_lua.m   |  54 ++++++++++-----------
 mod/box/index.h     |   4 +-
 mod/box/memcached.m |   6 +--
 mod/box/tree.m      |  26 +++++-----
 mod/box/tuple.h     |   6 +--
 mod/box/tuple.m     |   8 ++--
 src/pickle.m        |   4 +-
 10 files changed, 133 insertions(+), 130 deletions(-)

diff --git a/mod/box/box.h b/mod/box/box.h
index 4b7cbf8768..bf94f4c8e7 100644
--- a/mod/box/box.h
+++ b/mod/box/box.h
@@ -45,7 +45,12 @@ enum
 
 struct space {
 	Index *index[BOX_INDEX_MAX];
-	int cardinality;
+	/** If not set (is 0), any tuple in the
+	 * space can have any number of fields (but
+	 * @sa max_fieldno). If set, Each tuple
+	 * must have exactly this many fields.
+	 */
+	int arity;
 
 	/**
 	 * The number of indexes in the space.
@@ -75,7 +80,7 @@ struct space {
 	 * Each tuple in this space must have, therefore, at least
 	 * field_count fields.
 	 */
-	int field_count;
+	int max_fieldno;
 
 	bool enabled;
 };
diff --git a/mod/box/box.lua b/mod/box/box.lua
index deecd810dc..94f31c6d6d 100644
--- a/mod/box/box.lua
+++ b/mod/box/box.lua
@@ -29,15 +29,15 @@ box.flags = create_const_table(
 --
 --
 function box.select_limit(space, index, offset, limit, ...)
-    local cardinality = select('#', ...)
+    local part_count = select('#', ...)
     return box.process(17,
-                       box.pack('iiiiii'..string.rep('p', cardinality),
+                       box.pack('iiiiii'..string.rep('p', part_count),
                                  space,
                                  index,
                                  offset,
                                  limit,
                                  1, -- key count
-                                 cardinality, -- key cardinality
+                                 part_count, -- key part count
                                  ...))
 end
 
@@ -45,15 +45,15 @@ end
 --
 --
 function box.select(space, index, ...)
-    local cardinality = select('#', ...)
+    local part_count = select('#', ...)
     return box.process(17,
-                       box.pack('iiiiii'..string.rep('p', cardinality),
+                       box.pack('iiiiii'..string.rep('p', part_count),
                                  space,
                                  index,
                                  0, -- offset
                                  4294967295, -- limit
                                  1, -- key count
-                                 cardinality, -- key cardinality
+                                 part_count, -- key part count
                                  ...))
 end
 
@@ -80,35 +80,35 @@ end
 -- index is always 0. It doesn't accept compound keys
 --
 function box.delete(space, ...)
-    local cardinality = select('#', ...)
+    local part_count = select('#', ...)
     return box.process(21,
-                       box.pack('iii'..string.rep('p', cardinality),
+                       box.pack('iii'..string.rep('p', part_count),
                                  space,
                                  box.flags.BOX_RETURN_TUPLE,  -- flags
-                                 cardinality, -- key cardinality
+                                 part_count, -- key part count
                                  ...))
 end
 
 -- insert or replace a tuple
 function box.replace(space, ...)
-    local cardinality = select('#', ...)
+    local part_count = select('#', ...)
     return box.process(13,
-                       box.pack('iii'..string.rep('p', cardinality),
+                       box.pack('iii'..string.rep('p', part_count),
                                  space,
                                  box.flags.BOX_RETURN_TUPLE,  -- flags
-                                 cardinality, -- cardinality
+                                 part_count, -- key part count
                                  ...))
 end
 
 -- insert a tuple (produces an error if the tuple already exists)
 function box.insert(space, ...)
-    local cardinality = select('#', ...)
+    local part_count = select('#', ...)
     return box.process(13,
-                       box.pack('iii'..string.rep('p', cardinality),
+                       box.pack('iii'..string.rep('p', part_count),
                                 space,
                                 bit.bor(box.flags.BOX_RETURN_TUPLE,
                                         box.flags.BOX_ADD),  -- flags
-                                cardinality, -- cardinality
+                                part_count, -- key part count
                                 ...))
 end
 
@@ -119,7 +119,7 @@ function box.update(space, key, format, ...)
                        box.pack('iiipi'..format,
                                   space,
                                   1, -- flags, BOX_RETURN_TUPLE
-                                  1, -- cardinality
+                                  1, -- primary key part count
                                   key, -- primary key
                                   op_count, -- op count
                                   ...))
@@ -202,7 +202,7 @@ function box.update_ol(space, ops_list, ...)
 
     -- fill UPDATE command key
     format = format .. 'i'
-    table.insert(args_list, #key) -- key cardinality
+    table.insert(args_list, #key) -- key part count
     for itr, val in ipairs(key) do
         format = format .. 'p'
         table.insert(args_list, val) -- key field
diff --git a/mod/box/box.m b/mod/box/box.m
index 391402f3d7..b61f53117b 100644
--- a/mod/box/box.m
+++ b/mod/box/box.m
@@ -153,12 +153,12 @@ validate_indexes(struct box_txn *txn)
 	}
 
 	/* Check to see if the tuple has a sufficient number of fields. */
-	if (txn->tuple->cardinality < sp->field_count)
+	if (txn->tuple->field_count < sp->max_fieldno)
 		tnt_raise(IllegalParams, :"tuple must have all indexed fields");
 
 	/* Sweep through the tuple and check the field sizes. */
 	u8 *data = txn->tuple->data;
-	for (int f = 0; f < sp->field_count; ++f) {
+	for (int f = 0; f < sp->max_fieldno; ++f) {
 		/* Get the size of the current field and advance. */
 		u32 len = load_varint32((void **) &data);
 		data += len;
@@ -186,34 +186,34 @@ validate_indexes(struct box_txn *txn)
 }
 
 static void
-read_key(struct tbuf *data, void **key_ptr, u32 *key_cardinality_ptr)
+read_key(struct tbuf *data, void **key_ptr, u32 *key_part_count_ptr)
 {
 	void *key = NULL;
-	u32 key_cardinality = read_u32(data);
-	if (key_cardinality) {
+	u32 key_part_count = read_u32(data);
+	if (key_part_count) {
 		key = read_field(data);
 		/* advance remaining fields of a key */
-		for (int i = 1; i < key_cardinality; i++)
+		for (int i = 1; i < key_part_count; i++)
 			read_field(data);
 	}
 
 	*key_ptr = key;
-	*key_cardinality_ptr = key_cardinality;
+	*key_part_count_ptr = key_part_count;
 }
 
 static void __attribute__((noinline))
-prepare_replace(struct box_txn *txn, size_t cardinality, struct tbuf *data)
+prepare_replace(struct box_txn *txn, size_t field_count, struct tbuf *data)
 {
 	assert(data != NULL);
-	if (cardinality == 0)
-		tnt_raise(IllegalParams, :"tuple cardinality is 0");
+	if (field_count == 0)
+		tnt_raise(IllegalParams, :"tuple field count is 0");
 
-	if (data->size == 0 || data->size != valid_tuple(data, cardinality))
+	if (data->size == 0 || data->size != valid_tuple(data, field_count))
 		tnt_raise(IllegalParams, :"incorrect tuple length");
 
 	txn->tuple = tuple_alloc(data->size);
 	tuple_txn_ref(txn, txn->tuple);
-	txn->tuple->cardinality = cardinality;
+	txn->tuple->field_count = field_count;
 	memcpy(txn->tuple->data, data->data, data->size);
 
 	txn->old_tuple = [txn->index findByTuple: txn->tuple];
@@ -416,8 +416,8 @@ struct update_field {
 struct update_cmd {
 	/** Search key */
 	void *key;
-	/** Search key cardinality */
-	u32 key_cardinality;
+	/** Search key part count. */
+	u32 key_part_count;
 	/** Operations. */
 	struct update_op *op;
 	struct update_op *op_end;
@@ -733,7 +733,7 @@ parse_update_cmd(struct tbuf *data)
 	struct update_cmd *cmd = palloc(fiber->gc_pool,
 					sizeof(struct update_cmd));
 
-	read_key(data, &cmd->key, &cmd->key_cardinality);
+	read_key(data, &cmd->key, &cmd->key_part_count);
 	/* number of operations */
 	u32 op_cnt = read_u32(data);
 	if (op_cnt > BOX_UPDATE_OP_CNT_MAX)
@@ -858,7 +858,7 @@ init_update_operations(struct box_txn *txn, struct update_cmd *cmd)
 	struct update_op *op = cmd->op;
 	struct update_field *field = cmd->field;
 	void *old_data = txn->old_tuple->data;
-	int old_field_count = txn->old_tuple->cardinality;
+	int old_field_count = txn->old_tuple->field_count;
 
 	update_field_init(field, op, &old_data, old_field_count);
 	do {
@@ -952,12 +952,12 @@ do_update(struct box_txn *txn, struct update_cmd *cmd)
 	void *new_data = txn->tuple->data;
 	void *new_data_end = new_data + txn->tuple->bsize;
 	struct update_field *field;
-	txn->tuple->cardinality = 0;
+	txn->tuple->field_count = 0;
 
 	for (field = cmd->field; field < cmd->field_end; field++) {
 		if (field->first < field->end) { /* -> field is not deleted. */
 			new_data = save_varint32(new_data, field->new_len);
-			txn->tuple->cardinality++;
+			txn->tuple->field_count++;
 		}
 		void *new_field = new_data;
 		void *old_field = field->old;
@@ -1003,7 +1003,7 @@ do_update(struct box_txn *txn, struct update_cmd *cmd)
 		if (field->tail_field_count) {
 			memcpy(new_data, field->tail, field->tail_len);
 			new_data += field->tail_len;
-			txn->tuple->cardinality += field->tail_field_count;
+			txn->tuple->field_count += field->tail_field_count;
 		}
 	}
 }
@@ -1017,7 +1017,7 @@ prepare_update(struct box_txn *txn, struct tbuf *data)
 	struct update_cmd *cmd = parse_update_cmd(data);
 
 	/* Try to find the tuple. */
-	txn->old_tuple = [txn->index findByKey :cmd->key :cmd->key_cardinality];
+	txn->old_tuple = [txn->index findByKey :cmd->key :cmd->key_part_count];
 	if (txn->old_tuple == NULL) {
 		/* Not found. For simplicity, skip the logging. */
 		txn->flags |= BOX_NOT_STORE;
@@ -1065,12 +1065,12 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data)
 			return;
 
 		/* read key */
-		u32 key_cardinality;
+		u32 key_part_count;
 		void *key;
-		read_key(data, &key, &key_cardinality);
+		read_key(data, &key, &key_part_count);
 
 		struct iterator *it = index->position;
-		[index initIteratorByKey: it :ITER_FORWARD :key :key_cardinality];
+		[index initIteratorByKey: it :ITER_FORWARD :key :key_part_count];
 
 		while ((tuple = it->next_equal(it)) != NULL) {
 			if (tuple->flags & GHOST)
@@ -1097,11 +1097,11 @@ prepare_delete(struct box_txn *txn, struct tbuf *data)
 	u32 tuples_affected = 0;
 
 	/* read key */
-	u32 key_cardinality;
+	u32 key_part_count;
 	void *key;
-	read_key(data, &key, &key_cardinality);
+	read_key(data, &key, &key_part_count);
 	/* try to find tuple in primary index */
-	txn->old_tuple = [txn->index findByKey :key :key_cardinality];
+	txn->old_tuple = [txn->index findByKey :key :key_part_count];
 
 	if (txn->old_tuple == NULL) {
 		/*
@@ -1302,7 +1302,7 @@ txn_rollback(struct box_txn *txn)
 static void
 box_dispatch(struct box_txn *txn, struct tbuf *data)
 {
-	u32 cardinality;
+	u32 field_count;
 
 	say_debug("box_dispatch(%i)", txn->op);
 
@@ -1310,11 +1310,11 @@ box_dispatch(struct box_txn *txn, struct tbuf *data)
 	case REPLACE:
 		txn_assign_n(txn, data);
 		txn->flags |= read_u32(data) & BOX_ALLOWED_REQUEST_FLAGS;
-		cardinality = read_u32(data);
-		if (space[txn->n].cardinality > 0
-		    && space[txn->n].cardinality != cardinality)
-			tnt_raise(IllegalParams, :"tuple cardinality must match space cardinality");
-		prepare_replace(txn, cardinality, data);
+		field_count = read_u32(data);
+		if (space[txn->n].arity > 0
+		    && space[txn->n].arity != field_count)
+			tnt_raise(IllegalParams, :"tuple field count must match space cardinality");
+		prepare_replace(txn, field_count, data);
 		break;
 
 	case DELETE:
@@ -1372,7 +1372,7 @@ box_xlog_sprint(struct tbuf *buf, const struct tbuf *t)
 
 	u32 n, key_len;
 	void *key;
-	u32 cardinality, field_no;
+	u32 field_count, field_no;
 	u32 flags;
 	u32 op_cnt;
 
@@ -1392,10 +1392,10 @@ box_xlog_sprint(struct tbuf *buf, const struct tbuf *t)
 	switch (op) {
 	case REPLACE:
 		flags = read_u32(b);
-		cardinality = read_u32(b);
-		if (b->size != valid_tuple(b, cardinality))
+		field_count = read_u32(b);
+		if (b->size != valid_tuple(b, field_count))
 			abort();
-		tuple_print(buf, cardinality, b->data);
+		tuple_print(buf, field_count, b->data);
 		break;
 
 	case DELETE:
@@ -1516,7 +1516,7 @@ key_init(struct key_def *def, struct tarantool_cfg_space_index *cfg_index)
 	def->max_fieldno = 0;
 	def->part_count = 0;
 
-	/* Calculate key cardinality and maximal field number. */
+	/* Calculate key part count and maximal field number. */
 	for (int k = 0; cfg_index->key_field[k] != NULL; ++k) {
 		typeof(cfg_index->key_field[k]) cfg_key = cfg_index->key_field[k];
 
@@ -1571,20 +1571,20 @@ key_init(struct key_def *def, struct tarantool_cfg_space_index *cfg_index)
 static void
 space_init_field_types(struct space *space)
 {
-	int i, field_count;
+	int i, max_fieldno;
 	int key_count = space->key_count;
 	struct key_def *key_defs = space->key_defs;
 
 	/* find max max field no */
-	field_count = 0;
+	max_fieldno = 0;
 	for (i = 0; i < key_count; i++) {
-		field_count = MAX(field_count, key_defs[i].max_fieldno);
+		max_fieldno= MAX(max_fieldno, key_defs[i].max_fieldno);
 	}
 
 	/* alloc & init field type info */
-	space->field_count = field_count;
-	space->field_types = malloc(field_count * sizeof(enum field_data_type));
-	for (i = 0; i < field_count; i++) {
+	space->max_fieldno = max_fieldno;
+	space->field_types = malloc(max_fieldno * sizeof(enum field_data_type));
+	for (i = 0; i < max_fieldno; i++) {
 		space->field_types[i] = UNKNOWN;
 	}
 
@@ -1593,7 +1593,7 @@ space_init_field_types(struct space *space)
 		struct key_def *def = &key_defs[i];
 		for (int pi = 0; pi < def->part_count; pi++) {
 			struct key_part *part = &def->parts[pi];
-			assert(part->fieldno < field_count);
+			assert(part->fieldno < max_fieldno);
 			space->field_types[part->fieldno] = part->type;
 		}
 	}
@@ -1628,7 +1628,7 @@ space_config(void)
 		assert(cfg.memcached_port == 0 || i != cfg.memcached_space);
 
 		space[i].enabled = true;
-		space[i].cardinality = cfg_space->cardinality;
+		space[i].arity = cfg_space->cardinality;
 
 		/*
 		 * Collect key/field info. We need aggregate
@@ -1897,7 +1897,7 @@ check_spaces(struct tarantool_cfg *conf)
 		/* check spaces indexes */
 		for (size_t j = 0; space->index[j] != NULL; ++j) {
 			typeof(space->index[j]) index = space->index[j];
-			u32 index_cardinality = 0;
+			u32 key_part_count = 0;
 			enum index_type index_type;
 
 			/* check index bound */
@@ -1941,11 +1941,11 @@ check_spaces(struct tarantool_cfg *conf)
 					max_key_fieldno = key->fieldno;
 				}
 
-				++index_cardinality;
+				++key_part_count;
 			}
 
-			/* check index cardinality */
-			if (index_cardinality == 0) {
+			/* Check key part count. */
+			if (key_part_count == 0) {
 				out_warning(0, "(space = %zu index = %zu) "
 					    "at least one field must be defined", i, j);
 				return -1;
@@ -1960,19 +1960,17 @@ check_spaces(struct tarantool_cfg *conf)
 				return -1;
 			}
 
-			/* first space index must be unique and cardinality == 1 */
-			if (j == 0) {
-				if (index->unique == false) {
-					out_warning(0, "(space = %zu) space first index must be unique", i);
-					return -1;
-				}
+			/* First index must be unique. */
+			if (j == 0 && index->unique == false) {
+				out_warning(0, "(space = %zu) space first index must be unique", i);
+				return -1;
 			}
 
 			switch (index_type) {
 			case HASH:
 				/* check hash index */
 				/* hash index must has single-field key */
-				if (index_cardinality != 1) {
+				if (key_part_count != 1) {
 					out_warning(0, "(space = %zu index = %zu) "
 					            "hash index must has a single-field key", i, j);
 					return -1;
@@ -2209,7 +2207,7 @@ snapshot_write_tuple(struct log_io_iter *i, unsigned n, struct box_tuple *tuple)
 		return;
 
 	header.space = n;
-	header.tuple_size = tuple->cardinality;
+	header.tuple_size = tuple->field_count;
 	header.data_size = tuple->bsize;
 
 	row = tbuf_alloc(fiber->gc_pool);
diff --git a/mod/box/box_lua.m b/mod/box/box_lua.m
index 51da63dc0a..c161c3c697 100644
--- a/mod/box/box_lua.m
+++ b/mod/box/box_lua.m
@@ -105,7 +105,7 @@ static int
 lbox_tuple_len(struct lua_State *L)
 {
 	struct box_tuple *tuple = lua_checktuple(L, 1);
-	lua_pushnumber(L, tuple->cardinality);
+	lua_pushnumber(L, tuple->field_count);
 	return 1;
 }
 
@@ -125,15 +125,15 @@ lbox_tuple_slice(struct lua_State *L)
 		luaL_error(L, "tuple.slice(): bad arguments");
 	start = lua_tointeger(L, 2);
 	if (start < 0)
-		start += tuple->cardinality;
+		start += tuple->field_count;
 	if (argc == 2) {
 		end = lua_tointeger(L, 3);
 		if (end < 0)
-			end += tuple->cardinality;
-		else if (end > tuple->cardinality)
-			end = tuple->cardinality;
+			end += tuple->field_count;
+		else if (end > tuple->field_count)
+			end = tuple->field_count;
 	} else {
-		end = tuple->cardinality;
+		end = tuple->field_count;
 	}
 	if (end <= start)
 		luaL_error(L, "tuple.slice(): start must be less than end");
@@ -166,8 +166,8 @@ lbox_tuple_unpack(struct lua_State *L)
 		lua_pushlstring(L, (char *) field, len);
 		field += len;
 	}
-	assert(lua_gettop(L) == tuple->cardinality + 1);
-	return tuple->cardinality;
+	assert(lua_gettop(L) == tuple->field_count + 1);
+	return tuple->field_count;
 }
 
 /**
@@ -184,9 +184,9 @@ lbox_tuple_index(struct lua_State *L)
 	/* For integer indexes, implement [] operator */
 	if (lua_isnumber(L, 2)) {
 		int i = luaL_checkint(L, 2);
-		if (i >= tuple->cardinality)
+		if (i >= tuple->field_count)
 			luaL_error(L, "%s: index %d is out of bounds (0..%d)",
-				   tuplelib_name, i, tuple->cardinality-1);
+				   tuplelib_name, i, tuple->field_count-1);
 		void *field = tuple_field(tuple, i);
 		u32 len = load_varint32(&field);
 		lua_pushlstring(L, field, len);
@@ -204,7 +204,7 @@ lbox_tuple_tostring(struct lua_State *L)
 	struct box_tuple *tuple = lua_checktuple(L, 1);
 	/* @todo: print the tuple */
 	struct tbuf *tbuf = tbuf_alloc(fiber->gc_pool);
-	tuple_print(tbuf, tuple->cardinality, tuple->data);
+	tuple_print(tbuf, tuple->field_count, tuple->data);
 	lua_pushlstring(L, tbuf->data, tbuf->size);
 	return 1;
 }
@@ -463,17 +463,17 @@ lbox_index_move(struct lua_State *L, enum iterator_type type)
 		 * userdata: must be a key to start iteration from
 		 * an offset. Seed the iterator with this key.
 		 */
-		int cardinality;
+		int field_count;
 		void *key;
 
 		if (argc == 1 && lua_type(L, 2) == LUA_TUSERDATA) {
 			/* Searching by tuple. */
 			struct box_tuple *tuple = lua_checktuple(L, 2);
 			key = tuple->data;
-			cardinality = tuple->cardinality;
+			field_count = tuple->field_count;
 		} else {
 			/* Single or multi- part key. */
-			cardinality = argc;
+			field_count = argc;
 			struct tbuf *data = tbuf_alloc(fiber->gc_pool);
 			for (int i = 0; i < argc; ++i)
 				append_key_part(L, i + 2, data,
@@ -485,13 +485,13 @@ lbox_index_move(struct lua_State *L, enum iterator_type type)
 		 * indexes. HASH indexes can only use single-part
 		 * keys.
 		*/
-		assert(cardinality != 0);
-		if (cardinality > index->key_def->part_count)
+		assert(field_count != 0);
+		if (field_count > index->key_def->part_count)
 			luaL_error(L, "index.next(): key part count (%d) "
-				   "does not match index cardinality (%d)",
-				   cardinality, index->key_def->part_count);
+				   "does not match index field count (%d)",
+				   field_count, index->key_def->part_count);
 		it = [index allocIterator];
-		[index initIteratorByKey: it :type :key :cardinality];
+		[index initIteratorByKey: it :type :key :field_count];
 		lbox_pushiterator(L, it);
 	} else { /* 1 item on the stack and it's a userdata. */
 		it = lua_checkiterator(L, 2);
@@ -558,14 +558,14 @@ static const struct luaL_reg lbox_iterator_meta[] = {
 static void
 iov_add_lua_table(struct lua_State *L, int index)
 {
-	u32 *cardinality = palloc(fiber->gc_pool, sizeof(u32));
+	u32 *field_count = palloc(fiber->gc_pool, sizeof(u32));
 	u32 *tuple_len = palloc(fiber->gc_pool, sizeof(u32));
 
-	*cardinality = 0;
+	*field_count = 0;
 	*tuple_len = 0;
 
 	iov_add(tuple_len, sizeof(u32));
-	iov_add(cardinality, sizeof(u32));
+	iov_add(field_count, sizeof(u32));
 
 	u8 field_len_buf[5];
 	size_t field_len, field_len_len;
@@ -573,7 +573,7 @@ iov_add_lua_table(struct lua_State *L, int index)
 
 	lua_pushnil(L);  /* first key */
 	while (lua_next(L, index) != 0) {
-		++*cardinality;
+		++*field_count;
 
 		switch (lua_type(L, -1)) {
 		case LUA_TNUMBER:
@@ -635,7 +635,7 @@ void iov_add_ret(struct lua_State *L, int index)
 		size_t len = sizeof(u32);
 		u32 num = lua_tointeger(L, index);
 		tuple = tuple_alloc(len + varint32_sizeof(len));
-		tuple->cardinality = 1;
+		tuple->field_count = 1;
 		memcpy(save_varint32(tuple->data, len), &num, len);
 		break;
 	}
@@ -644,7 +644,7 @@ void iov_add_ret(struct lua_State *L, int index)
 		u64 num = tarantool_lua_tointeger64(L, index);
 		size_t len = sizeof(u64);
 		tuple = tuple_alloc(len + varint32_sizeof(len));
-		tuple->cardinality = 1;
+		tuple->field_count = 1;
 		memcpy(save_varint32(tuple->data, len), &num, len);
 		break;
 	}
@@ -653,7 +653,7 @@ void iov_add_ret(struct lua_State *L, int index)
 		size_t len;
 		const char *str = lua_tolstring(L, index, &len);
 		tuple = tuple_alloc(len + varint32_sizeof(len));
-		tuple->cardinality = 1;
+		tuple->field_count = 1;
 		memcpy(save_varint32(tuple->data, len), str, len);
 		break;
 	}
@@ -663,7 +663,7 @@ void iov_add_ret(struct lua_State *L, int index)
 		const char *str = tarantool_lua_tostring(L, index);
 		size_t len = strlen(str);
 		tuple = tuple_alloc(len + varint32_sizeof(len));
-		tuple->cardinality = 1;
+		tuple->field_count = 1;
 		memcpy(save_varint32(tuple->data, len), str, len);
 		break;
 	}
diff --git a/mod/box/index.h b/mod/box/index.h
index 8473edabbb..430d31d4ac 100644
--- a/mod/box/index.h
+++ b/mod/box/index.h
@@ -132,11 +132,11 @@ struct key_def {
 			:(enum iterator_type) type
 			:(void *) key :(int) part_count;
 /**
- * Check key cardinality.
+ * Check key part count.
  */
 - (void) checkKeyParts: (int) part_count :(bool) partial_key_allowed;
 /**
- * Unsafe search methods that do not check key cardinality.
+ * Unsafe search methods that do not check key part count.
  */
 - (struct box_tuple *) findUnsafe: (void *) key :(int) part_count;
 - (void) initIteratorUnsafe: (struct iterator *) iterator
diff --git a/mod/box/memcached.m b/mod/box/memcached.m
index 3669d19eff..a7ad8997b1 100644
--- a/mod/box/memcached.m
+++ b/mod/box/memcached.m
@@ -71,7 +71,7 @@ static void
 store(void *key, u32 exptime, u32 flags, u32 bytes, u8 *data)
 {
 	u32 box_flags = 0;
-	u32 cardinality = 4;
+	u32 field_count = 4;
 	static u64 cas = 42;
 	struct meta m;
 
@@ -79,7 +79,7 @@ store(void *key, u32 exptime, u32 flags, u32 bytes, u8 *data)
 
 	tbuf_append(req, &cfg.memcached_space, sizeof(u32));
 	tbuf_append(req, &box_flags, sizeof(box_flags));
-	tbuf_append(req, &cardinality, sizeof(cardinality));
+	tbuf_append(req, &field_count, sizeof(field_count));
 
 	tbuf_append_field(req, key);
 
@@ -435,7 +435,7 @@ memcached_space_init()
 	/* Configure memcached space. */
 	struct space *memc_s = &space[cfg.memcached_space];
 	memc_s->enabled = true;
-	memc_s->cardinality = 4;
+	memc_s->arity = 4;
 
 	memc_s->key_count = 1;
 	memc_s->key_defs = malloc(sizeof(struct key_def));
diff --git a/mod/box/tree.m b/mod/box/tree.m
index d2c6d08ad1..37b8367850 100644
--- a/mod/box/tree.m
+++ b/mod/box/tree.m
@@ -197,7 +197,7 @@ find_fixed_offset(struct space *space, int fieldno, int skip)
 
 	while (i < fieldno) {
 		/* if the field is unknown give up on it */
-		if (i >= space->field_count || space->field_types[i] == UNKNOWN) {
+		if (i >= space->max_fieldno || space->field_types[i] == UNKNOWN) {
 			return -1;
 		}
 
@@ -300,7 +300,7 @@ fold_with_sparse_parts(struct key_def *key_def, struct box_tuple *tuple, union s
 	memset(parts, 0, sizeof(parts[0]) * key_def->part_count);
 
 	for (int field = 0; field < key_def->max_fieldno; ++field) {
-		assert(field < tuple->cardinality);
+		assert(field < tuple->field_count);
 
 		u8 *data = part_data;
 		u32 len = load_varint32((void**) &data);
@@ -375,7 +375,7 @@ fold_with_dense_offset(struct key_def *key_def, struct box_tuple *tuple)
 	u8 *tuple_data = tuple->data;
 
 	for (int field = 0; field < key_def->max_fieldno; ++field) {
-		assert(field < tuple->cardinality);
+		assert(field < tuple->field_count);
 
 		u8 *data = tuple_data;
 		u32 len = load_varint32((void**) &data);
@@ -400,7 +400,7 @@ fold_with_num32_value(struct key_def *key_def, struct box_tuple *tuple)
 	u8 *tuple_data = tuple->data;
 
 	for (int field = 0; field < key_def->max_fieldno; ++field) {
-		assert(field < tuple->cardinality);
+		assert(field < tuple->field_count);
 
 		u8 *data = tuple_data;
 		u32 len = load_varint32((void**) &data);
@@ -541,8 +541,8 @@ dense_node_compare(struct key_def *key_def, u32 first_field,
 		   struct box_tuple *tuple_b, u32 offset_b)
 {
 	int part_count = key_def->part_count;
-	assert(first_field + part_count <= tuple_a->cardinality);
-	assert(first_field + part_count <= tuple_b->cardinality);
+	assert(first_field + part_count <= tuple_a->field_count);
+	assert(first_field + part_count <= tuple_b->field_count);
 
 	/* Allocate space for offsets. */
 	u32 *off_a = alloca(2 * part_count * sizeof(u32));
@@ -590,8 +590,8 @@ linear_node_compare(struct key_def *key_def,
 		    struct box_tuple *tuple_b, u32 offset_b)
 {
 	int part_count = key_def->part_count;
-	assert(first_field + part_count <= tuple_a->cardinality);
-	assert(first_field + part_count <= tuple_b->cardinality);
+	assert(first_field + part_count <= tuple_a->field_count);
+	assert(first_field + part_count <= tuple_b->field_count);
 
 	/* Compare key parts. */
 	u8 *ad = tuple_a->data + offset_a;
@@ -660,7 +660,7 @@ dense_key_node_compare(struct key_def *key_def,
 		       u32 first_field, struct box_tuple *tuple, u32 offset)
 {
 	int part_count = key_def->part_count;
-	assert(first_field + part_count <= tuple->cardinality);
+	assert(first_field + part_count <= tuple->field_count);
 
 	/* Allocate space for offsets. */
 	u32 *off = alloca(part_count * sizeof(u32));
@@ -705,7 +705,7 @@ linear_key_node_compare(struct key_def *key_def,
 			struct box_tuple *tuple, u32 offset)
 {
 	int part_count = key_def->part_count;
-	assert(first_field + part_count <= tuple->cardinality);
+	assert(first_field + part_count <= tuple->field_count);
 
 	/* Compare key parts. */
 	if (part_count > key_data->part_count)
@@ -881,10 +881,10 @@ tree_iterator_free(struct iterator *iterator)
 - (struct box_tuple *) findByTuple: (struct box_tuple *) tuple
 {
 	struct key_data *key_data
-		= alloca(sizeof(struct key_data) + _SIZEOF_SPARSE_PARTS(tuple->cardinality));
+		= alloca(sizeof(struct key_data) + _SIZEOF_SPARSE_PARTS(tuple->field_count));
 
 	key_data->data = tuple->data;
-	key_data->part_count = tuple->cardinality;
+	key_data->part_count = tuple->field_count;
 	fold_with_sparse_parts(key_def, tuple, key_data->parts);
 
 	void *node = sptree_index_find(&tree, key_data);
@@ -901,7 +901,7 @@ tree_iterator_free(struct iterator *iterator)
 - (void) replace: (struct box_tuple *) old_tuple
 		: (struct box_tuple *) new_tuple
 {
-	if (new_tuple->cardinality < key_def->max_fieldno)
+	if (new_tuple->field_count < key_def->max_fieldno)
 		tnt_raise(ClientError, :ER_NO_SUCH_FIELD,
 			  key_def->max_fieldno);
 
diff --git a/mod/box/tuple.h b/mod/box/tuple.h
index 0a3ed5ca73..60afa24846 100644
--- a/mod/box/tuple.h
+++ b/mod/box/tuple.h
@@ -53,7 +53,7 @@ struct box_tuple
 	/** length of the variable part of the tuple */
 	u32 bsize;
 	/** number of fields in the variable part. */
-	u32 cardinality;
+	u32 field_count;
 	/**
 	 * Fields can have variable length, and thus are packed
 	 * into a contiguous byte array. Each field is prefixed
@@ -91,13 +91,13 @@ tuple_field(struct box_tuple *tuple, size_t i);
  * key: { value, value, value }
  */
 void
-tuple_print(struct tbuf *buf, uint8_t cardinality, void *f);
+tuple_print(struct tbuf *buf, uint8_t field_count, void *f);
 
 /** Tuple length when adding to iov. */
 static inline size_t tuple_len(struct box_tuple *tuple)
 {
 	return tuple->bsize + sizeof(tuple->bsize) +
-		sizeof(tuple->cardinality);
+		sizeof(tuple->field_count);
 }
 #endif /* TARANTOOL_BOX_TUPLE_H_INCLUDED */
 
diff --git a/mod/box/tuple.m b/mod/box/tuple.m
index b7b5512267..225830e2e4 100644
--- a/mod/box/tuple.m
+++ b/mod/box/tuple.m
@@ -94,7 +94,7 @@ tuple_field(struct box_tuple *tuple, size_t i)
 {
 	void *field = tuple->data;
 
-	if (i >= tuple->cardinality)
+	if (i >= tuple->field_count)
 		return NULL;
 
 	while (i-- > 0)
@@ -136,15 +136,15 @@ print_field(struct tbuf *buf, void *f)
  * key: { value, value, value }
  */
 void
-tuple_print(struct tbuf *buf, uint8_t cardinality, void *f)
+tuple_print(struct tbuf *buf, uint8_t field_count, void *f)
 {
 	print_field(buf, f);
 	tbuf_printf(buf, ": {");
 	f = next_field(f);
 
-	for (size_t i = 1; i < cardinality; i++, f = next_field(f)) {
+	for (size_t i = 1; i < field_count; i++, f = next_field(f)) {
 		print_field(buf, f);
-		if (likely(i + 1 < cardinality))
+		if (likely(i + 1 < field_count))
 			tbuf_printf(buf, ", ");
 	}
 	tbuf_printf(buf, "}");
diff --git a/src/pickle.m b/src/pickle.m
index b92d12d839..faa9086ae4 100644
--- a/src/pickle.m
+++ b/src/pickle.m
@@ -190,12 +190,12 @@ read_str(struct tbuf *buf, u32 size)
 }
 
 u32
-valid_tuple(struct tbuf *buf, u32 cardinality)
+valid_tuple(struct tbuf *buf, u32 field_count)
 {
 	void *data = buf->data;
 	u32 r, size = buf->size;
 
-	for (int i = 0; i < cardinality; i++)
+	for (int i = 0; i < field_count; i++)
 		read_field(buf);
 
 	r = size - buf->size;
-- 
GitLab