Simplify the sqlite3BtreeInsert() interface by gathering the five arguments

describing the content to be inserted into the new BtreePayload structure, and
thus reducing the number of parameters from eight to four.

FossilOrigin-Name: 55f348cdd24c7812ea4b63345514764b69f64dc8
This commit is contained in:
drh 2016-05-21 20:03:42 +00:00
parent 16e2b9694a
commit 8eeb4463d9
6 changed files with 103 additions and 73 deletions

View File

@ -1,5 +1,5 @@
C Remove\ssome\sunused\slegacy\scode\sfrom\sthe\sbtree\sinsert\slogic.
D 2016-05-21T19:10:21.800
C Simplify\sthe\ssqlite3BtreeInsert()\sinterface\sby\sgathering\sthe\sfive\sarguments\ndescribing\sthe\scontent\sto\sbe\sinserted\sinto\sthe\snew\sBtreePayload\sstructure,\sand\nthus\sreducing\sthe\snumber\sof\sparameters\sfrom\seight\sto\sfour.
D 2016-05-21T20:03:42.980
F Makefile.in f59e0763ff448719fc1bd25513882b0567286317
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 306d73e854b1a92ea06e5d1e637faa5c44de53c7
@ -322,8 +322,8 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
F src/backup.c 6df65fdd569c901a418887a1a76f82ec35044556
F src/bitvec.c 3ee4c8b2c94ed3a7377256e18199e6ff5cf33f63
F src/btmutex.c bc87dd3b062cc26edfe79918de2200ccb8d41e73
F src/btree.c 09df167470e82b34f92d60cdd1a44f397ae8e105
F src/btree.h a5008b9afe56e8e54ade6c436a910f112defcca9
F src/btree.c e312cc3fdff31fdcc727b973fa0f195573b86393
F src/btree.h 1342a9b2cc2089e3534d3ef00204786783f6aea6
F src/btreeInt.h c18b7d2a3494695133e4e60ee36061d37f45d9a5
F src/build.c 785fa789319d93c6ae20efbd01d4da9ce8f8a793
F src/callback.c 2e76147783386374bf01b227f752c81ec872d730
@ -390,7 +390,7 @@ F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9
F src/tclsqlite.c 9c4c4589d078de37813ded708d8838b338ffb060
F src/test1.c c0e5b69f99e95a2c9f55fdb6e46b44f1a15292d8
F src/test2.c 5586f43fcd9a1be0830793cf9d354082c261b25b
F src/test3.c 0df6f8dbb4cbaa7106397c70a271fa6a43659042
F src/test3.c d2c9efd2985ff8f5502ffd3253156984778d77d8
F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e
F src/test5.c 5a34feec76d9b3a86aab30fd4f6cc9c48cbab4c1
F src/test6.c 2c014d4977efd6107ec9eef3dfdec56ac516f824
@ -444,7 +444,7 @@ F src/update.c 4f05ea8cddfa367d045e03589756c02199e8f9bd
F src/utf.c 699001c79f28e48e9bcdf8a463da029ea660540c
F src/util.c 810ec3f22e2d1b62e66c30fe3621ebdedd23584d
F src/vacuum.c feb1eabb20987983d9350cad98299b21fa811f52
F src/vdbe.c 975ac1d3d326bcb36b4e99255daeaf81ff6659d1
F src/vdbe.c f2706d5564d2b2887a27c909e8e1575250029503
F src/vdbe.h 5591b5add447096e31288b5a0a78ec5d7b5c5170
F src/vdbeInt.h ddb157974436d87652de7dc641f7191496d9a8cd
F src/vdbeapi.c ba85b78fe08dc4a9ce747e62c89a2b4a4547e74c
@ -1492,7 +1492,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 1dbaf7a119edc5150a5d4be1b72f652a574e5bc5
R 46d65e39155fd1bfd348824fd7a5487b
P 2ce1166717ac3c0cec37b2f6d70d8359fbaefc71
R 560ed5c35c4c6718e1a252b0b3f02845
U drh
Z 20edc765018d44eea5900b69fe208cfe
Z ca823f4324bb11c74d57e3f017091386

View File

@ -1 +1 @@
2ce1166717ac3c0cec37b2f6d70d8359fbaefc71
55f348cdd24c7812ea4b63345514764b69f64dc8

View File

