/ Check-in [c00e45ed]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge latest changes from the trunk into the sessions branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: c00e45ede7cbf71a3a6d1ccad0b9275010ca8493
User & Date: dan 2011-09-14 19:41:44
Context
2011-09-16
19:40
Merge the latest trunk changes into the sessions branch. check-in: 5efb0294 user: drh tags: sessions
2011-09-14
19:41
Merge latest changes from the trunk into the sessions branch. check-in: c00e45ed user: dan tags: sessions
18:19
Remove 'const' from a variable that might actually be written when an OOM error occurs. Also, fix a couple MSVC/Win32 compiler warnings. check-in: 3035dc1c user: mistachkin tags: trunk
2011-08-26
19:20
Merge the latest trunk changes into the sessions branch. check-in: eb036d6f user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   771    771   tclsqlite3$(TEXE):	tclsqlite-shell.lo libsqlite3.la
   772    772   	$(LTLINK) -o $@ tclsqlite-shell.lo \
   773    773   		 libsqlite3.la $(LIBTCL)
   774    774   
   775    775   # Rules to build opcodes.c and opcodes.h
   776    776   #
   777    777   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   778         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          778  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   779    779   
   780    780   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   781    781   	cat parse.h $(TOP)/src/vdbe.c | $(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   782    782   
   783    783   # Rules to build parse.c and parse.h - the outputs of lemon.
   784    784   #
   785    785   parse.h:	parse.c

Changes to Makefile.msc.

   805    805   tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib
   806    806   	$(LTLINK) tclsqlite-shell.lo \
   807    807   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) libsqlite3.lib $(LIBTCL)
   808    808   
   809    809   # Rules to build opcodes.c and opcodes.h
   810    810   #
   811    811   opcodes.c:	opcodes.h $(TOP)\mkopcodec.awk
   812         -	$(NAWK) "/#define OP_/ { print }" opcodes.h | sort /+45 | $(NAWK) -f $(TOP)\mkopcodec.awk > opcodes.c
          812  +	$(NAWK) -f $(TOP)\mkopcodec.awk opcodes.h > opcodes.c
   813    813   
   814    814   opcodes.h:	parse.h $(TOP)\src\vdbe.c $(TOP)\mkopcodeh.awk
   815    815   	type parse.h $(TOP)\src\vdbe.c | $(NAWK) -f $(TOP)\mkopcodeh.awk > opcodes.h
   816    816   
   817    817   # Rules to build parse.c and parse.h - the outputs of lemon.
   818    818   #
   819    819   parse.h:	parse.c
................................................................................
   934    934   	$(LTLINK) -DTCLSH=2 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 \
   935    935   		-DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE \
   936    936   		-DBUILD_sqlite -I$(TCLINCDIR) \
   937    937   		$(TESTFIXTURE_SRC) \
   938    938   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) $(LIBTCL) $(TLIBS)
   939    939   
   940    940   clean:
   941         -	del /Q *.lo *.lib *.obj sqlite3.exe libsqlite3.lib
          941  +	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
   942    942   	del /Q sqlite3.h opcodes.c opcodes.h
   943    943   	del /Q lemon.exe lempar.c parse.*
   944    944   	del /Q mkkeywordhash.exe keywordhash.h
   945    945   	-rmdir /Q/S tsrc
   946    946   	del /Q .target_source
   947    947   	del /Q testfixture.exe testfixture.exp test.db
   948    948   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def

Changes to Makefile.vxworks.

   513    513   	$(TCCX_SHARED) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c
   514    514   
   515    515   
   516    516   
   517    517   # Rules to build opcodes.c and opcodes.h
   518    518   #
   519    519   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   520         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          520  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   521    521   
   522    522   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   523    523   	cat parse.h $(TOP)/src/vdbe.c | \
   524    524   		$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   525    525   
   526    526   # Rules to build parse.c and parse.h - the outputs of lemon.
   527    527   #

Changes to ext/fts3/fts3.c.

  2062   2062     char *p2 = a2;
  2063   2063     char *p;
  2064   2064     char *aOut;
  2065   2065     int bFirstOut = 0;
  2066   2066   
  2067   2067     *paOut = 0;
  2068   2068     *pnOut = 0;
  2069         -  aOut = sqlite3_malloc(n1+n2);
         2069  +
         2070  +  /* Allocate space for the output. Both the input and output doclists
         2071  +  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
         2072  +  ** then the first docid in each list is simply encoded as a varint. For
         2073  +  ** each subsequent docid, the varint stored is the difference between the
         2074  +  ** current and previous docid (a positive number - since the list is in
         2075  +  ** ascending order).
         2076  +  **
         2077  +  ** The first docid written to the output is therefore encoded using the 
         2078  +  ** same number of bytes as it is in whichever of the input lists it is
         2079  +  ** read from. And each subsequent docid read from the same input list 
         2080  +  ** consumes either the same or less bytes as it did in the input (since
         2081  +  ** the difference between it and the previous value in the output must
         2082  +  ** be a positive value less than or equal to the delta value read from 
         2083  +  ** the input list). The same argument applies to all but the first docid
         2084  +  ** read from the 'other' list. And to the contents of all position lists
         2085  +  ** that will be copied and merged from the input to the output.
         2086  +  **
         2087  +  ** However, if the first docid copied to the output is a negative number,
         2088  +  ** then the encoding of the first docid from the 'other' input list may
         2089  +  ** be larger in the output than it was in the input (since the delta value
         2090  +  ** may be a larger positive integer than the actual docid).
         2091  +  **
         2092  +  ** The space required to store the output is therefore the sum of the
         2093  +  ** sizes of the two inputs, plus enough space for exactly one of the input
         2094  +  ** docids to grow. 
         2095  +  **
         2096  +  ** A symetric argument may be made if the doclists are in descending 
         2097  +  ** order.
         2098  +  */
         2099  +  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
  2070   2100     if( !aOut ) return SQLITE_NOMEM;
  2071   2101   
  2072   2102     p = aOut;
  2073   2103     fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2074   2104     fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2075   2105     while( p1 || p2 ){
  2076   2106       sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
................................................................................
  2089   2119         fts3PoslistCopy(&p, &p2);
  2090   2120         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2091   2121       }
  2092   2122     }
  2093   2123   
  2094   2124     *paOut = aOut;
  2095   2125     *pnOut = (p-aOut);
         2126  +  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
  2096   2127     return SQLITE_OK;
  2097   2128   }
  2098   2129   
  2099   2130   /*
  2100   2131   ** This function does a "phrase" merge of two doclists. In a phrase merge,
  2101   2132   ** the output contains a copy of each position from the right-hand input
  2102   2133   ** doclist for which there is a position in the left-hand input doclist

Changes to main.mk.

   428    428   	$(TCCX) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c
   429    429   
   430    430   
   431    431   
   432    432   # Rules to build opcodes.c and opcodes.h
   433    433   #
   434    434   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   435         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          435  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   436    436   
   437    437   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   438    438   	cat parse.h $(TOP)/src/vdbe.c | \
   439    439   		$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   440    440   
   441    441   # Rules to build parse.c and parse.h - the outputs of lemon.
   442    442   #

Changes to mkopcodec.awk.

    13     13     print "/* See the mkopcodec.awk script for details. */"
    14     14     printf "#if !defined(SQLITE_OMIT_EXPLAIN)"
    15     15     printf    " || !defined(NDEBUG)"
    16     16     printf    " || defined(VDBE_PROFILE)"
    17     17     print     " || defined(SQLITE_DEBUG)"
    18     18     print "const char *sqlite3OpcodeName(int i){"
    19     19     print " static const char *const azName[] = { \"?\","
           20  +  mx = 0
    20     21   }
    21     22   /define OP_/ {
    22     23     sub("OP_","",$2)
    23         -  i++
    24         -  printf "     /* %3d */ \"%s\",\n", $3, $2
           24  +  i = $3+0
           25  +  label[i] = $2
           26  +  if( mx<i ) mx = i
    25     27   }
    26     28   END {
           29  +  for(i=1; i<=mx; i++){
           30  +    printf "     /* %3d */ \"%s\",\n", i, label[i]
           31  +  }
    27     32     print "  };"
    28     33     print "  return azName[i];"
    29     34     print "}"
    30     35     print "#endif"
    31     36   }

Changes to src/btree.c.

   652    652     i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
   653    653     int bias,           /* Bias search to the high end */
   654    654     int *pRes           /* Write search results here */
   655    655   ){
   656    656     int rc;                    /* Status code */
   657    657     UnpackedRecord *pIdxKey;   /* Unpacked index key */
   658    658     char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
          659  +  char *pFree = 0;
   659    660   
   660    661     if( pKey ){
   661    662       assert( nKey==(i64)(int)nKey );
   662         -    pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
   663         -                                      aSpace, sizeof(aSpace));
          663  +    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
          664  +        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
          665  +    );
   664    666       if( pIdxKey==0 ) return SQLITE_NOMEM;
          667  +    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
   665    668     }else{
   666    669       pIdxKey = 0;
   667    670     }
   668    671     rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
   669         -  if( pKey ){
   670         -    sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
          672  +  if( pFree ){
          673  +    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
   671    674     }
   672    675     return rc;
   673    676   }
   674    677   
   675    678   /*
   676    679   ** Restore the cursor to the position it was in (or as close to as possible)
   677    680   ** when saveCursorPosition() was called. Note that this call deletes the 
................................................................................
  1730   1733   
  1731   1734     /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
  1732   1735     assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
  1733   1736   
  1734   1737     /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
  1735   1738     assert( (flags & BTREE_SINGLE)==0 || isTempDb );
  1736   1739   
  1737         -  /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */
  1738         -#ifdef SQLITE_OMIT_MERGE_SORT
  1739         -  assert( (flags & BTREE_SORTER)==0 );
  1740         -#endif
  1741         -
  1742         -  /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */
  1743         -  assert( (flags & BTREE_SORTER)==0 ||
  1744         -          (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL))
  1745         -                                        ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) );
  1746         -
  1747   1740     if( db->flags & SQLITE_NoReadlock ){
  1748   1741       flags |= BTREE_NO_READLOCK;
  1749   1742     }
  1750   1743     if( isMemdb ){
  1751   1744       flags |= BTREE_MEMORY;
  1752         -    flags &= ~BTREE_SORTER;
  1753   1745     }
  1754   1746     if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
  1755   1747       vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
  1756   1748     }
  1757   1749     p = sqlite3MallocZero(sizeof(Btree));
  1758   1750     if( !p ){
  1759   1751       return SQLITE_NOMEM;
................................................................................
  2750   2742       nCell = pPage->nCell;
  2751   2743   
  2752   2744       for(i=0; i<nCell; i++){
  2753   2745         u8 *pCell = findCell(pPage, i);
  2754   2746         if( eType==PTRMAP_OVERFLOW1 ){
  2755   2747           CellInfo info;
  2756   2748           btreeParseCellPtr(pPage, pCell, &info);
  2757         -        if( info.iOverflow ){
  2758         -          if( iFrom==get4byte(&pCell[info.iOverflow]) ){
  2759         -            put4byte(&pCell[info.iOverflow], iTo);
  2760         -            break;
  2761         -          }
         2749  +        if( info.iOverflow
         2750  +         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
         2751  +         && iFrom==get4byte(&pCell[info.iOverflow])
         2752  +        ){
         2753  +          put4byte(&pCell[info.iOverflow], iTo);
         2754  +          break;
  2762   2755           }
  2763   2756         }else{
  2764   2757           if( get4byte(pCell)==iFrom ){
  2765   2758             put4byte(pCell, iTo);
  2766   2759             break;
  2767   2760           }
  2768   2761         }
................................................................................
  3475   3468     assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
  3476   3469     assert( pBt->pPage1 && pBt->pPage1->aData );
  3477   3470   
  3478   3471     if( NEVER(wrFlag && pBt->readOnly) ){
  3479   3472       return SQLITE_READONLY;
  3480   3473     }
  3481   3474     if( iTable==1 && btreePagecount(pBt)==0 ){
  3482         -    return SQLITE_EMPTY;
         3475  +    assert( wrFlag==0 );
         3476  +    iTable = 0;
  3483   3477     }
  3484   3478   
  3485   3479     /* Now that no other errors can occur, finish filling in the BtCursor
  3486   3480     ** variables and link the cursor into the BtShared list.  */
  3487   3481     pCur->pgnoRoot = (Pgno)iTable;
  3488   3482     pCur->iPage = -1;
  3489   3483     pCur->pKeyInfo = pKeyInfo;
................................................................................
  4229   4223   
  4230   4224     if( pCur->iPage>=0 ){
  4231   4225       int i;
  4232   4226       for(i=1; i<=pCur->iPage; i++){
  4233   4227         releasePage(pCur->apPage[i]);
  4234   4228       }
  4235   4229       pCur->iPage = 0;
         4230  +  }else if( pCur->pgnoRoot==0 ){
         4231  +    pCur->eState = CURSOR_INVALID;
         4232  +    return SQLITE_OK;
  4236   4233     }else{
  4237   4234       rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
  4238   4235       if( rc!=SQLITE_OK ){
  4239   4236         pCur->eState = CURSOR_INVALID;
  4240   4237         return rc;
  4241   4238       }
  4242   4239       pCur->iPage = 0;
................................................................................
  4338   4335     int rc;
  4339   4336   
  4340   4337     assert( cursorHoldsMutex(pCur) );
  4341   4338     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4342   4339     rc = moveToRoot(pCur);
  4343   4340     if( rc==SQLITE_OK ){
  4344   4341       if( pCur->eState==CURSOR_INVALID ){
  4345         -      assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4342  +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4346   4343         *pRes = 1;
  4347   4344       }else{
  4348   4345         assert( pCur->apPage[pCur->iPage]->nCell>0 );
  4349   4346         *pRes = 0;
  4350   4347         rc = moveToLeftmost(pCur);
  4351   4348       }
  4352   4349     }
................................................................................
  4377   4374   #endif
  4378   4375       return SQLITE_OK;
  4379   4376     }
  4380   4377   
  4381   4378     rc = moveToRoot(pCur);
  4382   4379     if( rc==SQLITE_OK ){
  4383   4380       if( CURSOR_INVALID==pCur->eState ){
  4384         -      assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4381  +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4385   4382         *pRes = 1;
  4386   4383       }else{
  4387   4384         assert( pCur->eState==CURSOR_VALID );
  4388   4385         *pRes = 0;
  4389   4386         rc = moveToRightmost(pCur);
  4390   4387         pCur->atLast = rc==SQLITE_OK ?1:0;
  4391   4388       }
................................................................................
  4450   4447       }
  4451   4448     }
  4452   4449   
  4453   4450     rc = moveToRoot(pCur);
  4454   4451     if( rc ){
  4455   4452       return rc;
  4456   4453     }
  4457         -  assert( pCur->apPage[pCur->iPage] );
  4458         -  assert( pCur->apPage[pCur->iPage]->isInit );
  4459         -  assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
         4454  +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
         4455  +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
         4456  +  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
  4460   4457     if( pCur->eState==CURSOR_INVALID ){
  4461   4458       *pRes = -1;
  4462         -    assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4459  +    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4463   4460       return SQLITE_OK;
  4464   4461     }
  4465   4462     assert( pCur->apPage[0]->intKey || pIdxKey );
  4466   4463     for(;;){
  4467   4464       int lwr, upr, idx;
  4468   4465       Pgno chldPg;
  4469   4466       MemPage *pPage = pCur->apPage[pCur->iPage];
................................................................................
  5182   5179     u32 ovflPageSize;
  5183   5180   
  5184   5181     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  5185   5182     btreeParseCellPtr(pPage, pCell, &info);
  5186   5183     if( info.iOverflow==0 ){
  5187   5184       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
  5188   5185     }
         5186  +  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
         5187  +    return SQLITE_CORRUPT;  /* Cell extends past end of page */
         5188  +  }
  5189   5189     ovflPgno = get4byte(&pCell[info.iOverflow]);
  5190   5190     assert( pBt->usableSize > 4 );
  5191   5191     ovflPageSize = pBt->usableSize - 4;
  5192   5192     nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
  5193   5193     assert( ovflPgno==0 || nOvfl>0 );
  5194   5194     while( nOvfl-- ){
  5195   5195       Pgno iNext = 0;
................................................................................
  7284   7284       */
  7285   7285       zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
  7286   7286       releasePage(pPage);
  7287   7287     }
  7288   7288     return rc;  
  7289   7289   }
  7290   7290   int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
  7291         -  BtShared *pBt = p->pBt;
  7292   7291     int rc;
  7293   7292     sqlite3BtreeEnter(p);
  7294         -  if( (pBt->openFlags&BTREE_SINGLE) ){
  7295         -    pBt->nPage = 0;
  7296         -    sqlite3PagerTruncateImage(pBt->pPager, 1);
  7297         -    rc = newDatabase(pBt);
  7298         -  }else{
  7299         -    rc = btreeDropTable(p, iTable, piMoved);
  7300         -  }
         7293  +  rc = btreeDropTable(p, iTable, piMoved);
  7301   7294     sqlite3BtreeLeave(p);
  7302   7295     return rc;
  7303   7296   }
  7304   7297   
  7305   7298   
  7306   7299   /*
  7307   7300   ** This function may only be called if the b-tree connection already
................................................................................
  7372   7365   ** SQLITE_OK is returned if the operation is successfully executed. 
  7373   7366   ** Otherwise, if an error is encountered (i.e. an IO error or database
  7374   7367   ** corruption) an SQLite error code is returned.
  7375   7368   */
  7376   7369   int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
  7377   7370     i64 nEntry = 0;                      /* Value to return in *pnEntry */
  7378   7371     int rc;                              /* Return code */
         7372  +
         7373  +  if( pCur->pgnoRoot==0 ){
         7374  +    *pnEntry = 0;
         7375  +    return SQLITE_OK;
         7376  +  }
  7379   7377     rc = moveToRoot(pCur);
  7380   7378   
  7381   7379     /* Unless an error occurs, the following loop runs one iteration for each
  7382   7380     ** page in the B-Tree structure (not including overflow pages). 
  7383   7381     */
  7384   7382     while( rc==SQLITE_OK ){
  7385   7383       int iIdx;                          /* Index of child node in parent */

Changes to src/btree.h.

    57     57   ** pager.h.
    58     58   */
    59     59   #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
    60     60   #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
    61     61   #define BTREE_MEMORY        4  /* This is an in-memory DB */
    62     62   #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
    63     63   #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
    64         -#define BTREE_SORTER       32  /* Used as accumulator in external merge sort */
    65     64   
    66     65   int sqlite3BtreeClose(Btree*);
    67     66   int sqlite3BtreeSetCacheSize(Btree*,int);
    68     67   int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
    69     68   int sqlite3BtreeSyncDisabled(Btree*);
    70     69   int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
    71     70   int sqlite3BtreeGetPageSize(Btree*);

Changes to src/build.c.

  2322   2322   */
  2323   2323   static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
  2324   2324     Table *pTab = pIndex->pTable;  /* The table that is indexed */
  2325   2325     int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
  2326   2326     int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
  2327   2327     int iSorter = iTab;            /* Cursor opened by OpenSorter (if in use) */
  2328   2328     int addr1;                     /* Address of top of loop */
         2329  +  int addr2;                     /* Address to jump to for next iteration */
  2329   2330     int tnum;                      /* Root page of index */
  2330   2331     Vdbe *v;                       /* Generate code into this virtual machine */
  2331   2332     KeyInfo *pKey;                 /* KeyInfo for index */
  2332   2333     int regIdxKey;                 /* Registers containing the index key */
  2333   2334     int regRecord;                 /* Register holding assemblied index record */
  2334   2335     sqlite3 *db = pParse->db;      /* The database connection */
  2335   2336     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
  2336   2337   
  2337         -  /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly 
  2338         -  ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
  2339         -  ** defined or the system is configured to store temp files in-memory. */
  2340         -#ifdef SQLITE_OMIT_MERGE_SORT
  2341         -  static const int bUseSorter = 0;
  2342         -#else
  2343         -  const int bUseSorter = !sqlite3TempInMemory(pParse->db);
  2344         -#endif
  2345         -
  2346   2338   #ifndef SQLITE_OMIT_AUTHORIZATION
  2347   2339     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
  2348   2340         db->aDb[iDb].zName ) ){
  2349   2341       return;
  2350   2342     }
  2351   2343   #endif
  2352   2344   
