aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordan <dan@noemail.net>2011-08-12 15:02:00 +0000
committerdan <dan@noemail.net>2011-08-12 15:02:00 +0000
commit689ab897817c88f4ca42a8c591ef2f57b59bd94a (patch)
treeba857643e68ca0ab3faf5f475ff3fd862b44af60 /src
parent262765a74ea2e761a0a5d0723494869509b983ef (diff)
downloadsqlite-689ab897817c88f4ca42a8c591ef2f57b59bd94a.tar.gz
sqlite-689ab897817c88f4ca42a8c591ef2f57b59bd94a.zip
Add the SQLITE_OMIT_MERGE_SORT pre-processor directive. To omit the code in vdbesort.c.
FossilOrigin-Name: 4ced2394b10d0a4f86422ff893bcdf3cf32591e3
Diffstat (limited to 'src')
-rw-r--r--src/build.c35
-rw-r--r--src/ctime.c3
-rw-r--r--src/test_config.c6
-rw-r--r--src/vdbe.c24
-rw-r--r--src/vdbeInt.h10
-rw-r--r--src/vdbesort.c3
6 files changed, 61 insertions, 20 deletions
diff --git a/src/build.c b/src/build.c
index 7152ab8eb..91fe039c0 100644
--- a/src/build.c
+++ b/src/build.c
@@ -2308,7 +2308,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
Table *pTab = pIndex->pTable; /* The table that is indexed */
int iTab = pParse->nTab++; /* Btree cursor used for pTab */
int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
- int iSorter = pParse->nTab++; /* Btree cursor used for sorting */
+ int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
int addr1; /* Address of top of loop */
int tnum; /* Root page of index */
Vdbe *v; /* Generate code into this virtual machine */
@@ -2318,6 +2318,15 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
sqlite3 *db = pParse->db; /* The database connection */
int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
+ /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly
+ ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
+ ** defined or the system is configured to store temp files in-memory. */
+#ifdef SQLITE_OMIT_MERGE_SORT
+ static const int bUseSorter = 0;
+#else
+ const int bUseSorter = !sqlite3TempInMemory(pParse->db);
+#endif
+
#ifndef SQLITE_OMIT_AUTHORIZATION
if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
db->aDb[iDb].zName ) ){
@@ -2343,8 +2352,11 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
sqlite3VdbeChangeP5(v, 1);
}
- /* Open the sorter cursor. */
- sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
+ /* Open the sorter cursor if we are to use one. */
+ if( bUseSorter ){
+ iSorter = pParse->nTab++;
+ sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
+ }
/* Open the table. Loop through all rows of the table, inserting index
** records into the sorter. */
@@ -2352,13 +2364,14 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
regRecord = sqlite3GetTempReg(pParse);
regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
- sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
- sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
- sqlite3VdbeJumpHere(v, addr1);
- /* Rewind the sorter. Loop through index records in sorted order. */
- addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSorter, 0);
- sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
+ if( bUseSorter ){
+ sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
+ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
+ sqlite3VdbeJumpHere(v, addr1);
+ addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSorter, 0);
+ sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
+ }
if( pIndex->onError!=OE_None ){
const int regRowid = regIdxKey + pIndex->nColumn;
@@ -2378,15 +2391,15 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
sqlite3HaltConstraint(
pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
}
- sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
sqlite3ReleaseTempReg(pParse, regRecord);
sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
sqlite3VdbeJumpHere(v, addr1);
sqlite3VdbeAddOp1(v, OP_Close, iTab);
- sqlite3VdbeAddOp1(v, OP_Close, iSorter);
sqlite3VdbeAddOp1(v, OP_Close, iIdx);
+ sqlite3VdbeAddOp1(v, OP_Close, iSorter);
}
/*
diff --git a/src/ctime.c b/src/ctime.c
index a128f61a6..9d31596bb 100644
--- a/src/ctime.c
+++ b/src/ctime.c
@@ -257,6 +257,9 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_OMIT_MEMORYDB
"OMIT_MEMORYDB",
#endif
+#ifdef SQLITE_OMIT_MERGE_SORT
+ "OMIT_MERGE_SORT",
+#endif
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
"OMIT_OR_OPTIMIZATION",
#endif
diff --git a/src/test_config.c b/src/test_config.c
index e8d6f88f6..5b0ffaa69 100644
--- a/src/test_config.c
+++ b/src/test_config.c
@@ -363,6 +363,12 @@ Tcl_SetVar2(interp, "sqlite_options", "long_double",
Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "0", TCL_GLOBAL_ONLY);
#endif
+#ifdef SQLITE_OMIT_MERGE_SORT
+ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "0", TCL_GLOBAL_ONLY);
+#else
+ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY);
+#endif
+
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY);
#else
diff --git a/src/vdbe.c b/src/vdbe.c
index 97edf90f0..857881413 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -157,6 +157,13 @@ int sqlite3_found_count = 0;
*/
#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
+/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
+#ifdef SQLITE_OMIT_MERGE_SORT
+# define isSorter(x) 0
+#else
+# define isSorter(x) ((x)->pSorter!=0)
+#endif
+
/*
** Argument pMem points at a register that will be passed to a
** user-defined function or returned to the user as the result of a query.
@@ -3155,13 +3162,12 @@ case OP_OpenEphemeral: {
SQLITE_OPEN_DELETEONCLOSE |
SQLITE_OPEN_TRANSIENT_DB;
- int btflags = BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5;
-
assert( pOp->p1>=0 );
pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
if( pCx==0 ) goto no_mem;
pCx->nullRow = 1;
- rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, btflags, vfsFlags);
+ rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
+ BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
if( rc==SQLITE_OK ){
rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
}
@@ -3180,7 +3186,7 @@ case OP_OpenEphemeral: {
rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
(KeyInfo*)pOp->p4.z, pCx->pCursor);
pCx->pKeyInfo = pOp->p4.pKeyInfo;
- pCx->pKeyInfo->enc = ENC(db);
+ pCx->pKeyInfo->enc = ENC(p->db);
}
pCx->isTable = 0;
}else{
@@ -3190,9 +3196,11 @@ case OP_OpenEphemeral: {
}
pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
pCx->isIndex = !pCx->isTable;
+#ifndef SQLITE_OMIT_MERGE_SORT
if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
rc = sqlite3VdbeSorterInit(db, pCx);
}
+#endif
break;
}
@@ -4083,7 +4091,8 @@ case OP_RowData: {
assert( pC->nullRow==0 );
assert( pC->pseudoTableReg==0 );
- if( pC->pSorter ){
+ if( isSorter(pC) ){
+ assert( pOp->opcode==OP_RowKey );
rc = sqlite3VdbeSorterRowkey(db, pC, pOut);
break;
}
@@ -4268,7 +4277,7 @@ case OP_Rewind: { /* jump */
pC = p->apCsr[pOp->p1];
assert( pC!=0 );
res = 1;
- if( pC->pSorter ){
+ if( isSorter(pC) ){
rc = sqlite3VdbeSorterRewind(db, pC, &res);
}else if( (pCrsr = pC->pCursor)!=0 ){
rc = sqlite3BtreeFirst(pCrsr, &res);
@@ -4324,7 +4333,7 @@ case OP_Next: { /* jump */
if( pC==0 ){
break; /* See ticket #2273 */
}
- if( pC->pSorter ){
+ if( isSorter(pC) ){
assert( pOp->opcode==OP_Next );
rc = sqlite3VdbeSorterNext(db, pC, &res);
}else{
@@ -4340,7 +4349,6 @@ case OP_Next: { /* jump */
}
pC->nullRow = (u8)res;
pC->cacheStatus = CACHE_STALE;
-
if( res==0 ){
pc = pOp->p2 - 1;
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
diff --git a/src/vdbeInt.h b/src/vdbeInt.h
index b2f5ef661..1574f1d57 100644
--- a/src/vdbeInt.h
+++ b/src/vdbeInt.h
@@ -392,13 +392,21 @@ void sqlite3VdbeFrameDelete(VdbeFrame*);
int sqlite3VdbeFrameRestore(VdbeFrame *);
void sqlite3VdbeMemStoreType(Mem *pMem);
+#ifdef SQLITE_OMIT_MERGE_SORT
+# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
+# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
+# define sqlite3VdbeSorterClose(Y,Z)
+# define sqlite3VdbeSorterRowkey(X,Y,Z) SQLITE_OK
+# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
+# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
+#else
int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
-
int sqlite3VdbeSorterRowkey(sqlite3 *, VdbeCursor *, Mem *);
int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
+#endif
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
void sqlite3VdbeEnter(Vdbe*);
diff --git a/src/vdbesort.c b/src/vdbesort.c
index e7cb3aae0..0292ea933 100644
--- a/src/vdbesort.c
+++ b/src/vdbesort.c
@@ -18,6 +18,8 @@
#include "sqliteInt.h"
#include "vdbeInt.h"
+#ifndef SQLITE_OMIT_MERGE_SORT
+
typedef struct VdbeSorterIter VdbeSorterIter;
/*
@@ -684,3 +686,4 @@ int sqlite3VdbeSorterRowkey(sqlite3 *db, VdbeCursor *pCsr, Mem *pOut){
return SQLITE_OK;
}
+#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */