/ Check-in [f2407a40]
Login

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

Overview
Comment:Remove an unnecessary parameter from vdbeMergeEngineStep(). Rename a couple other routines to be more descriptive of what they do.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | threads
Files: files | file ages | folders
SHA1: f2407a40f339fa6c2cec194f78ae7c93655b1ec1
User & Date: drh 2014-07-28 20:16:41
Context
2014-07-29
00:23
Fix the build on windows. check-in: 2773a5f9 user: drh tags: threads
2014-07-28
20:16
Remove an unnecessary parameter from vdbeMergeEngineStep(). Rename a couple other routines to be more descriptive of what they do. check-in: f2407a40 user: drh tags: threads
19:58
Rename vdbeIncrMergerInit() to vdbeMergeEngineInit() - a much more accurate name. check-in: 5b084a2d user: drh tags: threads
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbesort.c.

  1403   1403     vdbeSorterWorkDebug(pTask, "exit");
  1404   1404     assert( rc!=SQLITE_OK || pList->pList==0 );
  1405   1405     assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
  1406   1406     return rc;
  1407   1407   }
  1408   1408   
  1409   1409   /*
  1410         -** Advance the MergeEngine pMerge (passed as the second argument) to
  1411         -** its next entry.  Set *pbEof to true there is no next entry because
         1410  +** Advance the MergeEngine to its next entry.
         1411  +** Set *pbEof to true there is no next entry because
  1412   1412   ** the MergeEngine has reached the end of all its inputs.
  1413   1413   **
  1414   1414   ** Return SQLITE_OK if successful or an error code if an error occurs.
  1415   1415   */
  1416   1416   static int vdbeMergeEngineStep(
  1417         -  SortSubtask *pTask,        /* The thread in which this MergeEngine runs */
  1418   1417     MergeEngine *pMerger,      /* The merge engine to advance to the next row */
  1419   1418     int *pbEof                 /* Set TRUE at EOF.  Set false for more content */
  1420   1419   ){
  1421   1420     int rc;
  1422   1421     int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
  1423         -
  1424         -  /* A MergeEngine object is only used by a single thread */
  1425         -  assert( pMerger->pTask==0 || pMerger->pTask==pTask );
  1426         -  pMerger->pTask = pTask;
         1422  +  SortSubtask *pTask = pMerger->pTask;
  1427   1423   
  1428   1424     /* Advance the current PmaReader */
  1429   1425     rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
  1430   1426   
  1431   1427     /* Update contents of aTree[] */
  1432   1428     if( rc==SQLITE_OK ){
  1433   1429       int i;                      /* Index of aTree[] to recalculate */
................................................................................
  1689   1685       ** In either case exit the loop. */
  1690   1686       if( pReader->pFd==0 ) break;
  1691   1687       if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
  1692   1688   
  1693   1689       /* Write the next key to the output. */
  1694   1690       vdbePmaWriteVarint(&writer, nKey);
  1695   1691       vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
  1696         -    rc = vdbeMergeEngineStep(pTask, pIncr->pMerger, &dummy);
         1692  +    assert( pIncr->pMerger->pTask==pTask );
         1693  +    rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
  1697   1694     }
  1698   1695   
  1699   1696     rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
  1700   1697     if( rc==SQLITE_OK ) rc = rc2;
  1701   1698     vdbeSorterPopulateDebug(pTask, "exit");
  1702   1699     return rc;
  1703   1700   }