................................................................................
  2364   2356     pKey = sqlite3IndexKeyinfo(pParse, pIndex);
  2365   2357     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2366   2358                       (char *)pKey, P4_KEYINFO_HANDOFF);
  2367   2359     if( memRootPage>=0 ){
  2368   2360       sqlite3VdbeChangeP5(v, 1);
  2369   2361     }
  2370   2362   
         2363  +#ifndef SQLITE_OMIT_MERGE_SORT
  2371   2364     /* Open the sorter cursor if we are to use one. */
  2372         -  if( bUseSorter ){
  2373         -    iSorter = pParse->nTab++;
  2374         -    sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
  2375         -    sqlite3VdbeChangeP5(v, BTREE_SORTER);
  2376         -  }
         2365  +  iSorter = pParse->nTab++;
         2366  +  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
         2367  +#endif
  2377   2368   
  2378   2369     /* Open the table. Loop through all rows of the table, inserting index
  2379   2370     ** records into the sorter. */
  2380   2371     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2381   2372     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
         2373  +  addr2 = addr1 + 1;
  2382   2374     regRecord = sqlite3GetTempReg(pParse);
  2383   2375     regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
  2384   2376   
  2385         -  if( bUseSorter ){
  2386         -    sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
  2387         -    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2388         -    sqlite3VdbeJumpHere(v, addr1);
  2389         -    addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0);
  2390         -    sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
         2377  +#ifndef SQLITE_OMIT_MERGE_SORT
         2378  +  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
         2379  +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
         2380  +  sqlite3VdbeJumpHere(v, addr1);
         2381  +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
         2382  +  if( pIndex->onError!=OE_None ){
         2383  +    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
         2384  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
         2385  +    addr2 = sqlite3VdbeCurrentAddr(v);
         2386  +    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
         2387  +    sqlite3HaltConstraint(
         2388  +        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
         2389  +    );
         2390  +  }else{
         2391  +    addr2 = sqlite3VdbeCurrentAddr(v);
  2391   2392     }
  2392         -
         2393  +  sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
         2394  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
         2395  +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         2396  +#else
  2393   2397     if( pIndex->onError!=OE_None ){
  2394   2398       const int regRowid = regIdxKey + pIndex->nColumn;
  2395   2399       const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
  2396   2400       void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
  2397   2401   
  2398   2402       /* The registers accessed by the OP_IsUnique opcode were allocated
  2399   2403       ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
................................................................................
  2404   2408       ** we can be sure that no other temp registers have been allocated
  2405   2409       ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
  2406   2410       */
  2407   2411       sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
  2408   2412       sqlite3HaltConstraint(
  2409   2413           pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
  2410   2414     }
  2411         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
         2415  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
  2412   2416     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         2417  +#endif
  2413   2418     sqlite3ReleaseTempReg(pParse, regRecord);
  2414         -  sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
         2419  +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
  2415   2420     sqlite3VdbeJumpHere(v, addr1);
  2416   2421   
  2417   2422     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2418   2423     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
  2419   2424     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
  2420   2425   }
  2421   2426   

Changes to src/ctime.c.

   140    140   #endif
   141    141   #ifdef SQLITE_INT64_TYPE
   142    142     "INT64_TYPE",
   143    143   #endif
   144    144   #ifdef SQLITE_LOCK_TRACE
   145    145     "LOCK_TRACE",
   146    146   #endif
          147  +#ifdef SQLITE_MAX_SCHEMA_RETRY
          148  +  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
          149  +#endif
   147    150   #ifdef SQLITE_MEMDEBUG
   148    151     "MEMDEBUG",
   149    152   #endif
   150    153   #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   151    154     "MIXED_ENDIAN_64BIT_FLOAT",
   152    155   #endif
   153    156   #ifdef SQLITE_NO_SYNC

