blob: 4e27676cb47d3c8cfd26d35e64454cdb8fe39809 [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**
53** The cost of an INSERT is roughly constant. (Sometime new memory
54** 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**
60** There is an added cost of O(N) when switching between TEST and
61** SMALLEST primitives.
62**
63** $Id: rowset.c,v 1.5 2009/04/22 00:47:01 drh Exp $
drh3d4501e2008-12-04 20:40:10 +000064*/
65#include "sqliteInt.h"
66
drh733bf1b2009-04-22 00:47:00 +000067
68/*
69** Target size for allocation chunks.
70*/
71#define ROWSET_ALLOCATION_SIZE 1024
72
drh3d4501e2008-12-04 20:40:10 +000073/*
74** The number of rowset entries per allocation chunk.
75*/
drh733bf1b2009-04-22 00:47:00 +000076#define ROWSET_ENTRY_PER_CHUNK \
77 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
drh3d4501e2008-12-04 20:40:10 +000078
79/*
drh733bf1b2009-04-22 00:47:00 +000080** Each entry in a RowSet is an instance of the following object.
drh3d4501e2008-12-04 20:40:10 +000081*/
82struct RowSetEntry {
83 i64 v; /* ROWID value for this entry */
drh733bf1b2009-04-22 00:47:00 +000084 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
85 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
drh3d4501e2008-12-04 20:40:10 +000086};
87
88/*
drh733bf1b2009-04-22 00:47:00 +000089** RowSetEntry objects are allocated in large chunks (instances of the
drh3d4501e2008-12-04 20:40:10 +000090** following structure) to reduce memory allocation overhead. The
91** chunks are kept on a linked list so that they can be deallocated
92** when the RowSet is destroyed.
93*/
94struct RowSetChunk {
drh733bf1b2009-04-22 00:47:00 +000095 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
drh3d4501e2008-12-04 20:40:10 +000096 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
97};
98
99/*
100** A RowSet in an instance of the following structure.
101**
102** A typedef of this structure if found in sqliteInt.h.
103*/
104struct RowSet {
105 struct RowSetChunk *pChunk; /* List of all chunk allocations */
106 sqlite3 *db; /* The database connection */
drh733bf1b2009-04-22 00:47:00 +0000107 struct RowSetEntry *pEntry; /* List of entries using pRight */
drh3d4501e2008-12-04 20:40:10 +0000108 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
109 struct RowSetEntry *pFresh; /* Source of new entry objects */
drh733bf1b2009-04-22 00:47:00 +0000110 struct RowSetEntry *pTree; /* Binary tree of entries */
drh3d4501e2008-12-04 20:40:10 +0000111 u16 nFresh; /* Number of objects on pFresh */
drh733bf1b2009-04-22 00:47:00 +0000112 u8 isSorted; /* True if pEntry is sorted */
113 u8 iBatch; /* Current insert batch */
drh3d4501e2008-12-04 20:40:10 +0000114};
115
116/*
117** Turn bulk memory into a RowSet object. N bytes of memory
118** are available at pSpace. The db pointer is used as a memory context
119** for any subsequent allocations that need to occur.
120** Return a pointer to the new RowSet object.
121**
drhe2f02ba2009-01-09 01:12:27 +0000122** It must be the case that N is sufficient to make a Rowset. If not
123** an assertion fault occurs.
124**
125** If N is larger than the minimum, use the surplus as an initial
126** allocation of entries available to be filled.
drh3d4501e2008-12-04 20:40:10 +0000127*/
128RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
129 RowSet *p;
drhe2f02ba2009-01-09 01:12:27 +0000130 assert( N >= sizeof(*p) );
131 p = pSpace;
132 p->pChunk = 0;
133 p->db = db;
134 p->pEntry = 0;
135 p->pLast = 0;
drh733bf1b2009-04-22 00:47:00 +0000136 p->pTree = 0;
drhe2f02ba2009-01-09 01:12:27 +0000137 p->pFresh = (struct RowSetEntry*)&p[1];
138 p->nFresh = (u16)((N - sizeof(*p))/sizeof(struct RowSetEntry));
139 p->isSorted = 1;
drh733bf1b2009-04-22 00:47:00 +0000140 p->iBatch = 0;
drh3d4501e2008-12-04 20:40:10 +0000141 return p;
142}
143
144/*
drh733bf1b2009-04-22 00:47:00 +0000145** Deallocate all chunks from a RowSet. This frees all memory that
146** the RowSet has allocated over its lifetime. This routine is
147** the destructor for the RowSet.
drh3d4501e2008-12-04 20:40:10 +0000148*/
149void sqlite3RowSetClear(RowSet *p){
150 struct RowSetChunk *pChunk, *pNextChunk;
151 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
drh733bf1b2009-04-22 00:47:00 +0000152 pNextChunk = pChunk->pNextChunk;
drh3d4501e2008-12-04 20:40:10 +0000153 sqlite3DbFree(p->db, pChunk);
154 }
155 p->pChunk = 0;
156 p->nFresh = 0;
157 p->pEntry = 0;
158 p->pLast = 0;
drh733bf1b2009-04-22 00:47:00 +0000159 p->pTree = 0;
drh3d4501e2008-12-04 20:40:10 +0000160 p->isSorted = 1;
161}
162
163/*
164** Insert a new value into a RowSet.
165**
166** The mallocFailed flag of the database connection is set if a
167** memory allocation fails.
168*/
169void sqlite3RowSetInsert(RowSet *p, i64 rowid){
drh733bf1b2009-04-22 00:47:00 +0000170 struct RowSetEntry *pEntry; /* The new entry */
171 struct RowSetEntry *pLast; /* The last prior entry */
drh4b2b8b72009-04-01 19:35:55 +0000172 assert( p!=0 );
drh3d4501e2008-12-04 20:40:10 +0000173 if( p->nFresh==0 ){
174 struct RowSetChunk *pNew;
175 pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
176 if( pNew==0 ){
177 return;
178 }
drh733bf1b2009-04-22 00:47:00 +0000179 pNew->pNextChunk = p->pChunk;
drh3d4501e2008-12-04 20:40:10 +0000180 p->pChunk = pNew;
181 p->pFresh = pNew->aEntry;
182 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
183 }
184 pEntry = p->pFresh++;
185 p->nFresh--;
186 pEntry->v = rowid;
drh733bf1b2009-04-22 00:47:00 +0000187 pEntry->pRight = 0;
drh3d4501e2008-12-04 20:40:10 +0000188 pLast = p->pLast;
189 if( pLast ){
190 if( p->isSorted && rowid<=pLast->v ){
191 p->isSorted = 0;
192 }
drh733bf1b2009-04-22 00:47:00 +0000193 pLast->pRight = pEntry;
drh3d4501e2008-12-04 20:40:10 +0000194 }else{
drh733bf1b2009-04-22 00:47:00 +0000195 assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
drh3d4501e2008-12-04 20:40:10 +0000196 p->pEntry = pEntry;
197 }
198 p->pLast = pEntry;
199}
200
201/*
drh733bf1b2009-04-22 00:47:00 +0000202** Merge two lists of RowSetEntry objects. Remove duplicates.
drh3d4501e2008-12-04 20:40:10 +0000203**
drh733bf1b2009-04-22 00:47:00 +0000204** The input lists are connected via pRight pointers and are
205** assumed to each already be in sorted order.
drh3d4501e2008-12-04 20:40:10 +0000206*/
drh733bf1b2009-04-22 00:47:00 +0000207static struct RowSetEntry *rowSetMerge(
drh3d4501e2008-12-04 20:40:10 +0000208 struct RowSetEntry *pA, /* First sorted list to be merged */
209 struct RowSetEntry *pB /* Second sorted list to be merged */
210){
211 struct RowSetEntry head;
212 struct RowSetEntry *pTail;
213
214 pTail = &head;
215 while( pA && pB ){
drh733bf1b2009-04-22 00:47:00 +0000216 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
217 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
drh3d4501e2008-12-04 20:40:10 +0000218 if( pA->v<pB->v ){
drh733bf1b2009-04-22 00:47:00 +0000219 pTail->pRight = pA;
220 pA = pA->pRight;
221 pTail = pTail->pRight;
drh3d4501e2008-12-04 20:40:10 +0000222 }else if( pB->v<pA->v ){
drh733bf1b2009-04-22 00:47:00 +0000223 pTail->pRight = pB;
224 pB = pB->pRight;
225 pTail = pTail->pRight;
drh3d4501e2008-12-04 20:40:10 +0000226 }else{
drh733bf1b2009-04-22 00:47:00 +0000227 pA = pA->pRight;
drh3d4501e2008-12-04 20:40:10 +0000228 }
229 }
230 if( pA ){
drh733bf1b2009-04-22 00:47:00 +0000231 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
232 pTail->pRight = pA;
drh3d4501e2008-12-04 20:40:10 +0000233 }else{
drh733bf1b2009-04-22 00:47:00 +0000234 assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
235 pTail->pRight = pB;
drh3d4501e2008-12-04 20:40:10 +0000236 }
drh733bf1b2009-04-22 00:47:00 +0000237 return head.pRight;
drh3d4501e2008-12-04 20:40:10 +0000238}
239
240/*
drh733bf1b2009-04-22 00:47:00 +0000241** Sort all elements on the pEntry list of the RowSet into ascending order.
drh3d4501e2008-12-04 20:40:10 +0000242*/
drh733bf1b2009-04-22 00:47:00 +0000243static void rowSetSort(RowSet *p){
drh3d4501e2008-12-04 20:40:10 +0000244 unsigned int i;
245 struct RowSetEntry *pEntry;
246 struct RowSetEntry *aBucket[40];
247
248 assert( p->isSorted==0 );
249 memset(aBucket, 0, sizeof(aBucket));
250 while( p->pEntry ){
251 pEntry = p->pEntry;
drh733bf1b2009-04-22 00:47:00 +0000252 p->pEntry = pEntry->pRight;
253 pEntry->pRight = 0;
drh3d4501e2008-12-04 20:40:10 +0000254 for(i=0; aBucket[i]; i++){
drh733bf1b2009-04-22 00:47:00 +0000255 pEntry = rowSetMerge(aBucket[i], pEntry);
drh3d4501e2008-12-04 20:40:10 +0000256 aBucket[i] = 0;
257 }
258 aBucket[i] = pEntry;
259 }
260 pEntry = 0;
261 for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
drh733bf1b2009-04-22 00:47:00 +0000262 pEntry = rowSetMerge(pEntry, aBucket[i]);
drh3d4501e2008-12-04 20:40:10 +0000263 }
264 p->pEntry = pEntry;
265 p->pLast = 0;
266 p->isSorted = 1;
267}
268
drh733bf1b2009-04-22 00:47:00 +0000269
drh3d4501e2008-12-04 20:40:10 +0000270/*
drh733bf1b2009-04-22 00:47:00 +0000271** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
272** Convert this tree into a linked list connected by the pRight pointers
273** and return pointers to the first and last elements of the new list.
274*/
275static void rowSetTreeToList(
276 struct RowSetEntry *pIn, /* Root of the input tree */
277 struct RowSetEntry **ppFirst, /* Write head of the output list here */
278 struct RowSetEntry **ppLast /* Write tail of the output list here */
279){
280 if( pIn==0 ){
281 *ppFirst = *ppLast = 0;
282 }
283 if( pIn->pLeft ){
284 struct RowSetEntry *p;
285 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
286 p->pRight = pIn;
287 }else{
288 *ppFirst = pIn;
289 }
290 if( pIn->pRight ){
291 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
292 }else{
293 *ppLast = pIn;
294 }
295 assert( (*ppLast)->pRight==0 );
296}
297
298
299/*
300** Convert a sorted list of elements (connected by pRight) into a binary
301** tree with depth of iDepth. A depth of 1 means the tree contains a single
302** node taken from the head of *ppList. A depth of 2 means a tree with
303** three nodes. And so forth.
304**
305** Use as many entries from the input list as required and update the
306** *ppList to point to the unused elements of the list. If the input
307** list contains too few elements, then construct an incomplete tree
308** and leave *ppList set to NULL.
309**
310** Return a pointer to the root of the constructed binary tree.
311*/
312static struct RowSetEntry *rowSetNDeepTree(
313 struct RowSetEntry **ppList,
314 int iDepth
315){
316 struct RowSetEntry *p; /* Root of the new tree */
317 struct RowSetEntry *pLeft; /* Left subtree */
318 if( *ppList==0 ){
319 return 0;
320 }
321 if( iDepth==1 ){
322 p = *ppList;
323 *ppList = p->pRight;
324 p->pLeft = p->pRight = 0;
325 return p;
326 }
327 pLeft = rowSetNDeepTree(ppList, iDepth-1);
328 p = *ppList;
329 if( p==0 ){
330 return pLeft;
331 }
332 p->pLeft = pLeft;
333 *ppList = p->pRight;
334 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
335 return p;
336}
337
338/*
339** Convert a sorted list of elements into a binary tree. Make the tree
340** as deep as it needs to be in order to contain the entire list.
341*/
342static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
343 int iDepth; /* Depth of the tree so far */
344 struct RowSetEntry *p; /* Current tree root */
345 struct RowSetEntry *pLeft; /* Left subtree */
346
347 if( pList==0 ){
348 return 0;
349 }
350 p = pList;
351 pList = p->pRight;
352 p->pLeft = p->pRight = 0;
353 for(iDepth=1; pList; iDepth++){
354 pLeft = p;
355 p = pList;
356 pList = p->pRight;
357 p->pLeft = pLeft;
358 p->pRight = rowSetNDeepTree(&pList, iDepth);
359 }
360 return p;
361}
362
363/*
364** Convert the list in p->pEntry into a sorted list if it is not
365** sorted already. If there is a binary tree on p->pTree, then
366** convert it into a list too and merge it into the p->pEntry list.
367*/
368static void rowSetToList(RowSet *p){
369 if( !p->isSorted ){
370 rowSetSort(p);
371 }
372 if( p->pTree ){
373 struct RowSetEntry *pHead, *pTail;
374 rowSetTreeToList(p->pTree, &pHead, &pTail);
375 p->pTree = 0;
376 p->pEntry = rowSetMerge(p->pEntry, pHead);
377 }
378}
379
380/*
381** Extract the smallest element from the RowSet.
drh3d4501e2008-12-04 20:40:10 +0000382** Write the element into *pRowid. Return 1 on success. Return
383** 0 if the RowSet is already empty.
drh733bf1b2009-04-22 00:47:00 +0000384**
385** After this routine has been called, the sqlite3RowSetInsert()
386** routine may not be called again.
drh3d4501e2008-12-04 20:40:10 +0000387*/
388int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
drh733bf1b2009-04-22 00:47:00 +0000389 rowSetToList(p);
drh3d4501e2008-12-04 20:40:10 +0000390 if( p->pEntry ){
391 *pRowid = p->pEntry->v;
drh733bf1b2009-04-22 00:47:00 +0000392 p->pEntry = p->pEntry->pRight;
drh3d4501e2008-12-04 20:40:10 +0000393 if( p->pEntry==0 ){
394 sqlite3RowSetClear(p);
395 }
396 return 1;
397 }else{
398 return 0;
399 }
400}
drh733bf1b2009-04-22 00:47:00 +0000401
402/*
403** Check to see if element iRowid was inserted into the the rowset as
404** part of any insert batch prior to iBatch. Return 1 or 0.
405*/
406int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
407 struct RowSetEntry *p;
408 if( iBatch!=pRowSet->iBatch ){
409 if( pRowSet->pEntry ){
410 rowSetToList(pRowSet);
411 pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
412 pRowSet->pEntry = 0;
413 pRowSet->pLast = 0;
414 }
415 pRowSet->iBatch = iBatch;
416 }
417 p = pRowSet->pTree;
418 while( p ){
419 if( p->v<iRowid ){
420 p = p->pRight;
421 }else if( p->v>iRowid ){
422 p = p->pLeft;
423 }else{
424 return 1;
425 }
426 }
427 return 0;
428}