From 6b12355570959f3efcba09f929080fc216e06c33 Mon Sep 17 00:00:00 2001
From: Dmitry Simonenko <pmwkaa@gmail.com>
Date: Fri, 21 Nov 2014 14:47:06 +0400
Subject: [PATCH] sophia-gh-577: reduce number of engine methods

---
 src/box/engine.cc        |  6 ------
 src/box/engine.h         |  2 --
 src/box/engine_sophia.cc | 46 +++++++++++++++++-----------------------
 src/box/engine_sophia.h  |  2 --
 src/box/txn.cc           | 10 ++++-----
 5 files changed, 24 insertions(+), 42 deletions(-)

diff --git a/src/box/engine.cc b/src/box/engine.cc
index e2fafcd7d0..ffa5283039 100644
--- a/src/box/engine.cc
+++ b/src/box/engine.cc
@@ -54,18 +54,12 @@ void EngineFactory::shutdown()
 void EngineFactory::begin(struct txn*, struct space*)
 {}
 
-void EngineFactory::begin_stmt(struct txn*, struct space*)
-{}
-
 void EngineFactory::commit(struct txn*)
 {}
 
 void EngineFactory::rollback(struct txn*)
 {}
 
-void EngineFactory::finish_stmt(struct txn_stmt*)
-{}
-
 void EngineFactory::recoveryEvent(enum engine_recovery_event)
 {}
 
diff --git a/src/box/engine.h b/src/box/engine.h
index a8a0773eaa..478ea0e043 100644
--- a/src/box/engine.h
+++ b/src/box/engine.h
@@ -126,10 +126,8 @@ class EngineFactory: public Object {
 	 * Engine specific transaction life-cycle routines.
 	 */
 	virtual void begin(struct txn*, struct space*);
-	virtual void begin_stmt(struct txn*, struct space*);
 	virtual void commit(struct txn*);
 	virtual void rollback(struct txn*);
-	virtual void finish_stmt(struct txn_stmt*);
 public:
 	/** Name of the engine. */
 	const char *name;
diff --git a/src/box/engine_sophia.cc b/src/box/engine_sophia.cc
index ee28d17613..5f0ec40261 100644
--- a/src/box/engine_sophia.cc
+++ b/src/box/engine_sophia.cc
@@ -232,22 +232,19 @@ SophiaFactory::keydefCheck(struct key_def *key_def)
 }
 
 void
-SophiaFactory::begin(struct txn * /* txn */, struct space *space)
-{
-	assert(space->engine->factory == this);
-	assert(tx == NULL);
-	SophiaIndex *index = (SophiaIndex *)index_find(space, 0);
-	assert(index->db != NULL);
-	tx = sp_begin(index->db);
-	if (tx == NULL)
-		sophia_raise(env);
-	tx_db = index->db;
-}
-
-void
-SophiaFactory::begin_stmt(struct txn * /* txn */, struct space *space)
+SophiaFactory::begin(struct txn * txn, struct space *space)
 {
 	assert(space->engine->factory == this);
+	if (txn->n_stmts == 1) {
+		assert(tx == NULL);
+		SophiaIndex *index = (SophiaIndex *)index_find(space, 0);
+		assert(index->db != NULL);
+		tx = sp_begin(index->db);
+		if (tx == NULL)
+			sophia_raise(env);
+		tx_db = index->db;
+		return;
+	}
 	assert(tx != NULL);
 	SophiaIndex *index = (SophiaIndex *)index_find(space, 0);
 	if (index->db != tx_db) {
@@ -274,7 +271,8 @@ SophiaFactory::commit(struct txn *txn)
 	assert(rc == 0);
 
 	/* b. create transaction log cursor and
-	 *    forge each statement's LSN number */
+	 *    forge each statement's LSN number.
+	*/
 	void *lc = sp_ctl(tx, "log_cursor");
 	if (lc == NULL) {
 		sp_rollback(tx);
@@ -287,6 +285,13 @@ SophiaFactory::commit(struct txn *txn)
 		void *v = sp_get(lc);
 		assert(v != NULL);
 		sp_set(v, "lsn", stmt->row->lsn);
+		/* remove tuple reference */
+		if (stmt->new_tuple) {
+			/* 2 refs: iproto case */
+			/* 3 refs: lua case */
+			assert(stmt->new_tuple->refs >= 2);
+			tuple_unref(stmt->new_tuple);
+		}
 	}
 	assert(sp_get(lc) == NULL);
 	sp_destroy(lc);
@@ -312,14 +317,3 @@ SophiaFactory::rollback(struct txn *)
 		sophia_raise(env);
 	assert(rc == 0);
 }
-
-void
-SophiaFactory::finish_stmt(struct txn_stmt *stmt)
-{
-	if (stmt->new_tuple) {
-		/* 2 refs: iproto case */
-		/* 3 refs: lua case */
-		assert(stmt->new_tuple->refs >= 2);
-		tuple_unref(stmt->new_tuple);
-	}
-}
diff --git a/src/box/engine_sophia.h b/src/box/engine_sophia.h
index f2bde2d44b..373ef7284e 100644
--- a/src/box/engine_sophia.h
+++ b/src/box/engine_sophia.h
@@ -37,10 +37,8 @@ struct SophiaFactory: public EngineFactory {
 	virtual void dropIndex(Index*);
 	virtual void keydefCheck(struct key_def*f);
 	virtual void begin(struct txn*, struct space*);
-	virtual void begin_stmt(struct txn*, struct space*);
 	virtual void commit(struct txn*);
 	virtual void rollback(struct txn*);
-	virtual void finish_stmt(struct txn_stmt*);
 	virtual void recoveryEvent(enum engine_recovery_event);
 	void *env;
 	void *tx;
diff --git a/src/box/txn.cc b/src/box/txn.cc
index 4124db0e2b..cb171fc339 100644
--- a/src/box/txn.cc
+++ b/src/box/txn.cc
@@ -169,12 +169,11 @@ txn_engine_begin_stmt(struct txn *txn, struct space *space)
 				tnt_raise(ClientError, ER_UNSUPPORTED,
 				          space->def.engine_name, "transactions");
 		}
-		factory->begin(txn, space);
-		return;
+	} else {
+		if (txn->engine->id != engine_id(space->engine))
+			tnt_raise(ClientError, ER_CROSS_ENGINE_TRANSACTION);
 	}
-	if (txn->engine->id != engine_id(space->engine))
-		tnt_raise(ClientError, ER_CROSS_ENGINE_TRANSACTION);
-	factory->begin_stmt(txn, space);
+	factory->begin(txn, space);
 }
 
 struct txn *
@@ -249,7 +248,6 @@ txn_finish(struct txn *txn)
 	rlist_foreach_entry(stmt, &txn->stmts, next) {
 		if (stmt->old_tuple)
 			tuple_unref(stmt->old_tuple);
-		txn->engine->finish_stmt(stmt);
 	}
 	TRASH(txn);
 	/** Free volatile txn memory. */
-- 
GitLab