Changes to src/expr.c.

  2283   2283         AggInfo *pAggInfo = pExpr->pAggInfo;
  2284   2284         struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
  2285   2285         if( !pAggInfo->directMode ){
  2286   2286           assert( pCol->iMem>0 );
  2287   2287           inReg = pCol->iMem;
  2288   2288           break;
  2289   2289         }else if( pAggInfo->useSortingIdx ){
  2290         -        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
         2290  +        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
  2291   2291                                 pCol->iSorterColumn, target);
  2292   2292           break;
  2293   2293         }
  2294   2294         /* Otherwise, fall thru into the TK_COLUMN case */
  2295   2295       }
  2296   2296       case TK_COLUMN: {
  2297   2297         if( pExpr->iTable<0 ){

Changes to src/os_common.h.

    25     25   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
    26     26   ** switch.  The following code should catch this problem at compile-time.
    27     27   */
    28     28   #ifdef MEMORY_DEBUG
    29     29   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
    30     30   #endif
    31     31   
    32         -#ifdef SQLITE_DEBUG
           32  +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
    33     33   # ifndef SQLITE_DEBUG_OS_TRACE
    34     34   #   define SQLITE_DEBUG_OS_TRACE 0
    35     35   # endif
    36     36     int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
    37     37   # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
    38     38   #else
    39     39   # define OSTRACE(X)

Changes to src/os_unix.c.

  2521   2521   ** to a non-zero value otherwise *pResOut is set to zero.  The return value
  2522   2522   ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
  2523   2523   */
  2524   2524   static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
  2525   2525     int rc = SQLITE_OK;
  2526   2526     int reserved = 0;
  2527   2527     unixFile *pFile = (unixFile*)id;
         2528  +  afpLockingContext *context;
  2528   2529     
  2529   2530     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  2530   2531     
  2531   2532     assert( pFile );
  2532         -  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
         2533  +  context = (afpLockingContext *) pFile->lockingContext;
  2533   2534     if( context->reserved ){
  2534   2535       *pResOut = 1;
  2535   2536       return SQLITE_OK;
  2536   2537     }
  2537   2538     unixEnterMutex(); /* Because pFile->pInode is shared across threads */
  2538   2539     
  2539   2540     /* Check if a thread in this process holds such a lock */
................................................................................
  2665   2666       }
  2666   2667     }
  2667   2668     
  2668   2669     /* If control gets to this point, then actually go ahead and make
  2669   2670     ** operating system calls for the specified lock.
  2670   2671     */
  2671   2672     if( eFileLock==SHARED_LOCK ){
  2672         -    int lrc1, lrc2, lrc1Errno;
         2673  +    int lrc1, lrc2, lrc1Errno = 0;
  2673   2674       long lk, mask;
  2674   2675       
  2675   2676       assert( pInode->nShared==0 );
  2676   2677       assert( pInode->eFileLock==0 );
  2677   2678           
  2678   2679       mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
  2679   2680       /* Now get the read-lock SHARED_LOCK */
................................................................................
  3141   3142   */
  3142   3143   int sqlite3_sync_count = 0;
  3143   3144   int sqlite3_fullsync_count = 0;
  3144   3145   #endif
  3145   3146   
  3146   3147   /*
  3147   3148   ** We do not trust systems to provide a working fdatasync().  Some do.
  3148         -** Others do no.  To be safe, we will stick with the (slower) fsync().
  3149         -** If you know that your system does support fdatasync() correctly,
         3149  +** Others do no.  To be safe, we will stick with the (slightly slower)
         3150  +** fsync(). If you know that your system does support fdatasync() correctly,
  3150   3151   ** then simply compile with -Dfdatasync=fdatasync
  3151   3152   */
  3152         -#if !defined(fdatasync) && !defined(__linux__)
         3153  +#if !defined(fdatasync)
  3153   3154   # define fdatasync fsync
  3154   3155   #endif
  3155   3156   
  3156   3157   /*
  3157   3158   ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
  3158   3159   ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
  3159   3160   ** only available on Mac OS X.  But that could change.
................................................................................
  3435   3436   ** proxying locking division.
  3436   3437   */
  3437   3438   static int proxyFileControl(sqlite3_file*,int,void*);
  3438   3439   #endif
  3439   3440   
  3440   3441   /* 
  3441   3442   ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
  3442         -** file-control operation.
  3443         -**
  3444         -** If the user has configured a chunk-size for this file, it could be
  3445         -** that the file needs to be extended at this point. Otherwise, the
  3446         -** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
         3443  +** file-control operation.  Enlarge the database to nBytes in size
         3444  +** (rounded up to the next chunk-size).  If the database is already
         3445  +** nBytes or larger, this routine is a no-op.
  3447   3446   */
  3448   3447   static int fcntlSizeHint(unixFile *pFile, i64 nByte){
  3449         -  { /* preserve indentation of removed "if" */
         3448  +  if( pFile->szChunk>0 ){
  3450   3449       i64 nSize;                    /* Required file size */
  3451         -    i64 szChunk;                  /* Chunk size */
  3452   3450       struct stat buf;              /* Used to hold return values of fstat() */
  3453   3451      
  3454   3452       if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
  3455   3453   
  3456         -    szChunk = pFile->szChunk;
  3457         -    if( szChunk==0 ){
  3458         -      nSize = nByte;
  3459         -    }else{
  3460         -      nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
  3461         -    }
         3454  +    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
  3462   3455       if( nSize>(i64)buf.st_size ){
  3463   3456   
  3464   3457   #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
  3465   3458         /* The code below is handling the return value of osFallocate() 
  3466   3459         ** correctly. posix_fallocate() is defined to "returns zero on success, 
  3467   3460         ** or an error number on  failure". See the manpage for details. */
  3468   3461         int err;
................................................................................
  3643   3636   ** All other fields are read/write.  The unixShm.pFile->mutex must be held
  3644   3637   ** while accessing any read/write fields.
  3645   3638   */
  3646   3639   struct unixShm {
  3647   3640     unixShmNode *pShmNode;     /* The underlying unixShmNode object */
  3648   3641     unixShm *pNext;            /* Next unixShm with the same unixShmNode */
  3649   3642     u8 hasMutex;               /* True if holding the unixShmNode mutex */
         3643  +  u8 id;                     /* Id of this connection within its unixShmNode */
  3650   3644     u16 sharedMask;            /* Mask of shared locks held */
  3651   3645     u16 exclMask;              /* Mask of exclusive locks held */
  3652         -#ifdef SQLITE_DEBUG
  3653         -  u8 id;                     /* Id of this connection within its unixShmNode */
  3654         -#endif
  3655   3646   };
  3656   3647   
  3657   3648   /*
  3658   3649   ** Constants used for locking
  3659   3650   */
  3660   3651   #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
  3661   3652   #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
................................................................................
  4953   4944     int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  4954   4945     int isCreate     = (flags & SQLITE_OPEN_CREATE);
  4955   4946     int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  4956   4947     int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  4957   4948   #if SQLITE_ENABLE_LOCKING_STYLE
  4958   4949     int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
  4959   4950   #endif
         4951  +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
         4952  +  struct statfs fsInfo;
         4953  +#endif
  4960   4954   
  4961   4955     /* If creating a master or main-file journal, this function will open
  4962   4956     ** a file-descriptor on the directory too. The first time unixSync()
  4963   4957     ** is called the directory file descriptor will be fsync()ed and close()d.
  4964   4958     */
  4965   4959     int syncDir = (isCreate && (
  4966   4960           eType==SQLITE_OPEN_MASTER_JOURNAL 
................................................................................
  5085   5079     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  5086   5080   #endif
  5087   5081   
  5088   5082     noLock = eType!=SQLITE_OPEN_MAIN_DB;
  5089   5083   
  5090   5084     
  5091   5085   #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  5092         -  struct statfs fsInfo;
  5093   5086     if( fstatfs(fd, &fsInfo) == -1 ){
  5094   5087       ((unixFile*)pFile)->lastErrno = errno;
  5095   5088       robust_close(p, fd, __LINE__);
  5096   5089       return SQLITE_IOERR_ACCESS;
  5097   5090     }
  5098   5091     if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
  5099   5092       ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
................................................................................
  5109   5102       int useProxy = 0;
  5110   5103   
  5111   5104       /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
  5112   5105       ** never use proxy, NULL means use proxy for non-local files only.  */
  5113   5106       if( envforce!=NULL ){
  5114   5107         useProxy = atoi(envforce)>0;
  5115   5108       }else{
  5116         -      struct statfs fsInfo;
  5117   5109         if( statfs(zPath, &fsInfo) == -1 ){
  5118   5110           /* In theory, the close(fd) call is sub-optimal. If the file opened
  5119   5111           ** with fd is a database file, and there are other connections open
  5120   5112           ** on that file that are currently holding advisory locks on it,
  5121   5113           ** then the call to close() will cancel those locks. In practice,
  5122   5114           ** we're assuming that statfs() doesn't fail very often. At least
  5123   5115           ** not while other file descriptors opened by the same process on
................................................................................
  5850   5842         int err = errno;
  5851   5843         if( pError ){
  5852   5844           *pError = err;
  5853   5845         }
  5854   5846         return SQLITE_IOERR;
  5855   5847       }
  5856   5848     }
         5849  +#else
         5850  +  UNUSED_PARAMETER(pError);
  5857   5851   #endif
  5858   5852   #ifdef SQLITE_TEST
  5859   5853     /* simulate multiple hosts by creating unique hostid file paths */
  5860   5854     if( sqlite3_hostid_num != 0){
  5861   5855       pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
  5862   5856     }
  5863   5857   #endif
................................................................................
  5942   5936   static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
  5943   5937     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
  5944   5938     unixFile *conchFile = pCtx->conchFile;
  5945   5939     int rc = SQLITE_OK;
  5946   5940     int nTries = 0;
  5947   5941     struct timespec conchModTime;
  5948   5942     
         5943  +  memset(&conchModTime, 0, sizeof(conchModTime));
  5949   5944     do {
  5950   5945       rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
  5951   5946       nTries ++;
  5952   5947       if( rc==SQLITE_BUSY ){
  5953   5948         /* If the lock failed (busy):
  5954   5949          * 1st try: get the mod time of the conch, wait 0.5s and try again. 
  5955   5950          * 2nd try: fail if the mod time changed or host id is different, wait 
................................................................................
  6173   6168           }
  6174   6169         }
  6175   6170         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
  6176   6171         
  6177   6172       end_takeconch:
  6178   6173         OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
  6179   6174         if( rc==SQLITE_OK && pFile->openFlags ){
         6175  +        int fd;
  6180   6176           if( pFile->h>=0 ){
  6181   6177             robust_close(pFile, pFile->h, __LINE__);
  6182   6178           }
  6183   6179           pFile->h = -1;
  6184         -        int fd = robust_open(pCtx->dbPath, pFile->openFlags,
         6180  +        fd = robust_open(pCtx->dbPath, pFile->openFlags,
  6185   6181                         SQLITE_DEFAULT_FILE_PERMISSIONS);
  6186   6182           OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
  6187   6183           if( fd>=0 ){
  6188   6184             pFile->h = fd;
  6189   6185           }else{
  6190   6186             rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
  6191   6187              during locking */

Changes to src/os_win.c.

  1212   1212     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
  1213   1213   
  1214   1214     /* If the user has configured a chunk-size for this file, truncate the
  1215   1215     ** file so that it consists of an integer number of chunks (i.e. the
  1216   1216     ** actual file size after the operation may be larger than the requested
  1217   1217     ** size).
  1218   1218     */
  1219         -  if( pFile->szChunk ){
         1219  +  if( pFile->szChunk>0 ){
  1220   1220       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  1221   1221     }
  1222   1222   
  1223   1223     /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
  1224   1224     if( seekWinFile(pFile, nByte) ){
  1225   1225       rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
  1226   1226     }else if( 0==SetEndOfFile(pFile->h) ){
................................................................................
  1599   1599         return SQLITE_OK;
  1600   1600       }
  1601   1601       case SQLITE_FCNTL_CHUNK_SIZE: {
  1602   1602         pFile->szChunk = *(int *)pArg;
  1603   1603         return SQLITE_OK;
  1604   1604       }
  1605   1605       case SQLITE_FCNTL_SIZE_HINT: {
  1606         -      winFile *pFile = (winFile*)id;
  1607         -      sqlite3_int64 oldSz;
  1608         -      int rc = winFileSize(id, &oldSz);
  1609         -      if( rc==SQLITE_OK ){
  1610         -        sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
  1611         -        if( newSz>oldSz ){
  1612         -          SimulateIOErrorBenign(1);
  1613         -          rc = winTruncate(id, newSz);
  1614         -          SimulateIOErrorBenign(0);
         1606  +      if( pFile->szChunk>0 ){
         1607  +        sqlite3_int64 oldSz;
         1608  +        int rc = winFileSize(id, &oldSz);
         1609  +        if( rc==SQLITE_OK ){
         1610  +          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
         1611  +          if( newSz>oldSz ){
         1612  +            SimulateIOErrorBenign(1);
         1613  +            rc = winTruncate(id, newSz);
         1614  +            SimulateIOErrorBenign(0);
         1615  +          }
  1615   1616           }
         1617  +        return rc;
  1616   1618         }
  1617         -      return rc;
         1619  +      return SQLITE_OK;
  1618   1620       }
  1619   1621       case SQLITE_FCNTL_PERSIST_WAL: {
  1620   1622         int bPersist = *(int*)pArg;
  1621   1623         if( bPersist<0 ){
  1622   1624           *(int*)pArg = pFile->bPersistWal;
  1623   1625         }else{
  1624   1626           pFile->bPersistWal = bPersist!=0;

Changes to src/pager.c.

   617    617     u8 fullSync;                /* Do extra syncs of the journal for robustness */
   618    618     u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
   619    619     u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
   620    620     u8 tempFile;                /* zFilename is a temporary file */
   621    621     u8 readOnly;                /* True for a read-only database */
   622    622     u8 memDb;                   /* True to inhibit all file I/O */
   623    623     u8 hasSeenStress;           /* pagerStress() called one or more times */
   624         -  u8 isSorter;                /* True for a PAGER_SORTER */
   625    624   
   626    625     /**************************************************************************
   627    626     ** The following block contains those class members that change during
   628    627     ** routine opertion.  Class members not in this block are either fixed
   629    628     ** when the pager is first created or else only change when there is a
   630    629     ** significant mode change (such as changing the page_size, locking_mode,
   631    630     ** or the journal_mode).  From another view, these class members describe
................................................................................
   841    840       assert( p->journalMode==PAGER_JOURNALMODE_OFF 
   842    841            || p->journalMode==PAGER_JOURNALMODE_MEMORY 
   843    842       );
   844    843       assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
   845    844       assert( pagerUseWal(p)==0 );
   846    845     }
   847    846   
   848         -  /* A sorter is a temp file that never spills to disk and always has
   849         -  ** the doNotSpill flag set
   850         -  */
   851         -  if( p->isSorter ){
   852         -    assert( p->tempFile );
   853         -    assert( p->doNotSpill );
   854         -    assert( p->fd->pMethods==0 );
   855         -  }
   856         -
   857    847     /* If changeCountDone is set, a RESERVED lock or greater must be held
   858    848     ** on the file.
   859    849     */
   860    850     assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
   861    851     assert( p->eLock!=PENDING_LOCK );
   862    852   
   863    853     switch( p->eState ){
................................................................................
  4553   4543     }else if( memDb ){
  4554   4544       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
  4555   4545     }
  4556   4546     /* pPager->xBusyHandler = 0; */
  4557   4547     /* pPager->pBusyHandlerArg = 0; */
  4558   4548     pPager->xReiniter = xReinit;
  4559   4549     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  4560         -#ifndef SQLITE_OMIT_MERGE_SORT
  4561         -  if( flags & PAGER_SORTER ){
  4562         -    pPager->doNotSpill = 1;
  4563         -    pPager->isSorter = 1;
  4564         -  }
  4565         -#endif
  4566   4550   
  4567   4551     *ppPager = pPager;
  4568   4552     return SQLITE_OK;
  4569   4553   }
  4570   4554   
  4571   4555   
  4572   4556   
................................................................................
  6103   6087   /*
  6104   6088   ** Return true if this is an in-memory pager.
  6105   6089   */
  6106   6090   int sqlite3PagerIsMemdb(Pager *pPager){
  6107   6091     return MEMDB;
  6108   6092   }
  6109   6093   
  6110         -#ifndef SQLITE_OMIT_MERGE_SORT
  6111         -/*
  6112         -** Return true if the pager has seen a pagerStress callback.
  6113         -*/
  6114         -int sqlite3PagerUnderStress(Pager *pPager){
  6115         -  assert( pPager->isSorter );
  6116         -  assert( pPager->doNotSpill );
  6117         -  return pPager->hasSeenStress;
  6118         -}
  6119         -#endif
  6120         -
  6121   6094   /*
  6122   6095   ** Check that there are at least nSavepoint savepoints open. If there are
  6123   6096   ** currently less than nSavepoints open, then open one or more savepoints
  6124   6097   ** to make up the difference. If the number of savepoints is already
  6125   6098   ** equal to nSavepoint, then this function is a no-op.
  6126   6099   **
  6127   6100   ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 

Changes to src/pager.h.

   152    152   const char *sqlite3PagerFilename(Pager*);
   153    153   const sqlite3_vfs *sqlite3PagerVfs(Pager*);
   154    154   sqlite3_file *sqlite3PagerFile(Pager*);
   155    155   const char *sqlite3PagerJournalname(Pager*);
   156    156   int sqlite3PagerNosync(Pager*);
   157    157   void *sqlite3PagerTempSpace(Pager*);
   158    158   int sqlite3PagerIsMemdb(Pager*);
   159         -#ifndef SQLITE_OMIT_MERGE_SORT
   160         -int sqlite3PagerUnderStress(Pager*);
   161         -#endif
   162    159   
   163    160   /* Functions used to truncate the database file. */
   164    161   void sqlite3PagerTruncateImage(Pager*,Pgno);
   165    162   
   166    163   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
   167    164   void *sqlite3PagerCodec(DbPage *);
   168    165   #endif

Changes to src/select.c.

   415    415     Select *pSelect,       /* The whole SELECT statement */
   416    416     int regData            /* Register holding data to be sorted */
   417    417   ){
   418    418     Vdbe *v = pParse->pVdbe;
   419    419     int nExpr = pOrderBy->nExpr;
   420    420     int regBase = sqlite3GetTempRange(pParse, nExpr+2);
   421    421     int regRecord = sqlite3GetTempReg(pParse);
          422  +  int op;
   422    423     sqlite3ExprCacheClear(pParse);
   423    424     sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
   424    425     sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
   425    426     sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
   426    427     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
   427         -  sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
          428  +  if( pSelect->selFlags & SF_UseSorter ){
          429  +    op = OP_SorterInsert;
          430  +  }else{
          431  +    op = OP_IdxInsert;
          432  +  }
          433  +  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
   428    434     sqlite3ReleaseTempReg(pParse, regRecord);
   429    435     sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
   430    436     if( pSelect->iLimit ){
   431    437       int addr1, addr2;
   432    438       int iLimit;
   433    439       if( pSelect->iOffset ){
   434    440         iLimit = pSelect->iOffset+1;
................................................................................
   889    895     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   890    896       pseudoTab = pParse->nTab++;
   891    897       sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
   892    898       regRowid = 0;
   893    899     }else{
   894    900       regRowid = sqlite3GetTempReg(pParse);
   895    901     }
   896         -  addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
   897         -  codeOffset(v, p, addrContinue);
   898         -  sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
          902  +  if( p->selFlags & SF_UseSorter ){
          903  +    int regSortOut = ++pParse->nMem;
          904  +    int ptab2 = pParse->nTab++;
          905  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
          906  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
          907  +    codeOffset(v, p, addrContinue);
          908  +    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
          909  +    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
          910  +    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
          911  +  }else{
          912  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
          913  +    codeOffset(v, p, addrContinue);
          914  +    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
          915  +  }
   899    916     switch( eDest ){
   900    917       case SRT_Table:
   901    918       case SRT_EphemTab: {
   902    919         testcase( eDest==SRT_Table );
   903    920         testcase( eDest==SRT_EphemTab );
   904    921         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
   905    922         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
................................................................................
   944    961     }
   945    962     sqlite3ReleaseTempReg(pParse, regRow);
   946    963     sqlite3ReleaseTempReg(pParse, regRowid);
   947    964   
   948    965     /* The bottom of the loop
   949    966     */
   950    967     sqlite3VdbeResolveLabel(v, addrContinue);
   951         -  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
          968  +  if( p->selFlags & SF_UseSorter ){
          969  +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
          970  +  }else{
          971  +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
          972  +  }
   952    973     sqlite3VdbeResolveLabel(v, addrBreak);
   953    974     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   954    975       sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
   955    976     }
   956    977   }
   957    978   
   958    979   /*
................................................................................
  3910   3931     }
  3911   3932   
  3912   3933     /* Set the limiter.
  3913   3934     */
  3914   3935     iEnd = sqlite3VdbeMakeLabel(v);
  3915   3936     p->nSelectRow = (double)LARGEST_INT64;
  3916   3937     computeLimitRegisters(pParse, p, iEnd);
         3938  +  if( p->iLimit==0 && addrSortIndex>=0 ){
         3939  +    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
         3940  +    p->selFlags |= SF_UseSorter;
         3941  +  }
  3917   3942   
  3918   3943     /* Open a virtual index to use for the distinct set.
  3919   3944     */
  3920   3945     if( p->selFlags & SF_Distinct ){
  3921   3946       KeyInfo *pKeyInfo;
  3922   3947       distinct = pParse->nTab++;
  3923   3948       pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
................................................................................
  3945   3970         sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
  3946   3971         p->addrOpenEphm[2] = -1;
  3947   3972       }
  3948   3973   
  3949   3974       if( pWInfo->eDistinct ){
  3950   3975         VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
  3951   3976        
  3952         -      assert( addrDistinctIndex>0 );
         3977  +      assert( addrDistinctIndex>=0 );
  3953   3978         pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
  3954   3979   
  3955   3980         assert( isDistinct );
  3956   3981         assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED 
  3957   3982              || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE 
  3958   3983         );
  3959   3984         distinct = -1;
................................................................................
  4004   4029       int iBMem;          /* First Mem address for previous GROUP BY */
  4005   4030       int iUseFlag;       /* Mem address holding flag indicating that at least
  4006   4031                           ** one row of the input to the aggregator has been
  4007   4032                           ** processed */
  4008   4033       int iAbortFlag;     /* Mem address which causes query abort if positive */
  4009   4034       int groupBySort;    /* Rows come from source in GROUP BY order */
  4010   4035       int addrEnd;        /* End of processing for this SELECT */
         4036  +    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
         4037  +    int sortOut = 0;    /* Output register from the sorter */
  4011   4038   
  4012   4039       /* Remove any and all aliases between the result set and the
  4013   4040       ** GROUP BY clause.
  4014   4041       */
  4015   4042       if( pGroupBy ){
  4016   4043         int k;                        /* Loop counter */
  4017   4044         struct ExprList_item *pItem;  /* For looping over expression in a list */
................................................................................
  4065   4092         int addrTopOfLoop;  /* Top of the input loop */
  4066   4093         int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
  4067   4094         int addrReset;      /* Subroutine for resetting the accumulator */
  4068   4095         int regReset;       /* Return address register for reset subroutine */
  4069   4096   
  4070   4097         /* If there is a GROUP BY clause we might need a sorting index to
  4071   4098         ** implement it.  Allocate that sorting index now.  If it turns out
  4072         -      ** that we do not need it after all, the OpenEphemeral instruction
         4099  +      ** that we do not need it after all, the OP_SorterOpen instruction
  4073   4100         ** will be converted into a Noop.  
  4074   4101         */
  4075   4102         sAggInfo.sortingIdx = pParse->nTab++;
  4076   4103         pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
  4077         -      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, 
         4104  +      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
  4078   4105             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
  4079   4106             0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
  4080   4107   
  4081   4108         /* Initialize memory locations used by GROUP BY aggregate processing
  4082   4109         */
  4083   4110         iUseFlag = ++pParse->nMem;
  4084   4111         iAbortFlag = ++pParse->nMem;
................................................................................
  4151   4178                 sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
  4152   4179               }
  4153   4180               j++;
  4154   4181             }
  4155   4182           }
  4156   4183           regRecord = sqlite3GetTempReg(pParse);
  4157   4184           sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
  4158         -        sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
         4185  +        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
  4159   4186           sqlite3ReleaseTempReg(pParse, regRecord);
  4160   4187           sqlite3ReleaseTempRange(pParse, regBase, nCol);
  4161   4188           sqlite3WhereEnd(pWInfo);
  4162         -        sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
         4189  +        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
         4190  +        sortOut = sqlite3GetTempReg(pParse);
         4191  +        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
         4192  +        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
  4163   4193           VdbeComment((v, "GROUP BY sort"));
  4164   4194           sAggInfo.useSortingIdx = 1;
  4165   4195           sqlite3ExprCacheClear(pParse);
  4166   4196         }
  4167   4197   
  4168   4198         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
  4169   4199         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  4170   4200         ** Then compare the current GROUP BY terms against the GROUP BY terms
  4171   4201         ** from the previous row currently stored in a0, a1, a2...
  4172   4202         */
  4173   4203         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  4174   4204         sqlite3ExprCacheClear(pParse);
         4205  +      if( groupBySort ){
         4206  +        sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
         4207  +      }
  4175   4208         for(j=0; j<pGroupBy->nExpr; j++){
  4176   4209           if( groupBySort ){
  4177         -          sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
         4210  +          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
         4211  +          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  4178   4212           }else{
  4179   4213             sAggInfo.directMode = 1;
  4180   4214             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
  4181   4215           }
  4182   4216         }
  4183   4217         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
  4184   4218                             (char*)pKeyInfo, P4_KEYINFO);
................................................................................
  4209   4243         updateAccumulator(pParse, &sAggInfo);
  4210   4244         sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
  4211   4245         VdbeComment((v, "indicate data in accumulator"));
  4212   4246   
  4213   4247         /* End of the loop
  4214   4248         */
  4215   4249         if( groupBySort ){
  4216         -        sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
         4250  +        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
  4217   4251         }else{
  4218   4252           sqlite3WhereEnd(pWInfo);
  4219   4253           sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
  4220   4254         }
  4221   4255   
  4222   4256         /* Output the final row of result
  4223   4257         */

Changes to src/sqliteInt.h.

   368    368   ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
   369    369   ** on the command-line
   370    370   */
   371    371   #ifndef SQLITE_TEMP_STORE
   372    372   # define SQLITE_TEMP_STORE 1
   373    373   #endif
   374    374   
   375         -/*
   376         -** If all temporary storage is in-memory, then omit the external merge-sort
   377         -** logic since it is superfluous.
   378         -*/
   379         -#if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT)
   380         -# define SQLITE_OMIT_MERGE_SORT
   381         -#endif
   382         -
   383    375   /*
   384    376   ** GCC does not define the offsetof() macro so we'll have to do it
   385    377   ** ourselves.
   386    378   */
   387    379   #ifndef offsetof
   388    380   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
   389    381   #endif
................................................................................
  1554   1546   */
  1555   1547   struct AggInfo {
  1556   1548     u8 directMode;          /* Direct rendering mode means take data directly
  1557   1549                             ** from source tables rather than from accumulators */
  1558   1550     u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
  1559   1551                             ** than the source table */
  1560   1552     int sortingIdx;         /* Cursor number of the sorting index */
         1553  +  int sortingIdxPTab;     /* Cursor number of pseudo-table */
  1561   1554     ExprList *pGroupBy;     /* The group by clause */
  1562   1555     int nSortingColumn;     /* Number of columns in the sorting index */
  1563   1556     struct AggInfo_col {    /* For each column used in source tables */
  1564   1557       Table *pTab;             /* Source table */
  1565   1558       int iTable;              /* Cursor number of the source table */
  1566   1559       int iColumn;             /* Column number within the source table */
  1567   1560       int iSorterColumn;       /* Column number in the sorting index */
................................................................................
  2086   2079   */
  2087   2080   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
  2088   2081   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
  2089   2082   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
  2090   2083   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  2091   2084   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  2092   2085   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
         2086  +#define SF_UseSorter       0x0040  /* Sort using a sorter */
  2093   2087   
  2094   2088   
  2095   2089   /*
  2096   2090   ** The results of a select can be distributed in several ways.  The
  2097   2091   ** "SRT" prefix means "SELECT Result Type".
  2098   2092   */
  2099   2093   #define SRT_Union        1  /* Store result as keys in an index */

Changes to src/test1.c.

  4391   4391   
  4392   4392   /*
  4393   4393   ** Fill the stack with a known bitpattern.
  4394   4394   */
  4395   4395   static void prepStack(void){
  4396   4396     int i;
  4397   4397     u32 bigBuf[65536];
  4398         -  for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
         4398  +  for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef;
  4399   4399     sqlite3_stack_baseline = (u8*)&bigBuf[65536];
  4400   4400   }
  4401   4401   
  4402   4402   /*
  4403   4403   ** Get the current stack depth.  Used for debugging only.
  4404   4404   */
  4405   4405   u64 sqlite3StackDepth(void){

Changes to src/test_thread.c.

   278    278     extern void Md5_Register(sqlite3*);
   279    279   
   280    280     UNUSED_PARAMETER(clientData);
   281    281     UNUSED_PARAMETER(objc);
   282    282   
   283    283     zFilename = Tcl_GetString(objv[2]);
   284    284     rc = sqlite3_open(zFilename, &db);
          285  +#ifdef SQLITE_HAS_CODEC
          286  +  if( db && objc>=4 ){
          287  +    const char *zKey;
          288  +    int nKey;
          289  +    zKey = Tcl_GetStringFromObj(objv[3], &nKey);
          290  +    rc = sqlite3_key(db, zKey, nKey);
          291  +    if( rc!=SQLITE_OK ){
          292  +      char *zErrMsg = sqlite3_mprintf("error %d: %s", rc, sqlite3_errmsg(db));
          293  +      sqlite3_close(db);
          294  +      Tcl_AppendResult(interp, zErrMsg, (char*)0);
          295  +      sqlite3_free(zErrMsg);
          296  +      return TCL_ERROR;
          297  +    }
          298  +  }
          299  +#endif
   285    300     Md5_Register(db);
   286    301     sqlite3_busy_handler(db, xBusy, 0);
   287    302     
   288    303     if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
   289    304     Tcl_AppendResult(interp, zBuf, 0);
   290    305   
   291    306     return TCL_OK;
................................................................................
   345    360   
   346    361     rc = Tcl_GetIndexFromObjStruct(
   347    362         interp, objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iIndex
   348    363     );
   349    364     if( rc!=TCL_OK ) return rc;
   350    365     pSub = &aSub[iIndex];
   351    366   
   352         -  if( objc!=(pSub->nArg+2) ){
          367  +  if( objc<(pSub->nArg+2) ){
   353    368       Tcl_WrongNumArgs(interp, 2, objv, pSub->zUsage);
   354    369       return TCL_ERROR;
   355    370     }
   356    371   
   357    372     return pSub->xProc(clientData, interp, objc, objv);
   358    373   }
   359    374   

Changes to src/vdbe.c.

   679    679       */
   680    680       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   681    681       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   682    682         assert( pOp->p2>0 );
   683    683         assert( pOp->p2<=p->nMem );
   684    684         pOut = &aMem[pOp->p2];
   685    685         memAboutToChange(p, pOut);
   686         -      sqlite3VdbeMemReleaseExternal(pOut);
          686  +      MemReleaseExt(pOut);
   687    687         pOut->flags = MEM_Int;
   688    688       }
   689    689   
   690    690       /* Sanity checking on other operands */
   691    691   #ifdef SQLITE_DEBUG
   692    692       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   693    693         assert( pOp->p1>0 );
................................................................................
  2124   2124     Mem sMem;          /* For storing the record being decoded */
  2125   2125     u8 *zIdx;          /* Index into header */
  2126   2126     u8 *zEndHdr;       /* Pointer to first byte after the header */
  2127   2127     u32 offset;        /* Offset into the data */
  2128   2128     u32 szField;       /* Number of bytes in the content of a field */
  2129   2129     int szHdr;         /* Size of the header size field at start of record */
  2130   2130     int avail;         /* Number of bytes of available data */
         2131  +  u32 t;             /* A type code from the record header */
  2131   2132     Mem *pReg;         /* PseudoTable input register */
  2132   2133   
  2133   2134   
  2134   2135     p1 = pOp->p1;
  2135   2136     p2 = pOp->p2;
  2136   2137     pC = 0;
  2137   2138     memset(&sMem, 0, sizeof(sMem));
  2138   2139     assert( p1<p->nCursor );
  2139   2140     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  2140   2141     pDest = &aMem[pOp->p3];
  2141   2142     memAboutToChange(p, pDest);
  2142         -  MemSetTypeFlag(pDest, MEM_Null);
  2143   2143     zRec = 0;
  2144   2144   
  2145   2145     /* This block sets the variable payloadSize to be the total number of
  2146   2146     ** bytes in the record.
  2147   2147     **
  2148   2148     ** zRec is set to be the complete text of the record if it is available.
  2149   2149     ** The complete record text is always available for pseudo-tables
................................................................................
  2179   2179         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2180   2180         payloadSize = (u32)payloadSize64;
  2181   2181       }else{
  2182   2182         assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2183   2183         rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2184   2184         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2185   2185       }
  2186         -  }else if( pC->pseudoTableReg>0 ){
         2186  +  }else if( ALWAYS(pC->pseudoTableReg>0) ){
  2187   2187       pReg = &aMem[pC->pseudoTableReg];
  2188   2188       assert( pReg->flags & MEM_Blob );
  2189   2189       assert( memIsValid(pReg) );
  2190   2190       payloadSize = pReg->n;
  2191   2191       zRec = pReg->z;
  2192   2192       pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2193   2193       assert( payloadSize==0 || zRec!=0 );
  2194   2194     }else{
  2195   2195       /* Consider the row to be NULL */
  2196   2196       payloadSize = 0;
  2197   2197     }
  2198   2198   
  2199         -  /* If payloadSize is 0, then just store a NULL */
         2199  +  /* If payloadSize is 0, then just store a NULL.  This can happen because of
         2200  +  ** nullRow or because of a corrupt database. */
  2200   2201     if( payloadSize==0 ){
  2201         -    assert( pDest->flags&MEM_Null );
         2202  +    MemSetTypeFlag(pDest, MEM_Null);
  2202   2203       goto op_column_out;
  2203   2204     }
  2204   2205     assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
  2205   2206     if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2206   2207       goto too_big;
  2207   2208     }
  2208   2209   
................................................................................
  2301   2302       ** arrays.  aType[i] will contain the type integer for the i-th
  2302   2303       ** column and aOffset[i] will contain the offset from the beginning
  2303   2304       ** of the record to the start of the data for the i-th column
  2304   2305       */
  2305   2306       for(i=0; i<nField; i++){
  2306   2307         if( zIdx<zEndHdr ){
  2307   2308           aOffset[i] = offset;
  2308         -        zIdx += getVarint32(zIdx, aType[i]);
  2309         -        szField = sqlite3VdbeSerialTypeLen(aType[i]);
         2309  +        if( zIdx[0]<0x80 ){
         2310  +          t = zIdx[0];
         2311  +          zIdx++;
         2312  +        }else{
         2313  +          zIdx += sqlite3GetVarint32(zIdx, &t);
         2314  +        }
         2315  +        aType[i] = t;
         2316  +        szField = sqlite3VdbeSerialTypeLen(t);
  2310   2317           offset += szField;
  2311   2318           if( offset<szField ){  /* True if offset overflows */
  2312   2319             zIdx = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  2313   2320             break;
  2314   2321           }
  2315   2322         }else{
  2316   2323           /* If i is less that nField, then there are less fields in this
................................................................................
  2343   2350     ** then there are not enough fields in the record to satisfy the
  2344   2351     ** request.  In this case, set the value NULL or to P4 if P4 is
  2345   2352     ** a pointer to a Mem object.
  2346   2353     */
  2347   2354     if( aOffset[p2] ){
  2348   2355       assert( rc==SQLITE_OK );
  2349   2356       if( zRec ){
  2350         -      sqlite3VdbeMemReleaseExternal(pDest);
         2357  +      MemReleaseExt(pDest);
  2351   2358         sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
  2352   2359       }else{
  2353   2360         len = sqlite3VdbeSerialTypeLen(aType[p2]);
  2354   2361         sqlite3VdbeMemMove(&sMem, pDest);
  2355   2362         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
  2356   2363         if( rc!=SQLITE_OK ){
  2357   2364           goto op_column_out;
................................................................................
  2360   2367         sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
  2361   2368       }
  2362   2369       pDest->enc = encoding;
  2363   2370     }else{
  2364   2371       if( pOp->p4type==P4_MEM ){
  2365   2372         sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2366   2373       }else{
  2367         -      assert( pDest->flags&MEM_Null );
         2374  +      MemSetTypeFlag(pDest, MEM_Null);
  2368   2375       }
  2369   2376     }
  2370   2377   
  2371   2378     /* If we dynamically allocated space to hold the data (in the
  2372   2379     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2373   2380     ** dynamically allocated space over to the pDest structure.
  2374   2381     ** This prevents a memory copy.
................................................................................
  2556   2563   */
  2557   2564   #ifndef SQLITE_OMIT_BTREECOUNT
  2558   2565   case OP_Count: {         /* out2-prerelease */
  2559   2566     i64 nEntry;
  2560   2567     BtCursor *pCrsr;
  2561   2568   
  2562   2569     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2563         -  if( pCrsr ){
         2570  +  if( ALWAYS(pCrsr) ){
  2564   2571       rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2565   2572     }else{
  2566   2573       nEntry = 0;
  2567   2574     }
  2568   2575     pOut->u.i = nEntry;
  2569   2576     break;
  2570   2577   }
................................................................................
  3118   3125     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3119   3126     if( pCur==0 ) goto no_mem;
  3120   3127     pCur->nullRow = 1;
  3121   3128     pCur->isOrdered = 1;
  3122   3129     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3123   3130     pCur->pKeyInfo = pKeyInfo;
  3124   3131   
  3125         -  /* Since it performs no memory allocation or IO, the only values that
  3126         -  ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. 
  3127         -  ** SQLITE_EMPTY is only returned when attempting to open the table
  3128         -  ** rooted at page 1 of a zero-byte database.  */
  3129         -  assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
  3130         -  if( rc==SQLITE_EMPTY ){
  3131         -    pCur->pCursor = 0;
  3132         -    rc = SQLITE_OK;
  3133         -  }
         3132  +  /* Since it performs no memory allocation or IO, the only value that
         3133  +  ** sqlite3BtreeCursor() may return is SQLITE_OK. */
         3134  +  assert( rc==SQLITE_OK );
  3134   3135   
  3135   3136     /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
  3136   3137     ** SQLite used to check if the root-page flags were sane at this point
  3137   3138     ** and report database corruption if they were not, but this check has
  3138   3139     ** since moved into the btree layer.  */  
  3139   3140     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3140   3141     pCur->isIndex = !pCur->isTable;
................................................................................
  3167   3168   /* Opcode: OpenAutoindex P1 P2 * P4 *
  3168   3169   **
  3169   3170   ** This opcode works the same as OP_OpenEphemeral.  It has a
  3170   3171   ** different name to distinguish its use.  Tables created using
  3171   3172   ** by this opcode will be used for automatically created transient
  3172   3173   ** indices in joins.
  3173   3174   */
  3174         -/* Opcode: OpenSorter P1 P2 * P4 *
  3175         -**
  3176         -** This opcode works like OP_OpenEphemeral except that it opens
  3177         -** a transient index that is specifically designed to sort large
  3178         -** tables using an external merge-sort algorithm.
  3179         -*/
  3180         -case OP_OpenSorter: 
  3181   3175   case OP_OpenAutoindex: 
  3182   3176   case OP_OpenEphemeral: {
  3183   3177     VdbeCursor *pCx;
  3184   3178     static const int vfsFlags = 
  3185   3179         SQLITE_OPEN_READWRITE |
  3186   3180         SQLITE_OPEN_CREATE |
  3187   3181         SQLITE_OPEN_EXCLUSIVE |
  3188   3182         SQLITE_OPEN_DELETEONCLOSE |
  3189   3183         SQLITE_OPEN_TRANSIENT_DB;
  3190   3184   
  3191   3185     assert( pOp->p1>=0 );
  3192         -  assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) );
  3193   3186     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3194   3187     if( pCx==0 ) goto no_mem;
  3195   3188     pCx->nullRow = 1;
  3196   3189     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
  3197   3190                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3198   3191     if( rc==SQLITE_OK ){
  3199   3192       rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
................................................................................
  3219   3212       }else{
  3220   3213         rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
  3221   3214         pCx->isTable = 1;
  3222   3215       }
  3223   3216     }
  3224   3217     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3225   3218     pCx->isIndex = !pCx->isTable;
         3219  +  break;
         3220  +}
         3221  +
         3222  +/* Opcode: OpenSorter P1 P2 * P4 *
         3223  +**
         3224  +** This opcode works like OP_OpenEphemeral except that it opens
         3225  +** a transient index that is specifically designed to sort large
         3226  +** tables using an external merge-sort algorithm.
         3227  +*/
         3228  +case OP_SorterOpen: {
         3229  +  VdbeCursor *pCx;
  3226   3230   #ifndef SQLITE_OMIT_MERGE_SORT
  3227         -  if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
  3228         -    rc = sqlite3VdbeSorterInit(db, pCx);
  3229         -  }
         3231  +  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
         3232  +  if( pCx==0 ) goto no_mem;
         3233  +  pCx->pKeyInfo = pOp->p4.pKeyInfo;
         3234  +  pCx->pKeyInfo->enc = ENC(p->db);
         3235  +  pCx->isSorter = 1;
         3236  +  rc = sqlite3VdbeSorterInit(db, pCx);
         3237  +#else
         3238  +  pOp->opcode = OP_OpenEphemeral;
         3239  +  pc--;
  3230   3240   #endif
  3231   3241     break;
  3232   3242   }
  3233   3243   
  3234   3244   /* Opcode: OpenPseudo P1 P2 P3 * *
  3235   3245   **
  3236   3246   ** Open a new cursor that points to a fake table that contains a single
................................................................................
  3339   3349     pC = p->apCsr[pOp->p1];
  3340   3350     assert( pC!=0 );
  3341   3351     assert( pC->pseudoTableReg==0 );
  3342   3352     assert( OP_SeekLe == OP_SeekLt+1 );
  3343   3353     assert( OP_SeekGe == OP_SeekLt+2 );
  3344   3354     assert( OP_SeekGt == OP_SeekLt+3 );
  3345   3355     assert( pC->isOrdered );
  3346         -  if( pC->pCursor!=0 ){
         3356  +  if( ALWAYS(pC->pCursor!=0) ){
  3347   3357       oc = pOp->opcode;
  3348   3358       pC->nullRow = 0;
  3349   3359       if( pC->isTable ){
  3350   3360         /* The input value in P3 might be of any type: integer, real, string,
  3351   3361         ** blob, or NULL.  But it needs to be an integer before we can do
  3352   3362         ** the seek, so covert it. */
  3353   3363         pIn3 = &aMem[pOp->p3];
................................................................................
  3527   3537   ** See also: Found, NotExists, IsUnique
  3528   3538   */
  3529   3539   case OP_NotFound:       /* jump, in3 */
  3530   3540   case OP_Found: {        /* jump, in3 */
  3531   3541     int alreadyExists;
  3532   3542     VdbeCursor *pC;
  3533   3543     int res;
         3544  +  char *pFree;
  3534   3545     UnpackedRecord *pIdxKey;
  3535   3546     UnpackedRecord r;
  3536   3547     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
  3537   3548   
  3538   3549   #ifdef SQLITE_TEST
  3539   3550     sqlite3_found_count++;
  3540   3551   #endif
................................................................................
  3554   3565         r.aMem = pIn3;
  3555   3566   #ifdef SQLITE_DEBUG
  3556   3567         { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3557   3568   #endif
  3558   3569         r.flags = UNPACKED_PREFIX_MATCH;
  3559   3570         pIdxKey = &r;
  3560   3571       }else{
         3572  +      pIdxKey = sqlite3VdbeAllocUnpackedRecord(
         3573  +          pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
         3574  +      ); 
         3575  +      if( pIdxKey==0 ) goto no_mem;
  3561   3576         assert( pIn3->flags & MEM_Blob );
  3562   3577         assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
  3563         -      pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
  3564         -                                        aTempRec, sizeof(aTempRec));
  3565         -      if( pIdxKey==0 ){
  3566         -        goto no_mem;
  3567         -      }
         3578  +      sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
  3568   3579         pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  3569   3580       }
  3570   3581       rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3571   3582       if( pOp->p4.i==0 ){
  3572         -      sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
         3583  +      sqlite3DbFree(db, pFree);
  3573   3584       }
  3574   3585       if( rc!=SQLITE_OK ){
  3575   3586         break;
  3576   3587       }
  3577   3588       alreadyExists = (res==0);
  3578   3589       pC->deferredMoveto = 0;
  3579   3590       pC->cacheStatus = CACHE_STALE;
................................................................................
  3697   3708     assert( pIn3->flags & MEM_Int );
  3698   3709     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3699   3710     pC = p->apCsr[pOp->p1];
  3700   3711     assert( pC!=0 );
  3701   3712     assert( pC->isTable );
  3702   3713     assert( pC->pseudoTableReg==0 );
  3703   3714     pCrsr = pC->pCursor;
  3704         -  if( pCrsr!=0 ){
         3715  +  if( ALWAYS(pCrsr!=0) ){
  3705   3716       res = 0;
  3706   3717       iKey = pIn3->u.i;
  3707   3718       rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3708   3719       pC->lastRowid = pIn3->u.i;
  3709   3720       pC->rowidIsValid = res==0 ?1:0;
  3710   3721       pC->nullRow = 0;
  3711   3722       pC->cacheStatus = CACHE_STALE;
................................................................................
  4116   4127   ** This is used by trigger programs.
  4117   4128   */
  4118   4129   case OP_ResetCount: {
  4119   4130     sqlite3VdbeSetChanges(db, p->nChange);
  4120   4131     p->nChange = 0;
  4121   4132     break;
  4122   4133   }
         4134  +
         4135  +/* Opcode: SorterCompare P1 P2 P3
         4136  +**
         4137  +** P1 is a sorter cursor. This instruction compares the record blob in 
         4138  +** register P3 with the entry that the sorter cursor currently points to.
         4139  +** If, excluding the rowid fields at the end, the two records are a match,
         4140  +** fall through to the next instruction. Otherwise, jump to instruction P2.
         4141  +*/
         4142  +case OP_SorterCompare: {
         4143  +  VdbeCursor *pC;
         4144  +  int res;
         4145  +
         4146  +  pC = p->apCsr[pOp->p1];
         4147  +  assert( isSorter(pC) );
         4148  +  pIn3 = &aMem[pOp->p3];
         4149  +  rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
         4150  +  if( res ){
         4151  +    pc = pOp->p2-1;
         4152  +  }
         4153  +  break;
         4154  +};
         4155  +
         4156  +/* Opcode: SorterData P1 P2 * * *
         4157  +**
         4158  +** Write into register P2 the current sorter data for sorter cursor P1.
         4159  +*/
         4160  +case OP_SorterData: {
         4161  +  VdbeCursor *pC;
         4162  +#ifndef SQLITE_OMIT_MERGE_SORT
         4163  +  pOut = &aMem[pOp->p2];
         4164  +  pC = p->apCsr[pOp->p1];
         4165  +  assert( pC->isSorter );
         4166  +  rc = sqlite3VdbeSorterRowkey(pC, pOut);
         4167  +#else
         4168  +  pOp->opcode = OP_RowKey;
         4169  +  pc--;
         4170  +#endif
         4171  +  break;
         4172  +}
  4123   4173   
  4124   4174   /* Opcode: RowData P1 P2 * * *
  4125   4175   **
  4126   4176   ** Write into register P2 the complete row data for cursor P1.
  4127   4177   ** There is no interpretation of the data.  
  4128   4178   ** It is just copied onto the P2 register exactly as 
  4129   4179   ** it is found in the database file.
................................................................................
  4150   4200   
  4151   4201     pOut = &aMem[pOp->p2];
  4152   4202     memAboutToChange(p, pOut);
  4153   4203   
  4154   4204     /* Note that RowKey and RowData are really exactly the same instruction */
  4155   4205     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4156   4206     pC = p->apCsr[pOp->p1];
  4157         -  assert( pC->isTable || pOp->opcode==OP_RowKey );
         4207  +  assert( pC->isSorter==0 );
         4208  +  assert( pC->isTable || pOp->opcode!=OP_RowData );
  4158   4209     assert( pC->isIndex || pOp->opcode==OP_RowData );
  4159   4210     assert( pC!=0 );
  4160   4211     assert( pC->nullRow==0 );
  4161   4212     assert( pC->pseudoTableReg==0 );
  4162         -
  4163         -  if( isSorter(pC) ){
  4164         -    assert( pOp->opcode==OP_RowKey );
  4165         -    rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4166         -    break;
  4167         -  }
  4168         -
         4213  +  assert( !pC->isSorter );
  4169   4214     assert( pC->pCursor!=0 );
  4170   4215     pCrsr = pC->pCursor;
  4171   4216     assert( sqlite3BtreeCursorIsValid(pCrsr) );
  4172   4217   
  4173   4218     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  4174   4219     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  4175   4220     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
................................................................................
  4266   4311     VdbeCursor *pC;
  4267   4312   
  4268   4313     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4269   4314     pC = p->apCsr[pOp->p1];
  4270   4315     assert( pC!=0 );
  4271   4316     pC->nullRow = 1;
  4272   4317     pC->rowidIsValid = 0;
         4318  +  assert( pC->pCursor || pC->pVtabCursor );
  4273   4319     if( pC->pCursor ){
  4274   4320       sqlite3BtreeClearCursor(pC->pCursor);
  4275   4321     }
  4276   4322     break;
  4277   4323   }
  4278   4324   
  4279   4325   /* Opcode: Last P1 P2 * * *
................................................................................
  4289   4335     BtCursor *pCrsr;
  4290   4336     int res;
  4291   4337   
  4292   4338     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4293   4339     pC = p->apCsr[pOp->p1];
  4294   4340     assert( pC!=0 );
  4295   4341     pCrsr = pC->pCursor;
  4296         -  if( pCrsr==0 ){
         4342  +  if( NEVER(pCrsr==0) ){
  4297   4343       res = 1;
  4298   4344     }else{
  4299   4345       rc = sqlite3BtreeLast(pCrsr, &res);
  4300   4346     }
  4301   4347     pC->nullRow = (u8)res;
  4302   4348     pC->deferredMoveto = 0;
  4303   4349     pC->rowidIsValid = 0;
................................................................................
  4317   4363   ** Sorting is accomplished by writing records into a sorting index,
  4318   4364   ** then rewinding that index and playing it back from beginning to
  4319   4365   ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
  4320   4366   ** rewinding so that the global variable will be incremented and
  4321   4367   ** regression tests can determine whether or not the optimizer is
  4322   4368   ** correctly optimizing out sorts.
  4323   4369   */
         4370  +case OP_SorterSort:    /* jump */
         4371  +#ifdef SQLITE_OMIT_MERGE_SORT
         4372  +  pOp->opcode = OP_Sort;
         4373  +#endif
  4324   4374   case OP_Sort: {        /* jump */
  4325   4375   #ifdef SQLITE_TEST
  4326   4376     sqlite3_sort_count++;
  4327   4377     sqlite3_search_count--;
  4328   4378   #endif
  4329   4379     p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
  4330   4380     /* Fall through into OP_Rewind */
................................................................................
  4341   4391     VdbeCursor *pC;
  4342   4392     BtCursor *pCrsr;
  4343   4393     int res;
  4344   4394   
  4345   4395     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4346   4396     pC = p->apCsr[pOp->p1];
  4347   4397     assert( pC!=0 );
         4398  +  assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
  4348   4399     res = 1;
  4349   4400     if( isSorter(pC) ){
  4350   4401       rc = sqlite3VdbeSorterRewind(db, pC, &res);
  4351         -  }else if( (pCrsr = pC->pCursor)!=0 ){
         4402  +  }else{
         4403  +    pCrsr = pC->pCursor;
         4404  +    assert( pCrsr );
  4352   4405       rc = sqlite3BtreeFirst(pCrsr, &res);
  4353   4406       pC->atFirst = res==0 ?1:0;
  4354   4407       pC->deferredMoveto = 0;
  4355   4408       pC->cacheStatus = CACHE_STALE;
  4356   4409       pC->rowidIsValid = 0;
  4357   4410     }
  4358   4411     pC->nullRow = (u8)res;
................................................................................
  4359   4412     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4360   4413     if( res ){
  4361   4414       pc = pOp->p2 - 1;
  4362   4415     }
  4363   4416     break;
  4364   4417   }
  4365   4418   
  4366         -/* Opcode: Next P1 P2 * * P5
         4419  +/* Opcode: Next P1 P2 * P4 P5
  4367   4420   **
  4368   4421   ** Advance cursor P1 so that it points to the next key/data pair in its
  4369   4422   ** table or index.  If there are no more key/value pairs then fall through
  4370   4423   ** to the following instruction.  But if the cursor advance was successful,
  4371   4424   ** jump immediately to P2.
  4372   4425   **
  4373   4426   ** The P1 cursor must be for a real table, not a pseudo-table.
         4427  +**
         4428  +** P4 is always of type P4_ADVANCE. The function pointer points to
         4429  +** sqlite3BtreeNext().
  4374   4430   **
  4375   4431   ** If P5 is positive and the jump is taken, then event counter
  4376   4432   ** number P5-1 in the prepared statement is incremented.
  4377   4433   **
  4378   4434   ** See also: Prev
  4379   4435   */
  4380   4436   /* Opcode: Prev P1 P2 * * P5
................................................................................
  4381   4437   **
  4382   4438   ** Back up cursor P1 so that it points to the previous key/data pair in its
  4383   4439   ** table or index.  If there is no previous key/value pairs then fall through
  4384   4440   ** to the following instruction.  But if the cursor backup was successful,
  4385   4441   ** jump immediately to P2.
  4386   4442   **
  4387   4443   ** The P1 cursor must be for a real table, not a pseudo-table.
         4444  +**
         4445  +** P4 is always of type P4_ADVANCE. The function pointer points to
         4446  +** sqlite3BtreePrevious().
  4388   4447   **
  4389   4448   ** If P5 is positive and the jump is taken, then event counter
  4390   4449   ** number P5-1 in the prepared statement is incremented.
  4391   4450   */
         4451  +case OP_SorterNext:    /* jump */
         4452  +#ifdef SQLITE_OMIT_MERGE_SORT
         4453  +  pOp->opcode = OP_Next;
         4454  +#endif
  4392   4455   case OP_Prev:          /* jump */
  4393   4456   case OP_Next: {        /* jump */
  4394   4457     VdbeCursor *pC;
  4395         -  BtCursor *pCrsr;
  4396   4458     int res;
  4397   4459   
  4398   4460     CHECK_FOR_INTERRUPT;
  4399   4461     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4400   4462     assert( pOp->p5<=ArraySize(p->aCounter) );
  4401   4463     pC = p->apCsr[pOp->p1];
  4402   4464     if( pC==0 ){
  4403   4465       break;  /* See ticket #2273 */
  4404   4466     }
         4467  +  assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
  4405   4468     if( isSorter(pC) ){
  4406         -    assert( pOp->opcode==OP_Next );
         4469  +    assert( pOp->opcode==OP_SorterNext );
  4407   4470       rc = sqlite3VdbeSorterNext(db, pC, &res);
  4408   4471     }else{
  4409         -    pCrsr = pC->pCursor;
  4410         -    if( pCrsr==0 ){
  4411         -      pC->nullRow = 1;
  4412         -      break;
  4413         -    }
  4414   4472       res = 1;
  4415   4473       assert( pC->deferredMoveto==0 );
  4416         -    rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
  4417         -                                sqlite3BtreePrevious(pCrsr, &res);
         4474  +    assert( pC->pCursor );
         4475  +    assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
         4476  +    assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
         4477  +    rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4418   4478     }
  4419   4479     pC->nullRow = (u8)res;
  4420   4480     pC->cacheStatus = CACHE_STALE;
  4421   4481     if( res==0 ){
  4422   4482       pc = pOp->p2 - 1;
  4423   4483       if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
  4424   4484   #ifdef SQLITE_TEST
................................................................................
  4437   4497   **
  4438   4498   ** P3 is a flag that provides a hint to the b-tree layer that this
  4439   4499   ** insert is likely to be an append.
  4440   4500   **
  4441   4501   ** This instruction only works for indices.  The equivalent instruction
  4442   4502   ** for tables is OP_Insert.
  4443   4503   */
         4504  +case OP_SorterInsert:       /* in2 */
         4505  +#ifdef SQLITE_OMIT_MERGE_SORT
         4506  +  pOp->opcode = OP_IdxInsert;
         4507  +#endif
  4444   4508   case OP_IdxInsert: {        /* in2 */
  4445   4509     VdbeCursor *pC;
  4446   4510     BtCursor *pCrsr;
  4447   4511     int nKey;
  4448   4512     const char *zKey;
  4449   4513   
  4450   4514     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4451   4515     pC = p->apCsr[pOp->p1];
  4452   4516     assert( pC!=0 );
         4517  +  assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  4453   4518     pIn2 = &aMem[pOp->p2];
  4454   4519     assert( pIn2->flags & MEM_Blob );
  4455   4520     pCrsr = pC->pCursor;
  4456   4521     if( ALWAYS(pCrsr!=0) ){
  4457   4522       assert( pC->isTable==0 );
  4458   4523       rc = ExpandBlob(pIn2);
  4459   4524       if( rc==SQLITE_OK ){
  4460         -      nKey = pIn2->n;
  4461         -      zKey = pIn2->z;
  4462         -      rc = sqlite3VdbeSorterWrite(db, pC, nKey);
  4463         -      if( rc==SQLITE_OK ){
         4525  +      if( isSorter(pC) ){
         4526  +        rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
         4527  +      }else{
         4528  +        nKey = pIn2->n;
         4529  +        zKey = pIn2->z;
  4464   4530           rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
  4465   4531               ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4466         -        );
         4532  +            );
  4467   4533           assert( pC->deferredMoveto==0 );
         4534  +        pC->cacheStatus = CACHE_STALE;
  4468   4535         }
  4469         -      pC->cacheStatus = CACHE_STALE;
  4470   4536       }
  4471   4537     }
  4472   4538     break;
  4473   4539   }
  4474   4540   
  4475   4541   /* Opcode: IdxDelete P1 P2 P3 * *
  4476   4542   **

Changes to src/vdbe.h.

    58     58       CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
    59     59       Mem *pMem;             /* Used when p4type is P4_MEM */
    60     60       VTable *pVtab;         /* Used when p4type is P4_VTAB */
    61     61       KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    62     62       int *ai;               /* Used when p4type is P4_INTARRAY */
    63     63       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
    64     64       Table *pTab;           /* Used when p4type is P4_TABLE */
           65  +    int (*xAdvance)(BtCursor *, int *);
    65     66     } p4;
    66     67   #ifdef SQLITE_DEBUG
    67     68     char *zComment;          /* Comment to improve readability */
    68     69   #endif
    69     70   #ifdef VDBE_PROFILE
    70     71     int cnt;                 /* Number of times this instruction was executed */
    71     72     u64 cycles;              /* Total time spent executing this instruction */
................................................................................
   113    114   #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
   114    115   #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   115    116   #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   116    117   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   117    118   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   118    119   #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   119    120   #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
   120         -#define P4_TABLE    (-19) /* P4 is a pointer to a Table structure */
          121  +#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
          122  +#define P4_TABLE    (-20) /* P4 is a pointer to a Table structure */
   121    123   
   122    124   /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
   123    125   ** is made.  That copy is freed when the Vdbe is finalized.  But if the
   124    126   ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
   125    127   ** gets freed when the Vdbe is finalized so it still should be obtained
   126    128   ** from a single sqliteMalloc().  But no copy is made and the calling
   127    129   ** function should *not* try to free the KeyInfo.
................................................................................
   208    210   VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
   209    211   sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
   210    212   void sqlite3VdbeSetVarmask(Vdbe*, int);
   211    213   #ifndef SQLITE_OMIT_TRACE
   212    214     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   213    215   #endif
   214    216   
   215         -UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
   216         -void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
          217  +void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   217    218   int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
          219  +UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   218    220   
   219    221   #ifndef SQLITE_OMIT_TRIGGER
   220    222   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   221    223   #endif
   222    224   
   223    225   
   224    226   #ifndef NDEBUG

Changes to src/vdbeInt.h.

    55     55     Bool atFirst;         /* True if pointing to first entry */
    56     56     Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
    57     57     Bool nullRow;         /* True if pointing to a row with no data */
    58     58     Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
    59     59     Bool isTable;         /* True if a table requiring integer keys */
    60     60     Bool isIndex;         /* True if an index containing keys only - no data */
    61     61     Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
           62  +  Bool isSorter;        /* True if a new-style sorter */
    62     63     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    63     64     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
    64     65     i64 seqCount;         /* Sequence counter */
    65     66     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    66     67     i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
    67         -  VdbeSorter *pSorter;  /* Sorter object for OP_OpenSorter cursors */
           68  +  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    68     69   
    69     70     /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
    70     71     ** OP_IsUnique opcode on this cursor. */
    71     72     int seekResult;
    72     73   
    73     74     /* Cached information about the header for the data record that the
    74     75     ** cursor is currently pointing to.  Only valid if cacheStatus matches
................................................................................
   399    400   double sqlite3VdbeRealValue(Mem*);
   400    401   void sqlite3VdbeIntegerAffinity(Mem*);
   401    402   int sqlite3VdbeMemRealify(Mem*);
   402    403   int sqlite3VdbeMemNumerify(Mem*);
   403    404   int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   404    405   void sqlite3VdbeMemRelease(Mem *p);
   405    406   void sqlite3VdbeMemReleaseExternal(Mem *p);
          407  +#define MemReleaseExt(X)  \
          408  +  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
          409  +    sqlite3VdbeMemReleaseExternal(X);
   406    410   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   407    411   const char *sqlite3OpcodeName(int);
   408    412   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   409    413   int sqlite3VdbeCloseStatement(Vdbe *, int);
   410    414   void sqlite3VdbeFrameDelete(VdbeFrame*);
   411    415   int sqlite3VdbeFrameRestore(VdbeFrame *);
   412    416   void sqlite3VdbeMemStoreType(Mem *pMem);
................................................................................
   416    420   #ifdef SQLITE_OMIT_MERGE_SORT
   417    421   # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
   418    422   # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
   419    423   # define sqlite3VdbeSorterClose(Y,Z)
   420    424   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
   421    425   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
   422    426   # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
          427  +# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
   423    428   #else
   424    429   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   425         -int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
   426    430   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   427    431   int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
   428         -int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
   429    432   int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
          433  +int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
          434  +int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
          435  +int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
   430    436   #endif
   431    437   
   432    438   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   433    439     void sqlite3VdbeEnter(Vdbe*);
   434    440     void sqlite3VdbeLeave(Vdbe*);
   435    441   #else
   436    442   # define sqlite3VdbeEnter(X)

Changes to src/vdbeapi.c.

  1322   1322     Vdbe *pVdbe = (Vdbe*)pStmt;
  1323   1323     int v = pVdbe->aCounter[op-1];
  1324   1324     if( resetFlag ) pVdbe->aCounter[op-1] = 0;
  1325   1325     return v;
  1326   1326   }
  1327   1327   
  1328   1328   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
         1329  +/*
         1330  +** Allocate and populate an UnpackedRecord structure based on the serialized
         1331  +** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
         1332  +** if successful, or a NULL pointer if an OOM error is encountered.
         1333  +*/
         1334  +static UnpackedRecord *vdbeUnpackRecord(
         1335  +  KeyInfo *pKeyInfo, 
         1336  +  int nKey, 
         1337  +  const void *pKey
         1338  +){
         1339  +  char *dummy;                    /* Dummy argument for AllocUnpackedRecord() */
         1340  +  UnpackedRecord *pRet;           /* Return value */
         1341  +
         1342  +  pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo, 0, 0, &dummy);
         1343  +  if( pRet ){
         1344  +    sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
         1345  +  }
         1346  +  return pRet;
         1347  +}
         1348  +
  1329   1349   /*
  1330   1350   ** This function is called from within a pre-update callback to retrieve
  1331   1351   ** a field of the row currently being updated or deleted.
  1332   1352   */
  1333   1353   int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
  1334   1354     PreUpdate *p = db->pPreUpdate;
  1335   1355     int rc = SQLITE_OK;
................................................................................
  1352   1372   
  1353   1373       rc = sqlite3BtreeDataSize(p->pCsr->pCursor, &nRec);
  1354   1374       if( rc!=SQLITE_OK ) goto preupdate_old_out;
  1355   1375       aRec = sqlite3DbMallocRaw(db, nRec);
  1356   1376       if( !aRec ) goto preupdate_old_out;
  1357   1377       rc = sqlite3BtreeData(p->pCsr->pCursor, 0, nRec, aRec);
  1358   1378       if( rc==SQLITE_OK ){
  1359         -      p->pUnpacked = sqlite3VdbeRecordUnpack(&p->keyinfo, nRec, aRec, 0, 0);
         1379  +      p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
  1360   1380         if( !p->pUnpacked ) rc = SQLITE_NOMEM;
  1361   1381       }
  1362   1382       if( rc!=SQLITE_OK ){
  1363   1383         sqlite3DbFree(db, aRec);
  1364   1384         goto preupdate_old_out;
  1365   1385       }
  1366   1386       p->aRecord = aRec;
................................................................................
  1434   1454       /* For an INSERT, memory cell p->iNewReg contains the serialized record
  1435   1455       ** that is being inserted. Deserialize it. */
  1436   1456       UnpackedRecord *pUnpack = p->pNewUnpacked;
  1437   1457       if( !pUnpack ){
  1438   1458         Mem *pData = &p->v->aMem[p->iNewReg];
  1439   1459         rc = sqlite3VdbeMemExpandBlob(pData);
  1440   1460         if( rc!=SQLITE_OK ) goto preupdate_new_out;
  1441         -      pUnpack = sqlite3VdbeRecordUnpack(&p->keyinfo, pData->n, pData->z, 0, 0);
         1461  +      pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
  1442   1462         if( !pUnpack ){
  1443   1463           rc = SQLITE_NOMEM;
  1444   1464           goto preupdate_new_out;
  1445   1465         }
  1446   1466         p->pNewUnpacked = pUnpack;
  1447   1467       }
  1448   1468       if( iIdx>=pUnpack->nField ){

Changes to src/vdbeaux.c.

   430    430       }else if( opcode==OP_VFilter ){
   431    431         int n;
   432    432         assert( p->nOp - i >= 3 );
   433    433         assert( pOp[-1].opcode==OP_Integer );
   434    434         n = pOp[-1].p1;
   435    435         if( n>nMaxArgs ) nMaxArgs = n;
   436    436   #endif
          437  +    }else if( opcode==OP_Next || opcode==OP_SorterNext ){
          438  +      pOp->p4.xAdvance = sqlite3BtreeNext;
          439  +      pOp->p4type = P4_ADVANCE;
          440  +    }else if( opcode==OP_Prev ){
          441  +      pOp->p4.xAdvance = sqlite3BtreePrevious;
          442  +      pOp->p4type = P4_ADVANCE;
   437    443       }
   438    444   
   439    445       if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
   440    446         assert( -1-pOp->p2<p->nLabel );
   441    447         pOp->p2 = aLabel[-1-pOp->p2];
   442    448       }
   443    449     }
................................................................................
   828    834   ** having to double-check to make sure that the result is non-negative. But
   829    835   ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
   830    836   ** check the value of p->nOp-1 before continuing.
   831    837   */
   832    838   VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
   833    839     /* C89 specifies that the constant "dummy" will be initialized to all
   834    840     ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
   835         -  static const VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
          841  +  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
   836    842     assert( p->magic==VDBE_MAGIC_INIT );
   837    843     if( addr<0 ){
   838    844   #ifdef SQLITE_OMIT_TRACE
   839    845       if( p->nOp==0 ) return (VdbeOp*)&dummy;
   840    846   #endif
   841    847       addr = p->nOp - 1;
   842    848     }
................................................................................
   935    941       case P4_INTARRAY: {
   936    942         sqlite3_snprintf(nTemp, zTemp, "intarray");
   937    943         break;
   938    944       }
   939    945       case P4_SUBPROGRAM: {
   940    946         sqlite3_snprintf(nTemp, zTemp, "program");
   941    947         break;
          948  +    }
          949  +    case P4_ADVANCE: {
          950  +      zTemp[0] = 0;
          951  +      break;
   942    952       }
   943    953       default: {
   944    954         zP4 = pOp->p4.z;
   945    955         if( zP4==0 ){
   946    956           zP4 = zTemp;
   947    957           zTemp[0] = 0;
   948    958         }
................................................................................
  2817   2827         }
  2818   2828         return len;
  2819   2829       }
  2820   2830     }
  2821   2831     return 0;
  2822   2832   }
  2823   2833   
  2824         -
  2825   2834   /*
  2826         -** Given the nKey-byte encoding of a record in pKey[], parse the
  2827         -** record into a UnpackedRecord structure.  Return a pointer to
  2828         -** that structure.
         2835  +** This routine is used to allocate sufficient space for an UnpackedRecord
         2836  +** structure large enough to be used with sqlite3VdbeRecordUnpack() if
         2837  +** the first argument is a pointer to KeyInfo structure pKeyInfo.
  2829   2838   **
  2830         -** The calling function might provide szSpace bytes of memory
  2831         -** space at pSpace.  This space can be used to hold the returned
  2832         -** VDbeParsedRecord structure if it is large enough.  If it is
  2833         -** not big enough, space is obtained from sqlite3_malloc().
         2839  +** The space is either allocated using sqlite3DbMallocRaw() or from within
         2840  +** the unaligned buffer passed via the second and third arguments (presumably
         2841  +** stack space). If the former, then *ppFree is set to a pointer that should
         2842  +** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
         2843  +** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
         2844  +** before returning.
  2834   2845   **
  2835         -** The returned structure should be closed by a call to
  2836         -** sqlite3VdbeDeleteUnpackedRecord().
  2837         -*/ 
  2838         -UnpackedRecord *sqlite3VdbeRecordUnpack(
  2839         -  KeyInfo *pKeyInfo,     /* Information about the record format */
  2840         -  int nKey,              /* Size of the binary record */
  2841         -  const void *pKey,      /* The binary record */
  2842         -  char *pSpace,          /* Unaligned space available to hold the object */
  2843         -  int szSpace            /* Size of pSpace[] in bytes */
         2846  +** If an OOM error occurs, NULL is returned.
         2847  +*/
         2848  +UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
         2849  +  KeyInfo *pKeyInfo,              /* Description of the record */
         2850  +  char *pSpace,                   /* Unaligned space available */
         2851  +  int szSpace,                    /* Size of pSpace[] in bytes */
         2852  +  char **ppFree                   /* OUT: Caller should free this pointer */
  2844   2853   ){
  2845         -  const unsigned char *aKey = (const unsigned char *)pKey;
  2846         -  UnpackedRecord *p;  /* The unpacked record that we will return */
  2847         -  int nByte;          /* Memory space needed to hold p, in bytes */
  2848         -  int d;
  2849         -  u32 idx;
  2850         -  u16 u;              /* Unsigned loop counter */
  2851         -  u32 szHdr;
  2852         -  Mem *pMem;
  2853         -  int nOff;           /* Increase pSpace by this much to 8-byte align it */
  2854         -  
  2855         -  /*
  2856         -  ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
         2854  +  UnpackedRecord *p;              /* Unpacked record to return */
         2855  +  int nOff;                       /* Increment pSpace by nOff to align it */
         2856  +  int nByte;                      /* Number of bytes required for *p */
         2857  +
         2858  +  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
  2857   2859     ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
  2858   2860     ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
  2859   2861     */
  2860   2862     nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
  2861         -  pSpace += nOff;
  2862         -  szSpace -= nOff;
  2863   2863     nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
  2864         -  if( nByte>szSpace ){
  2865         -    p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
  2866         -    if( p==0 ) return 0;
  2867         -    p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
         2864  +  if( nByte>szSpace+nOff ){
         2865  +    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
         2866  +    *ppFree = (char *)p;
         2867  +    if( !p ) return 0;
  2868   2868     }else{
  2869         -    p = (UnpackedRecord*)pSpace;
  2870         -    p->flags = UNPACKED_NEED_DESTROY;
         2869  +    p = (UnpackedRecord*)&pSpace[nOff];
         2870  +    *ppFree = 0;
  2871   2871     }
         2872  +
         2873  +  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
  2872   2874     p->pKeyInfo = pKeyInfo;
  2873   2875     p->nField = pKeyInfo->nField + 1;
  2874         -  p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
         2876  +  return p;
         2877  +}
         2878  +
         2879  +/*
         2880  +** Given the nKey-byte encoding of a record in pKey[], populate the 
         2881  +** UnpackedRecord structure indicated by the fourth argument with the
         2882  +** contents of the decoded record.
         2883  +*/ 
         2884  +void sqlite3VdbeRecordUnpack(
         2885  +  KeyInfo *pKeyInfo,     /* Information about the record format */
         2886  +  int nKey,              /* Size of the binary record */
         2887  +  const void *pKey,      /* The binary record */
         2888  +  UnpackedRecord *p      /* Populate this structure before returning. */
         2889  +){
         2890  +  const unsigned char *aKey = (const unsigned char *)pKey;
         2891  +  int d; 
         2892  +  u32 idx;                        /* Offset in aKey[] to read from */
         2893  +  u16 u;                          /* Unsigned loop counter */
         2894  +  u32 szHdr;
         2895  +  Mem *pMem = p->aMem;
         2896  +
         2897  +  p->flags = 0;
  2875   2898     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  2876   2899     idx = getVarint32(aKey, szHdr);
  2877   2900     d = szHdr;
  2878   2901     u = 0;
  2879   2902     while( idx<szHdr && u<p->nField && d<=nKey ){
  2880   2903       u32 serial_type;
  2881   2904   
................................................................................
  2887   2910       pMem->z = 0;
  2888   2911       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  2889   2912       pMem++;
  2890   2913       u++;
  2891   2914     }
  2892   2915     assert( u<=pKeyInfo->nField + 1 );
  2893   2916     p->nField = u;
  2894         -  return (void*)p;
  2895         -}
  2896         -
  2897         -/*
  2898         -** This routine destroys a UnpackedRecord object.
  2899         -*/
  2900         -void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
  2901         -#ifdef SQLITE_DEBUG
  2902         -  int i;
  2903         -  Mem *pMem;
  2904         -
  2905         -  assert( p!=0 );
  2906         -  assert( p->flags & UNPACKED_NEED_DESTROY );
  2907         -  for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
  2908         -    /* The unpacked record is always constructed by the
  2909         -    ** sqlite3VdbeUnpackRecord() function above, which makes all
  2910         -    ** strings and blobs static.  And none of the elements are
  2911         -    ** ever transformed, so there is never anything to delete.
  2912         -    */
  2913         -    if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
  2914         -  }
  2915         -#endif
  2916         -  if( p->flags & UNPACKED_NEED_FREE ){
  2917         -    sqlite3DbFree(p->pKeyInfo->db, p);
  2918         -  }
  2919   2917   }
  2920   2918   
  2921   2919   /*
  2922   2920   ** This function compares the two table rows or index records
  2923   2921   ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
  2924   2922   ** or positive integer if key1 is less than, equal to or 
  2925   2923   ** greater than key2.  The {nKey1, pKey1} key must be a blob
................................................................................
  3241   3239       v->expmask = 0xffffffff;
  3242   3240     }else{
  3243   3241       v->expmask |= ((u32)1 << (iVar-1));
  3244   3242     }
  3245   3243   }
  3246   3244   
  3247   3245   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
         3246  +
         3247  +/*
         3248  +** If the second argument is not NULL, release any allocations associated 
         3249  +** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
         3250  +** structure itself, using sqlite3DbFree().
         3251  +**
         3252  +** This function is used to free UnpackedRecord structures allocated by
         3253  +** the vdbeUnpackRecord() function found in vdbeapi.c.
         3254  +*/
         3255  +static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
         3256  +  if( p ){
         3257  +    int i;
         3258  +    for(i=0; i<p->nField; i++){
         3259  +      Mem *pMem = &p->aMem[i];
         3260  +      if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
         3261  +    }
         3262  +    sqlite3DbFree(db, p);
         3263  +  }
         3264  +}
         3265  +
  3248   3266   /*
  3249   3267   ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
  3250   3268   ** then cursor passed as the second argument should point to the row about
  3251   3269   ** to be update or deleted. If the application calls sqlite3_preupdate_old(),
  3252   3270   ** the required value will be read from the row the cursor points to.
  3253   3271   */
  3254   3272   void sqlite3VdbePreUpdateHook(
................................................................................
  3288   3306     preupdate.iKey2 = iKey2;
  3289   3307     preupdate.iPKey = pTab->iPKey;
  3290   3308   
  3291   3309     db->pPreUpdate = &preupdate;
  3292   3310     db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
  3293   3311     db->pPreUpdate = 0;
  3294   3312     sqlite3DbFree(db, preupdate.aRecord);
  3295         -  if( preupdate.pUnpacked ){
  3296         -    sqlite3VdbeDeleteUnpackedRecord(preupdate.pUnpacked);
  3297         -  }
  3298         -  if( preupdate.pNewUnpacked ){
  3299         -    sqlite3VdbeDeleteUnpackedRecord(preupdate.pNewUnpacked);
  3300         -  }
         3313  +  vdbeFreeUnpacked(db, preupdate.pUnpacked);
         3314  +  vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
  3301   3315     if( preupdate.aNew ){
  3302   3316       int i;
  3303   3317       for(i=0; i<pCsr->nField; i++){
  3304   3318         sqlite3VdbeMemRelease(&preupdate.aNew[i]);
  3305   3319       }
  3306   3320       sqlite3DbFree(db, preupdate.aNew);
  3307   3321     }
  3308   3322   }
  3309   3323   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */

Changes to src/vdbemem.c.

   267    267   /*
   268    268   ** If the memory cell contains a string value that must be freed by
   269    269   ** invoking an external callback, free it now. Calling this function
   270    270   ** does not free any Mem.zMalloc buffer.
   271    271   */
   272    272   void sqlite3VdbeMemReleaseExternal(Mem *p){
   273    273     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
   274         -  testcase( p->flags & MEM_Agg );
   275         -  testcase( p->flags & MEM_Dyn );
   276         -  testcase( p->flags & MEM_RowSet );
   277         -  testcase( p->flags & MEM_Frame );
   278         -  if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
   279         -    if( p->flags&MEM_Agg ){
   280         -      sqlite3VdbeMemFinalize(p, p->u.pDef);
   281         -      assert( (p->flags & MEM_Agg)==0 );
   282         -      sqlite3VdbeMemRelease(p);
   283         -    }else if( p->flags&MEM_Dyn && p->xDel ){
   284         -      assert( (p->flags&MEM_RowSet)==0 );
   285         -      p->xDel((void *)p->z);
   286         -      p->xDel = 0;
   287         -    }else if( p->flags&MEM_RowSet ){
   288         -      sqlite3RowSetClear(p->u.pRowSet);
   289         -    }else if( p->flags&MEM_Frame ){
   290         -      sqlite3VdbeMemSetNull(p);
   291         -    }
          274  +  if( p->flags&MEM_Agg ){
          275  +    sqlite3VdbeMemFinalize(p, p->u.pDef);
          276  +    assert( (p->flags & MEM_Agg)==0 );
          277  +    sqlite3VdbeMemRelease(p);
          278  +  }else if( p->flags&MEM_Dyn && p->xDel ){
          279  +    assert( (p->flags&MEM_RowSet)==0 );
          280  +    p->xDel((void *)p->z);
          281  +    p->xDel = 0;
          282  +  }else if( p->flags&MEM_RowSet ){
          283  +    sqlite3RowSetClear(p->u.pRowSet);
          284  +  }else if( p->flags&MEM_Frame ){
          285  +    sqlite3VdbeMemSetNull(p);
   292    286     }
   293    287   }
   294    288   
   295    289   /*
   296    290   ** Release any memory held by the Mem. This may leave the Mem in an
   297    291   ** inconsistent state, for example with (Mem.z==0) and
   298    292   ** (Mem.type==SQLITE_TEXT).
   299    293   */
   300    294   void sqlite3VdbeMemRelease(Mem *p){
   301         -  sqlite3VdbeMemReleaseExternal(p);
          295  +  MemReleaseExt(p);
   302    296     sqlite3DbFree(p->db, p->zMalloc);
   303    297     p->z = 0;
   304    298     p->zMalloc = 0;
   305    299     p->xDel = 0;
   306    300   }
   307    301   
   308    302   /*
................................................................................
   616    610   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   617    611   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   618    612   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   619    613   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   620    614   */
   621    615   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   622    616     assert( (pFrom->flags & MEM_RowSet)==0 );
   623         -  sqlite3VdbeMemReleaseExternal(pTo);
          617  +  MemReleaseExt(pTo);
   624    618     memcpy(pTo, pFrom, MEMCELLSIZE);
   625    619     pTo->xDel = 0;
   626    620     if( (pFrom->flags&MEM_Static)==0 ){
   627    621       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   628    622       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   629    623       pTo->flags |= srcType;
   630    624     }
................................................................................
   634    628   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   635    629   ** freed before the copy is made.
   636    630   */
   637    631   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   638    632     int rc = SQLITE_OK;
   639    633   
   640    634     assert( (pFrom->flags & MEM_RowSet)==0 );
   641         -  sqlite3VdbeMemReleaseExternal(pTo);
          635  +  MemReleaseExt(pTo);
   642    636     memcpy(pTo, pFrom, MEMCELLSIZE);
   643    637     pTo->flags &= ~MEM_Dyn;
   644    638   
   645    639     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   646    640       if( 0==(pFrom->flags&MEM_Static) ){
   647    641         pTo->flags |= MEM_Ephem;
   648    642         rc = sqlite3VdbeMemMakeWriteable(pTo);

Changes to src/vdbesort.c.

    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
    21     21   #ifndef SQLITE_OMIT_MERGE_SORT
    22     22   
    23     23   typedef struct VdbeSorterIter VdbeSorterIter;
           24  +typedef struct SorterRecord SorterRecord;
    24     25   
    25     26   /*
    26     27   ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
    27     28   **
    28     29   ** As keys are added to the sorter, they are written to disk in a series
    29     30   ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
    30     31   ** the same as the cache-size allowed for temporary databases. In order
................................................................................
    88     89   **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
    89     90   **
    90     91   ** In other words, each time we advance to the next sorter element, log2(N)
    91     92   ** key comparison operations are required, where N is the number of segments
    92     93   ** being merged (rounded up to the next power of 2).
    93     94   */
    94     95   struct VdbeSorter {
    95         -  int nWorking;                   /* Start a new b-tree after this many pages */
    96         -  int nBtree;                     /* Current size of b-tree contents as PMA */
           96  +  int nInMemory;                  /* Current size of pRecord list as PMA */
    97     97     int nTree;                      /* Used size of aTree/aIter (power of 2) */
    98     98     VdbeSorterIter *aIter;          /* Array of iterators to merge */
    99     99     int *aTree;                     /* Current state of incremental merge */
   100    100     i64 iWriteOff;                  /* Current write offset within file pTemp1 */
   101    101     i64 iReadOff;                   /* Current read offset within file pTemp1 */
   102    102     sqlite3_file *pTemp1;           /* PMA file 1 */
   103    103     int nPMA;                       /* Number of PMAs stored in pTemp1 */
          104  +  SorterRecord *pRecord;          /* Head of in-memory record list */
          105  +  int mnPmaSize;                  /* Minimum PMA size, in bytes */
          106  +  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
          107  +  UnpackedRecord *pUnpacked;      /* Used to unpack keys */
   104    108   };
   105    109   
   106    110   /*
   107    111   ** The following type is an iterator for a PMA. It caches the current key in 
   108    112   ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
   109    113   */
   110    114   struct VdbeSorterIter {
................................................................................
   112    116     i64 iEof;                       /* 1 byte past EOF for this iterator */
   113    117     sqlite3_file *pFile;            /* File iterator is reading from */
   114    118     int nAlloc;                     /* Bytes of space at aAlloc */
   115    119     u8 *aAlloc;                     /* Allocated space */
   116    120     int nKey;                       /* Number of bytes in key */
   117    121     u8 *aKey;                       /* Pointer to current key */
   118    122   };
          123  +
          124  +/*
          125  +** A structure to store a single record. All in-memory records are connected
          126  +** together into a linked list headed at VdbeSorter.pRecord using the 
          127  +** SorterRecord.pNext pointer.
          128  +*/
          129  +struct SorterRecord {
          130  +  void *pVal;
          131  +  int nVal;
          132  +  SorterRecord *pNext;
          133  +};
   119    134   
   120    135   /* Minimum allowable value for the VdbeSorter.nWorking variable */
   121    136   #define SORTER_MIN_WORKING 10
   122    137   
   123    138   /* Maximum number of segments to merge in a single pass. */
   124    139   #define SORTER_MAX_MERGE_COUNT 16
   125    140   
................................................................................
   138    153   */
   139    154   static int vdbeSorterIterNext(
   140    155     sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
   141    156     VdbeSorterIter *pIter           /* Iterator to advance */
   142    157   ){
   143    158     int rc;                         /* Return Code */
   144    159     int nRead;                      /* Number of bytes read */
   145         -  int nRec;                       /* Size of record in bytes */
   146         -  int iOff;                       /* Size of serialized size varint in bytes */
          160  +  int nRec = 0;                   /* Size of record in bytes */
          161  +  int iOff = 0;                   /* Size of serialized size varint in bytes */
   147    162   
   148         -  nRead = pIter->iEof - pIter->iReadOff;
   149         -  if( nRead>5 ) nRead = 5;
          163  +  assert( pIter->iEof>=pIter->iReadOff );
          164  +  if( pIter->iEof-pIter->iReadOff>5 ){
          165  +    nRead = 5;
          166  +  }else{
          167  +    nRead = (int)(pIter->iEof - pIter->iReadOff);
          168  +  }
   150    169     if( nRead<=0 ){
   151    170       /* This is an EOF condition */
   152    171       vdbeSorterIterZero(db, pIter);
   153    172       return SQLITE_OK;
   154    173     }
   155    174   
   156    175     rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
   157         -  iOff = getVarint32(pIter->aAlloc, nRec);
   158         -
   159         -  if( rc==SQLITE_OK && (iOff+nRec)>nRead ){
   160         -    int nRead2;                   /* Number of extra bytes to read */
   161         -    if( (iOff+nRec)>pIter->nAlloc ){
   162         -      int nNew = pIter->nAlloc*2;
   163         -      while( (iOff+nRec)>nNew ) nNew = nNew*2;
   164         -      pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
   165         -      if( !pIter->aAlloc ) return SQLITE_NOMEM;
   166         -      pIter->nAlloc = nNew;
          176  +  if( rc==SQLITE_OK ){
          177  +    iOff = getVarint32(pIter->aAlloc, nRec);
          178  +    if( (iOff+nRec)>nRead ){
          179  +      int nRead2;                   /* Number of extra bytes to read */
          180  +      if( (iOff+nRec)>pIter->nAlloc ){
          181  +        int nNew = pIter->nAlloc*2;
          182  +        while( (iOff+nRec)>nNew ) nNew = nNew*2;
          183  +        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
          184  +        if( !pIter->aAlloc ) return SQLITE_NOMEM;
          185  +        pIter->nAlloc = nNew;
          186  +      }
          187  +  
          188  +      nRead2 = iOff + nRec - nRead;
          189  +      rc = sqlite3OsRead(
          190  +          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
          191  +      );
   167    192       }
   168         -
   169         -    nRead2 = iOff + nRec - nRead;
   170         -    rc = sqlite3OsRead(
   171         -        pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
   172         -    );
   173    193     }
   174    194   
   175         -  assert( nRec>0 || rc!=SQLITE_OK );
          195  +  assert( rc!=SQLITE_OK || nRec>0 );
   176    196     pIter->iReadOff += iOff+nRec;
   177    197     pIter->nKey = nRec;
   178    198     pIter->aKey = &pIter->aAlloc[iOff];
   179    199     return rc;
   180    200   }
   181    201   
   182    202   /*
................................................................................
   212    232   ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
   213    233   ** the first byte past the end of the varint before returning. *piVal is
   214    234   ** set to the integer value read. If an error occurs, the final values of
   215    235   ** both *piOffset and *piVal are undefined.
   216    236   */
   217    237   static int vdbeSorterReadVarint(
   218    238     sqlite3_file *pFile,            /* File to read from */
   219         -  i64 iEof,                       /* Total number of bytes in file */
   220    239     i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
   221    240     i64 *piVal                      /* OUT: Value read from file */
   222    241   ){
   223    242     u8 aVarint[9];                  /* Buffer large enough for a varint */
   224    243     i64 iOff = *piOffset;           /* Offset in file to read from */
   225         -  int nRead = 9;                  /* Number of bytes to read from file */
   226    244     int rc;                         /* Return code */
   227    245   
   228         -  assert( iEof>iOff );
   229         -  if( (iEof-iOff)<nRead ){
   230         -    nRead = iEof-iOff;
   231         -  }
   232         -
   233         -  rc = sqlite3OsRead(pFile, aVarint, nRead, iOff);
          246  +  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
   234    247     if( rc==SQLITE_OK ){
   235    248       *piOffset += getVarint(aVarint, (u64 *)piVal);
   236    249     }
   237    250   
   238    251     return rc;
   239    252   }
   240    253   
................................................................................
   258    271     pIter->pFile = pSorter->pTemp1;
   259    272     pIter->iReadOff = iStart;
   260    273     pIter->nAlloc = 128;
   261    274     pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
   262    275     if( !pIter->aAlloc ){
   263    276       rc = SQLITE_NOMEM;
   264    277     }else{
   265         -    i64 iEof = pSorter->iWriteOff;     /* EOF of file pSorter->pTemp1 */
   266    278       i64 nByte;                         /* Total size of PMA in bytes */
   267         -    rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte);
          279  +    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
   268    280       *pnByte += nByte;
   269    281       pIter->iEof = pIter->iReadOff + nByte;
   270    282     }
   271    283     if( rc==SQLITE_OK ){
   272    284       rc = vdbeSorterIterNext(db, pIter);
   273    285     }
   274    286     return rc;
   275    287   }
   276    288   
          289  +
          290  +/*
          291  +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
          292  +** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
          293  +** used by the comparison. If an error occurs, return an SQLite error code.
          294  +** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
          295  +** value, depending on whether key1 is smaller, equal to or larger than key2.
          296  +**
          297  +** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
          298  +** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
          299  +** is true and key1 contains even a single NULL value, it is considered to
          300  +** be less than key2. Even if key2 also contains NULL values.
          301  +**
          302  +** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
          303  +** has been allocated and contains an unpacked record that is used as key2.
          304  +*/
          305  +static void vdbeSorterCompare(
          306  +  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
          307  +  int bOmitRowid,                 /* Ignore rowid field at end of keys */
          308  +  void *pKey1, int nKey1,         /* Left side of comparison */
          309  +  void *pKey2, int nKey2,         /* Right side of comparison */
          310  +  int *pRes                       /* OUT: Result of comparison */
          311  +){
          312  +  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
          313  +  VdbeSorter *pSorter = pCsr->pSorter;
          314  +  UnpackedRecord *r2 = pSorter->pUnpacked;
          315  +  int i;
          316  +
          317  +  if( pKey2 ){
          318  +    sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
          319  +  }
          320  +
          321  +  if( bOmitRowid ){
          322  +    r2->nField = pKeyInfo->nField;
          323  +    assert( r2->nField>0 );
          324  +    for(i=0; i<r2->nField; i++){
          325  +      if( r2->aMem[i].flags & MEM_Null ){
          326  +        *pRes = -1;
          327  +        return;
          328  +      }
          329  +    }
          330  +    r2->flags |= UNPACKED_PREFIX_MATCH;
          331  +  }
          332  +
          333  +  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
          334  +}
          335  +
   277    336   /*
   278    337   ** This function is called to compare two iterator keys when merging 
   279    338   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
   280    339   ** value to recalculate.
   281    340   */
   282    341   static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
   283    342     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
   301    360     p2 = &pSorter->aIter[i2];
   302    361   
   303    362     if( p1->pFile==0 ){
   304    363       iRes = i2;
   305    364     }else if( p2->pFile==0 ){
   306    365       iRes = i1;
   307    366     }else{
   308         -    char aSpace[150];
   309         -    UnpackedRecord *r1;
   310         -
   311         -    r1 = sqlite3VdbeRecordUnpack(
   312         -        pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace)
          367  +    int res;
          368  +    assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
          369  +    vdbeSorterCompare(
          370  +        pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
   313    371       );
   314         -    if( r1==0 ) return SQLITE_NOMEM;
   315         -
   316         -    if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){
          372  +    if( res<=0 ){
   317    373         iRes = i1;
   318    374       }else{
   319    375         iRes = i2;
   320    376       }
   321         -    sqlite3VdbeDeleteUnpackedRecord(r1);
   322    377     }
   323    378   
   324    379     pSorter->aTree[iOut] = iRes;
   325    380     return SQLITE_OK;
   326    381   }
   327    382   
   328    383   /*
   329    384   ** Initialize the temporary index cursor just opened as a sorter cursor.
   330    385   */
   331    386   int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
   332         -  assert( pCsr->pKeyInfo && pCsr->pBt );
   333         -  pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
   334         -  return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM);
          387  +  int pgsz;                       /* Page size of main database */
          388  +  int mxCache;                    /* Cache size */
          389  +  VdbeSorter *pSorter;            /* The new sorter */
          390  +  char *d;                        /* Dummy */
          391  +
          392  +  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
          393  +  pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
          394  +  if( pSorter==0 ){
          395  +    return SQLITE_NOMEM;
          396  +  }
          397  +  
          398  +  pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
          399  +  if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
          400  +  assert( pSorter->pUnpacked==(UnpackedRecord *)d );
          401  +
          402  +  if( !sqlite3TempInMemory(db) ){
          403  +    pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
          404  +    pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
          405  +    mxCache = db->aDb[0].pSchema->cache_size;
          406  +    if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
          407  +    pSorter->mxPmaSize = mxCache * pgsz;
          408  +  }
          409  +
          410  +  return SQLITE_OK;
          411  +}
          412  +
          413  +/*
          414  +** Free the list of sorted records starting at pRecord.
          415  +*/
          416  +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
          417  +  SorterRecord *p;
          418  +  SorterRecord *pNext;
          419  +  for(p=pRecord; p; p=pNext){
          420  +    pNext = p->pNext;
          421  +    sqlite3DbFree(db, p);
          422  +  }
   335    423   }
   336    424   
   337    425   /*
   338    426   ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
   339    427   */
   340    428   void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
   341    429     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
   346    434           vdbeSorterIterZero(db, &pSorter->aIter[i]);
   347    435         }
   348    436         sqlite3DbFree(db, pSorter->aIter);
   349    437       }
   350    438       if( pSorter->pTemp1 ){
   351    439         sqlite3OsCloseFree(pSorter->pTemp1);
   352    440       }
          441  +    vdbeSorterRecordFree(db, pSorter->pRecord);
          442  +    sqlite3DbFree(db, pSorter->pUnpacked);
   353    443       sqlite3DbFree(db, pSorter);
   354    444       pCsr->pSorter = 0;
   355    445     }
   356    446   }
   357    447   
   358    448   /*
   359    449   ** Allocate space for a file-handle and open a temporary file. If successful,
................................................................................
   365    455     return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
   366    456         SQLITE_OPEN_TEMP_JOURNAL |
   367    457         SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
   368    458         SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
   369    459     );
   370    460   }
   371    461   
          462  +/*
          463  +** Merge the two sorted lists p1 and p2 into a single list.
          464  +** Set *ppOut to the head of the new list.
          465  +*/
          466  +static void vdbeSorterMerge(
          467  +  VdbeCursor *pCsr,               /* For pKeyInfo */
          468  +  SorterRecord *p1,               /* First list to merge */
          469  +  SorterRecord *p2,               /* Second list to merge */
          470  +  SorterRecord **ppOut            /* OUT: Head of merged list */
          471  +){
          472  +  SorterRecord *pFinal = 0;
          473  +  SorterRecord **pp = &pFinal;
          474  +  void *pVal2 = p2 ? p2->pVal : 0;
          475  +
          476  +  while( p1 && p2 ){
          477  +    int res;
          478  +    vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
          479  +    if( res<=0 ){
          480  +      *pp = p1;
          481  +      pp = &p1->pNext;
          482  +      p1 = p1->pNext;
          483  +      pVal2 = 0;
          484  +    }else{
          485  +      *pp = p2;
          486  +       pp = &p2->pNext;
          487  +      p2 = p2->pNext;
          488  +      if( p2==0 ) break;
          489  +      pVal2 = p2->pVal;
          490  +    }
          491  +  }
          492  +  *pp = p1 ? p1 : p2;
          493  +  *ppOut = pFinal;
          494  +}
          495  +
          496  +/*
          497  +** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
          498  +** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
          499  +** occurs.
          500  +*/
          501  +static int vdbeSorterSort(VdbeCursor *pCsr){
          502  +  int i;
          503  +  SorterRecord **aSlot;
          504  +  SorterRecord *p;
          505  +  VdbeSorter *pSorter = pCsr->pSorter;
          506  +
          507  +  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
          508  +  if( !aSlot ){
          509  +    return SQLITE_NOMEM;
          510  +  }
          511  +
          512  +  p = pSorter->pRecord;
          513  +  while( p ){
          514  +    SorterRecord *pNext = p->pNext;
          515  +    p->pNext = 0;
          516  +    for(i=0; aSlot[i]; i++){
          517  +      vdbeSorterMerge(pCsr, p, aSlot[i], &p);
          518  +      aSlot[i] = 0;
          519  +    }
          520  +    aSlot[i] = p;
          521  +    p = pNext;
          522  +  }
          523  +
          524  +  p = 0;
          525  +  for(i=0; i<64; i++){
          526  +    vdbeSorterMerge(pCsr, p, aSlot[i], &p);
          527  +  }
          528  +  pSorter->pRecord = p;
          529  +
          530  +  sqlite3_free(aSlot);
          531  +  return SQLITE_OK;
          532  +}
          533  +
   372    534   
   373    535   /*
   374         -** Write the current contents of the b-tree to a PMA. Return SQLITE_OK
   375         -** if successful, or an SQLite error code otherwise.
          536  +** Write the current contents of the in-memory linked-list to a PMA. Return
          537  +** SQLITE_OK if successful, or an SQLite error code otherwise.
   376    538   **
   377    539   ** The format of a PMA is:
   378    540   **
   379    541   **     * A varint. This varint contains the total number of bytes of content
   380    542   **       in the PMA (not including the varint itself).
   381    543   **
   382    544   **     * One or more records packed end-to-end in order of ascending keys. 
   383    545   **       Each record consists of a varint followed by a blob of data (the 
   384    546   **       key). The varint is the number of bytes in the blob of data.
   385    547   */
   386         -static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){
          548  +static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
   387    549     int rc = SQLITE_OK;             /* Return code */
   388    550     VdbeSorter *pSorter = pCsr->pSorter;
   389         -  int res = 0;
   390    551   
   391         -  /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held
   392         -  ** in memory and so an I/O error is not possible. */
   393         -  rc = sqlite3BtreeFirst(pCsr->pCursor, &res);
   394         -  if( NEVER(rc!=SQLITE_OK) || res ) return rc;
   395         -  assert( pSorter->nBtree>0 );
          552  +  if( pSorter->nInMemory==0 ){
          553  +    assert( pSorter->pRecord==0 );
          554  +    return rc;
          555  +  }
          556  +
          557  +  rc = vdbeSorterSort(pCsr);
   396    558   
   397    559     /* If the first temporary PMA file has not been opened, open it now. */
   398         -  if( pSorter->pTemp1==0 ){
          560  +  if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
   399    561       rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
   400    562       assert( rc!=SQLITE_OK || pSorter->pTemp1 );
   401    563       assert( pSorter->iWriteOff==0 );
   402    564       assert( pSorter->nPMA==0 );
   403    565     }
   404    566   
   405    567     if( rc==SQLITE_OK ){
   406         -    i64 iWriteOff = pSorter->iWriteOff;
   407         -    void *aMalloc = 0;            /* Array used to hold a single record */
   408         -    int nMalloc = 0;              /* Allocated size of aMalloc[] in bytes */
          568  +    i64 iOff = pSorter->iWriteOff;
          569  +    SorterRecord *p;
          570  +    SorterRecord *pNext = 0;
          571  +    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   409    572   
   410    573       pSorter->nPMA++;
   411         -    for(
   412         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff);
   413         -      rc==SQLITE_OK && res==0;
   414         -      rc = sqlite3BtreeNext(pCsr->pCursor, &res)
   415         -    ){
   416         -      i64 nKey;                   /* Size of this key in bytes */
          574  +    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
          575  +    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
          576  +      pNext = p->pNext;
          577  +      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
   417    578   
   418         -      /* Write the size of the record in bytes to the output file */
   419         -      (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey);
   420         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff);
   421         -
   422         -      /* Make sure the aMalloc[] buffer is large enough for the record */
   423         -      if( rc==SQLITE_OK && nKey>nMalloc ){
   424         -        aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey);
   425         -        if( !aMalloc ){ 
   426         -          rc = SQLITE_NOMEM; 
   427         -        }else{
   428         -          nMalloc = nKey;
   429         -        }
          579  +      if( rc==SQLITE_OK ){
          580  +        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
          581  +        iOff += p->nVal;
   430    582         }
   431    583   
   432         -      /* Write the record itself to the output file */
   433         -      if( rc==SQLITE_OK ){
   434         -        /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */
   435         -        rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc);
   436         -        if( ALWAYS(rc==SQLITE_OK) ){
   437         -          rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff);
   438         -          iWriteOff += nKey;
   439         -        }
   440         -      }
   441         -
   442         -      if( rc!=SQLITE_OK ) break;
          584  +      sqlite3DbFree(db, p);
   443    585       }
   444    586   
   445    587       /* This assert verifies that unless an error has occurred, the size of 
   446    588       ** the PMA on disk is the same as the expected size stored in
   447         -    ** pSorter->nBtree. */ 
   448         -    assert( rc!=SQLITE_OK || pSorter->nBtree==(
   449         -          iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree)
          589  +    ** pSorter->nInMemory. */ 
          590  +    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
          591  +          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
   450    592       ));
   451    593   
   452         -    pSorter->iWriteOff = iWriteOff;
   453         -    sqlite3DbFree(db, aMalloc);
          594  +    pSorter->iWriteOff = iOff;
          595  +    if( rc==SQLITE_OK ){
          596  +      /* Terminate each file with 8 extra bytes so that from any offset
          597  +      ** in the file we can always read 9 bytes without a SHORT_READ error */
          598  +      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
          599  +    }
          600  +    pSorter->pRecord = p;
   454    601     }
   455    602   
   456         -  pSorter->nBtree = 0;
   457    603     return rc;
   458    604   }
   459    605   
   460    606   /*
   461         -** This function is called on a sorter cursor by the VDBE before each row 
   462         -** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in
   463         -** bytes, about to be inserted.
   464         -**
   465         -** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr
   466         -** is large enough, its contents are written to a sorted PMA on disk and the
   467         -** tree emptied. This prevents the b-tree (which must be small enough to
   468         -** fit entirely in the cache in order to support efficient inserts) from
   469         -** growing too large.
   470         -**
   471         -** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
          607  +** Add a record to the sorter.
   472    608   */
   473         -int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){
   474         -  int rc = SQLITE_OK;             /* Return code */
          609  +int sqlite3VdbeSorterWrite(
          610  +  sqlite3 *db,                    /* Database handle */
          611  +  VdbeCursor *pCsr,               /* Sorter cursor */
          612  +  Mem *pVal                       /* Memory cell containing record */
          613  +){
   475    614     VdbeSorter *pSorter = pCsr->pSorter;
   476         -  if( pSorter ){
   477         -    Pager *pPager = sqlite3BtreePager(pCsr->pBt);
   478         -    int nPage;                    /* Current size of temporary file in pages */
   479         -
   480         -    /* Sorters never spill to disk */
   481         -    assert( sqlite3PagerFile(pPager)->pMethods==0 );
   482         -
   483         -    /* Determine how many pages the temporary b-tree has grown to */
   484         -    sqlite3PagerPagecount(pPager, &nPage);
   485         -
   486         -    /* If pSorter->nWorking is still zero, but the temporary file has been
   487         -    ** created in the file-system, then the most recent insert into the
   488         -    ** current b-tree segment probably caused the cache to overflow (it is
   489         -    ** also possible that sqlite3_release_memory() was called). So set the
   490         -    ** size of the working set to a little less than the current size of the 
   491         -    ** file in pages.  */
   492         -    if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){
   493         -      pSorter->nWorking = nPage-5;
   494         -      if( pSorter->nWorking<SORTER_MIN_WORKING ){
   495         -        pSorter->nWorking = SORTER_MIN_WORKING;
   496         -      }
   497         -    }
   498         -
   499         -    /* If the number of pages used by the current b-tree segment is greater
   500         -    ** than the size of the working set (VdbeSorter.nWorking), start a new
   501         -    ** segment b-tree.  */
   502         -    if( pSorter->nWorking && nPage>=pSorter->nWorking ){
   503         -      BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */
   504         -      int iRoot;                  /* Root page of new tree */
   505         -
   506         -      /* Copy the current contents of the b-tree into a PMA in sorted order.
   507         -      ** Close the currently open b-tree cursor. */
   508         -      rc = vdbeSorterBtreeToPMA(db, pCsr);
   509         -      sqlite3BtreeCloseCursor(p);
   510         -
   511         -      if( rc==SQLITE_OK ){
   512         -        rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0);
   513         -#ifdef SQLITE_DEBUG
   514         -        sqlite3PagerPagecount(pPager, &nPage);
   515         -        assert( rc!=SQLITE_OK || nPage==1 );
   516         -#endif
   517         -      }
   518         -      if( rc==SQLITE_OK ){
   519         -        rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY);
   520         -      }
   521         -      if( rc==SQLITE_OK ){
   522         -        assert( iRoot==2 );
   523         -        rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p);
   524         -      }
   525         -    }
   526         -
   527         -    pSorter->nBtree += sqlite3VarintLen(nKey) + nKey;
          615  +  int rc = SQLITE_OK;             /* Return Code */
          616  +  SorterRecord *pNew;             /* New list element */
          617  +
          618  +  assert( pSorter );
          619  +  pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
          620  +
          621  +  pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
          622  +  if( pNew==0 ){
          623  +    rc = SQLITE_NOMEM;
          624  +  }else{
          625  +    pNew->pVal = (void *)&pNew[1];
          626  +    memcpy(pNew->pVal, pVal->z, pVal->n);
          627  +    pNew->nVal = pVal->n;
          628  +    pNew->pNext = pSorter->pRecord;
          629  +    pSorter->pRecord = pNew;
          630  +  }
          631  +
          632  +  /* See if the contents of the sorter should now be written out. They
          633  +  ** are written out when either of the following are true:
          634  +  **
          635  +  **   * The total memory allocated for the in-memory list is greater 
          636  +  **     than (page-size * cache-size), or
          637  +  **
          638  +  **   * The total memory allocated for the in-memory list is greater 
          639  +  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
          640  +  */
          641  +  if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
          642  +        (pSorter->nInMemory>pSorter->mxPmaSize)
          643  +     || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
          644  +  )){
          645  +    rc = vdbeSorterListToPMA(db, pCsr);
          646  +    pSorter->nInMemory = 0;
   528    647     }
          648  +
   529    649     return rc;
   530    650   }
   531    651   
   532    652   /*
   533    653   ** Helper function for sqlite3VdbeSorterRewind(). 
   534    654   */
   535    655   static int vdbeSorterInitMerge(
................................................................................
   539    659   ){
   540    660     VdbeSorter *pSorter = pCsr->pSorter;
   541    661     int rc = SQLITE_OK;             /* Return code */
   542    662     int i;                          /* Used to iterator through aIter[] */
   543    663     i64 nByte = 0;                  /* Total bytes in all opened PMAs */
   544    664   
   545    665     /* Initialize the iterators. */
   546         -  for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){
          666  +  for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
   547    667       VdbeSorterIter *pIter = &pSorter->aIter[i];
   548    668       rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
   549    669       pSorter->iReadOff = pIter->iEof;
   550         -    assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK );
   551         -    if( pSorter->iReadOff>=pSorter->iWriteOff ) break;
          670  +    assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
          671  +    if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
   552    672     }
   553    673   
   554    674     /* Initialize the aTree[] array. */
   555    675     for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
   556    676       rc = vdbeSorterDoCompare(pCsr, i);
   557    677     }
   558    678   
