From 586cdecf81c1be417cc21a51ae6684bd20bf758b Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Thu, 18 Jul 2013 11:57:06 +0400
Subject: [PATCH] Rename space->n to space_id(). Remove key_def->cmp_order,
 which was unused.

Remove key_def->max_fieldno as well.
Rename space_n() to space_id(), space_by_n to space_by_id().
---
 src/box/box.cc           |  2 +-
 src/box/box_lua_space.cc |  4 ++--
 src/box/key_def.cc       | 13 -------------
 src/box/key_def.h        | 26 +++++++-------------------
 src/box/space.cc         | 25 +++++++++++++------------
 src/box/space.h          | 32 ++++++++++++++++----------------
 src/box/tuple.cc         |  8 ++++++--
 src/memcached.cc         |  6 +-----
 8 files changed, 46 insertions(+), 70 deletions(-)

diff --git a/src/box/box.cc b/src/box/box.cc
index 05bb028822..c3569e67bf 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -387,7 +387,7 @@ snapshot_space(struct space *sp, void *udata)
 	pk->initIterator(it, ITER_ALL, NULL, 0);
 
 	while ((tuple = it->next(it)))
-		snapshot_write_tuple(ud->l, ud->batch, space_n(sp), tuple);
+		snapshot_write_tuple(ud->l, ud->batch, space_id(sp), tuple);
 }
 
 void
diff --git a/src/box/box_lua_space.cc b/src/box/box_lua_space.cc
index 23b5a03706..d2fd3951a0 100644
--- a/src/box/box_lua_space.cc
+++ b/src/box/box_lua_space.cc
@@ -56,7 +56,7 @@ lbox_pushspace(struct lua_State *L, struct space *space)
 
 	/* space.n */
 	lua_pushstring(L, "n");
-	lua_pushnumber(L, space->no);
+	lua_pushnumber(L, space_id(space));
 	lua_settable(L, -3);
 
 	/* all exists spaces are enabled */
@@ -133,7 +133,7 @@ lbox_pushspace(struct lua_State *L, struct space *space)
 static void
 lbox_add_space(struct space *space, struct lua_State *L)
 {
-	lua_pushnumber(L, space->no);
+	lua_pushnumber(L, space_id(space));
 	lbox_pushspace(L, space);
 	lua_settable(L, -3);
 }
diff --git a/src/box/key_def.cc b/src/box/key_def.cc
index 9f1d4733a0..1e26da14cd 100644
--- a/src/box/key_def.cc
+++ b/src/box/key_def.cc
@@ -41,7 +41,6 @@ key_def_create(struct key_def *def, uint32_t id,
 	       struct tarantool_cfg_space_index *cfg_index)
 {
 	def->id = id;
-	def->max_fieldno = 0;
 	def->part_count = 0;
 
 	def->type = STR2ENUM(index_type, cfg_index->type);
@@ -57,7 +56,6 @@ key_def_create(struct key_def *def, uint32_t id,
 			break;
 		}
 
-		def->max_fieldno = MAX(def->max_fieldno, cfg_key->fieldno);
 		def->part_count++;
 	}
 
