xref: /sqlite-3.40.0/src/rowset.c (revision 45dc9ca4)
13d4501e5Sdrh /*
23d4501e5Sdrh ** 2008 December 3
33d4501e5Sdrh **
43d4501e5Sdrh ** The author disclaims copyright to this source code.  In place of
53d4501e5Sdrh ** a legal notice, here is a blessing:
63d4501e5Sdrh **
73d4501e5Sdrh **    May you do good and not evil.
83d4501e5Sdrh **    May you find forgiveness for yourself and forgive others.
93d4501e5Sdrh **    May you share freely, never taking more than you give.
103d4501e5Sdrh **
113d4501e5Sdrh *************************************************************************
123d4501e5Sdrh **
133d4501e5Sdrh ** This module implements an object we call a "RowSet".
143d4501e5Sdrh **
15733bf1b1Sdrh ** The RowSet object is a collection of rowids.  Rowids
16733bf1b1Sdrh ** are inserted into the RowSet in an arbitrary order.  Inserts
17733bf1b1Sdrh ** can be intermixed with tests to see if a given rowid has been
18733bf1b1Sdrh ** previously inserted into the RowSet.
193d4501e5Sdrh **
20733bf1b1Sdrh ** After all inserts are finished, it is possible to extract the
21733bf1b1Sdrh ** elements of the RowSet in sorted order.  Once this extraction
22733bf1b1Sdrh ** process has started, no new elements may be inserted.
233d4501e5Sdrh **
24733bf1b1Sdrh ** Hence, the primitive operations for a RowSet are:
25a9e364f0Sdrh **
26733bf1b1Sdrh **    CREATE
27733bf1b1Sdrh **    INSERT
28733bf1b1Sdrh **    TEST
29733bf1b1Sdrh **    SMALLEST
30733bf1b1Sdrh **    DESTROY
31733bf1b1Sdrh **
32733bf1b1Sdrh ** The CREATE and DESTROY primitives are the constructor and destructor,
33733bf1b1Sdrh ** obviously.  The INSERT primitive adds a new element to the RowSet.
34733bf1b1Sdrh ** TEST checks to see if an element is already in the RowSet.  SMALLEST
35733bf1b1Sdrh ** extracts the least value from the RowSet.
36733bf1b1Sdrh **
37733bf1b1Sdrh ** The INSERT primitive might allocate additional memory.  Memory is
38733bf1b1Sdrh ** allocated in chunks so most INSERTs do no allocation.  There is an
39733bf1b1Sdrh ** upper bound on the size of allocated memory.  No memory is freed
40733bf1b1Sdrh ** until DESTROY.
41733bf1b1Sdrh **
42733bf1b1Sdrh ** The TEST primitive includes a "batch" number.  The TEST primitive
43733bf1b1Sdrh ** will only see elements that were inserted before the last change
44733bf1b1Sdrh ** in the batch number.  In other words, if an INSERT occurs between
45733bf1b1Sdrh ** two TESTs where the TESTs have the same batch nubmer, then the
46733bf1b1Sdrh ** value added by the INSERT will not be visible to the second TEST.
47733bf1b1Sdrh ** The initial batch number is zero, so if the very first TEST contains
48733bf1b1Sdrh ** a non-zero batch number, it will see all prior INSERTs.
49733bf1b1Sdrh **
50733bf1b1Sdrh ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
51733bf1b1Sdrh ** that is attempted.
52733bf1b1Sdrh **
5360ec914cSpeter.d.reid ** The cost of an INSERT is roughly constant.  (Sometimes new memory
54733bf1b1Sdrh ** has to be allocated on an INSERT.)  The cost of a TEST with a new
55733bf1b1Sdrh ** batch number is O(NlogN) where N is the number of elements in the RowSet.
56733bf1b1Sdrh ** The cost of a TEST using the same batch number is O(logN).  The cost
57733bf1b1Sdrh ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
58733bf1b1Sdrh ** primitives are constant time.  The cost of DESTROY is O(N).
59733bf1b1Sdrh **
60aa50271aSdrh ** TEST and SMALLEST may not be used by the same RowSet.  This used to
61aa50271aSdrh ** be possible, but the feature was not used, so it was removed in order
62aa50271aSdrh ** to simplify the code.
633d4501e5Sdrh */
643d4501e5Sdrh #include "sqliteInt.h"
653d4501e5Sdrh 
66733bf1b1Sdrh 
67733bf1b1Sdrh /*
68733bf1b1Sdrh ** Target size for allocation chunks.
69733bf1b1Sdrh */
70733bf1b1Sdrh #define ROWSET_ALLOCATION_SIZE 1024
71733bf1b1Sdrh 
723d4501e5Sdrh /*
733d4501e5Sdrh ** The number of rowset entries per allocation chunk.
743d4501e5Sdrh */
75733bf1b1Sdrh #define ROWSET_ENTRY_PER_CHUNK  \
76733bf1b1Sdrh                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
773d4501e5Sdrh 
783d4501e5Sdrh /*
79733bf1b1Sdrh ** Each entry in a RowSet is an instance of the following object.
803343b439Sdrh **
813343b439Sdrh ** This same object is reused to store a linked list of trees of RowSetEntry
823343b439Sdrh ** objects.  In that alternative use, pRight points to the next entry
833343b439Sdrh ** in the list, pLeft points to the tree, and v is unused.  The
843343b439Sdrh ** RowSet.pForest value points to the head of this forest list.
853d4501e5Sdrh */
863d4501e5Sdrh struct RowSetEntry {
873d4501e5Sdrh   i64 v;                        /* ROWID value for this entry */
88733bf1b1Sdrh   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
89733bf1b1Sdrh   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
903d4501e5Sdrh };
913d4501e5Sdrh 
923d4501e5Sdrh /*
93733bf1b1Sdrh ** RowSetEntry objects are allocated in large chunks (instances of the
943d4501e5Sdrh ** following structure) to reduce memory allocation overhead.  The
953d4501e5Sdrh ** chunks are kept on a linked list so that they can be deallocated
963d4501e5Sdrh ** when the RowSet is destroyed.
973d4501e5Sdrh */
983d4501e5Sdrh struct RowSetChunk {
99733bf1b1Sdrh   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
1003d4501e5Sdrh   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
1013d4501e5Sdrh };
1023d4501e5Sdrh 
1033d4501e5Sdrh /*
1043d4501e5Sdrh ** A RowSet in an instance of the following structure.
1053d4501e5Sdrh **
1063d4501e5Sdrh ** A typedef of this structure if found in sqliteInt.h.
1073d4501e5Sdrh */
1083d4501e5Sdrh struct RowSet {
1093d4501e5Sdrh   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
1103d4501e5Sdrh   sqlite3 *db;                   /* The database connection */
111733bf1b1Sdrh   struct RowSetEntry *pEntry;    /* List of entries using pRight */
1123d4501e5Sdrh   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
1133d4501e5Sdrh   struct RowSetEntry *pFresh;    /* Source of new entry objects */
1143343b439Sdrh   struct RowSetEntry *pForest;   /* List of binary trees of entries */
1153d4501e5Sdrh   u16 nFresh;                    /* Number of objects on pFresh */
116d83cad23Sdrh   u16 rsFlags;                   /* Various flags */
117d83cad23Sdrh   int iBatch;                    /* Current insert batch */
1183d4501e5Sdrh };
1193d4501e5Sdrh 
1203d4501e5Sdrh /*
1213343b439Sdrh ** Allowed values for RowSet.rsFlags
1223343b439Sdrh */
1233343b439Sdrh #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
1243343b439Sdrh #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
1253343b439Sdrh 
1263343b439Sdrh /*
1279d67afc4Sdrh ** Allocate a RowSet object.  Return NULL if a memory allocation
1289d67afc4Sdrh ** error occurs.
1293d4501e5Sdrh */
sqlite3RowSetInit(sqlite3 * db)1309d67afc4Sdrh RowSet *sqlite3RowSetInit(sqlite3 *db){
1319d67afc4Sdrh   RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
1329d67afc4Sdrh   if( p ){
1339d67afc4Sdrh     int N = sqlite3DbMallocSize(db, p);
1343d4501e5Sdrh     p->pChunk = 0;
1353d4501e5Sdrh     p->db = db;
1363d4501e5Sdrh     p->pEntry = 0;
1373d4501e5Sdrh     p->pLast = 0;
1383343b439Sdrh     p->pForest = 0;
13949145af9Sdrh     p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
14049145af9Sdrh     p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
1413343b439Sdrh     p->rsFlags = ROWSET_SORTED;
142733bf1b1Sdrh     p->iBatch = 0;
1439d67afc4Sdrh   }
1443d4501e5Sdrh   return p;
1453d4501e5Sdrh }
1463d4501e5Sdrh 
1473d4501e5Sdrh /*
148733bf1b1Sdrh ** Deallocate all chunks from a RowSet.  This frees all memory that
149733bf1b1Sdrh ** the RowSet has allocated over its lifetime.  This routine is
150733bf1b1Sdrh ** the destructor for the RowSet.
1513d4501e5Sdrh */
sqlite3RowSetClear(void * pArg)1529d67afc4Sdrh void sqlite3RowSetClear(void *pArg){
1539d67afc4Sdrh   RowSet *p = (RowSet*)pArg;
1543d4501e5Sdrh   struct RowSetChunk *pChunk, *pNextChunk;
1553d4501e5Sdrh   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
156733bf1b1Sdrh     pNextChunk = pChunk->pNextChunk;
1573d4501e5Sdrh     sqlite3DbFree(p->db, pChunk);
1583d4501e5Sdrh   }
1593d4501e5Sdrh   p->pChunk = 0;
1603d4501e5Sdrh   p->nFresh = 0;
1613d4501e5Sdrh   p->pEntry = 0;
1623d4501e5Sdrh   p->pLast = 0;
1633343b439Sdrh   p->pForest = 0;
1643343b439Sdrh   p->rsFlags = ROWSET_SORTED;
1653343b439Sdrh }
1663343b439Sdrh 
1673343b439Sdrh /*
1689d67afc4Sdrh ** Deallocate all chunks from a RowSet.  This frees all memory that
1699d67afc4Sdrh ** the RowSet has allocated over its lifetime.  This routine is
1709d67afc4Sdrh ** the destructor for the RowSet.
1719d67afc4Sdrh */
sqlite3RowSetDelete(void * pArg)1729d67afc4Sdrh void sqlite3RowSetDelete(void *pArg){
1739d67afc4Sdrh   sqlite3RowSetClear(pArg);
1749d67afc4Sdrh   sqlite3DbFree(((RowSet*)pArg)->db, pArg);
1759d67afc4Sdrh }
1769d67afc4Sdrh 
1779d67afc4Sdrh /*
1783343b439Sdrh ** Allocate a new RowSetEntry object that is associated with the
1793343b439Sdrh ** given RowSet.  Return a pointer to the new and completely uninitialized
180*45dc9ca4Spdr ** object.
1813343b439Sdrh **
1823343b439Sdrh ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
1833343b439Sdrh ** routine returns NULL.
1843343b439Sdrh */
rowSetEntryAlloc(RowSet * p)1853343b439Sdrh static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
1863343b439Sdrh   assert( p!=0 );
187396794f0Sdrh   if( p->nFresh==0 ){  /*OPTIMIZATION-IF-FALSE*/
188396794f0Sdrh     /* We could allocate a fresh RowSetEntry each time one is needed, but it
189396794f0Sdrh     ** is more efficient to pull a preallocated entry from the pool */
1903343b439Sdrh     struct RowSetChunk *pNew;
191575fad65Sdrh     pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
1923343b439Sdrh     if( pNew==0 ){
1933343b439Sdrh       return 0;
1943343b439Sdrh     }
1953343b439Sdrh     pNew->pNextChunk = p->pChunk;
1963343b439Sdrh     p->pChunk = pNew;
1973343b439Sdrh     p->pFresh = pNew->aEntry;
1983343b439Sdrh     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
1993343b439Sdrh   }
2003343b439Sdrh   p->nFresh--;
2013343b439Sdrh   return p->pFresh++;
2023d4501e5Sdrh }
2033d4501e5Sdrh 
2043d4501e5Sdrh /*
2053d4501e5Sdrh ** Insert a new value into a RowSet.
2063d4501e5Sdrh **
2073d4501e5Sdrh ** The mallocFailed flag of the database connection is set if a
2083d4501e5Sdrh ** memory allocation fails.
2093d4501e5Sdrh */
sqlite3RowSetInsert(RowSet * p,i64 rowid)2103d4501e5Sdrh void sqlite3RowSetInsert(RowSet *p, i64 rowid){
211733bf1b1Sdrh   struct RowSetEntry *pEntry;  /* The new entry */
212733bf1b1Sdrh   struct RowSetEntry *pLast;   /* The last prior entry */
2133343b439Sdrh 
2143343b439Sdrh   /* This routine is never called after sqlite3RowSetNext() */
2153343b439Sdrh   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
2163343b439Sdrh 
2173343b439Sdrh   pEntry = rowSetEntryAlloc(p);
2183343b439Sdrh   if( pEntry==0 ) return;
2193d4501e5Sdrh   pEntry->v = rowid;
220733bf1b1Sdrh   pEntry->pRight = 0;
2213d4501e5Sdrh   pLast = p->pLast;
2223d4501e5Sdrh   if( pLast ){
223396794f0Sdrh     if( rowid<=pLast->v ){  /*OPTIMIZATION-IF-FALSE*/
224396794f0Sdrh       /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
225396794f0Sdrh       ** where possible */
2263343b439Sdrh       p->rsFlags &= ~ROWSET_SORTED;
2273d4501e5Sdrh     }
228733bf1b1Sdrh     pLast->pRight = pEntry;
2293d4501e5Sdrh   }else{
2303d4501e5Sdrh     p->pEntry = pEntry;
2313d4501e5Sdrh   }
2323d4501e5Sdrh   p->pLast = pEntry;
2333d4501e5Sdrh }
2343d4501e5Sdrh 
2353d4501e5Sdrh /*
236733bf1b1Sdrh ** Merge two lists of RowSetEntry objects.  Remove duplicates.
2373d4501e5Sdrh **
238733bf1b1Sdrh ** The input lists are connected via pRight pointers and are
239733bf1b1Sdrh ** assumed to each already be in sorted order.
2403d4501e5Sdrh */
rowSetEntryMerge(struct RowSetEntry * pA,struct RowSetEntry * pB)2413343b439Sdrh static struct RowSetEntry *rowSetEntryMerge(
2423d4501e5Sdrh   struct RowSetEntry *pA,    /* First sorted list to be merged */
2433d4501e5Sdrh   struct RowSetEntry *pB     /* Second sorted list to be merged */
2443d4501e5Sdrh ){
2453d4501e5Sdrh   struct RowSetEntry head;
2463d4501e5Sdrh   struct RowSetEntry *pTail;
2473d4501e5Sdrh 
2483d4501e5Sdrh   pTail = &head;
249b982bfeaSdrh   assert( pA!=0 && pB!=0 );
250b982bfeaSdrh   for(;;){
251733bf1b1Sdrh     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
252733bf1b1Sdrh     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
253b982bfeaSdrh     if( pA->v<=pB->v ){
254b982bfeaSdrh       if( pA->v<pB->v ) pTail = pTail->pRight = pA;
255733bf1b1Sdrh       pA = pA->pRight;
256b982bfeaSdrh       if( pA==0 ){
257733bf1b1Sdrh         pTail->pRight = pB;
258b982bfeaSdrh         break;
259b982bfeaSdrh       }
260b982bfeaSdrh     }else{
261b982bfeaSdrh       pTail = pTail->pRight = pB;
262733bf1b1Sdrh       pB = pB->pRight;
263b982bfeaSdrh       if( pB==0 ){
264733bf1b1Sdrh         pTail->pRight = pA;
265b982bfeaSdrh         break;
266b982bfeaSdrh       }
267b982bfeaSdrh     }
2683d4501e5Sdrh   }
269733bf1b1Sdrh   return head.pRight;
2703d4501e5Sdrh }
2713d4501e5Sdrh 
2723d4501e5Sdrh /*
2733343b439Sdrh ** Sort all elements on the list of RowSetEntry objects into order of
2743343b439Sdrh ** increasing v.
2753d4501e5Sdrh */
rowSetEntrySort(struct RowSetEntry * pIn)2763343b439Sdrh static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
2773d4501e5Sdrh   unsigned int i;
2783343b439Sdrh   struct RowSetEntry *pNext, *aBucket[40];
2793d4501e5Sdrh 
2803d4501e5Sdrh   memset(aBucket, 0, sizeof(aBucket));
2813343b439Sdrh   while( pIn ){
2823343b439Sdrh     pNext = pIn->pRight;
2833343b439Sdrh     pIn->pRight = 0;
2843d4501e5Sdrh     for(i=0; aBucket[i]; i++){
2853343b439Sdrh       pIn = rowSetEntryMerge(aBucket[i], pIn);
2863d4501e5Sdrh       aBucket[i] = 0;
2873d4501e5Sdrh     }
2883343b439Sdrh     aBucket[i] = pIn;
2893343b439Sdrh     pIn = pNext;
2903d4501e5Sdrh   }
291b982bfeaSdrh   pIn = aBucket[0];
292b982bfeaSdrh   for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
293b982bfeaSdrh     if( aBucket[i]==0 ) continue;
294b982bfeaSdrh     pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
2953d4501e5Sdrh   }
2963343b439Sdrh   return pIn;
2973d4501e5Sdrh }
2983d4501e5Sdrh 
299733bf1b1Sdrh 
3003d4501e5Sdrh /*
301733bf1b1Sdrh ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
302733bf1b1Sdrh ** Convert this tree into a linked list connected by the pRight pointers
303733bf1b1Sdrh ** and return pointers to the first and last elements of the new list.
304733bf1b1Sdrh */
rowSetTreeToList(struct RowSetEntry * pIn,struct RowSetEntry ** ppFirst,struct RowSetEntry ** ppLast)305733bf1b1Sdrh static void rowSetTreeToList(
306733bf1b1Sdrh   struct RowSetEntry *pIn,         /* Root of the input tree */
307733bf1b1Sdrh   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
308733bf1b1Sdrh   struct RowSetEntry **ppLast      /* Write tail of the output list here */
309733bf1b1Sdrh ){
3106149526cSdrh   assert( pIn!=0 );
311733bf1b1Sdrh   if( pIn->pLeft ){
312733bf1b1Sdrh     struct RowSetEntry *p;
313733bf1b1Sdrh     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
314733bf1b1Sdrh     p->pRight = pIn;
315733bf1b1Sdrh   }else{
316733bf1b1Sdrh     *ppFirst = pIn;
317733bf1b1Sdrh   }
318733bf1b1Sdrh   if( pIn->pRight ){
319733bf1b1Sdrh     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
320733bf1b1Sdrh   }else{
321733bf1b1Sdrh     *ppLast = pIn;
322733bf1b1Sdrh   }
323733bf1b1Sdrh   assert( (*ppLast)->pRight==0 );
324733bf1b1Sdrh }
325733bf1b1Sdrh 
326733bf1b1Sdrh 
327733bf1b1Sdrh /*
328733bf1b1Sdrh ** Convert a sorted list of elements (connected by pRight) into a binary
329733bf1b1Sdrh ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
330733bf1b1Sdrh ** node taken from the head of *ppList.  A depth of 2 means a tree with
331733bf1b1Sdrh ** three nodes.  And so forth.
332733bf1b1Sdrh **
333733bf1b1Sdrh ** Use as many entries from the input list as required and update the
334733bf1b1Sdrh ** *ppList to point to the unused elements of the list.  If the input
335733bf1b1Sdrh ** list contains too few elements, then construct an incomplete tree
336733bf1b1Sdrh ** and leave *ppList set to NULL.
337733bf1b1Sdrh **
338733bf1b1Sdrh ** Return a pointer to the root of the constructed binary tree.
339733bf1b1Sdrh */
rowSetNDeepTree(struct RowSetEntry ** ppList,int iDepth)340733bf1b1Sdrh static struct RowSetEntry *rowSetNDeepTree(
341733bf1b1Sdrh   struct RowSetEntry **ppList,
342733bf1b1Sdrh   int iDepth
343733bf1b1Sdrh ){
344733bf1b1Sdrh   struct RowSetEntry *p;         /* Root of the new tree */
345733bf1b1Sdrh   struct RowSetEntry *pLeft;     /* Left subtree */
346396794f0Sdrh   if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
347396794f0Sdrh     /* Prevent unnecessary deep recursion when we run out of entries */
348733bf1b1Sdrh     return 0;
349733bf1b1Sdrh   }
350cb6d66beSdrh   if( iDepth>1 ){   /*OPTIMIZATION-IF-TRUE*/
3512075fb08Smistachkin     /* This branch causes a *balanced* tree to be generated.  A valid tree
352396794f0Sdrh     ** is still generated without this branch, but the tree is wildly
353396794f0Sdrh     ** unbalanced and inefficient. */
354733bf1b1Sdrh     pLeft = rowSetNDeepTree(ppList, iDepth-1);
355733bf1b1Sdrh     p = *ppList;
356396794f0Sdrh     if( p==0 ){     /*OPTIMIZATION-IF-FALSE*/
357396794f0Sdrh       /* It is safe to always return here, but the resulting tree
358396794f0Sdrh       ** would be unbalanced */
359733bf1b1Sdrh       return pLeft;
360733bf1b1Sdrh     }
361733bf1b1Sdrh     p->pLeft = pLeft;
362733bf1b1Sdrh     *ppList = p->pRight;
363733bf1b1Sdrh     p->pRight = rowSetNDeepTree(ppList, iDepth-1);
364cb6d66beSdrh   }else{
365cb6d66beSdrh     p = *ppList;
366cb6d66beSdrh     *ppList = p->pRight;
367cb6d66beSdrh     p->pLeft = p->pRight = 0;
368cb6d66beSdrh   }
369733bf1b1Sdrh   return p;
370733bf1b1Sdrh }
371733bf1b1Sdrh 
372733bf1b1Sdrh /*
373733bf1b1Sdrh ** Convert a sorted list of elements into a binary tree. Make the tree
374733bf1b1Sdrh ** as deep as it needs to be in order to contain the entire list.
375733bf1b1Sdrh */
rowSetListToTree(struct RowSetEntry * pList)376733bf1b1Sdrh static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
377733bf1b1Sdrh   int iDepth;           /* Depth of the tree so far */
378733bf1b1Sdrh   struct RowSetEntry *p;       /* Current tree root */
379733bf1b1Sdrh   struct RowSetEntry *pLeft;   /* Left subtree */
380733bf1b1Sdrh 
3816149526cSdrh   assert( pList!=0 );
382733bf1b1Sdrh   p = pList;
383733bf1b1Sdrh   pList = p->pRight;
384733bf1b1Sdrh   p->pLeft = p->pRight = 0;
385733bf1b1Sdrh   for(iDepth=1; pList; iDepth++){
386733bf1b1Sdrh     pLeft = p;
387733bf1b1Sdrh     p = pList;
388733bf1b1Sdrh     pList = p->pRight;
389733bf1b1Sdrh     p->pLeft = pLeft;
390733bf1b1Sdrh     p->pRight = rowSetNDeepTree(&pList, iDepth);
391733bf1b1Sdrh   }
392733bf1b1Sdrh   return p;
393733bf1b1Sdrh }
394733bf1b1Sdrh 
395733bf1b1Sdrh /*
396733bf1b1Sdrh ** Extract the smallest element from the RowSet.
3973d4501e5Sdrh ** Write the element into *pRowid.  Return 1 on success.  Return
3983d4501e5Sdrh ** 0 if the RowSet is already empty.
399733bf1b1Sdrh **
400733bf1b1Sdrh ** After this routine has been called, the sqlite3RowSetInsert()
401733bf1b1Sdrh ** routine may not be called again.
402aa50271aSdrh **
403aa50271aSdrh ** This routine may not be called after sqlite3RowSetTest() has
404aa50271aSdrh ** been used.  Older versions of RowSet allowed that, but as the
405aa50271aSdrh ** capability was not used by the code generator, it was removed
406aa50271aSdrh ** for code economy.
4073d4501e5Sdrh */
sqlite3RowSetNext(RowSet * p,i64 * pRowid)4083d4501e5Sdrh int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
4093343b439Sdrh   assert( p!=0 );
410aa50271aSdrh   assert( p->pForest==0 );  /* Cannot be used with sqlite3RowSetText() */
4113343b439Sdrh 
4123343b439Sdrh   /* Merge the forest into a single sorted list on first call */
413aa50271aSdrh   if( (p->rsFlags & ROWSET_NEXT)==0 ){  /*OPTIMIZATION-IF-FALSE*/
414aa50271aSdrh     if( (p->rsFlags & ROWSET_SORTED)==0 ){  /*OPTIMIZATION-IF-FALSE*/
415aa50271aSdrh       p->pEntry = rowSetEntrySort(p->pEntry);
416aa50271aSdrh     }
417aa50271aSdrh     p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
418aa50271aSdrh   }
4193343b439Sdrh 
4203343b439Sdrh   /* Return the next entry on the list */
4213d4501e5Sdrh   if( p->pEntry ){
4223d4501e5Sdrh     *pRowid = p->pEntry->v;
423733bf1b1Sdrh     p->pEntry = p->pEntry->pRight;
424aa50271aSdrh     if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
425aa50271aSdrh       /* Free memory immediately, rather than waiting on sqlite3_finalize() */
4263d4501e5Sdrh       sqlite3RowSetClear(p);
4273d4501e5Sdrh     }
4283d4501e5Sdrh     return 1;
4293d4501e5Sdrh   }else{
4303d4501e5Sdrh     return 0;
4313d4501e5Sdrh   }
4323d4501e5Sdrh }
433733bf1b1Sdrh 
434733bf1b1Sdrh /*
435d5578433Smistachkin ** Check to see if element iRowid was inserted into the rowset as
436733bf1b1Sdrh ** part of any insert batch prior to iBatch.  Return 1 or 0.
4373343b439Sdrh **
43860ec914cSpeter.d.reid ** If this is the first test of a new batch and if there exist entries
43960ec914cSpeter.d.reid ** on pRowSet->pEntry, then sort those entries into the forest at
4403343b439Sdrh ** pRowSet->pForest so that they can be tested.
441733bf1b1Sdrh */
sqlite3RowSetTest(RowSet * pRowSet,int iBatch,sqlite3_int64 iRowid)442d83cad23Sdrh int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
4433343b439Sdrh   struct RowSetEntry *p, *pTree;
4443343b439Sdrh 
4453343b439Sdrh   /* This routine is never called after sqlite3RowSetNext() */
4463343b439Sdrh   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
4473343b439Sdrh 
448aa50271aSdrh   /* Sort entries into the forest on the first test of a new batch.
449aa50271aSdrh   ** To save unnecessary work, only do this when the batch number changes.
4503343b439Sdrh   */
451aa50271aSdrh   if( iBatch!=pRowSet->iBatch ){  /*OPTIMIZATION-IF-FALSE*/
4523343b439Sdrh     p = pRowSet->pEntry;
4533343b439Sdrh     if( p ){
4543343b439Sdrh       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
455aa50271aSdrh       if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
456*45dc9ca4Spdr         /* Only sort the current set of entries if they need it */
4573343b439Sdrh         p = rowSetEntrySort(p);
4583343b439Sdrh       }
4593343b439Sdrh       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
4603343b439Sdrh         ppPrevTree = &pTree->pRight;
4613343b439Sdrh         if( pTree->pLeft==0 ){
4623343b439Sdrh           pTree->pLeft = rowSetListToTree(p);
4633343b439Sdrh           break;
4643343b439Sdrh         }else{
4653343b439Sdrh           struct RowSetEntry *pAux, *pTail;
4663343b439Sdrh           rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
4673343b439Sdrh           pTree->pLeft = 0;
4683343b439Sdrh           p = rowSetEntryMerge(pAux, p);
4693343b439Sdrh         }
4703343b439Sdrh       }
4713343b439Sdrh       if( pTree==0 ){
4723343b439Sdrh         *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
4733343b439Sdrh         if( pTree ){
4743343b439Sdrh           pTree->v = 0;
4753343b439Sdrh           pTree->pRight = 0;
4763343b439Sdrh           pTree->pLeft = rowSetListToTree(p);
4773343b439Sdrh         }
4783343b439Sdrh       }
479733bf1b1Sdrh       pRowSet->pEntry = 0;
480733bf1b1Sdrh       pRowSet->pLast = 0;
4813343b439Sdrh       pRowSet->rsFlags |= ROWSET_SORTED;
482733bf1b1Sdrh     }
483733bf1b1Sdrh     pRowSet->iBatch = iBatch;
484733bf1b1Sdrh   }
4853343b439Sdrh 
4863343b439Sdrh   /* Test to see if the iRowid value appears anywhere in the forest.
4873343b439Sdrh   ** Return 1 if it does and 0 if not.
4883343b439Sdrh   */
4893343b439Sdrh   for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
4903343b439Sdrh     p = pTree->pLeft;
491733bf1b1Sdrh     while( p ){
492733bf1b1Sdrh       if( p->v<iRowid ){
493733bf1b1Sdrh         p = p->pRight;
494733bf1b1Sdrh       }else if( p->v>iRowid ){
495733bf1b1Sdrh         p = p->pLeft;
496733bf1b1Sdrh       }else{
497733bf1b1Sdrh         return 1;
498733bf1b1Sdrh       }
499733bf1b1Sdrh     }
5003343b439Sdrh   }
501733bf1b1Sdrh   return 0;
502733bf1b1Sdrh }
503