................................................................................
   571    691     i64 iWrite2 = 0;                /* Write offset for pTemp2 */
   572    692     int nIter;                      /* Number of iterators used */
   573    693     int nByte;                      /* Bytes of space required for aIter/aTree */
   574    694     int N = 2;                      /* Power of 2 >= nIter */
   575    695   
   576    696     assert( pSorter );
   577    697   
   578         -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
   579         -  rc = vdbeSorterBtreeToPMA(db, pCsr);
   580         -  sqlite3BtreeCloseCursor(pCsr->pCursor);
   581         -  if( rc!=SQLITE_OK ) return rc;
          698  +  /* If no data has been written to disk, then do not do so now. Instead,
          699  +  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
          700  +  ** from the in-memory list.  */
   582    701     if( pSorter->nPMA==0 ){
   583         -    *pbEof = 1;
   584         -    return SQLITE_OK;
          702  +    *pbEof = !pSorter->pRecord;
          703  +    assert( pSorter->aTree==0 );
          704  +    return vdbeSorterSort(pCsr);
   585    705     }
          706  +
          707  +  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
          708  +  rc = vdbeSorterListToPMA(db, pCsr);
          709  +  if( rc!=SQLITE_OK ) return rc;
   586    710   
   587    711     /* Allocate space for aIter[] and aTree[]. */
   588    712     nIter = pSorter->nPMA;
   589    713     if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
   590    714     assert( nIter>0 );
   591    715     while( N<nIter ) N += N;
   592    716     nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
