/ Check-in [6622d876]
Login

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

Overview
Comment:Change the name of vdbeIncrInit2 to vdbePmaReaderIncrInit. Add a header comment to the same function.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | threads
Files: files | file ages | folders
SHA1:6622d87675c1d7992b1e6d52ae7da1007a1568a4
User & Date: dan 2014-04-16 17:41:22
Context
2014-04-16
19:04
Clarify the purpose of the nField argument passed to sqlite3VdbeSorterInit(). check-in: c0c8cff1 user: dan tags: threads
17:41
Change the name of vdbeIncrInit2 to vdbePmaReaderIncrInit. Add a header comment to the same function. check-in: 6622d876 user: dan tags: threads
16:43
Rework the way trees of MergeEngine objects are built in vdbesort.c to make it easier to follow. Fix memory leaks that could follow an OOM or IO error. Add various comments to explain functions in vdbesort.c. check-in: 69026ec7 user: dan tags: threads
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbesort.c.

  1785   1785       pIncr->pTask->file2.iEof -= pIncr->mxSz;
  1786   1786     }
  1787   1787   }
  1788   1788   
  1789   1789   #define INCRINIT2_NORMAL 0
  1790   1790   #define INCRINIT2_TASK   1
  1791   1791   #define INCRINIT2_ROOT   2
  1792         -static int vdbeIncrInit2(PmaReader *pIter, int eMode);
         1792  +static int vdbePmaReaderIncrInit(PmaReader *pIter, int eMode);
  1793   1793   
  1794   1794   /*
  1795   1795   ** Initialize the merger argument passed as the second argument. Once this
  1796   1796   ** function returns, the first key of merged data may be read from the merger
  1797   1797   ** object in the usual fashion.
  1798   1798   **
  1799   1799   ** If argument eMode is INCRINIT2_ROOT, then it is assumed that any IncrMerge
................................................................................
  1800   1800   ** objects attached to the PmaReader objects that the merger reads from have
  1801   1801   ** already been populated, but that they have not yet populated aFile[0] and
  1802   1802   ** set the PmaReader objects up to read from it. In this case all that is
  1803   1803   ** required is to call vdbePmaReaderNext() on each iterator to point it at
  1804   1804   ** its first key.
  1805   1805   **
  1806   1806   ** Otherwise, if eMode is any value other than INCRINIT2_ROOT, then use 
  1807         -** vdbeIncrInit2() to initialize each PmaReader that feeds data to pMerger.
         1807  +** vdbePmaReaderIncrInit() to initialize each PmaReader that feeds data 
         1808  +** to pMerger.
  1808   1809   **
  1809   1810   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  1810   1811   */
  1811   1812   static int vdbeIncrInitMerger(
  1812   1813     SortSubtask *pTask, 
  1813   1814     MergeEngine *pMerger, 
  1814   1815     int eMode                       /* One of the INCRINIT2_XXX constants */
................................................................................
  1816   1817     int rc = SQLITE_OK;             /* Return code */
  1817   1818     int i;                          /* For iterating through PmaReader objects */
  1818   1819   
  1819   1820     for(i=0; rc==SQLITE_OK && i<pMerger->nTree; i++){
  1820   1821       if( eMode==INCRINIT2_ROOT ){
  1821   1822         rc = vdbePmaReaderNext(&pMerger->aIter[i]);
  1822   1823       }else{
  1823         -      rc = vdbeIncrInit2(&pMerger->aIter[i], INCRINIT2_NORMAL);
         1824  +      rc = vdbePmaReaderIncrInit(&pMerger->aIter[i], INCRINIT2_NORMAL);
  1824   1825       }
  1825   1826     }
  1826   1827   
  1827   1828     for(i=pMerger->nTree-1; rc==SQLITE_OK && i>0; i--){
  1828   1829       rc = vdbeSorterDoCompare(pTask, pMerger, i);
  1829   1830     }
  1830   1831   
  1831   1832     return rc;
  1832   1833   }
  1833   1834   
  1834         -static int vdbeIncrInit2(PmaReader *pIter, int eMode){
         1835  +static int vdbePmaReaderIncrInit(PmaReader *pIter, int eMode){
  1835   1836     int rc = SQLITE_OK;
  1836   1837     IncrMerger *pIncr = pIter->pIncr;
  1837   1838     if( pIncr ){
  1838   1839       SortSubtask *pTask = pIncr->pTask;
  1839   1840       sqlite3 *db = pTask->pSorter->db;
  1840   1841   
  1841   1842       rc = vdbeIncrInitMerger(pTask, pIncr->pMerger, eMode);
................................................................................
  1875   1876       }
  1876   1877     }
  1877   1878     return rc;
  1878   1879   }
  1879   1880   
  1880   1881   #if SQLITE_MAX_WORKER_THREADS>0
  1881   1882   /*
  1882         -** The main routine for vdbeIncrInit2() operations run in background threads.
         1883  +** The main routine for vdbePmaReaderIncrInit() operations run in 
         1884  +** background threads.
  1883   1885   */
  1884         -static void *vdbeIncrInit2Thread(void *pCtx){
         1886  +static void *vdbePmaReaderBgInit(void *pCtx){
  1885   1887     PmaReader *pReader = (PmaReader*)pCtx;
  1886         -  void *pRet = SQLITE_INT_TO_PTR( vdbeIncrInit2(pReader, INCRINIT2_TASK) );
         1888  +  void *pRet = SQLITE_INT_TO_PTR(vdbePmaReaderIncrInit(pReader,INCRINIT2_TASK));
  1887   1889     pReader->pIncr->pTask->bDone = 1;
  1888   1890     return pRet;
  1889   1891   }
  1890   1892   
  1891   1893   /*
  1892         -** Use a background thread to invoke vdbeIncrInit2(INCRINIT2_TASK) on the
  1893         -** the PmaReader object passed as the first argument.
         1894  +** Use a background thread to invoke vdbePmaReaderIncrInit(INCRINIT2_TASK) 
         1895  +** on the the PmaReader object passed as the first argument.
  1894   1896   **
  1895   1897   ** This call will initialize the various fields of the pIter->pIncr 
  1896   1898   ** structure and, if it is a multi-threaded IncrMerger, launch a 
  1897   1899   ** background thread to populate aFile[1].
  1898   1900   */
  1899         -static int vdbeIncrBgInit2(PmaReader *pIter){
         1901  +static int vdbePmaReaderBgIncrInit(PmaReader *pIter){
  1900   1902     void *pCtx = (void*)pIter;
  1901         -  return vdbeSorterCreateThread(pIter->pIncr->pTask, vdbeIncrInit2Thread, pCtx);
         1903  +  return vdbeSorterCreateThread(pIter->pIncr->pTask, vdbePmaReaderBgInit, pCtx);
  1902   1904   }
  1903   1905   #endif
  1904   1906   
  1905   1907   /*
  1906   1908   ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
  1907   1909   ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
  1908   1910   ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
................................................................................
  2083   2085       pMain = 0;
  2084   2086     }
  2085   2087     *ppOut = pMain;
  2086   2088     return rc;
  2087   2089   }
  2088   2090   
  2089   2091   /*
  2090         -** Populate iterator *pIter so that it may be used to iterate through all 
  2091         -** keys stored in all PMAs created by this sorter.
         2092  +** This function is called as part of an sqlite3VdbeSorterRewind() operation
         2093  +** on a sorter that has written two or more PMAs to temporary files. It sets
         2094  +** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
         2095  +** (for multi-threaded sorters) so that it can be used to iterate through
         2096  +** all records stored in the sorter.
         2097  +**
         2098  +** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  2092   2099   */
  2093         -static int vdbePmaReaderIncrInit(VdbeSorter *pSorter){
         2100  +static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
  2094   2101     int rc;                         /* Return code */
  2095   2102     SortSubtask *pTask0 = &pSorter->aTask[0];
  2096   2103     MergeEngine *pMain = 0;
  2097   2104     sqlite3 *db = pTask0->pSorter->db;
  2098   2105     int iTask;
  2099   2106   
  2100   2107     rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
................................................................................
  2119   2126                 assert( pIncr->pTask!=pLast );
  2120   2127               }
  2121   2128             }
  2122   2129             if( pSorter->nTask>1 ){
  2123   2130               for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
  2124   2131                 PmaReader *p = &pMain->aIter[iTask];
  2125   2132                 assert( p->pIncr==0 || p->pIncr->pTask==&pSorter->aTask[iTask] );
  2126         -              if( p->pIncr ){ rc = vdbeIncrBgInit2(p); }
         2133  +              if( p->pIncr ){ rc = vdbePmaReaderBgIncrInit(p); }
  2127   2134               }
  2128   2135             }
  2129   2136           }
  2130   2137           pMain = 0;
  2131   2138         }
  2132   2139         if( rc==SQLITE_OK ){
  2133   2140           int eMode = (pSorter->nTask>1 ? INCRINIT2_ROOT : INCRINIT2_NORMAL);
  2134         -        rc = vdbeIncrInit2(pIter, eMode);
         2141  +        rc = vdbePmaReaderIncrInit(pIter, eMode);
  2135   2142         }
  2136   2143       }else
  2137   2144   #endif
  2138   2145       {
  2139   2146         rc = vdbeIncrInitMerger(pTask0, pMain, INCRINIT2_NORMAL);
  2140   2147         pSorter->pMerger = pMain;
  2141   2148         pMain = 0;
................................................................................
  2183   2190   
  2184   2191     vdbeSorterRewindDebug(db, "rewind");
  2185   2192   
  2186   2193     /* Assuming no errors have occurred, set up a merger structure to 
  2187   2194     ** incrementally read and merge all remaining PMAs.  */
  2188   2195     assert( pSorter->pReader==0 );
  2189   2196     if( rc==SQLITE_OK ){
  2190         -    rc = vdbePmaReaderIncrInit(pSorter);
         2197  +    rc = vdbeSorterSetupMerge(pSorter);
  2191   2198       *pbEof = 0;
  2192   2199     }
  2193   2200   
  2194   2201     vdbeSorterRewindDebug(db, "rewinddone");
  2195   2202     return rc;
  2196   2203   }
  2197   2204