From 74606f3a6872efc506ac264aa26d54fae108ea78 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Sat, 12 May 2012 23:13:29 +0400
Subject: [PATCH] Code cleanup.

Cherry-picking code from txn-refine.
Remove an extra member in struct box_txn.
Rename txn->tuple to txn->new_tuple.
---
 mod/box/box.h   |   3 +-
 mod/box/box.m   | 101 ++++++++++++++++++++++++------------------------
 mod/box/space.m |   2 +
 3 files changed, 53 insertions(+), 53 deletions(-)

diff --git a/mod/box/box.h b/mod/box/box.h
index a1838244c0..a02b602465 100644
--- a/mod/box/box.h
+++ b/mod/box/box.h
@@ -56,11 +56,10 @@ struct box_txn {
 	struct box_out *out;
 	struct space *space;
 	Index *index;
-	int n;
 
 	struct tbuf *ref_tuples;
 	struct box_tuple *old_tuple;
-	struct box_tuple *tuple;
+	struct box_tuple *new_tuple;
 	struct box_tuple *lock_tuple;
 
 	struct tbuf req;
diff --git a/mod/box/box.m b/mod/box/box.m
index 7352542a7c..7639a26de9 100644
--- a/mod/box/box.m
+++ b/mod/box/box.m
@@ -77,8 +77,6 @@ STRS(update_op_codes, UPDATE_OP_CODES);
 
 const int BOX_REF_THRESHOLD = 8196;
 
-struct space *space = NULL;
-
 struct box_snap_row {
 	u32 space;
 	u32 tuple_size;
@@ -124,7 +122,7 @@ tuple_txn_ref(struct box_txn *txn, struct box_tuple *tuple)
 static void
 validate_indexes(struct box_txn *txn)
 {
-	struct space *sp = &space[txn->n];
+	struct space *sp = txn->space;
 	int n = index_count(sp);
 
 	/* Only secondary indexes are validated here. So check to see
@@ -134,11 +132,11 @@ validate_indexes(struct box_txn *txn)
 	}
 
 	/* Check to see if the tuple has a sufficient number of fields. */
-	if (txn->tuple->field_count < sp->max_fieldno)
+	if (txn->new_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;
+	u8 *data = txn->new_tuple->data;
 	for (int f = 0; f < sp->max_fieldno; ++f) {
 		/* Get the size of the current field and advance. */
 		u32 len = load_varint32((void **) &data);
@@ -157,9 +155,9 @@ validate_indexes(struct box_txn *txn)
 
 	/* Check key uniqueness */
 	for (int i = 1; i < n; ++i) {
-		Index *index = space[txn->n].index[i];
+		Index *index = txn->space->index[i];
 		if (index->key_def->is_unique) {
-			struct box_tuple *tuple = [index findByTuple: txn->tuple];
+			struct box_tuple *tuple = [index findByTuple: txn->new_tuple];
 			if (tuple != NULL && tuple != txn->old_tuple)
 				tnt_raise(ClientError, :ER_INDEX_VIOLATION);
 		}
@@ -192,12 +190,12 @@ prepare_replace(struct box_txn *txn, size_t field_count, struct tbuf *data)
 	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->field_count = field_count;
-	memcpy(txn->tuple->data, data->data, data->size);
+	txn->new_tuple = tuple_alloc(data->size);
+	tuple_txn_ref(txn, txn->new_tuple);
+	txn->new_tuple->field_count = field_count;
+	memcpy(txn->new_tuple->data, data->data, data->size);
 
-	txn->old_tuple = [txn->index findByTuple: txn->tuple];
+	txn->old_tuple = [txn->index findByTuple: txn->new_tuple];
 
 	if (txn->old_tuple != NULL)
 		tuple_txn_ref(txn, txn->old_tuple);
@@ -213,7 +211,8 @@ prepare_replace(struct box_txn *txn, size_t field_count, struct tbuf *data)
 	if (txn->old_tuple != NULL) {
 #ifndef NDEBUG
 		void *ka, *kb;
-		ka = tuple_field(txn->tuple, txn->index->key_def->parts[0].fieldno);
+		ka = tuple_field(txn->new_tuple,
+				 txn->index->key_def->parts[0].fieldno);
 		kb = tuple_field(txn->old_tuple, txn->index->key_def->parts[0].fieldno);
 		int kal, kab;
 		kal = load_varint32(&ka);
@@ -222,13 +221,13 @@ prepare_replace(struct box_txn *txn, size_t field_count, struct tbuf *data)
 #endif
 		lock_tuple(txn, txn->old_tuple);
 	} else {
-		lock_tuple(txn, txn->tuple);
+		lock_tuple(txn, txn->new_tuple);
 		/*
 		 * Mark the tuple as ghost before attempting an
 		 * index replace: if it fails, txn_rollback() will
 		 * look at the flag and remove the tuple.
 		 */
-		txn->tuple->flags |= GHOST;
+		txn->new_tuple->flags |= GHOST;
 		/*
 		 * If the tuple doesn't exist, insert a GHOST
 		 * tuple in all indices in order to avoid a race
@@ -239,48 +238,48 @@ prepare_replace(struct box_txn *txn, size_t field_count, struct tbuf *data)
 		 * Tuple reference counter will be incremented in
 		 * txn_commit().
 		 */
-		int n = index_count(&space[txn->n]);
+		int n = index_count(txn->space);
 		for (int i = 0; i < n; i++) {
-			Index *index = space[txn->n].index[i];
-			[index replace: NULL :txn->tuple];
+			Index *index = txn->space->index[i];
+			[index replace: NULL :txn->new_tuple];
 		}
 	}
 
 	txn->out->dup_u32(1); /* Affected tuples */
 
 	if (txn->flags & BOX_RETURN_TUPLE)
-		txn->out->add_tuple(txn->tuple);
+		txn->out->add_tuple(txn->new_tuple);
 }
 
 static void
 commit_replace(struct box_txn *txn)
 {
 	if (txn->old_tuple != NULL) {
-		int n = index_count(&space[txn->n]);
+		int n = index_count(txn->space);
 		for (int i = 0; i < n; i++) {
-			Index *index = space[txn->n].index[i];
-			[index replace: txn->old_tuple :txn->tuple];
+			Index *index = txn->space->index[i];
+			[index replace: txn->old_tuple :txn->new_tuple];
 		}
 
 		tuple_ref(txn->old_tuple, -1);
 	}
 
-	if (txn->tuple != NULL) {
-		txn->tuple->flags &= ~GHOST;
-		tuple_ref(txn->tuple, +1);
+	if (txn->new_tuple != NULL) {
+		txn->new_tuple->flags &= ~GHOST;
+		tuple_ref(txn->new_tuple, +1);
 	}
 }
 
 static void
 rollback_replace(struct box_txn *txn)
 {
-	say_debug("rollback_replace: txn->tuple:%p", txn->tuple);
+	say_debug("rollback_replace: txn->new_tuple:%p", txn->new_tuple);
 
-	if (txn->tuple && txn->tuple->flags & GHOST) {
-		int n = index_count(&space[txn->n]);
+	if (txn->new_tuple && txn->new_tuple->flags & GHOST) {
+		int n = index_count(txn->space);
 		for (int i = 0; i < n; i++) {
-			Index *index = space[txn->n].index[i];
-			[index remove: txn->tuple];
+			Index *index = txn->space->index[i];
+			[index remove: txn->new_tuple];
 		}
 	}
 }
@@ -930,15 +929,15 @@ init_update_operations(struct box_txn *txn, struct update_cmd *cmd)
 static void
 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;
+	void *new_data = txn->new_tuple->data;
+	void *new_data_end = new_data + txn->new_tuple->bsize;
 	struct update_field *field;
-	txn->tuple->field_count = 0;
+	txn->new_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->field_count++;
+			txn->new_tuple->field_count++;
 		}
 		void *new_field = new_data;
 		void *old_field = field->old;
@@ -984,7 +983,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->field_count += field->tail_field_count;
+			txn->new_tuple->field_count += field->tail_field_count;
 		}
 	}
 }
@@ -1010,8 +1009,8 @@ prepare_update(struct box_txn *txn, struct tbuf *data)
 
 	init_update_operations(txn, cmd);
 	/* allocate new tuple */
-	txn->tuple = tuple_alloc(cmd->new_tuple_len);
-	tuple_txn_ref(txn, txn->tuple);
+	txn->new_tuple = tuple_alloc(cmd->new_tuple_len);
+	tuple_txn_ref(txn, txn->new_tuple);
 	do_update(txn, cmd);
 	lock_tuple(txn, txn->old_tuple);
 	validate_indexes(txn);
@@ -1019,8 +1018,8 @@ prepare_update(struct box_txn *txn, struct tbuf *data)
 out:
 	txn->out->dup_u32(tuples_affected);
 
-	if (txn->flags & BOX_RETURN_TUPLE && txn->tuple)
-		txn->out->add_tuple(txn->tuple);
+	if (txn->flags & BOX_RETURN_TUPLE && txn->new_tuple)
+		txn->out->add_tuple(txn->new_tuple);
 }
 
 /** }}} */
@@ -1110,9 +1109,9 @@ commit_delete(struct box_txn *txn)
 	if (txn->old_tuple == NULL)
 		return;
 
-	int n = index_count(&space[txn->n]);
+	int n = index_count(txn->space);
 	for (int i = 0; i < n; i++) {
-		Index *index = space[txn->n].index[i];
+		Index *index = txn->space->index[i];
 		[index remove: txn->old_tuple];
 	}
 
@@ -1178,15 +1177,15 @@ txn_begin()
 
 void txn_assign_n(struct box_txn *txn, struct tbuf *data)
 {
-	txn->n = read_u32(data);
+	int space_no = read_u32(data);
 
-	if (txn->n < 0 || txn->n >= BOX_SPACE_MAX)
-		tnt_raise(ClientError, :ER_NO_SUCH_SPACE, txn->n);
+	if (space_no < 0 || space_no >= BOX_SPACE_MAX)
+		tnt_raise(ClientError, :ER_NO_SUCH_SPACE, space_no);
 
-	txn->space = &space[txn->n];
+	txn->space = &space[space_no];
 
 	if (!txn->space->enabled)
-		tnt_raise(ClientError, :ER_SPACE_DISABLED, txn->n);
+		tnt_raise(ClientError, :ER_SPACE_DISABLED, space_no);
 
 	txn->index = txn->space->index[0];
 }
@@ -1292,8 +1291,7 @@ box_dispatch(struct box_txn *txn, struct tbuf *data)
 		txn_assign_n(txn, data);
 		txn->flags |= read_u32(data) & BOX_ALLOWED_REQUEST_FLAGS;
 		field_count = read_u32(data);
-		if (space[txn->n].arity > 0
-		    && space[txn->n].arity != field_count)
+		if (txn->space->arity > 0 && txn->space->arity != field_count)
 			tnt_raise(IllegalParams, :"tuple field count must match space cardinality");
 		prepare_replace(txn, field_count, data);
 		break;
@@ -1315,9 +1313,10 @@ box_dispatch(struct box_txn *txn, struct tbuf *data)
 		u32 offset = read_u32(data);
 		u32 limit = read_u32(data);
 
-		if (i >= space[txn->n].key_count)
-			tnt_raise(LoggedError, :ER_NO_SUCH_INDEX, i, txn->n);
-		txn->index = space[txn->n].index[i];
+		if (i >= txn->space->key_count)
+			tnt_raise(LoggedError, :ER_NO_SUCH_INDEX, i,
+				  space_n(txn->space));
+		txn->index = txn->space->index[i];
 
 		process_select(txn, limit, offset, data);
 		break;
diff --git a/mod/box/space.m b/mod/box/space.m
index 9d427dad68..08fc81494b 100644
--- a/mod/box/space.m
+++ b/mod/box/space.m
@@ -33,6 +33,8 @@
 #include <tarantool.h>
 #include <exception.h>
 
+struct space *space = NULL;
+
 bool secondary_indexes_enabled = false;
 /** Free a key definition. */
 static void
-- 
GitLab