................................................................................
   666    790   }
   667    791   
   668    792   /*
   669    793   ** Advance to the next element in the sorter.
   670    794   */
   671    795   int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
   672    796     VdbeSorter *pSorter = pCsr->pSorter;
   673         -  int iPrev = pSorter->aTree[1];  /* Index of iterator to advance */
   674         -  int i;                          /* Index of aTree[] to recalculate */
   675    797     int rc;                         /* Return code */
   676    798   
   677         -  rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
   678         -  for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
   679         -    rc = vdbeSorterDoCompare(pCsr, i);
   680         -  }
          799  +  if( pSorter->aTree ){
          800  +    int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
          801  +    int i;                        /* Index of aTree[] to recalculate */
          802  +
          803  +    rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
          804  +    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
          805  +      rc = vdbeSorterDoCompare(pCsr, i);
          806  +    }
   681    807   
   682         -  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
          808  +    *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
          809  +  }else{
          810  +    SorterRecord *pFree = pSorter->pRecord;
          811  +    pSorter->pRecord = pFree->pNext;
          812  +    pFree->pNext = 0;
          813  +    vdbeSorterRecordFree(db, pFree);
          814  +    *pbEof = !pSorter->pRecord;
          815  +    rc = SQLITE_OK;
          816  +  }
   683    817     return rc;
   684    818   }
          819  +
          820  +/*
          821  +** Return a pointer to a buffer owned by the sorter that contains the 
          822  +** current key.
          823  +*/
          824  +static void *vdbeSorterRowkey(
          825  +  VdbeSorter *pSorter,            /* Sorter object */
          826  +  int *pnKey                      /* OUT: Size of current key in bytes */
          827  +){
          828  +  void *pKey;
          829  +  if( pSorter->aTree ){
          830  +    VdbeSorterIter *pIter;
          831  +    pIter = &pSorter->aIter[ pSorter->aTree[1] ];
          832  +    *pnKey = pIter->nKey;
          833  +    pKey = pIter->aKey;
          834  +  }else{
          835  +    *pnKey = pSorter->pRecord->nVal;
          836  +    pKey = pSorter->pRecord->pVal;
          837  +  }
          838  +  return pKey;
          839  +}
   685    840   
   686    841   /*
   687    842   ** Copy the current sorter key into the memory cell pOut.
   688    843   */
   689    844   int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
   690    845     VdbeSorter *pSorter = pCsr->pSorter;
   691         -  VdbeSorterIter *pIter;
          846  +  void *pKey; int nKey;           /* Sorter key to copy into pOut */
   692    847   
   693         -  pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   694         -
   695         -  /* Coverage testing note: As things are currently, this call will always
   696         -  ** succeed. This is because the memory cell passed by the VDBE layer 
   697         -  ** happens to be the same one as was used to assemble the keys before they
   698         -  ** were passed to the sorter - meaning it is always large enough for the
   699         -  ** largest key. But this could change very easily, so we leave the call
   700         -  ** to sqlite3VdbeMemGrow() in. */
   701         -  if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){
          848  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
          849  +  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
   702    850       return SQLITE_NOMEM;
   703    851     }
   704         -  pOut->n = pIter->nKey;
          852  +  pOut->n = nKey;
   705    853     MemSetTypeFlag(pOut, MEM_Blob);
   706         -  memcpy(pOut->z, pIter->aKey, pIter->nKey);
          854  +  memcpy(pOut->z, pKey, nKey);
          855  +
          856  +  return SQLITE_OK;
          857  +}
          858  +
          859  +/*
          860  +** Compare the key in memory cell pVal with the key that the sorter cursor
          861  +** passed as the first argument currently points to. For the purposes of
          862  +** the comparison, ignore the rowid field at the end of each record.
          863  +**
          864  +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
          865  +** Otherwise, set *pRes to a negative, zero or positive value if the
          866  +** key in pVal is smaller than, equal to or larger than the current sorter
          867  +** key.
          868  +*/
          869  +int sqlite3VdbeSorterCompare(
          870  +  VdbeCursor *pCsr,               /* Sorter cursor */
          871  +  Mem *pVal,                      /* Value to compare to current sorter key */
          872  +  int *pRes                       /* OUT: Result of comparison */
          873  +){
          874  +  VdbeSorter *pSorter = pCsr->pSorter;
          875  +  void *pKey; int nKey;           /* Sorter key to compare pVal with */
   707    876   
          877  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
          878  +  vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
   708    879     return SQLITE_OK;
   709    880   }
   710    881   
   711    882   #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */

