4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code for the VdbeSorter object, used in concert with
13 ** a VdbeCursor to sort large numbers of keys (as may be required, for
14 ** example, by CREATE INDEX statements on tables too large to fit in main
18 #include "sqliteInt.h"
22 typedef struct VdbeSorterIter VdbeSorterIter
;
23 typedef struct SorterRecord SorterRecord
;
24 typedef struct FileWriter FileWriter
;
27 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
29 ** As keys are added to the sorter, they are written to disk in a series
30 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
31 ** the same as the cache-size allowed for temporary databases. In order
32 ** to allow the caller to extract keys from the sorter in sorted order,
33 ** all PMAs currently stored on disk must be merged together. This comment
34 ** describes the data structure used to do so. The structure supports
35 ** merging any number of arrays in a single pass with no redundant comparison
38 ** The aIter[] array contains an iterator for each of the PMAs being merged.
39 ** An aIter[] iterator either points to a valid key or else is at EOF. For
40 ** the purposes of the paragraphs below, we assume that the array is actually
41 ** N elements in size, where N is the smallest power of 2 greater to or equal
42 ** to the number of iterators being merged. The extra aIter[] elements are
43 ** treated as if they are empty (always at EOF).
45 ** The aTree[] array is also N elements in size. The value of N is stored in
46 ** the VdbeSorter.nTree variable.
48 ** The final (N/2) elements of aTree[] contain the results of comparing
49 ** pairs of iterator keys together. Element i contains the result of
50 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
51 ** aTree element is set to the index of it.
53 ** For the purposes of this comparison, EOF is considered greater than any
54 ** other key value. If the keys are equal (only possible with two EOF
55 ** values), it doesn't matter which index is stored.
57 ** The (N/4) elements of aTree[] that precede the final (N/2) described
58 ** above contains the index of the smallest of each block of 4 iterators.
59 ** And so on. So that aTree[1] contains the index of the iterator that
60 ** currently points to the smallest key value. aTree[0] is unused.
66 ** aIter[2] -> Elderberry
67 ** aIter[3] -> Currant
68 ** aIter[4] -> Grapefruit
73 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
75 ** The current element is "Apple" (the value of the key indicated by
76 ** iterator 5). When the Next() operation is invoked, iterator 5 will
77 ** be advanced to the next key in its segment. Say the next key is
80 ** aIter[5] -> Eggplant
82 ** The contents of aTree[] are updated first by comparing the new iterator
83 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
84 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
85 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
86 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
87 ** so the value written into element 1 of the array is 0. As follows:
89 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
91 ** In other words, each time we advance to the next sorter element, log2(N)
92 ** key comparison operations are required, where N is the number of segments
93 ** being merged (rounded up to the next power of 2).
96 i64 iWriteOff
; /* Current write offset within file pTemp1 */
97 i64 iReadOff
; /* Current read offset within file pTemp1 */
98 int nInMemory
; /* Current size of pRecord list as PMA */
99 int nTree
; /* Used size of aTree/aIter (power of 2) */
100 int nPMA
; /* Number of PMAs stored in pTemp1 */
101 int mnPmaSize
; /* Minimum PMA size, in bytes */
102 int mxPmaSize
; /* Maximum PMA size, in bytes. 0==no limit */
103 VdbeSorterIter
*aIter
; /* Array of iterators to merge */
104 int *aTree
; /* Current state of incremental merge */
105 sqlite3_file
*pTemp1
; /* PMA file 1 */
106 SorterRecord
*pRecord
; /* Head of in-memory record list */
107 UnpackedRecord
*pUnpacked
; /* Used to unpack keys */
111 ** The following type is an iterator for a PMA. It caches the current key in
112 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
114 struct VdbeSorterIter
{
115 i64 iReadOff
; /* Current read offset */
116 i64 iEof
; /* 1 byte past EOF for this iterator */
117 int nAlloc
; /* Bytes of space at aAlloc */
118 int nKey
; /* Number of bytes in key */
119 sqlite3_file
*pFile
; /* File iterator is reading from */
120 u8
*aAlloc
; /* Allocated space */
121 u8
*aKey
; /* Pointer to current key */
122 u8
*aBuffer
; /* Current read buffer */
123 int nBuffer
; /* Size of read buffer in bytes */
127 ** An instance of this structure is used to organize the stream of records
128 ** being written to files by the merge-sort code into aligned, page-sized
129 ** blocks. Doing all I/O in aligned page-sized blocks helps I/O to go
130 ** faster on many operating systems.
133 int eFWErr
; /* Non-zero if in an error state */
134 u8
*aBuffer
; /* Pointer to write buffer */
135 int nBuffer
; /* Size of write buffer in bytes */
136 int iBufStart
; /* First byte of buffer to write */
137 int iBufEnd
; /* Last byte of buffer to write */
138 i64 iWriteOff
; /* Offset of start of buffer in file */
139 sqlite3_file
*pFile
; /* File to write to */
143 ** A structure to store a single record. All in-memory records are connected
144 ** together into a linked list headed at VdbeSorter.pRecord using the
145 ** SorterRecord.pNext pointer.
147 struct SorterRecord
{
153 /* Minimum allowable value for the VdbeSorter.nWorking variable */
154 #define SORTER_MIN_WORKING 10
156 /* Maximum number of segments to merge in a single pass. */
157 #define SORTER_MAX_MERGE_COUNT 16
160 ** Free all memory belonging to the VdbeSorterIter object passed as the second
161 ** argument. All structure fields are set to zero before returning.
163 static void vdbeSorterIterZero(sqlite3
*db
, VdbeSorterIter
*pIter
){
164 sqlite3DbFree(db
, pIter
->aAlloc
);
165 sqlite3DbFree(db
, pIter
->aBuffer
);
166 memset(pIter
, 0, sizeof(VdbeSorterIter
));
170 ** Read nByte bytes of data from the stream of data iterated by object p.
171 ** If successful, set *ppOut to point to a buffer containing the data
172 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
175 ** The buffer indicated by *ppOut may only be considered valid until the
176 ** next call to this function.
178 static int vdbeSorterIterRead(
179 sqlite3
*db
, /* Database handle (for malloc) */
180 VdbeSorterIter
*p
, /* Iterator */
181 int nByte
, /* Bytes of data to read */
182 u8
**ppOut
/* OUT: Pointer to buffer containing data */
184 int iBuf
; /* Offset within buffer to read from */
185 int nAvail
; /* Bytes of data available in buffer */
186 assert( p
->aBuffer
);
188 /* If there is no more data to be read from the buffer, read the next
189 ** p->nBuffer bytes of data from the file into it. Or, if there are less
190 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
191 iBuf
= p
->iReadOff
% p
->nBuffer
;
193 int nRead
; /* Bytes to read from disk */
194 int rc
; /* sqlite3OsRead() return code */
196 /* Determine how many bytes of data to read. */
197 if( (p
->iEof
- p
->iReadOff
) > (i64
)p
->nBuffer
){
200 nRead
= (int)(p
->iEof
- p
->iReadOff
);
204 /* Read data from the file. Return early if an error occurs. */
205 rc
= sqlite3OsRead(p
->pFile
, p
->aBuffer
, nRead
, p
->iReadOff
);
206 assert( rc
!=SQLITE_IOERR_SHORT_READ
);
207 if( rc
!=SQLITE_OK
) return rc
;
209 nAvail
= p
->nBuffer
- iBuf
;
212 /* The requested data is available in the in-memory buffer. In this
213 ** case there is no need to make a copy of the data, just return a
214 ** pointer into the buffer to the caller. */
215 *ppOut
= &p
->aBuffer
[iBuf
];
216 p
->iReadOff
+= nByte
;
218 /* The requested data is not all available in the in-memory buffer.
219 ** In this case, allocate space at p->aAlloc[] to copy the requested
220 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
221 int nRem
; /* Bytes remaining to copy */
223 /* Extend the p->aAlloc[] allocation if required. */
224 if( p
->nAlloc
<nByte
){
225 int nNew
= p
->nAlloc
*2;
226 while( nByte
>nNew
) nNew
= nNew
*2;
227 p
->aAlloc
= sqlite3DbReallocOrFree(db
, p
->aAlloc
, nNew
);
228 if( !p
->aAlloc
) return SQLITE_NOMEM
;
232 /* Copy as much data as is available in the buffer into the start of
234 memcpy(p
->aAlloc
, &p
->aBuffer
[iBuf
], nAvail
);
235 p
->iReadOff
+= nAvail
;
236 nRem
= nByte
- nAvail
;
238 /* The following loop copies up to p->nBuffer bytes per iteration into
239 ** the p->aAlloc[] buffer. */
241 int rc
; /* vdbeSorterIterRead() return code */
242 int nCopy
; /* Number of bytes to copy */
243 u8
*aNext
; /* Pointer to buffer to copy data from */
246 if( nRem
>p
->nBuffer
) nCopy
= p
->nBuffer
;
247 rc
= vdbeSorterIterRead(db
, p
, nCopy
, &aNext
);
248 if( rc
!=SQLITE_OK
) return rc
;
249 assert( aNext
!=p
->aAlloc
);
250 memcpy(&p
->aAlloc
[nByte
- nRem
], aNext
, nCopy
);
261 ** Read a varint from the stream of data accessed by p. Set *pnOut to
264 static int vdbeSorterIterVarint(sqlite3
*db
, VdbeSorterIter
*p
, u64
*pnOut
){
267 iBuf
= p
->iReadOff
% p
->nBuffer
;
268 if( iBuf
&& (p
->nBuffer
-iBuf
)>=9 ){
269 p
->iReadOff
+= sqlite3GetVarint(&p
->aBuffer
[iBuf
], pnOut
);
274 rc
= vdbeSorterIterRead(db
, p
, 1, &a
);
276 aVarint
[(i
++)&0xf] = a
[0];
277 }while( (a
[0]&0x80)!=0 );
278 sqlite3GetVarint(aVarint
, pnOut
);
286 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
287 ** no error occurs, or an SQLite error code if one does.
289 static int vdbeSorterIterNext(
290 sqlite3
*db
, /* Database handle (for sqlite3DbMalloc() ) */
291 VdbeSorterIter
*pIter
/* Iterator to advance */
293 int rc
; /* Return Code */
294 u64 nRec
= 0; /* Size of record in bytes */
296 if( pIter
->iReadOff
>=pIter
->iEof
){
297 /* This is an EOF condition */
298 vdbeSorterIterZero(db
, pIter
);
302 rc
= vdbeSorterIterVarint(db
, pIter
, &nRec
);
304 pIter
->nKey
= (int)nRec
;
305 rc
= vdbeSorterIterRead(db
, pIter
, (int)nRec
, &pIter
->aKey
);
312 ** Initialize iterator pIter to scan through the PMA stored in file pFile
313 ** starting at offset iStart and ending at offset iEof-1. This function
314 ** leaves the iterator pointing to the first key in the PMA (or EOF if the
317 static int vdbeSorterIterInit(
318 sqlite3
*db
, /* Database handle */
319 const VdbeSorter
*pSorter
, /* Sorter object */
320 i64 iStart
, /* Start offset in pFile */
321 VdbeSorterIter
*pIter
, /* Iterator to populate */
322 i64
*pnByte
/* IN/OUT: Increment this value by PMA size */
327 nBuf
= sqlite3BtreeGetPageSize(db
->aDb
[0].pBt
);
329 assert( pSorter
->iWriteOff
>iStart
);
330 assert( pIter
->aAlloc
==0 );
331 assert( pIter
->aBuffer
==0 );
332 pIter
->pFile
= pSorter
->pTemp1
;
333 pIter
->iReadOff
= iStart
;
335 pIter
->aAlloc
= (u8
*)sqlite3DbMallocRaw(db
, pIter
->nAlloc
);
336 pIter
->nBuffer
= nBuf
;
337 pIter
->aBuffer
= (u8
*)sqlite3DbMallocRaw(db
, nBuf
);
339 if( !pIter
->aBuffer
){
344 iBuf
= iStart
% nBuf
;
346 int nRead
= nBuf
- iBuf
;
347 if( (iStart
+ nRead
) > pSorter
->iWriteOff
){
348 nRead
= (int)(pSorter
->iWriteOff
- iStart
);
351 pSorter
->pTemp1
, &pIter
->aBuffer
[iBuf
], nRead
, iStart
353 assert( rc
!=SQLITE_IOERR_SHORT_READ
);
357 u64 nByte
; /* Size of PMA in bytes */
358 pIter
->iEof
= pSorter
->iWriteOff
;
359 rc
= vdbeSorterIterVarint(db
, pIter
, &nByte
);
360 pIter
->iEof
= pIter
->iReadOff
+ nByte
;
366 rc
= vdbeSorterIterNext(db
, pIter
);
373 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
374 ** size nKey2 bytes). Argument pKeyInfo supplies the collation functions
375 ** used by the comparison. If an error occurs, return an SQLite error code.
376 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
377 ** value, depending on whether key1 is smaller, equal to or larger than key2.
379 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
380 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
381 ** is true and key1 contains even a single NULL value, it is considered to
382 ** be less than key2. Even if key2 also contains NULL values.
384 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
385 ** has been allocated and contains an unpacked record that is used as key2.
387 static void vdbeSorterCompare(
388 const VdbeCursor
*pCsr
, /* Cursor object (for pKeyInfo) */
389 int bOmitRowid
, /* Ignore rowid field at end of keys */
390 const void *pKey1
, int nKey1
, /* Left side of comparison */
391 const void *pKey2
, int nKey2
, /* Right side of comparison */
392 int *pRes
/* OUT: Result of comparison */
394 KeyInfo
*pKeyInfo
= pCsr
->pKeyInfo
;
395 VdbeSorter
*pSorter
= pCsr
->pSorter
;
396 UnpackedRecord
*r2
= pSorter
->pUnpacked
;
400 sqlite3VdbeRecordUnpack(pKeyInfo
, nKey2
, pKey2
, r2
);
404 r2
->nField
= pKeyInfo
->nField
;
405 assert( r2
->nField
>0 );
406 for(i
=0; i
<r2
->nField
; i
++){
407 if( r2
->aMem
[i
].flags
& MEM_Null
){
412 r2
->flags
|= UNPACKED_PREFIX_MATCH
;
415 *pRes
= sqlite3VdbeRecordCompare(nKey1
, pKey1
, r2
);
419 ** This function is called to compare two iterator keys when merging
420 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
421 ** value to recalculate.
423 static int vdbeSorterDoCompare(const VdbeCursor
*pCsr
, int iOut
){
424 VdbeSorter
*pSorter
= pCsr
->pSorter
;
431 assert( iOut
<pSorter
->nTree
&& iOut
>0 );
433 if( iOut
>=(pSorter
->nTree
/2) ){
434 i1
= (iOut
- pSorter
->nTree
/2) * 2;
437 i1
= pSorter
->aTree
[iOut
*2];
438 i2
= pSorter
->aTree
[iOut
*2+1];
441 p1
= &pSorter
->aIter
[i1
];
442 p2
= &pSorter
->aIter
[i2
];
446 }else if( p2
->pFile
==0 ){
450 assert( pCsr
->pSorter
->pUnpacked
!=0 ); /* allocated in vdbeSorterMerge() */
452 pCsr
, 0, p1
->aKey
, p1
->nKey
, p2
->aKey
, p2
->nKey
, &res
461 pSorter
->aTree
[iOut
] = iRes
;
466 ** Initialize the temporary index cursor just opened as a sorter cursor.
468 int sqlite3VdbeSorterInit(sqlite3
*db
, VdbeCursor
*pCsr
){
469 int pgsz
; /* Page size of main database */
470 int mxCache
; /* Cache size */
471 VdbeSorter
*pSorter
; /* The new sorter */
474 assert( pCsr
->pKeyInfo
&& pCsr
->pBt
==0 );
475 pCsr
->pSorter
= pSorter
= sqlite3DbMallocZero(db
, sizeof(VdbeSorter
));
480 pSorter
->pUnpacked
= sqlite3VdbeAllocUnpackedRecord(pCsr
->pKeyInfo
, 0, 0, &d
);
481 if( pSorter
->pUnpacked
==0 ) return SQLITE_NOMEM
;
482 assert( pSorter
->pUnpacked
==(UnpackedRecord
*)d
);
484 if( !sqlite3TempInMemory(db
) ){
485 pgsz
= sqlite3BtreeGetPageSize(db
->aDb
[0].pBt
);
486 pSorter
->mnPmaSize
= SORTER_MIN_WORKING
* pgsz
;
487 mxCache
= db
->aDb
[0].pSchema
->cache_size
;
488 if( mxCache
<SORTER_MIN_WORKING
) mxCache
= SORTER_MIN_WORKING
;
489 pSorter
->mxPmaSize
= mxCache
* pgsz
;
496 ** Free the list of sorted records starting at pRecord.
498 static void vdbeSorterRecordFree(sqlite3
*db
, SorterRecord
*pRecord
){
501 for(p
=pRecord
; p
; p
=pNext
){
503 sqlite3DbFree(db
, p
);
508 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
510 void sqlite3VdbeSorterClose(sqlite3
*db
, VdbeCursor
*pCsr
){
511 VdbeSorter
*pSorter
= pCsr
->pSorter
;
513 if( pSorter
->aIter
){
515 for(i
=0; i
<pSorter
->nTree
; i
++){
516 vdbeSorterIterZero(db
, &pSorter
->aIter
[i
]);
518 sqlite3DbFree(db
, pSorter
->aIter
);
520 if( pSorter
->pTemp1
){
521 sqlite3OsCloseFree(pSorter
->pTemp1
);
523 vdbeSorterRecordFree(db
, pSorter
->pRecord
);
524 sqlite3DbFree(db
, pSorter
->pUnpacked
);
525 sqlite3DbFree(db
, pSorter
);
531 ** Allocate space for a file-handle and open a temporary file. If successful,
532 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
533 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
535 static int vdbeSorterOpenTempFile(sqlite3
*db
, sqlite3_file
**ppFile
){
537 return sqlite3OsOpenMalloc(db
->pVfs
, 0, ppFile
,
538 SQLITE_OPEN_TEMP_JOURNAL
|
539 SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
|
540 SQLITE_OPEN_EXCLUSIVE
| SQLITE_OPEN_DELETEONCLOSE
, &dummy
545 ** Merge the two sorted lists p1 and p2 into a single list.
546 ** Set *ppOut to the head of the new list.
548 static void vdbeSorterMerge(
549 const VdbeCursor
*pCsr
, /* For pKeyInfo */
550 SorterRecord
*p1
, /* First list to merge */
551 SorterRecord
*p2
, /* Second list to merge */
552 SorterRecord
**ppOut
/* OUT: Head of merged list */
554 SorterRecord
*pFinal
= 0;
555 SorterRecord
**pp
= &pFinal
;
556 void *pVal2
= p2
? p2
->pVal
: 0;
560 vdbeSorterCompare(pCsr
, 0, p1
->pVal
, p1
->nVal
, pVal2
, p2
->nVal
, &res
);
579 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
580 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
583 static int vdbeSorterSort(const VdbeCursor
*pCsr
){
585 SorterRecord
**aSlot
;
587 VdbeSorter
*pSorter
= pCsr
->pSorter
;
589 aSlot
= (SorterRecord
**)sqlite3MallocZero(64 * sizeof(SorterRecord
*));
594 p
= pSorter
->pRecord
;
596 SorterRecord
*pNext
= p
->pNext
;
598 for(i
=0; aSlot
[i
]; i
++){
599 vdbeSorterMerge(pCsr
, p
, aSlot
[i
], &p
);
608 vdbeSorterMerge(pCsr
, p
, aSlot
[i
], &p
);
610 pSorter
->pRecord
= p
;
617 ** Initialize a file-writer object.
619 static void fileWriterInit(
620 sqlite3
*db
, /* Database (for malloc) */
621 sqlite3_file
*pFile
, /* File to write to */
622 FileWriter
*p
, /* Object to populate */
623 i64 iStart
/* Offset of pFile to begin writing at */
625 int nBuf
= sqlite3BtreeGetPageSize(db
->aDb
[0].pBt
);
627 memset(p
, 0, sizeof(FileWriter
));
628 p
->aBuffer
= (u8
*)sqlite3DbMallocRaw(db
, nBuf
);
630 p
->eFWErr
= SQLITE_NOMEM
;
632 p
->iBufEnd
= p
->iBufStart
= (iStart
% nBuf
);
633 p
->iWriteOff
= iStart
- p
->iBufStart
;
640 ** Write nData bytes of data to the file-write object. Return SQLITE_OK
641 ** if successful, or an SQLite error code if an error occurs.
643 static void fileWriterWrite(FileWriter
*p
, u8
*pData
, int nData
){
645 while( nRem
>0 && p
->eFWErr
==0 ){
647 if( nCopy
>(p
->nBuffer
- p
->iBufEnd
) ){
648 nCopy
= p
->nBuffer
- p
->iBufEnd
;
651 memcpy(&p
->aBuffer
[p
->iBufEnd
], &pData
[nData
-nRem
], nCopy
);
653 if( p
->iBufEnd
==p
->nBuffer
){
654 p
->eFWErr
= sqlite3OsWrite(p
->pFile
,
655 &p
->aBuffer
[p
->iBufStart
], p
->iBufEnd
- p
->iBufStart
,
656 p
->iWriteOff
+ p
->iBufStart
658 p
->iBufStart
= p
->iBufEnd
= 0;
659 p
->iWriteOff
+= p
->nBuffer
;
661 assert( p
->iBufEnd
<p
->nBuffer
);
668 ** Flush any buffered data to disk and clean up the file-writer object.
669 ** The results of using the file-writer after this call are undefined.
670 ** Return SQLITE_OK if flushing the buffered data succeeds or is not
671 ** required. Otherwise, return an SQLite error code.
673 ** Before returning, set *piEof to the offset immediately following the
674 ** last byte written to the file.
676 static int fileWriterFinish(sqlite3
*db
, FileWriter
*p
, i64
*piEof
){
678 if( p
->eFWErr
==0 && ALWAYS(p
->aBuffer
) && p
->iBufEnd
>p
->iBufStart
){
679 p
->eFWErr
= sqlite3OsWrite(p
->pFile
,
680 &p
->aBuffer
[p
->iBufStart
], p
->iBufEnd
- p
->iBufStart
,
681 p
->iWriteOff
+ p
->iBufStart
684 *piEof
= (p
->iWriteOff
+ p
->iBufEnd
);
685 sqlite3DbFree(db
, p
->aBuffer
);
687 memset(p
, 0, sizeof(FileWriter
));
692 ** Write value iVal encoded as a varint to the file-write object. Return
693 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
695 static void fileWriterWriteVarint(FileWriter
*p
, u64 iVal
){
698 nByte
= sqlite3PutVarint(aByte
, iVal
);
699 fileWriterWrite(p
, aByte
, nByte
);
703 ** Write the current contents of the in-memory linked-list to a PMA. Return
704 ** SQLITE_OK if successful, or an SQLite error code otherwise.
706 ** The format of a PMA is:
708 ** * A varint. This varint contains the total number of bytes of content
709 ** in the PMA (not including the varint itself).
711 ** * One or more records packed end-to-end in order of ascending keys.
712 ** Each record consists of a varint followed by a blob of data (the
713 ** key). The varint is the number of bytes in the blob of data.
715 static int vdbeSorterListToPMA(sqlite3
*db
, const VdbeCursor
*pCsr
){
716 int rc
= SQLITE_OK
; /* Return code */
717 VdbeSorter
*pSorter
= pCsr
->pSorter
;
720 memset(&writer
, 0, sizeof(FileWriter
));
722 if( pSorter
->nInMemory
==0 ){
723 assert( pSorter
->pRecord
==0 );
727 rc
= vdbeSorterSort(pCsr
);
729 /* If the first temporary PMA file has not been opened, open it now. */
730 if( rc
==SQLITE_OK
&& pSorter
->pTemp1
==0 ){
731 rc
= vdbeSorterOpenTempFile(db
, &pSorter
->pTemp1
);
732 assert( rc
!=SQLITE_OK
|| pSorter
->pTemp1
);
733 assert( pSorter
->iWriteOff
==0 );
734 assert( pSorter
->nPMA
==0 );
739 SorterRecord
*pNext
= 0;
741 fileWriterInit(db
, pSorter
->pTemp1
, &writer
, pSorter
->iWriteOff
);
743 fileWriterWriteVarint(&writer
, pSorter
->nInMemory
);
744 for(p
=pSorter
->pRecord
; p
; p
=pNext
){
746 fileWriterWriteVarint(&writer
, p
->nVal
);
747 fileWriterWrite(&writer
, p
->pVal
, p
->nVal
);
748 sqlite3DbFree(db
, p
);
750 pSorter
->pRecord
= p
;
751 rc
= fileWriterFinish(db
, &writer
, &pSorter
->iWriteOff
);
758 ** Add a record to the sorter.
760 int sqlite3VdbeSorterWrite(
761 sqlite3
*db
, /* Database handle */
762 const VdbeCursor
*pCsr
, /* Sorter cursor */
763 Mem
*pVal
/* Memory cell containing record */
765 VdbeSorter
*pSorter
= pCsr
->pSorter
;
766 int rc
= SQLITE_OK
; /* Return Code */
767 SorterRecord
*pNew
; /* New list element */
770 pSorter
->nInMemory
+= sqlite3VarintLen(pVal
->n
) + pVal
->n
;
772 pNew
= (SorterRecord
*)sqlite3DbMallocRaw(db
, pVal
->n
+ sizeof(SorterRecord
));
776 pNew
->pVal
= (void *)&pNew
[1];
777 memcpy(pNew
->pVal
, pVal
->z
, pVal
->n
);
778 pNew
->nVal
= pVal
->n
;
779 pNew
->pNext
= pSorter
->pRecord
;
780 pSorter
->pRecord
= pNew
;
783 /* See if the contents of the sorter should now be written out. They
784 ** are written out when either of the following are true:
786 ** * The total memory allocated for the in-memory list is greater
787 ** than (page-size * cache-size), or
789 ** * The total memory allocated for the in-memory list is greater
790 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
792 if( rc
==SQLITE_OK
&& pSorter
->mxPmaSize
>0 && (
793 (pSorter
->nInMemory
>pSorter
->mxPmaSize
)
794 || (pSorter
->nInMemory
>pSorter
->mnPmaSize
&& sqlite3HeapNearlyFull())
797 i64 nExpect
= pSorter
->iWriteOff
798 + sqlite3VarintLen(pSorter
->nInMemory
)
799 + pSorter
->nInMemory
;
801 rc
= vdbeSorterListToPMA(db
, pCsr
);
802 pSorter
->nInMemory
= 0;
803 assert( rc
!=SQLITE_OK
|| (nExpect
==pSorter
->iWriteOff
) );
810 ** Helper function for sqlite3VdbeSorterRewind().
812 static int vdbeSorterInitMerge(
813 sqlite3
*db
, /* Database handle */
814 const VdbeCursor
*pCsr
, /* Cursor handle for this sorter */
815 i64
*pnByte
/* Sum of bytes in all opened PMAs */
817 VdbeSorter
*pSorter
= pCsr
->pSorter
;
818 int rc
= SQLITE_OK
; /* Return code */
819 int i
; /* Used to iterator through aIter[] */
820 i64 nByte
= 0; /* Total bytes in all opened PMAs */
822 /* Initialize the iterators. */
823 for(i
=0; i
<SORTER_MAX_MERGE_COUNT
; i
++){
824 VdbeSorterIter
*pIter
= &pSorter
->aIter
[i
];
825 rc
= vdbeSorterIterInit(db
, pSorter
, pSorter
->iReadOff
, pIter
, &nByte
);
826 pSorter
->iReadOff
= pIter
->iEof
;
827 assert( rc
!=SQLITE_OK
|| pSorter
->iReadOff
<=pSorter
->iWriteOff
);
828 if( rc
!=SQLITE_OK
|| pSorter
->iReadOff
>=pSorter
->iWriteOff
) break;
831 /* Initialize the aTree[] array. */
832 for(i
=pSorter
->nTree
-1; rc
==SQLITE_OK
&& i
>0; i
--){
833 rc
= vdbeSorterDoCompare(pCsr
, i
);
841 ** Once the sorter has been populated, this function is called to prepare
842 ** for iterating through its contents in sorted order.
844 int sqlite3VdbeSorterRewind(sqlite3
*db
, const VdbeCursor
*pCsr
, int *pbEof
){
845 VdbeSorter
*pSorter
= pCsr
->pSorter
;
846 int rc
; /* Return code */
847 sqlite3_file
*pTemp2
= 0; /* Second temp file to use */
848 i64 iWrite2
= 0; /* Write offset for pTemp2 */
849 int nIter
; /* Number of iterators used */
850 int nByte
; /* Bytes of space required for aIter/aTree */
851 int N
= 2; /* Power of 2 >= nIter */
855 /* If no data has been written to disk, then do not do so now. Instead,
856 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
857 ** from the in-memory list. */
858 if( pSorter
->nPMA
==0 ){
859 *pbEof
= !pSorter
->pRecord
;
860 assert( pSorter
->aTree
==0 );
861 return vdbeSorterSort(pCsr
);
864 /* Write the current in-memory list to a PMA. */
865 rc
= vdbeSorterListToPMA(db
, pCsr
);
866 if( rc
!=SQLITE_OK
) return rc
;
868 /* Allocate space for aIter[] and aTree[]. */
869 nIter
= pSorter
->nPMA
;
870 if( nIter
>SORTER_MAX_MERGE_COUNT
) nIter
= SORTER_MAX_MERGE_COUNT
;
872 while( N
<nIter
) N
+= N
;
873 nByte
= N
* (sizeof(int) + sizeof(VdbeSorterIter
));
874 pSorter
->aIter
= (VdbeSorterIter
*)sqlite3DbMallocZero(db
, nByte
);
875 if( !pSorter
->aIter
) return SQLITE_NOMEM
;
876 pSorter
->aTree
= (int *)&pSorter
->aIter
[N
];
880 int iNew
; /* Index of new, merged, PMA */
883 rc
==SQLITE_OK
&& iNew
*SORTER_MAX_MERGE_COUNT
<pSorter
->nPMA
;
886 int rc2
; /* Return code from fileWriterFinish() */
887 FileWriter writer
; /* Object used to write to disk */
888 i64 nWrite
; /* Number of bytes in new PMA */
890 memset(&writer
, 0, sizeof(FileWriter
));
892 /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
893 ** initialize an iterator for each of them and break out of the loop.
894 ** These iterators will be incrementally merged as the VDBE layer calls
895 ** sqlite3VdbeSorterNext().
897 ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
898 ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
899 ** are merged into a single PMA that is written to file pTemp2.
901 rc
= vdbeSorterInitMerge(db
, pCsr
, &nWrite
);
902 assert( rc
!=SQLITE_OK
|| pSorter
->aIter
[ pSorter
->aTree
[1] ].pFile
);
903 if( rc
!=SQLITE_OK
|| pSorter
->nPMA
<=SORTER_MAX_MERGE_COUNT
){
907 /* Open the second temp file, if it is not already open. */
909 assert( iWrite2
==0 );
910 rc
= vdbeSorterOpenTempFile(db
, &pTemp2
);
915 fileWriterInit(db
, pTemp2
, &writer
, iWrite2
);
916 fileWriterWriteVarint(&writer
, nWrite
);
917 while( rc
==SQLITE_OK
&& bEof
==0 ){
918 VdbeSorterIter
*pIter
= &pSorter
->aIter
[ pSorter
->aTree
[1] ];
919 assert( pIter
->pFile
);
921 fileWriterWriteVarint(&writer
, pIter
->nKey
);
922 fileWriterWrite(&writer
, pIter
->aKey
, pIter
->nKey
);
923 rc
= sqlite3VdbeSorterNext(db
, pCsr
, &bEof
);
925 rc2
= fileWriterFinish(db
, &writer
, &iWrite2
);
926 if( rc
==SQLITE_OK
) rc
= rc2
;
930 if( pSorter
->nPMA
<=SORTER_MAX_MERGE_COUNT
){
933 sqlite3_file
*pTmp
= pSorter
->pTemp1
;
934 pSorter
->nPMA
= iNew
;
935 pSorter
->pTemp1
= pTemp2
;
937 pSorter
->iWriteOff
= iWrite2
;
938 pSorter
->iReadOff
= 0;
941 }while( rc
==SQLITE_OK
);
944 sqlite3OsCloseFree(pTemp2
);
946 *pbEof
= (pSorter
->aIter
[pSorter
->aTree
[1]].pFile
==0);
951 ** Advance to the next element in the sorter.
953 int sqlite3VdbeSorterNext(sqlite3
*db
, const VdbeCursor
*pCsr
, int *pbEof
){
954 VdbeSorter
*pSorter
= pCsr
->pSorter
;
955 int rc
; /* Return code */
957 if( pSorter
->aTree
){
958 int iPrev
= pSorter
->aTree
[1];/* Index of iterator to advance */
959 int i
; /* Index of aTree[] to recalculate */
961 rc
= vdbeSorterIterNext(db
, &pSorter
->aIter
[iPrev
]);
962 for(i
=(pSorter
->nTree
+iPrev
)/2; rc
==SQLITE_OK
&& i
>0; i
=i
/2){
963 rc
= vdbeSorterDoCompare(pCsr
, i
);
966 *pbEof
= (pSorter
->aIter
[pSorter
->aTree
[1]].pFile
==0);
968 SorterRecord
*pFree
= pSorter
->pRecord
;
969 pSorter
->pRecord
= pFree
->pNext
;
971 vdbeSorterRecordFree(db
, pFree
);
972 *pbEof
= !pSorter
->pRecord
;
979 ** Return a pointer to a buffer owned by the sorter that contains the
982 static void *vdbeSorterRowkey(
983 const VdbeSorter
*pSorter
, /* Sorter object */
984 int *pnKey
/* OUT: Size of current key in bytes */
987 if( pSorter
->aTree
){
988 VdbeSorterIter
*pIter
;
989 pIter
= &pSorter
->aIter
[ pSorter
->aTree
[1] ];
990 *pnKey
= pIter
->nKey
;
993 *pnKey
= pSorter
->pRecord
->nVal
;
994 pKey
= pSorter
->pRecord
->pVal
;
1000 ** Copy the current sorter key into the memory cell pOut.
1002 int sqlite3VdbeSorterRowkey(const VdbeCursor
*pCsr
, Mem
*pOut
){
1003 VdbeSorter
*pSorter
= pCsr
->pSorter
;
1004 void *pKey
; int nKey
; /* Sorter key to copy into pOut */
1006 pKey
= vdbeSorterRowkey(pSorter
, &nKey
);
1007 if( sqlite3VdbeMemGrow(pOut
, nKey
, 0) ){
1008 return SQLITE_NOMEM
;
1011 MemSetTypeFlag(pOut
, MEM_Blob
);
1012 memcpy(pOut
->z
, pKey
, nKey
);
1018 ** Compare the key in memory cell pVal with the key that the sorter cursor
1019 ** passed as the first argument currently points to. For the purposes of
1020 ** the comparison, ignore the rowid field at the end of each record.
1022 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
1023 ** Otherwise, set *pRes to a negative, zero or positive value if the
1024 ** key in pVal is smaller than, equal to or larger than the current sorter
1027 int sqlite3VdbeSorterCompare(
1028 const VdbeCursor
*pCsr
, /* Sorter cursor */
1029 Mem
*pVal
, /* Value to compare to current sorter key */
1030 int *pRes
/* OUT: Result of comparison */
1032 VdbeSorter
*pSorter
= pCsr
->pSorter
;
1033 void *pKey
; int nKey
; /* Sorter key to compare pVal with */
1035 pKey
= vdbeSorterRowkey(pSorter
, &nKey
);
1036 vdbeSorterCompare(pCsr
, 1, pVal
->z
, pVal
->n
, pKey
, nKey
, pRes
);