@@ -65,13 +63,6 @@ key_def_create(struct key_def *def, uint32_t id,
 	def->parts = (struct key_part *) malloc(sizeof(struct key_part) *
 						def->part_count);
 
-	uint32_t cmp_order_size = (def->max_fieldno + 1) * sizeof(uint32_t);
-	/* init compare order array */
-	def->cmp_order = (uint32_t *) malloc(cmp_order_size);
-
-	for (uint32_t fieldno = 0; fieldno <= def->max_fieldno; fieldno++)
-		def->cmp_order[fieldno] = UINT32_MAX;
-
 	/* fill fields and compare order */
 	for (uint32_t k = 0; cfg_index->key_field[k] != NULL; ++k) {
 		auto cfg_key = cfg_index->key_field[k];
@@ -84,9 +75,6 @@ key_def_create(struct key_def *def, uint32_t id,
 		/* fill keys */
 		def->parts[k].fieldno = cfg_key->fieldno;
 		def->parts[k].type = STR2ENUM(field_type, cfg_key->type);
-		/* fill compare order */
-		if (def->cmp_order[cfg_key->fieldno] == UINT32_MAX)
-			def->cmp_order[cfg_key->fieldno] = k;
 	}
 	def->is_unique = cfg_index->unique;
 }
@@ -96,6 +84,5 @@ void
 key_def_destroy(struct key_def *key_def)
 {
 	free(key_def->parts);
-	free(key_def->cmp_order);
 }
 
diff --git a/src/box/key_def.h b/src/box/key_def.h
index 4ff32ca2c9..fb297c91e7 100644
--- a/src/box/key_def.h
+++ b/src/box/key_def.h
@@ -62,28 +62,16 @@ struct key_part {
 
 /* Descriptor of a multipart key. */
 struct key_def {
+	/** Ordinal index number in the index array. */
 	uint32_t id;
-	/* Description of parts of a multipart index. */
-	struct key_part *parts;
-	/*
-	 * An array holding field positions in 'parts' array.
-	 * Imagine there is index[1] = { key_field[0].fieldno=5,
-	 * key_field[1].fieldno=3 }.
-	 * 'parts' array for such index contains data from
-	 * key_field[0] and key_field[1] respectively.
-	 * max_fieldno is 5, and cmp_order array holds offsets of
-	 * field 3 and 5 in 'parts' array: -1, -1, -1, 0, -1, 1.
-	 */
-	uint32_t *cmp_order;
-	/* The size of the 'parts' array. */
+	/** The size of the 'parts' array. */
 	uint32_t part_count;
-	/*
-	 * Max fieldno in 'parts' array. Defines the size of
-	 * cmp_order array (which is max_fieldno + 1).
-	 */
-	uint32_t max_fieldno;
-	bool is_unique;
+	/** Description of parts of a multipart index. */
+	struct key_part *parts;
+	/** Index type. */
 	enum index_type type;
+	/** Is this key unique. */
+	bool is_unique;
 };
 
 struct tarantool_cfg_space_index;
diff --git a/src/box/space.cc b/src/box/space.cc
index 3615fbd473..eadc687016 100644
--- a/src/box/space.cc
+++ b/src/box/space.cc
@@ -57,12 +57,12 @@ static bool primary_indexes_enabled = false;
 
 
 static void
-space_create(struct space *space, uint32_t space_no,
+space_create(struct space *space, uint32_t id,
 	     struct key_def *key_defs, uint32_t key_count,
 	     uint32_t arity)
 {
 	memset(space, 0, sizeof(struct space));
-	space->no = space_no;
+	space->id = id;
 	space->arity = arity;
 	space->key_count = key_count;
 	space->format = tuple_format_new(key_defs, key_count);
@@ -88,18 +88,18 @@ space_destroy(struct space *space)
 }
 
 struct space *
-space_new(uint32_t space_no, struct key_def *key_defs,
+space_new(uint32_t id, struct key_def *key_defs,
 	  uint32_t key_count, uint32_t arity)
 {
-	struct space *space = space_by_n(space_no);
+	struct space *space = space_by_id(id);
 	if (space)
-		tnt_raise(LoggedError, ER_SPACE_EXISTS, space_no);
+		tnt_raise(LoggedError, ER_SPACE_EXISTS, id);
 
 	space = (struct space *) malloc(sizeof(struct space));
 
-	space_create(space, space_no, key_defs, key_count, arity);
+	space_create(space, id, key_defs, key_count, arity);
 
-	const struct mh_i32ptr_node_t node = { space->no, space };
+	const struct mh_i32ptr_node_t node = { space_id(space), space };
 	mh_i32ptr_put(spaces, &node, NULL, NULL);
 
 	return space;
@@ -108,7 +108,7 @@ space_new(uint32_t space_no, struct key_def *key_defs,
 static void
 space_delete(struct space *space)
 {
-	const struct mh_i32ptr_node_t node = { space->no, NULL };
+	const struct mh_i32ptr_node_t node = { space_id(space), NULL };
 	mh_int_t k = mh_i32ptr_get(spaces, &node, NULL);
 	assert(k != mh_end(spaces));
 	mh_i32ptr_del(spaces, k, NULL);
@@ -118,9 +118,9 @@ space_delete(struct space *space)
 
 /* return space by its number */
 struct space *
-space_by_n(uint32_t n)
+space_by_id(uint32_t id)
 {
-	const struct mh_i32ptr_node_t node = { n, NULL };
+	const struct mh_i32ptr_node_t node = { id, NULL };
 	mh_int_t space = mh_i32ptr_get(spaces, &node, NULL);
 	if (space == mh_end(spaces))
 		return NULL;
@@ -311,7 +311,8 @@ build_secondary_indexes(void)
 		if (space->key_count <= 1)
 			continue; /* no secondary keys */
 
-		say_info("Building secondary keys in space %d...", space->no);
+		say_info("Building secondary keys in space %d...",
+			 space_id(space));
 
 		Index *pk = space->index[0];
 		for (uint32_t j = 1; j < space->key_count; j++) {
@@ -319,7 +320,7 @@ build_secondary_indexes(void)
 			index->build(pk);
 		}
 
-		say_info("Space %d: done", space->no);
+		say_info("Space %d: done", space_id(space));
 	}
 
 	/* enable secondary indexes now */
diff --git a/src/box/space.h b/src/box/space.h
index 92fa3a92ad..5425aa6995 100644
--- a/src/box/space.h
+++ b/src/box/space.h
@@ -53,8 +53,8 @@ struct space {
 	 */
 	uint32_t key_count;
 
-	/** Space number. */
-	uint32_t no;
+	/** Space numeric id. */
+	uint32_t id;
 
 	/** Default tuple format used by this space */
 	struct tuple_format *format;
@@ -62,7 +62,7 @@ struct space {
 
 
 /** Get space ordinal number. */
-static inline uint32_t space_n(struct space *sp) { return sp->no; }
+static inline uint32_t space_id(struct space *space) { return space->id; }
 
 /**
  * @brief A single method to handle REPLACE, DELETE and UPDATE.
@@ -183,20 +183,20 @@ space_foreach(void (*func)(struct space *sp, void *udata), void *udata);
  *
  * @return NULL if space not found, otherwise space object.
  */
-struct space *space_by_n(uint32_t space_no);
+struct space *space_by_id(uint32_t id);
 
 static inline struct space *
-space_find(uint32_t space_no)
+space_find(uint32_t id)
 {
-	struct space *s = space_by_n(space_no);
-	if (s)
-		return s;
+	struct space *space = space_by_id(id);
+	if (space)
+		return space;
 
-	tnt_raise(ClientError, ER_NO_SUCH_SPACE, space_no);
+	tnt_raise(ClientError, ER_NO_SUCH_SPACE, id);
 }
 
 struct space *
-space_new(uint32_t space_no, struct key_def *key_defs,
+space_new(uint32_t id, struct key_def *key_defs,
 	  uint32_t key_count, uint32_t arity);
 
 
@@ -210,13 +210,13 @@ void end_build_primary_indexes(void);
 void build_secondary_indexes(void);
 
 static inline Index *
-index_find(struct space *sp, uint32_t index_no)
+index_find(struct space *space, uint32_t index_id)
 {
-	Index *idx = space_index(sp, index_no);
-	if (idx == NULL)
-		tnt_raise(LoggedError, ER_NO_SUCH_INDEX, index_no,
-			  space_n(sp));
-	return idx;
+	Index *index = space_index(space, index_id);
+	if (index == NULL)
+		tnt_raise(LoggedError, ER_NO_SUCH_INDEX, index_id,
+			  space_id(space));
+	return index;
 }
 
 #endif /* TARANTOOL_BOX_SPACE_H_INCLUDED */
diff --git a/src/box/tuple.cc b/src/box/tuple.cc
index e4916d195e..32c8bb9d7e 100644
--- a/src/box/tuple.cc
+++ b/src/box/tuple.cc
@@ -76,8 +76,12 @@ tuple_format_alloc_and_register(struct key_def *key_def,
 	uint32_t field_count;
 
 	/* find max max field no */
-	for (; key_def < end; key_def++)
-		max_fieldno= MAX(max_fieldno, key_def->max_fieldno);
+	for (; key_def < end; key_def++) {
+		struct key_part *part = key_def->parts;
+		struct key_part *pend = part + key_def->part_count;
+		for (; part < pend; part++)
+			max_fieldno= MAX(max_fieldno, part->fieldno);
+	}
 
 	if (formats_size == formats_capacity) {
 		uint32_t new_capacity = formats_capacity ?
diff --git a/src/memcached.cc b/src/memcached.cc
index 7e30250c50..eb7aedd75d 100644
--- a/src/memcached.cc
+++ b/src/memcached.cc
@@ -487,7 +487,7 @@ memcached_init(const char *bind_ipaddr, int memcached_port)
 
 	stat_base = stat_register(memcached_stat_strs, memcached_stat_MAX);
 
-	struct space *sp = space_by_n(cfg.memcached_space);
+	struct space *sp = space_by_id(cfg.memcached_space);
 	memcached_index = space_index(sp, 0);
 
 	/* run memcached server */
@@ -512,14 +512,10 @@ memcached_space_init()
 	key_def.type = HASH;
 
 	key_def.parts = (struct key_part *) malloc(sizeof(struct key_part));
-	key_def.cmp_order = (uint32_t *) malloc(sizeof(uint32_t));
 
 	key_def.parts[0].fieldno = 0;
 	key_def.parts[0].type = STRING;
 
-	key_def.max_fieldno = 1;
-	key_def.cmp_order[0] = 0;
-
 	(void) space_new(cfg.memcached_space, &key_def, 1, 4);
 }
 
-- 
GitLab