Changes to test/distinct.test.

    41     41     uplevel [list do_test $tn [list is_distinct_noop $sql] 0]
    42     42   }
    43     43   
    44     44   proc do_temptables_test {tn sql temptables} {
    45     45     uplevel [list do_test $tn [subst -novar {
    46     46       set ret ""
    47     47       db eval "EXPLAIN [set sql]" {
    48         -      if {$opcode == "OpenEphemeral"} { 
           48  +      if {$opcode == "OpenEphemeral" || $opcode == "SorterOpen"} { 
    49     49           if {$p5 != "10" && $p5!="00"} { error "p5 = $p5" }
    50     50           if {$p5 == "10"} {
    51     51             lappend ret hash
    52     52           } else {
    53     53             lappend ret btree
    54     54           }
    55     55         }

Changes to test/fts3sort.test.

   153    153       INSERT INTO t2 VALUES('cc aa');
   154    154       SELECT docid FROM t2 WHERE t2 MATCH 'aa';
   155    155     END;
   156    156   } {3 1}
   157    157   do_execsql_test 2.3 {
   158    158     SELECT docid FROM t2 WHERE t2 MATCH 'aa';
   159    159   } {3 1}
          160  +
          161  +#-------------------------------------------------------------------------
          162  +# Test that ticket [56be976859] has been fixed.
          163  +#
          164  +do_execsql_test 3.1 {
          165  +  CREATE VIRTUAL TABLE t3 USING fts4(x, order=DESC);
          166  +  INSERT INTO t3(docid, x) VALUES(113382409004785664, 'aa');
          167  +  INSERT INTO t3(docid, x) VALUES(1, 'ab');
          168  +  SELECT rowid FROM t3 WHERE x MATCH 'a*' ORDER BY docid DESC;
          169  +} {113382409004785664 1}
          170  +do_execsql_test 3.2 {
          171  +  CREATE VIRTUAL TABLE t4 USING fts4(x);
          172  +  INSERT INTO t4(docid, x) VALUES(-113382409004785664, 'aa');
          173  +  INSERT INTO t4(docid, x) VALUES(1, 'ab');
          174  +  SELECT rowid FROM t4 WHERE x MATCH 'a*';
          175  +} {-113382409004785664 1}
          176  +
          177  +
   160    178   
   161    179   finish_test
   162    180   

Changes to test/index4.test.

   104    104       DROP TABLE t1;
   105    105       CREATE TABLE t1(x);
   106    106     COMMIT;
   107    107     CREATE INDEX i1 ON t1(x); 
   108    108     PRAGMA integrity_check
   109    109   } {ok}
   110    110   
          111  +do_execsql_test 2.1 {
          112  +  BEGIN;
          113  +    CREATE TABLE t2(x);
          114  +    INSERT INTO t2 VALUES(14);
          115  +    INSERT INTO t2 VALUES(35);
          116  +    INSERT INTO t2 VALUES(15);
          117  +    INSERT INTO t2 VALUES(35);
          118  +    INSERT INTO t2 VALUES(16);
          119  +  COMMIT;
          120  +}
          121  +do_catchsql_test 2.2 {
          122  +  CREATE UNIQUE INDEX i3 ON t2(x);
          123  +} {1 {indexed columns are not unique}}
          124  +
   111    125   
   112    126   finish_test

Changes to test/misc3.test.

   266    266           a INTEGER DEFAULT 54321,
   267    267           b TEXT DEFAULT "hello",
   268    268           c REAL DEFAULT 3.1415926
   269    269         );
   270    270         CREATE UNIQUE INDEX ex1i1 ON ex1(a);
   271    271         EXPLAIN REINDEX;
   272    272       }]
   273         -    regexp { IsUnique \d+ \d+ \d+ \d+ } $x
          273  +    regexp { SorterCompare \d+ \d+ \d+ } $x
   274    274     } {1}
   275    275     if {[regexp {16} [db one {PRAGMA encoding}]]} {
   276    276       do_test misc3-6.11-utf16 {
   277    277         set x [execsql {
   278    278           EXPLAIN SELECT a+123456789012, b*4.5678, c FROM ex1 ORDER BY +a, b DESC
   279    279         }]
   280    280         set y [regexp { 123456789012 } $x]

Changes to test/pager1.test.

  2433   2433     }
  2434   2434     db close
  2435   2435     sqlite3 db test.db
  2436   2436     execsql {
  2437   2437       BEGIN;
  2438   2438       INSERT INTO t1 VALUES(1, randomblob(10000));
  2439   2439     }
         2440  +  file_control_chunksize_test db main 1024
  2440   2441     file_control_sizehint_test db main 20971520; # 20MB
  2441   2442     execsql {
  2442   2443       PRAGMA cache_size = 10;
  2443   2444       INSERT INTO t1 VALUES(1, randomblob(10000));
  2444   2445       INSERT INTO t1 VALUES(2, randomblob(10000));
  2445   2446       INSERT INTO t1 SELECT x+2, randomblob(10000) from t1;
  2446   2447       INSERT INTO t1 SELECT x+4, randomblob(10000) from t1;

Changes to test/server1.test.

    25     25     finish_test
    26     26     return
    27     27   }
    28     28   
    29     29   # The sample server implementation does not work right when memory
    30     30   # management is enabled.
    31     31   #
    32         -ifcapable memorymanage {
           32  +ifcapable (memorymanage||mutex_noop) {
    33     33     finish_test
    34     34     return
    35     35   }
    36     36   
    37     37   # Create some data to work with
    38     38   #
    39     39   do_test server1-1.1 {

Changes to test/thread001.test.

    38     38     catchsql { DROP TABLE ab; }
    39     39   
    40     40     do_test thread001.$tn.0 {
    41     41       db close
    42     42       sqlite3_enable_shared_cache $shared_cache
    43     43       sqlite3_enable_shared_cache $shared_cache
    44     44     } $shared_cache
    45         -  sqlite3 db test.db -fullmutex 1
           45  +  sqlite3 db test.db -fullmutex 1 -key xyzzy
    46     46   
    47     47     set dbconfig ""
    48     48     if {$same_db} {
    49     49       set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
    50     50     }
    51     51   
    52     52     # Set up a database and a schema. The database contains a single
................................................................................
    73     73       execsql { PRAGMA integrity_check }
    74     74     } {ok}
    75     75   
    76     76     set thread_program {
    77     77       #sqlthread parent {puts STARTING..}
    78     78       set needToClose 0
    79     79       if {![info exists ::DB]} {
    80         -      set ::DB [sqlthread open test.db]
           80  +      set ::DB [sqlthread open test.db xyzzy]
    81     81         #sqlthread parent "puts \"OPEN $::DB\""
    82     82         set needToClose 1
    83     83       }
    84     84     
    85     85       for {set i 0} {$i < 100} {incr i} {
    86     86         # Test that the invariant is true.
    87     87         do_test t1 {

Changes to test/thread002.test.

    12     12   #   This test attempts to deadlock SQLite in shared-cache mode.
    13     13   #     
    14     14   #
    15     15   # $Id: thread002.test,v 1.9 2009/03/26 14:48:07 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   
           19  +set do_not_use_codec 1
    19     20   source $testdir/tester.tcl
    20     21   if {[run_thread_tests]==0} { finish_test ; return }
           22  +
    21     23   
    22     24   db close
    23     25   set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
    24     26   
    25     27   set ::NTHREAD 10
    26     28   
    27     29   do_test thread002.1 {

Changes to test/thread003.test.

    76     76   #
    77     77   set nSecond 30
    78     78   puts "Starting thread003.2 (should run for ~$nSecond seconds)"
    79     79   do_test thread003.2 {
    80     80     foreach zFile {test.db test2.db} {
    81     81       set SCRIPT [format {
    82     82         set iEnd [expr {[clock_seconds] + %d}]
    83         -      set ::DB [sqlthread open %s]
           83  +      set ::DB [sqlthread open %s xyzzy]
    84     84     
    85     85         # Set the cache size to 15 pages per cache. 30 available globally.
    86     86         execsql { PRAGMA cache_size = 15 }
    87     87     
    88     88         while {[clock_seconds] < $iEnd} {
    89     89           set iQuery [expr {int(rand()*5000)}]
    90     90           execsql " SELECT * FROM t1 WHERE a = $iQuery "
................................................................................
   113    113   set nSecond 30
   114    114   puts "Starting thread003.3 (should run for ~$nSecond seconds)"
   115    115   do_test thread003.3 {
   116    116     foreach zFile {test.db test2.db} {
   117    117       set SCRIPT [format {
   118    118         set iStart [clock_seconds]
   119    119         set iEnd [expr {[clock_seconds] + %d}]
   120         -      set ::DB [sqlthread open %s]
          120  +      set ::DB [sqlthread open %s xyzzy]
   121    121     
   122    122         # Set the cache size to 15 pages per cache. 30 available globally.
   123    123         execsql { PRAGMA cache_size = 15 }
   124    124     
   125    125         while {[clock_seconds] < $iEnd} {
   126    126           set iQuery [expr {int(rand()*5000)}]
   127    127           execsql "SELECT * FROM t1 WHERE a = $iQuery"
................................................................................
   152    152   set nSecond 30
   153    153   puts "Starting thread003.4 (should run for ~$nSecond seconds)"
   154    154   unset -nocomplain finished(1)
   155    155   unset -nocomplain finished(2)
   156    156   do_test thread003.4 {
   157    157     thread_spawn finished(1) $thread_procs [format {
   158    158       set iEnd [expr {[clock_seconds] + %d}]
   159         -    set ::DB [sqlthread open test.db]
          159  +    set ::DB [sqlthread open test.db xyzzy]
   160    160   
   161    161       # Set the cache size to 15 pages per cache. 30 available globally.
   162    162       execsql { PRAGMA cache_size = 15 }
   163    163   
   164    164       while {[clock_seconds] < $iEnd} {
   165    165         set iQuery [expr {int(rand()*5000)}]
   166    166         execsql "SELECT * FROM t1 WHERE a = $iQuery"

Changes to test/wal5.test.

   231    231       } {}
   232    232       do_test 2.3.$tn.2 { file_page_counts } {1 5 1 5}
   233    233       do_test 2.3.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2}
   234    234       do_test 2.3.$tn.4 { sql1 { INSERT INTO t1 VALUES(3, 4) } } {}
   235    235       do_test 2.3.$tn.5 { sql1 { INSERT INTO t2 VALUES(3, 4) } } {}
   236    236       do_test 2.3.$tn.6 { file_page_counts } {1 7 1 7}
   237    237       do_test 2.3.$tn.7 { code1 { do_wal_checkpoint db -mode full } } {1 7 5}
   238         -    do_test 2.3.$tn.8 { file_page_counts } {2 7 2 7}
          238  +    do_test 2.3.$tn.8 { file_page_counts } {1 7 2 7}
   239    239     }
   240    240   
   241    241     # Check that checkpoints block on the correct locks. And respond correctly
   242    242     # if they cannot obtain those locks. There are three locks that a checkpoint
   243    243     # may block on (in the following order):
   244    244     #
   245    245     #   1. The writer lock: FULL and RESTART checkpoints block until any writer
................................................................................
   339    339   
   340    340       do_test 3.$tn.6 { code3 { do_wal_checkpoint db3 } } {0 0 0}
   341    341     }
   342    342   }
   343    343   
   344    344   
   345    345   finish_test
          346  +

Changes to tool/lemon.c.

  2518   2518     filesize = ftell(fp);
  2519   2519     rewind(fp);
  2520   2520     filebuf = (char *)malloc( filesize+1 );
  2521   2521     if( filebuf==0 ){
  2522   2522       ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
  2523   2523         filesize+1);
  2524   2524       gp->errorcnt++;
         2525  +    fclose(fp);
  2525   2526       return;
  2526   2527     }
  2527   2528     if( fread(filebuf,1,filesize,fp)!=filesize ){
  2528   2529       ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
  2529   2530         filesize);
  2530   2531       free(filebuf);
  2531   2532       gp->errorcnt++;
         2533  +    fclose(fp);
  2532   2534       return;
  2533   2535     }
  2534   2536     fclose(fp);
  2535   2537     filebuf[filesize] = 0;
  2536   2538   
  2537   2539     /* Make an initial pass through the file to handle %ifdef and %ifndef */
  2538   2540     preprocess_input(filebuf);

Changes to tool/spaceanal.tcl.

    37     37   
    38     38   # In-memory database for collecting statistics. This script loops through
    39     39   # the tables and indices in the database being analyzed, adding a row for each
    40     40   # to an in-memory database (for which the schema is shown below). It then
    41     41   # queries the in-memory db to produce the space-analysis report.
    42     42   #
    43     43   sqlite3 mem :memory:
    44         -set tabledef\
    45         -{CREATE TABLE space_used(
           44  +set tabledef {CREATE TABLE space_used(
    46     45      name clob,        -- Name of a table or index in the database file
    47     46      tblname clob,     -- Name of associated table
    48     47      is_index boolean, -- TRUE if it is an index, false for a table
    49     48      nentry int,       -- Number of entries in the BTree
    50     49      leaf_entries int, -- Number of leaf entries
    51     50      payload int,      -- Total amount of data stored in this table or index
    52     51      ovfl_payload int, -- Total amount of data stored on overflow pages
................................................................................
   289    288     if {$int_pages>0} {
   290    289       statline {Index pages used} $int_pages
   291    290     }
   292    291     statline {Primary pages used} $leaf_pages
   293    292     statline {Overflow pages used} $ovfl_pages
   294    293     statline {Total pages used} $total_pages
   295    294     if {$int_unused>0} {
   296         -    set int_unused_percent \
   297         -         [percent $int_unused [expr {$int_pages*$pageSize}] {of index space}]
          295  +    set int_unused_percent [
          296  +         percent $int_unused [expr {$int_pages*$pageSize}] {of index space}]
   298    297       statline "Unused bytes on index pages" $int_unused $int_unused_percent
   299    298     }
   300         -  statline "Unused bytes on primary pages" $leaf_unused \
   301         -     [percent $leaf_unused [expr {$leaf_pages*$pageSize}] {of primary space}]
   302         -  statline "Unused bytes on overflow pages" $ovfl_unused \
   303         -     [percent $ovfl_unused [expr {$ovfl_pages*$pageSize}] {of overflow space}]
   304         -  statline "Unused bytes on all pages" $total_unused \
   305         -               [percent $total_unused $storage {of all space}]
          299  +  statline "Unused bytes on primary pages" $leaf_unused [
          300  +     percent $leaf_unused [expr {$leaf_pages*$pageSize}] {of primary space}]
          301  +  statline "Unused bytes on overflow pages" $ovfl_unused [
          302  +     percent $ovfl_unused [expr {$ovfl_pages*$pageSize}] {of overflow space}]
          303  +  statline "Unused bytes on all pages" $total_unused [
          304  +               percent $total_unused $storage {of all space}]
   306    305     return 1
   307    306   }
   308    307   
   309    308   # Calculate the overhead in pages caused by auto-vacuum. 
   310    309   #
   311    310   # This procedure calculates and returns the number of pages used by the 
   312    311   # auto-vacuum 'pointer-map'. If the database does not support auto-vacuum,
................................................................................
   448    447   Page size in bytes
   449    448   
   450    449       The number of bytes in a single page of the database file.  
   451    450       Usually 1024.
   452    451   
   453    452   Number of pages in the whole file
   454    453   }
   455         -puts \
   456         -"    The number of $pageSize-byte pages that go into forming the complete
          454  +puts "    The number of $pageSize-byte pages that go into forming the complete
   457    455       database"
   458         -puts \
   459         -{
          456  +puts {
   460    457   Pages that store data
   461    458   
   462    459       The number of pages that store data, either as primary B*Tree pages or
   463    460       as overflow pages.  The number at the right is the data pages divided by
   464    461       the total number of pages in the file.
   465    462   
   466    463   Pages on the freelist

Changes to tool/tostr.awk.

     1      1   #!/usr/bin/awk
     2      2   #
     3      3   # Convert input text into a C string
     4      4   #
     5      5   {
     6         -  gsub(/\\/,"\\\\");
     7      6     gsub(/\"/,"\\\"");
     8      7     print "\"" $0 "\\n\"";
     9      8   }