@ -6072,9 +6072,7 @@ static int clearCell(
static int fillInCell(
MemPage *pPage, /* The page that contains the cell */
unsigned char *pCell, /* Complete text of the cell */
const void *pKey, i64 nKey, /* The key */
const void *pData,int nData, /* The data */
int nZero, /* Extra zero bytes to append to pData */
const BtreePayload *pX, /* Payload with which to construct the cell */
int *pnSize /* Write cell size here */
){
int nPayload;
@ -6098,24 +6096,23 @@ static int fillInCell(
/* Fill in the header. */
nHeader = pPage->childPtrSize;
nPayload = nData + nZero;
nPayload = pX->nData + pX->nZero;
if( pPage->intKeyLeaf ){
nHeader += putVarint32(&pCell[nHeader], nPayload);
}else{
assert( nData==0 );
assert( nZero==0 );
assert( pX->nData==0 );
assert( pX->nZero==0 );
}
nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
/* Fill in the payload size */
/* Fill in the payload */
if( pPage->intKey ){
pSrc = pData;
nSrc = nData;
pSrc = pX->pData;
nSrc = pX->nData;
}else{
assert( nKey<=0x7fffffff && pKey!=0 );
nPayload = (int)nKey;
pSrc = pKey;
nSrc = (int)nKey;
assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
nSrc = nPayload = (int)pX->nKey;
pSrc = pX->pKey;
}
if( nPayload<=pPage->maxLocal ){
n = nHeader + nPayload;
@ -6154,7 +6151,7 @@ static int fillInCell(
CellInfo info;
pPage->xParseCell(pPage, pCell, &info);
assert( nHeader==(int)(info.pPayload - pCell) );
assert( info.nKey==nKey );
assert( info.nKey==pX->nKey );
assert( *pnSize == info.nSize );
assert( spaceLeft == info.nLocal );
}
@ -7923,13 +7920,19 @@ static int balance(BtCursor *pCur){
/*
** Insert a new record into the BTree. The key is given by (pKey,nKey)
** and the data is given by (pData,nData). The cursor is used only to
** define what table the record should be inserted into. The cursor
** is left pointing at a random location.
** Insert a new record into the BTree. The content of the new record
** is described by the pX object. The pCur cursor is used only to
** define what table the record should be inserted into, and is left
** pointing at a random location.
**
** For an INTKEY table, only the nKey value of the key is used. pKey is
** ignored. For a ZERODATA table, the pData and nData are both ignored.
** For a table btree (used for rowid tables), only the pX.nKey value of
** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
** hold the content of the row.
**
** For an index btree (used for indexes and WITHOUT ROWID tables), the
** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
** pX.pData,nData,nZero fields must be zero.
**
** If the seekResult parameter is non-zero, then a successful call to
** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
@ -7946,9 +7949,7 @@ static int balance(BtCursor *pCur){
*/
int sqlite3BtreeInsert(
BtCursor *pCur, /* Insert data into the table of this cursor */
const void *pKey, i64 nKey, /* The key of the new record */
const void *pData, int nData, /* The data of the new record */
int nZero, /* Number of extra 0 bytes to append to data */
const BtreePayload *pX, /* Content of the row to be inserted */
int appendBias, /* True if this is likely an append */
int seekResult /* Result of prior MovetoUnpacked() call */
){
@ -7978,7 +7979,7 @@ int sqlite3BtreeInsert(
** keys with no associated data. If the cursor was opened expecting an
** intkey table, the caller should be inserting integer keys with a
** blob of associated data. */
assert( (pKey==0)==(pCur->pKeyInfo==0) );
assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
/* Save the positions of any other cursors open on this table.
**
@ -7997,38 +7998,38 @@ int sqlite3BtreeInsert(
}
if( pCur->pKeyInfo==0 ){
assert( pKey==0 );
assert( pX->pKey==0 );
/* If this is an insert into a table b-tree, invalidate any incrblob
** cursors open on the row being replaced */
invalidateIncrblobCursors(p, nKey, 0);
invalidateIncrblobCursors(p, pX->nKey, 0);
/* If the cursor is currently on the last row and we are appending a
** new row onto the end, set the "loc" to avoid an unnecessary
** btreeMoveto() call */
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
&& pCur->info.nKey==nKey-1 ){
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
&& pCur->info.nKey==pX->nKey-1 ){
loc = -1;
}else if( loc==0 ){
rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
if( rc ) return rc;
}
}else if( loc==0 ){
rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
if( rc ) return rc;
}
assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
pPage = pCur->apPage[pCur->iPage];
assert( pPage->intKey || nKey>=0 );
assert( pPage->intKey || pX->nKey>=0 );
assert( pPage->leaf || !pPage->intKey );
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
pCur->pgnoRoot, nKey, nData, pPage->pgno,
pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
loc==0 ? "overwrite" : "new entry"));
assert( pPage->isInit );
newCell = pBt->pTmpSpace;
assert( newCell!=0 );
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
rc = fillInCell(pPage, newCell, pX, &szNew);
if( rc ) goto end_insert;
assert( szNew==pPage->xCellSize(pPage, newCell) );
assert( szNew <= MX_CELL_SIZE(pBt) );

View File

@ -39,6 +39,7 @@
typedef struct Btree Btree;
typedef struct BtCursor BtCursor;
typedef struct BtShared BtShared;
typedef struct BtreePayload BtreePayload;
int sqlite3BtreeOpen(
@ -250,9 +251,34 @@ int sqlite3BtreeDelete(BtCursor*, u8 flags);
#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
const void *pData, int nData,
int nZero, int bias, int seekResult);
/* An instance of the BtreePayload object describes the content of a single
** entry in either an index or table btree.
**
** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
** an arbitrary key and no data. These btrees have pKey,nKey set to their
** key and pData,nData,nZero set to zero.
**
** Table btrees (used for rowid tables) contain an integer rowid used as
** the key and passed in the nKey field. The pKey field is zero.
** pData,nData hold the content of the new entry. nZero extra zero bytes
** are appended to the end of the content when constructing the entry.
**
** This object is used to pass information into sqlite3BtreeInsert(). 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 BtreePayload {
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 */
int nData; /* Size of pData. 0 if none. */
int nZero; /* Extra zero data appended after pData,nData */
};
int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
int bias, int seekResult);
int sqlite3BtreeFirst(BtCursor*, int *pRes);
int sqlite3BtreeLast(BtCursor*, int *pRes);
int sqlite3BtreeNext(BtCursor*, int *pRes);

View File

@ -618,27 +618,27 @@ static int btree_insert(
){
BtCursor *pCur;
int rc;
void *pKey = 0;
int nKey = 0;
void *pData = 0;
int nData = 0;
BtreePayload x;
if( objc!=4 && objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE");
return TCL_ERROR;
}
memset(&x, 0, sizeof(x));
if( objc==4 ){
if( Tcl_GetIntFromObj(interp, objv[2], &nKey) ) return TCL_ERROR;
pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &nData);
if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR;
x.nKey = rc;
x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &x.nData);
}else{
pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &nKey);
x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &rc);
x.nKey = rc;
}
pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
sqlite3_mutex_enter(pCur->pBtree->db->mutex);
sqlite3BtreeEnter(pCur->pBtree);
rc = sqlite3BtreeInsert(pCur, pKey, nKey, pData, nData, 0, 0, 0);
rc = sqlite3BtreeInsert(pCur, &x, 0, 0);
sqlite3BtreeLeave(pCur->pBtree);
sqlite3_mutex_leave(pCur->pBtree->db->mutex);