................................................................................
  1776   1773   
  1777   1774   /*
  1778   1775   ** Allocate and return a new IncrMerger object to read data from pMerger.
  1779   1776   **
  1780   1777   ** If an OOM condition is encountered, return NULL. In this case free the
  1781   1778   ** pMerger argument before returning.
  1782   1779   */
  1783         -static int vdbeIncrNew(
         1780  +static int vdbeIncrMergerNew(
  1784   1781     SortSubtask *pTask,     /* The thread that will be using the new IncrMerger */
  1785   1782     MergeEngine *pMerger,   /* The MergeEngine that the IncrMerger will control */
  1786   1783     IncrMerger **ppOut      /* Write the new IncrMerger here */
  1787   1784   ){
  1788   1785     int rc = SQLITE_OK;
  1789   1786     IncrMerger *pIncr = *ppOut = (IncrMerger*)
  1790   1787          (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
................................................................................
  1800   1797     return rc;
  1801   1798   }
  1802   1799   
  1803   1800   #if SQLITE_MAX_WORKER_THREADS>0
  1804   1801   /*
  1805   1802   ** Set the "use-threads" flag on object pIncr.
  1806   1803   */
  1807         -static void vdbeIncrSetThreads(IncrMerger *pIncr){
         1804  +static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
  1808   1805     pIncr->bUseThread = 1;
  1809   1806     pIncr->pTask->file2.iEof -= pIncr->mxSz;
  1810   1807   }
  1811   1808   #endif /* SQLITE_MAX_WORKER_THREADS>0 */
  1812   1809   
  1813   1810   
  1814   1811   
................................................................................
  1898   1895     int eMode                       /* One of the INCRINIT_XXX constants */
  1899   1896   ){
  1900   1897     int rc = SQLITE_OK;             /* Return code */
  1901   1898     int i;                          /* For looping over PmaReader objects */
  1902   1899     int nTree = pMerger->nTree;
  1903   1900   
  1904   1901     /* Verify that the MergeEngine is assigned to a single thread */
  1905         -  assert( pMerger->pTask==0 || pMerger->pTask==pTask );
         1902  +  assert( pMerger->pTask==0 ); // || pMerger->pTask==pTask );
  1906   1903     pMerger->pTask = pTask;
  1907   1904   
  1908   1905     for(i=0; i<nTree; i++){
  1909   1906       if( eMode==INCRINIT_ROOT ){
  1910   1907         /* PmaReaders should be normally initialized in order, as if they are
  1911   1908         ** reading from the same temp file this makes for more linear file IO.
  1912   1909         ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
................................................................................
  2121   2118   ){
  2122   2119     int rc = SQLITE_OK;
  2123   2120     int nDiv = 1;
  2124   2121     int i;
  2125   2122     MergeEngine *p = pRoot;
  2126   2123     IncrMerger *pIncr;
  2127   2124   
  2128         -  rc = vdbeIncrNew(pTask, pLeaf, &pIncr);
         2125  +  rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
  2129   2126   
  2130   2127     for(i=1; i<nDepth; i++){
  2131   2128       nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
  2132   2129     }
  2133   2130   
  2134   2131     for(i=1; i<nDepth && rc==SQLITE_OK; i++){
  2135   2132       int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
................................................................................
  2136   2133       PmaReader *pReadr = &p->aReadr[iIter];
  2137   2134   
  2138   2135       if( pReadr->pIncr==0 ){
  2139   2136         MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
  2140   2137         if( pNew==0 ){
  2141   2138           rc = SQLITE_NOMEM;
  2142   2139         }else{
  2143         -        rc = vdbeIncrNew(pTask, pNew, &pReadr->pIncr);
         2140  +        rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
  2144   2141         }
  2145   2142       }
  2146   2143       if( rc==SQLITE_OK ){
  2147   2144         p = pReadr->pIncr->pMerger;
  2148   2145         nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
  2149   2146       }
  2150   2147     }
................................................................................
  2212   2209             }
  2213   2210           }
  2214   2211         }
  2215   2212   
  2216   2213         if( rc==SQLITE_OK ){
  2217   2214   #if SQLITE_MAX_WORKER_THREADS>0
  2218   2215           if( pMain!=0 ){
  2219         -          rc = vdbeIncrNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
         2216  +          rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
  2220   2217           }else
  2221   2218   #endif
  2222   2219           {
  2223   2220             assert( pMain==0 );
  2224   2221             pMain = pRoot;
  2225   2222           }
  2226   2223         }else{
................................................................................
  2265   2262         rc = vdbeSortAllocUnpacked(pLast);
  2266   2263         if( rc==SQLITE_OK ){
  2267   2264           pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
  2268   2265           pSorter->pReader = pReadr;
  2269   2266           if( pReadr==0 ) rc = SQLITE_NOMEM;
  2270   2267         }
  2271   2268         if( rc==SQLITE_OK ){
  2272         -        rc = vdbeIncrNew(pLast, pMain, &pReadr->pIncr);
         2269  +        rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
  2273   2270           if( rc==SQLITE_OK ){
  2274         -          vdbeIncrSetThreads(pReadr->pIncr);
         2271  +          vdbeIncrMergerSetThreads(pReadr->pIncr);
  2275   2272             for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
  2276   2273               IncrMerger *pIncr;
  2277   2274               if( (pIncr = pMain->aReadr[iTask].pIncr) ){
  2278         -              vdbeIncrSetThreads(pIncr);
         2275  +              vdbeIncrMergerSetThreads(pIncr);
  2279   2276                 assert( pIncr->pTask!=pLast );
  2280   2277               }
  2281   2278             }
  2282   2279             for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
  2283   2280               PmaReader *p = &pMain->aReadr[iTask];
  2284   2281               assert( p->pIncr==0 || p->pIncr->pTask==&pSorter->aTask[iTask] );
  2285   2282               if( p->pIncr ){ 
................................................................................
  2375   2372   #if SQLITE_MAX_WORKER_THREADS>0
  2376   2373       if( pSorter->bUseThreads ){
  2377   2374         rc = vdbePmaReaderNext(pSorter->pReader);
  2378   2375         *pbEof = (pSorter->pReader->pFd==0);
  2379   2376       }else
  2380   2377   #endif
  2381   2378       /*if( !pSorter->bUseThreads )*/ {
  2382         -      rc = vdbeMergeEngineStep(&pSorter->aTask[0], pSorter->pMerger, pbEof);
         2379  +      assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
         2380  +      rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof);
  2383   2381       }
  2384   2382     }else{
  2385   2383       SorterRecord *pFree = pSorter->list.pList;
  2386   2384       pSorter->list.pList = pFree->u.pNext;
  2387   2385       pFree->u.pNext = 0;
  2388   2386       if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
  2389   2387       *pbEof = !pSorter->list.pList;