annotate sqlite/vdbefifo.c @ 1652:62c5bff8a05b trunk

[svn] - more hammering
author nenolod
date Thu, 07 Sep 2006 22:00:35 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2005 June 16
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 *************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 ** This file implements a FIFO queue of rowids used for processing
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 ** UPDATE and DELETE statements.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 #include "vdbeInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 ** Allocate a new FifoPage and return a pointer to it. Return NULL if
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 ** we run out of memory. Leave space on the page for nEntry entries.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 static FifoPage *allocatePage(int nEntry){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 FifoPage *pPage;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 if( nEntry>32767 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 nEntry = 32767;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 pPage = sqliteMallocRaw( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 if( pPage ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 pPage->nSlot = nEntry;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 pPage->iWrite = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 pPage->iRead = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 pPage->pNext = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 return pPage;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 ** Initialize a Fifo structure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 void sqlite3VdbeFifoInit(Fifo *pFifo){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 memset(pFifo, 0, sizeof(*pFifo));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 ** Push a single 64-bit integer value into the Fifo. Return SQLITE_OK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 ** normally. SQLITE_NOMEM is returned if we are unable to allocate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 ** memory.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 int sqlite3VdbeFifoPush(Fifo *pFifo, i64 val){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 FifoPage *pPage;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 pPage = pFifo->pLast;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 if( pPage==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 pPage = pFifo->pLast = pFifo->pFirst = allocatePage(20);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 if( pPage==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 }else if( pPage->iWrite>=pPage->nSlot ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 pPage->pNext = allocatePage(pFifo->nEntry);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 if( pPage->pNext==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 pPage = pFifo->pLast = pPage->pNext;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 pPage->aSlot[pPage->iWrite++] = val;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 pFifo->nEntry++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 ** Extract a single 64-bit integer value from the Fifo. The integer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 ** extracted is the one least recently inserted. If the Fifo is empty
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 ** return SQLITE_DONE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 int sqlite3VdbeFifoPop(Fifo *pFifo, i64 *pVal){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 FifoPage *pPage;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 if( pFifo->nEntry==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 return SQLITE_DONE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 assert( pFifo->nEntry>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 pPage = pFifo->pFirst;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 assert( pPage!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 assert( pPage->iWrite>pPage->iRead );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 assert( pPage->iWrite<=pPage->nSlot );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 assert( pPage->iRead<pPage->nSlot );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 assert( pPage->iRead>=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 *pVal = pPage->aSlot[pPage->iRead++];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 pFifo->nEntry--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 if( pPage->iRead>=pPage->iWrite ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 pFifo->pFirst = pPage->pNext;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 sqliteFree(pPage);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 if( pFifo->nEntry==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 assert( pFifo->pLast==pPage );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 pFifo->pLast = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 assert( pFifo->pFirst!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 assert( pFifo->nEntry>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 ** Delete all information from a Fifo object. Free all memory held
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 ** by the Fifo.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 void sqlite3VdbeFifoClear(Fifo *pFifo){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 FifoPage *pPage, *pNextPage;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 for(pPage=pFifo->pFirst; pPage; pPage=pNextPage){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 pNextPage = pPage->pNext;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 sqliteFree(pPage);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 sqlite3VdbeFifoInit(pFifo);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 }