View File

@ -4285,13 +4285,12 @@ case OP_Insert:
case OP_InsertInt: {
Mem *pData; /* MEM cell holding data for the record to be inserted */
Mem *pKey; /* MEM cell holding key for the record */
i64 iKey; /* The integer ROWID or key for the record to be inserted */
VdbeCursor *pC; /* Cursor to table into which insert is written */
int nZero; /* Number of zero-bytes to append */
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
const char *zDb; /* database name - used by the update hook */
Table *pTab; /* Table structure - used by update and pre-update hooks */
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
BtreePayload x; /* Payload to be inserted */
op = 0;
pData = &aMem[pOp->p2];
@ -4310,10 +4309,10 @@ case OP_InsertInt: {
assert( pKey->flags & MEM_Int );
assert( memIsValid(pKey) );
REGISTER_TRACE(pOp->p3, pKey);
iKey = pKey->u.i;
x.nKey = pKey->u.i;
}else{
assert( pOp->opcode==OP_InsertInt );
iKey = pOp->p3;
x.nKey = pOp->p3;
}
if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
@ -4334,26 +4333,28 @@ case OP_InsertInt: {
&& pOp->p4type==P4_TABLE
&& !(pOp->p5 & OPFLAG_ISUPDATE)
){
sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, iKey, pOp->p2);
sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
}
#endif
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
if( pData->flags & MEM_Null ){
pData->z = 0;
pData->n = 0;
x.pData = 0;
x.nData = 0;
}else{
assert( pData->flags & (MEM_Blob|MEM_Str) );
x.pData = pData->z;
x.nData = pData->n;
}
seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
if( pData->flags & MEM_Zero ){
nZero = pData->u.nZero;
x.nZero = pData->u.nZero;
}else{
nZero = 0;
x.nZero = 0;
}
rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey,
pData->z, pData->n, nZero,
x.pKey = 0;
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
(pOp->p5 & OPFLAG_APPEND)!=0, seekResult
);
pC->deferredMoveto = 0;
@ -4362,7 +4363,7 @@ case OP_InsertInt: {
/* Invoke the update-hook if required. */
if( rc ) goto abort_due_to_error;
if( db->xUpdateCallback && op ){
db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, iKey);
db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
}
break;
}
@ -4977,8 +4978,7 @@ next_tail:
case OP_SorterInsert: /* in2 */
case OP_IdxInsert: { /* in2 */
VdbeCursor *pC;
int nKey;
const char *zKey;
BtreePayload x;
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
pC = p->apCsr[pOp->p1];
@ -4994,9 +4994,12 @@ case OP_IdxInsert: { /* in2 */
if( pOp->opcode==OP_SorterInsert ){
rc = sqlite3VdbeSorterWrite(pC, pIn2);
}else{
nKey = pIn2->n;
zKey = pIn2->z;
rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3,
x.nKey = pIn2->n;
x.pKey = pIn2->z;
x.nData = 0;
x.nZero = 0;
x.pData = 0;
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, pOp->p3,
((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
);
assert( pC->deferredMoveto==0 );