blob: aa81607b9fdc1864c5d542973fb2b632055995ab [file] [log] [blame]
drh3d4501e2008-12-04 20:40:10 +00001/*
2** 2008 December 3
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12**
drh733bf1b2009-04-22 00:47:00 +000013** This module implements an object we call a "RowSet".
drh3d4501e2008-12-04 20:40:10 +000014**
drh733bf1b2009-04-22 00:47:00 +000015** The RowSet object is a collection of rowids. Rowids
16** are inserted into the RowSet in an arbitrary order. Inserts
17** can be intermixed with tests to see if a given rowid has been
18** previously inserted into the RowSet.
drh3d4501e2008-12-04 20:40:10 +000019**
drh733bf1b2009-04-22 00:47:00 +000020** After all inserts are finished, it is possible to extract the
21** elements of the RowSet in sorted order. Once this extraction
22** process has started, no new elements may be inserted.
drh3d4501e2008-12-04 20:40:10 +000023**
drh733bf1b2009-04-22 00:47:00 +000024** Hence, the primitive operations for a RowSet are:
drha9e364f2009-01-13 20:14:15 +000025**
drh733bf1b2009-04-22 00:47:00 +000026** CREATE
27** INSERT
28** TEST
29** SMALLEST
30** DESTROY
31**
32** The CREATE and DESTROY primitives are the constructor and destructor,
33** obviously. The INSERT primitive adds a new element to the RowSet.
34** TEST checks to see if an element is already in the RowSet. SMALLEST
35** extracts the least value from the RowSet.
36**
37** The INSERT primitive might allocate additional memory. Memory is
38** allocated in chunks so most INSERTs do no allocation. There is an
39** upper bound on the size of allocated memory. No memory is freed
40** until DESTROY.
41**
42** The TEST primitive includes a "batch" number. The TEST primitive
43** will only see elements that were inserted before the last change
44** in the batch number. In other words, if an INSERT occurs between
45** two TESTs where the TESTs have the same batch nubmer, then the
46** value added by the INSERT will not be visible to the second TEST.
47** The initial batch number is zero, so if the very first TEST contains
48** a non-zero batch number, it will see all prior INSERTs.
49**
50** No INSERTs may occurs after a SMALLEST. An assertion will fail if
51** that is attempted.
52**
peter.d.reid60ec9142014-09-06 16:39:46 +000053** The cost of an INSERT is roughly constant. (Sometimes new memory
drh733bf1b2009-04-22 00:47:00 +000054** has to be allocated on an INSERT.) The cost of a TEST with a new
55** batch number is O(NlogN) where N is the number of elements in the RowSet.
56** The cost of a TEST using the same batch number is O(logN). The cost
57** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
58** primitives are constant time. The cost of DESTROY is O(N).
59**
drhaa502712016-04-28 22:29:31 +000060** TEST and SMALLEST may not be used by the same RowSet. This used to
61** be possible, but the feature was not used, so it was removed in order
62** to simplify the code.
drh3d4501e2008-12-04 20:40:10 +000063*/
64#include "sqliteInt.h"
65
drh733bf1b2009-04-22 00:47:00 +000066
67/*
68** Target size for allocation chunks.
69*/
70#define ROWSET_ALLOCATION_SIZE 1024
71
drh3d4501e2008-12-04 20:40:10 +000072/*
73** The number of rowset entries per allocation chunk.
74*/
drh733bf1b2009-04-22 00:47:00 +000075#define ROWSET_ENTRY_PER_CHUNK \
76 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
drh3d4501e2008-12-04 20:40:10 +000077
78/*
drh733bf1b2009-04-22 00:47:00 +000079** Each entry in a RowSet is an instance of the following object.
drh3343b432012-04-05 01:37:32 +000080**
81** This same object is reused to store a linked list of trees of RowSetEntry
82** objects. In that alternative use, pRight points to the next entry
83** in the list, pLeft points to the tree, and v is unused. The
84** RowSet.pForest value points to the head of this forest list.
drh3d4501e2008-12-04 20:40:10 +000085*/
86struct RowSetEntry {
87 i64 v; /* ROWID value for this entry */
drh733bf1b2009-04-22 00:47:00 +000088 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
89 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
drh3d4501e2008-12-04 20:40:10 +000090};
91
92/*
drh733bf1b2009-04-22 00:47:00 +000093** RowSetEntry objects are allocated in large chunks (instances of the
drh3d4501e2008-12-04 20:40:10 +000094** following structure) to reduce memory allocation overhead. The
95** chunks are kept on a linked list so that they can be deallocated
96** when the RowSet is destroyed.
97*/
98struct RowSetChunk {
drh733bf1b2009-04-22 00:47:00 +000099 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
drh3d4501e2008-12-04 20:40:10 +0000100 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
101};
102
103/*
104** A RowSet in an instance of the following structure.
105**
106** A typedef of this structure if found in sqliteInt.h.
107*/
108struct RowSet {
109 struct RowSetChunk *pChunk; /* List of all chunk allocations */
110 sqlite3 *db; /* The database connection */
drh733bf1b2009-04-22 00:47:00 +0000111 struct RowSetEntry *pEntry; /* List of entries using pRight */
drh3d4501e2008-12-04 20:40:10 +0000112 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
113 struct RowSetEntry *pFresh; /* Source of new entry objects */
drh3343b432012-04-05 01:37:32 +0000114 struct RowSetEntry *pForest; /* List of binary trees of entries */
drh3d4501e2008-12-04 20:40:10 +0000115 u16 nFresh; /* Number of objects on pFresh */
drhd83cad22014-04-10 02:24:48 +0000116 u16 rsFlags; /* Various flags */
117 int iBatch; /* Current insert batch */
drh3d4501e2008-12-04 20:40:10 +0000118};
119
120/*
drh3343b432012-04-05 01:37:32 +0000121** Allowed values for RowSet.rsFlags
122*/
123#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
124#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
125
126/*
drh3d4501e2008-12-04 20:40:10 +0000127** Turn bulk memory into a RowSet object. N bytes of memory
128** are available at pSpace. The db pointer is used as a memory context
129** for any subsequent allocations that need to occur.
130** Return a pointer to the new RowSet object.
131**
drhe2f02ba2009-01-09 01:12:27 +0000132** It must be the case that N is sufficient to make a Rowset. If not
133** an assertion fault occurs.
134**
135** If N is larger than the minimum, use the surplus as an initial
136** allocation of entries available to be filled.
drh3d4501e2008-12-04 20:40:10 +0000137*/
138RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
139 RowSet *p;
drh49145af2009-05-22 01:00:12 +0000140 assert( N >= ROUND8(sizeof(*p)) );
drhe2f02ba2009-01-09 01:12:27 +0000141 p = pSpace;
142 p->pChunk = 0;
143 p->db = db;
144 p->pEntry = 0;
145 p->pLast = 0;
drh3343b432012-04-05 01:37:32 +0000146 p->pForest = 0;
drh49145af2009-05-22 01:00:12 +0000147 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
148 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
drh3343b432012-04-05 01:37:32 +0000149 p->rsFlags = ROWSET_SORTED;
drh733bf1b2009-04-22 00:47:00 +0000150 p->iBatch = 0;
drh3d4501e2008-12-04 20:40:10 +0000151 return p;
152}
153
154/*
drh733bf1b2009-04-22 00:47:00 +0000155** Deallocate all chunks from a RowSet. This frees all memory that
156** the RowSet has allocated over its lifetime. This routine is
157** the destructor for the RowSet.
drh3d4501e2008-12-04 20:40:10 +0000158*/
159void sqlite3RowSetClear(RowSet *p){
160 struct RowSetChunk *pChunk, *pNextChunk;
161 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
drh733bf1b2009-04-22 00:47:00 +0000162 pNextChunk = pChunk->pNextChunk;
drh3d4501e2008-12-04 20:40:10 +0000163 sqlite3DbFree(p->db, pChunk);
164 }
165 p->pChunk = 0;
166 p->nFresh = 0;
167 p->pEntry = 0;
168 p->pLast = 0;
drh3343b432012-04-05 01:37:32 +0000169 p->pForest = 0;
170 p->rsFlags = ROWSET_SORTED;
171}
172
173/*
174** Allocate a new RowSetEntry object that is associated with the
175** given RowSet. Return a pointer to the new and completely uninitialized
176** objected.
177**
178** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
179** routine returns NULL.
180*/
181static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
182 assert( p!=0 );
drh396794f2016-04-28 20:11:12 +0000183 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
184 /* We could allocate a fresh RowSetEntry each time one is needed, but it
185 ** is more efficient to pull a preallocated entry from the pool */
drh3343b432012-04-05 01:37:32 +0000186 struct RowSetChunk *pNew;
drh575fad62016-02-05 13:38:36 +0000187 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
drh3343b432012-04-05 01:37:32 +0000188 if( pNew==0 ){
189 return 0;
190 }
191 pNew->pNextChunk = p->pChunk;
192 p->pChunk = pNew;
193 p->pFresh = pNew->aEntry;
194 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
195 }
196 p->nFresh--;
197 return p->pFresh++;
drh3d4501e2008-12-04 20:40:10 +0000198}
199
200/*
201** Insert a new value into a RowSet.
202**
203** The mallocFailed flag of the database connection is set if a
204** memory allocation fails.
205*/
206void sqlite3RowSetInsert(RowSet *p, i64 rowid){
drh733bf1b2009-04-22 00:47:00 +0000207 struct RowSetEntry *pEntry; /* The new entry */
208 struct RowSetEntry *pLast; /* The last prior entry */
drh3343b432012-04-05 01:37:32 +0000209
210 /* This routine is never called after sqlite3RowSetNext() */
211 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
212
213 pEntry = rowSetEntryAlloc(p);
214 if( pEntry==0 ) return;
drh3d4501e2008-12-04 20:40:10 +0000215 pEntry->v = rowid;
drh733bf1b2009-04-22 00:47:00 +0000216 pEntry->pRight = 0;
drh3d4501e2008-12-04 20:40:10 +0000217 pLast = p->pLast;
218 if( pLast ){
drh396794f2016-04-28 20:11:12 +0000219 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
220 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
221 ** where possible */
drh3343b432012-04-05 01:37:32 +0000222 p->rsFlags &= ~ROWSET_SORTED;
drh3d4501e2008-12-04 20:40:10 +0000223 }
drh733bf1b2009-04-22 00:47:00 +0000224 pLast->pRight = pEntry;
drh3d4501e2008-12-04 20:40:10 +0000225 }else{
drh3d4501e2008-12-04 20:40:10 +0000226 p->pEntry = pEntry;
227 }
228 p->pLast = pEntry;
229}
230
231/*
drh733bf1b2009-04-22 00:47:00 +0000232** Merge two lists of RowSetEntry objects. Remove duplicates.
drh3d4501e2008-12-04 20:40:10 +0000233**
drh733bf1b2009-04-22 00:47:00 +0000234** The input lists are connected via pRight pointers and are
235** assumed to each already be in sorted order.
drh3d4501e2008-12-04 20:40:10 +0000236*/
drh3343b432012-04-05 01:37:32 +0000237static struct RowSetEntry *rowSetEntryMerge(
drh3d4501e2008-12-04 20:40:10 +0000238 struct RowSetEntry *pA, /* First sorted list to be merged */
239 struct RowSetEntry *pB /* Second sorted list to be merged */
240){
241 struct RowSetEntry head;
242 struct RowSetEntry *pTail;
243
244 pTail = &head;
drhb982bfe2016-05-20 14:54:54 +0000245 assert( pA!=0 && pB!=0 );
246 for(;;){
drh733bf1b2009-04-22 00:47:00 +0000247 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
248 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
drhb982bfe2016-05-20 14:54:54 +0000249 if( pA->v<=pB->v ){
250 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
drh733bf1b2009-04-22 00:47:00 +0000251 pA = pA->pRight;
drhb982bfe2016-05-20 14:54:54 +0000252 if( pA==0 ){
253 pTail->pRight = pB;
254 break;
255 }
drh3d4501e2008-12-04 20:40:10 +0000256 }else{
drhb982bfe2016-05-20 14:54:54 +0000257 pTail = pTail->pRight = pB;
258 pB = pB->pRight;
259 if( pB==0 ){
260 pTail->pRight = pA;
261 break;
262 }
drh3d4501e2008-12-04 20:40:10 +0000263 }
264 }
drh733bf1b2009-04-22 00:47:00 +0000265 return head.pRight;
drh3d4501e2008-12-04 20:40:10 +0000266}
267
268/*
drh3343b432012-04-05 01:37:32 +0000269** Sort all elements on the list of RowSetEntry objects into order of
270** increasing v.
drh3d4501e2008-12-04 20:40:10 +0000271*/
drh3343b432012-04-05 01:37:32 +0000272static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
drh3d4501e2008-12-04 20:40:10 +0000273 unsigned int i;
drh3343b432012-04-05 01:37:32 +0000274 struct RowSetEntry *pNext, *aBucket[40];
drh3d4501e2008-12-04 20:40:10 +0000275
drh3d4501e2008-12-04 20:40:10 +0000276 memset(aBucket, 0, sizeof(aBucket));
drh3343b432012-04-05 01:37:32 +0000277 while( pIn ){
278 pNext = pIn->pRight;
279 pIn->pRight = 0;
drh3d4501e2008-12-04 20:40:10 +0000280 for(i=0; aBucket[i]; i++){
drh3343b432012-04-05 01:37:32 +0000281 pIn = rowSetEntryMerge(aBucket[i], pIn);
drh3d4501e2008-12-04 20:40:10 +0000282 aBucket[i] = 0;
283 }
drh3343b432012-04-05 01:37:32 +0000284 aBucket[i] = pIn;
285 pIn = pNext;
drh3d4501e2008-12-04 20:40:10 +0000286 }
drhb982bfe2016-05-20 14:54:54 +0000287 pIn = aBucket[0];
288 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
289 if( aBucket[i]==0 ) continue;
290 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
drh3d4501e2008-12-04 20:40:10 +0000291 }
drh3343b432012-04-05 01:37:32 +0000292 return pIn;
drh3d4501e2008-12-04 20:40:10 +0000293}
294
drh733bf1b2009-04-22 00:47:00 +0000295
drh3d4501e2008-12-04 20:40:10 +0000296/*
drh733bf1b2009-04-22 00:47:00 +0000297** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
298** Convert this tree into a linked list connected by the pRight pointers
299** and return pointers to the first and last elements of the new list.
300*/
301static void rowSetTreeToList(
302 struct RowSetEntry *pIn, /* Root of the input tree */
303 struct RowSetEntry **ppFirst, /* Write head of the output list here */
304 struct RowSetEntry **ppLast /* Write tail of the output list here */
305){
drh61495262009-04-22 15:32:59 +0000306 assert( pIn!=0 );
drh733bf1b2009-04-22 00:47:00 +0000307 if( pIn->pLeft ){
308 struct RowSetEntry *p;
309 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
310 p->pRight = pIn;
311 }else{
312 *ppFirst = pIn;
313 }
314 if( pIn->pRight ){
315 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
316 }else{
317 *ppLast = pIn;
318 }
319 assert( (*ppLast)->pRight==0 );
320}
321
322
323/*
324** Convert a sorted list of elements (connected by pRight) into a binary
325** tree with depth of iDepth. A depth of 1 means the tree contains a single
326** node taken from the head of *ppList. A depth of 2 means a tree with
327** three nodes. And so forth.
328**
329** Use as many entries from the input list as required and update the
330** *ppList to point to the unused elements of the list. If the input
331** list contains too few elements, then construct an incomplete tree
332** and leave *ppList set to NULL.
333**
334** Return a pointer to the root of the constructed binary tree.
335*/
336static struct RowSetEntry *rowSetNDeepTree(
337 struct RowSetEntry **ppList,
338 int iDepth
339){
340 struct RowSetEntry *p; /* Root of the new tree */
341 struct RowSetEntry *pLeft; /* Left subtree */
drh396794f2016-04-28 20:11:12 +0000342 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
343 /* Prevent unnecessary deep recursion when we run out of entries */
344 return 0;
drh733bf1b2009-04-22 00:47:00 +0000345 }
drhcb6d66b2016-04-28 18:53:08 +0000346 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
mistachkin2075fb02016-04-28 19:23:10 +0000347 /* This branch causes a *balanced* tree to be generated. A valid tree
drh396794f2016-04-28 20:11:12 +0000348 ** is still generated without this branch, but the tree is wildly
349 ** unbalanced and inefficient. */
drhcb6d66b2016-04-28 18:53:08 +0000350 pLeft = rowSetNDeepTree(ppList, iDepth-1);
351 p = *ppList;
drh396794f2016-04-28 20:11:12 +0000352 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
353 /* It is safe to always return here, but the resulting tree
354 ** would be unbalanced */
drhcb6d66b2016-04-28 18:53:08 +0000355 return pLeft;
356 }
357 p->pLeft = pLeft;
358 *ppList = p->pRight;
359 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
360 }else{
drh733bf1b2009-04-22 00:47:00 +0000361 p = *ppList;
362 *ppList = p->pRight;
363 p->pLeft = p->pRight = 0;
drh733bf1b2009-04-22 00:47:00 +0000364 }
drh733bf1b2009-04-22 00:47:00 +0000365 return p;
366}
367
368/*
369** Convert a sorted list of elements into a binary tree. Make the tree
370** as deep as it needs to be in order to contain the entire list.
371*/
372static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
373 int iDepth; /* Depth of the tree so far */
374 struct RowSetEntry *p; /* Current tree root */
375 struct RowSetEntry *pLeft; /* Left subtree */
376
drh61495262009-04-22 15:32:59 +0000377 assert( pList!=0 );
drh733bf1b2009-04-22 00:47:00 +0000378 p = pList;
379 pList = p->pRight;
380 p->pLeft = p->pRight = 0;
381 for(iDepth=1; pList; iDepth++){
382 pLeft = p;
383 p = pList;
384 pList = p->pRight;
385 p->pLeft = pLeft;
386 p->pRight = rowSetNDeepTree(&pList, iDepth);
387 }
388 return p;
389}
390
391/*
drh733bf1b2009-04-22 00:47:00 +0000392** Extract the smallest element from the RowSet.
drh3d4501e2008-12-04 20:40:10 +0000393** Write the element into *pRowid. Return 1 on success. Return
394** 0 if the RowSet is already empty.
drh733bf1b2009-04-22 00:47:00 +0000395**
396** After this routine has been called, the sqlite3RowSetInsert()
drhaa502712016-04-28 22:29:31 +0000397** routine may not be called again.
398**
399** This routine may not be called after sqlite3RowSetTest() has
400** been used. Older versions of RowSet allowed that, but as the
401** capability was not used by the code generator, it was removed
402** for code economy.
drh3d4501e2008-12-04 20:40:10 +0000403*/
404int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
drh3343b432012-04-05 01:37:32 +0000405 assert( p!=0 );
drhaa502712016-04-28 22:29:31 +0000406 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
drh3343b432012-04-05 01:37:32 +0000407
408 /* Merge the forest into a single sorted list on first call */
drhaa502712016-04-28 22:29:31 +0000409 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
410 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
411 p->pEntry = rowSetEntrySort(p->pEntry);
412 }
413 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
414 }
drh3343b432012-04-05 01:37:32 +0000415
416 /* Return the next entry on the list */
drh3d4501e2008-12-04 20:40:10 +0000417 if( p->pEntry ){
418 *pRowid = p->pEntry->v;
drh733bf1b2009-04-22 00:47:00 +0000419 p->pEntry = p->pEntry->pRight;
drhaa502712016-04-28 22:29:31 +0000420 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
421 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
drh3d4501e2008-12-04 20:40:10 +0000422 sqlite3RowSetClear(p);
423 }
424 return 1;
425 }else{
426 return 0;
427 }
428}
drh733bf1b2009-04-22 00:47:00 +0000429
430/*
mistachkind5578432012-08-25 10:01:29 +0000431** Check to see if element iRowid was inserted into the rowset as
drh733bf1b2009-04-22 00:47:00 +0000432** part of any insert batch prior to iBatch. Return 1 or 0.
drh3343b432012-04-05 01:37:32 +0000433**
peter.d.reid60ec9142014-09-06 16:39:46 +0000434** If this is the first test of a new batch and if there exist entries
435** on pRowSet->pEntry, then sort those entries into the forest at
drh3343b432012-04-05 01:37:32 +0000436** pRowSet->pForest so that they can be tested.
drh733bf1b2009-04-22 00:47:00 +0000437*/
drhd83cad22014-04-10 02:24:48 +0000438int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
drh3343b432012-04-05 01:37:32 +0000439 struct RowSetEntry *p, *pTree;
440
441 /* This routine is never called after sqlite3RowSetNext() */
442 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
443
drhaa502712016-04-28 22:29:31 +0000444 /* Sort entries into the forest on the first test of a new batch.
445 ** To save unnecessary work, only do this when the batch number changes.
drh3343b432012-04-05 01:37:32 +0000446 */
drhaa502712016-04-28 22:29:31 +0000447 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
drh3343b432012-04-05 01:37:32 +0000448 p = pRowSet->pEntry;
449 if( p ){
450 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
drhaa502712016-04-28 22:29:31 +0000451 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
452 /* Only sort the current set of entiries if they need it */
drh3343b432012-04-05 01:37:32 +0000453 p = rowSetEntrySort(p);
454 }
455 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
456 ppPrevTree = &pTree->pRight;
457 if( pTree->pLeft==0 ){
458 pTree->pLeft = rowSetListToTree(p);
459 break;
460 }else{
461 struct RowSetEntry *pAux, *pTail;
462 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
463 pTree->pLeft = 0;
464 p = rowSetEntryMerge(pAux, p);
465 }
466 }
467 if( pTree==0 ){
468 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
469 if( pTree ){
470 pTree->v = 0;
471 pTree->pRight = 0;
472 pTree->pLeft = rowSetListToTree(p);
473 }
474 }
drh733bf1b2009-04-22 00:47:00 +0000475 pRowSet->pEntry = 0;
476 pRowSet->pLast = 0;
drh3343b432012-04-05 01:37:32 +0000477 pRowSet->rsFlags |= ROWSET_SORTED;
drh733bf1b2009-04-22 00:47:00 +0000478 }
479 pRowSet->iBatch = iBatch;
480 }
drh3343b432012-04-05 01:37:32 +0000481
482 /* Test to see if the iRowid value appears anywhere in the forest.
483 ** Return 1 if it does and 0 if not.
484 */
485 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
486 p = pTree->pLeft;
487 while( p ){
488 if( p->v<iRowid ){
489 p = p->pRight;
490 }else if( p->v>iRowid ){
491 p = p->pLeft;
492 }else{
493 return 1;
494 }
drh733bf1b2009-04-22 00:47:00 +0000495 }
496 }
497 return 0;
498}