[patches] [PATCH V3 08/10] sql: delete CursorPayload structure

Bulat Niatshin niatshin at tarantool.org
Mon Mar 5 14:53:27 MSK 2018


- Delete CursorPayload structure and delegate all responsibility to
  BtCursor structure, because it already has all necessary fields for
  that.
- Refactor tarantoolSqlite3Insert, tarantoolSqlite3Replace,
  tarantoolSqlite3EphemeralInsert, remove second argument from that.

For #3119
---
 src/box/sql.c              | 34 +++++++++++++++++-----------------
 src/box/sql/cursor.h       | 19 -------------------
 src/box/sql/tarantoolInt.h |  6 +++---
 src/box/sql/vdbe.c         | 27 +++++++++++++++++----------
 4 files changed, 37 insertions(+), 49 deletions(-)

diff --git a/src/box/sql.c b/src/box/sql.c
index 9ce270da9..db0267709 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -488,18 +488,18 @@ int tarantoolSqlite3EphemeralCreate(BtCursor *pCur, uint32_t field_count,
  *
  * @retval SQLITE_OK on success, SQLITE_TARANTOOL_ERROR otherwise.
  */
-int tarantoolSqlite3EphemeralInsert(BtCursor *pCur, const CursorPayload *pX)
+int tarantoolSqlite3EphemeralInsert(BtCursor *pCur)
 {
 	assert(pCur);
 	assert(pCur->curFlags & BTCF_TEphemCursor);
 	struct ta_cursor *c = pCur->pTaCursor;
 	assert(c);
 	assert(c->ephem_space);
-	mp_tuple_assert(pX->pKey, pX->pKey + pX->nKey);
+	mp_tuple_assert(pCur->pKey, pCur->pKey + pCur->nKey);
 
 	struct space *space = c->ephem_space;
-	if (space_ephemeral_replace(space, pX->pKey,
-				    pX->pKey + pX->nKey) != 0) {
+	if (space_ephemeral_replace(space, pCur->pKey,
+				    pCur->pKey + pCur->nKey) != 0) {
 		diag_log();
 		return SQL_TARANTOOL_INSERT_FAIL;
 	}
@@ -519,36 +519,36 @@ int tarantoolSqlite3EphemeralDrop(BtCursor *pCur)
 	return SQLITE_OK;
 }
 
-static int insertOrReplace(BtCursor *pCur, const CursorPayload *pX,
-		           int operationType)
+static int insert_or_replace(BtCursor *pCur, int operation_type)
 {
+	assert(pCur != NULL);
 	assert(pCur->curFlags & BTCF_TaCursor);
-	assert(operationType == TARANTOOL_INDEX_INSERT ||
-	       operationType == TARANTOOL_INDEX_REPLACE);
+	assert(operation_type == TARANTOOL_INDEX_INSERT ||
+	       operation_type == TARANTOOL_INDEX_REPLACE);
 
 	int space_id = SQLITE_PAGENO_TO_SPACEID(pCur->pgnoRoot);
 	int rc;
-	if (operationType == TARANTOOL_INDEX_INSERT) {
-		rc = box_insert(space_id, pX->pKey,
-				(const char *)pX->pKey + pX->nKey,
+	if (operation_type == TARANTOOL_INDEX_INSERT) {
+		rc = box_insert(space_id, pCur->pKey,
+				(const char *)pCur->pKey + pCur->nKey,
 				NULL /* result */);
 	} else {
-		rc = box_replace(space_id, pX->pKey,
-				 (const char *)pX->pKey + pX->nKey,
+		rc = box_replace(space_id, pCur->pKey,
+				 (const char *)pCur->pKey + pCur->nKey,
 				 NULL /* result */);
 	}
 
 	return rc == 0 ? SQLITE_OK : SQL_TARANTOOL_INSERT_FAIL;;
 }
 
-int tarantoolSqlite3Insert(BtCursor *pCur, const CursorPayload *pX)
+int tarantoolSqlite3Insert(BtCursor *pCur)
 {
-	return insertOrReplace(pCur, pX, TARANTOOL_INDEX_INSERT);
+	return insert_or_replace(pCur, TARANTOOL_INDEX_INSERT);
 }
 
-int tarantoolSqlite3Replace(BtCursor *pCur, const CursorPayload *pX)
+int tarantoolSqlite3Replace(BtCursor *pCur)
 {
-	return insertOrReplace(pCur, pX, TARANTOOL_INDEX_REPLACE);
+	return insert_or_replace(pCur, TARANTOOL_INDEX_REPLACE);
 }
 
 /*
diff --git a/src/box/sql/cursor.h b/src/box/sql/cursor.h
index e89ee10d7..646d9913d 100644
--- a/src/box/sql/cursor.h
+++ b/src/box/sql/cursor.h
@@ -34,7 +34,6 @@
 
 typedef u32 Pgno;
 typedef struct BtCursor BtCursor;
-typedef struct CursorPayload CursorPayload;
 
 /*
  * Values that may be OR'd together to form the argument to the
@@ -80,24 +79,6 @@ void sqlite3CursorHintFlags(BtCursor *, unsigned);
 int sqlite3CloseCursor(BtCursor *);
 int sqlite3CursorMovetoUnpacked(BtCursor *, UnpackedRecord * pUnKey, int *pRes);
 
-/* An instance of the CursorPayload object describes the content of a single
- * entry in index.
- *
- * This object is used to pass information into tarantoolSqlite3Insert().  The
- * same information used to be passed as five separate parameters.  But placing
- * the information into this object helps to keep the interface more
- * organized and understandable, and it also helps the resulting code to
- * run a little faster by using fewer registers for parameter passing.
- */
-struct CursorPayload {
-	const void *pKey;	/* Key content for indexes.  NULL for tables */
-	sqlite3_int64 nKey;	/* Size of pKey for indexes.  PRIMARY KEY for tabs */
-	const void *pData;	/* Data for tables.  NULL for indexes */
-	struct Mem *aMem;	/* First of nMem value in the unpacked pKey */
-	u16 nMem;		/* Number of aMem[] value.  Might be zero */
-	int nData;		/* Size of pData.  0 if none. */
-};
-
 int sqlite3CursorNext(BtCursor *, int *pRes);
 int sqlite3CursorPrevious(BtCursor *, int *pRes);
 int sqlite3CursorPayload(BtCursor *, u32 offset, u32 amt, void *);
diff --git a/src/box/sql/tarantoolInt.h b/src/box/sql/tarantoolInt.h
index a7b6bd7f2..39fdbcd76 100644
--- a/src/box/sql/tarantoolInt.h
+++ b/src/box/sql/tarantoolInt.h
@@ -74,8 +74,8 @@ int tarantoolSqlite3Previous(BtCursor * pCur, int *pRes);
 int tarantoolSqlite3MovetoUnpacked(BtCursor * pCur, UnpackedRecord * pIdxKey,
 				   int *pRes);
 int tarantoolSqlite3Count(BtCursor * pCur, i64 * pnEntry);
-int tarantoolSqlite3Insert(BtCursor * pCur, const CursorPayload * pX);
-int tarantoolSqlite3Replace(BtCursor * pCur, const CursorPayload * pX);
+int tarantoolSqlite3Insert(BtCursor * pCur);
+int tarantoolSqlite3Replace(BtCursor * pCur);
 int tarantoolSqlite3Delete(BtCursor * pCur, u8 flags);
 int tarantoolSqlite3ClearTable(int iTable);
 
@@ -96,7 +96,7 @@ int tarantoolSqlite3RenameParentTable(int iTab, const char *zOldParentName,
 /* Interface for ephemeral tables. */
 int tarantoolSqlite3EphemeralCreate(BtCursor * pCur, uint32_t filed_count,
 				    struct coll *aColl);
-int tarantoolSqlite3EphemeralInsert(BtCursor * pCur, const CursorPayload * pX);
+int tarantoolSqlite3EphemeralInsert(BtCursor * pCur);
 int tarantoolSqlite3EphemeralDelete(BtCursor * pCur);
 int tarantoolSqlite3EphemeralCount(BtCursor * pCur, i64 * pnEntry);
 int tarantoolSqlite3EphemeralDrop(BtCursor * pCur);
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 38b31ff56..7391952f3 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -4457,7 +4457,6 @@ case OP_SorterInsert:       /* in2 */
 case OP_IdxReplace:
 case OP_IdxInsert: {        /* in2 */
 	VdbeCursor *pC;
-	CursorPayload x;
 
 	assert(pOp->p1>=0 && pOp->p1<p->nCursor);
 	pC = p->apCsr[pOp->p1];
@@ -4472,27 +4471,35 @@ case OP_IdxInsert: {        /* in2 */
 	if (pOp->opcode==OP_SorterInsert) {
 		rc = sqlite3VdbeSorterWrite(pC, pIn2);
 	} else {
-		x.nKey = pIn2->n;
-		x.pKey = pIn2->z;
-		x.aMem = aMem + pOp->p3;
-		x.nMem = (u16)pOp->p4.i;
-
 		BtCursor *pBtCur = pC->uc.pCursor;
-		assert((x.pKey == 0) == (pBtCur->pKeyInfo == 0));
+		pBtCur->nKey = pIn2->n;
+		pBtCur->pKey = pIn2->z;
+
 		if (pBtCur->curFlags & BTCF_TaCursor) {
 			/* Make sure that memory has been allocated on region. */
 			assert(aMem[pOp->p2].flags & MEM_Ephem);
 			if (pOp->opcode == OP_IdxInsert)
-				rc = tarantoolSqlite3Insert(pBtCur, &x);
+				rc = tarantoolSqlite3Insert(pBtCur);
 			else
-				rc = tarantoolSqlite3Replace(pBtCur, &x);
+				rc = tarantoolSqlite3Replace(pBtCur);
 		} else if (pBtCur->curFlags & BTCF_TEphemCursor) {
-			rc = tarantoolSqlite3EphemeralInsert(pBtCur, &x);
+			rc = tarantoolSqlite3EphemeralInsert(pBtCur);
 		} else {
 			unreachable();
 		}
 		assert(pC->deferredMoveto==0);
 		pC->cacheStatus = CACHE_STALE;
+
+		/*
+		 * Memory for tuple passed to Tarantool is
+		 * allocated in region. This memory will be
+		 * automatically released by Tarantool.
+		 * However, VDBE in the end will also try to
+		 * release it, so memory
+		 * should be explicitly nulllified.
+		 */
+		pBtCur->nKey = 0;
+		pBtCur->pKey = NULL;
 	}
 
 	if (pOp->p5 & OPFLAG_OE_IGNORE) {
-- 
2.14.1




More information about the Tarantool-patches mailing list