blob: 976d2ce82f90b1cbc27ab4503ea7d2414cde36b6 [file] [log] [blame]
drhf5e7bb52008-02-18 14:47:33 +00001/*
2** 2008 February 16
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** This file implements an object that represents a fixed-length
13** bitmap. Bits are numbered starting with 1.
14**
drhdfe88ec2008-11-03 20:55:06 +000015** A bitmap is used to record which pages of a database file have been
16** journalled during a transaction, or which pages have the "dont-write"
17** property. Usually only a few pages are meet either condition.
18** So the bitmap is usually sparse and has low cardinality.
drhf5e7bb52008-02-18 14:47:33 +000019** But sometimes (for example when during a DROP of a large table) most
drhdfe88ec2008-11-03 20:55:06 +000020** or all of the pages in a database can get journalled. In those cases,
21** the bitmap becomes dense with high cardinality. The algorithm needs
22** to handle both cases well.
drhf5e7bb52008-02-18 14:47:33 +000023**
24** The size of the bitmap is fixed when the object is created.
25**
26** All bits are clear when the bitmap is created. Individual bits
27** may be set or cleared one at a time.
28**
29** Test operations are about 100 times more common that set operations.
30** Clear operations are exceedingly rare. There are usually between
31** 5 and 500 set operations per Bitvec object, though the number of sets can
32** sometimes grow into tens of thousands or larger. The size of the
33** Bitvec object is the number of pages in the database file at the
34** start of a transaction, and is thus usually less than a few thousand,
35** but can be as large as 2 billion for a really big database.
36**
drhdfe88ec2008-11-03 20:55:06 +000037** @(#) $Id: bitvec.c,v 1.7 2008/11/03 20:55:07 drh Exp $
drhf5e7bb52008-02-18 14:47:33 +000038*/
39#include "sqliteInt.h"
40
41#define BITVEC_SZ 512
mlcreechdda5b682008-03-14 13:02:08 +000042/* Round the union size down to the nearest pointer boundary, since that's how
43** it will be aligned within the Bitvec struct. */
drh3088d592008-03-21 16:45:47 +000044#define BITVEC_USIZE (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*))
mlcreechdda5b682008-03-14 13:02:08 +000045#define BITVEC_NCHAR BITVEC_USIZE
drhf5e7bb52008-02-18 14:47:33 +000046#define BITVEC_NBIT (BITVEC_NCHAR*8)
mlcreechdda5b682008-03-14 13:02:08 +000047#define BITVEC_NINT (BITVEC_USIZE/4)
drhf5e7bb52008-02-18 14:47:33 +000048#define BITVEC_MXHASH (BITVEC_NINT/2)
mlcreechdda5b682008-03-14 13:02:08 +000049#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
drhf5e7bb52008-02-18 14:47:33 +000050
51#define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT)
52
53/*
54** A bitmap is an instance of the following structure.
55**
56** This bitmap records the existance of zero or more bits
57** with values between 1 and iSize, inclusive.
58**
59** There are three possible representations of the bitmap.
60** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
61** bitmap. The least significant bit is bit 1.
62**
63** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
64** a hash table that will hold up to BITVEC_MXHASH distinct values.
65**
66** Otherwise, the value i is redirected into one of BITVEC_NPTR
67** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
68** handles up to iDivisor separate values of i. apSub[0] holds
69** values between 1 and iDivisor. apSub[1] holds values between
70** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
71** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
72** to hold deal with values between 1 and iDivisor.
73*/
74struct Bitvec {
75 u32 iSize; /* Maximum bit index */
76 u32 nSet; /* Number of bits that are set */
77 u32 iDivisor; /* Number of bits handled by each apSub[] entry */
78 union {
79 u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */
80 u32 aHash[BITVEC_NINT]; /* Hash table representation */
81 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
82 } u;
83};
84
85/*
86** Create a new bitmap object able to handle bits between 0 and iSize,
87** inclusive. Return a pointer to the new object. Return NULL if
88** malloc fails.
89*/
90Bitvec *sqlite3BitvecCreate(u32 iSize){
91 Bitvec *p;
92 assert( sizeof(*p)==BITVEC_SZ );
93 p = sqlite3MallocZero( sizeof(*p) );
94 if( p ){
95 p->iSize = iSize;
96 }
97 return p;
98}
99
100/*
101** Check to see if the i-th bit is set. Return true or false.
102** If p is NULL (if the bitmap has not been created) or if
103** i is out of range, then return false.
104*/
105int sqlite3BitvecTest(Bitvec *p, u32 i){
drhf5e7bb52008-02-18 14:47:33 +0000106 if( p==0 ) return 0;
drh3088d592008-03-21 16:45:47 +0000107 if( i>p->iSize || i==0 ) return 0;
drhf5e7bb52008-02-18 14:47:33 +0000108 if( p->iSize<=BITVEC_NBIT ){
109 i--;
110 return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0;
111 }
112 if( p->iDivisor>0 ){
113 u32 bin = (i-1)/p->iDivisor;
114 i = (i-1)%p->iDivisor + 1;
115 return sqlite3BitvecTest(p->u.apSub[bin], i);
116 }else{
117 u32 h = BITVEC_HASH(i);
118 while( p->u.aHash[h] ){
119 if( p->u.aHash[h]==i ) return 1;
120 h++;
121 if( h>=BITVEC_NINT ) h = 0;
122 }
123 return 0;
124 }
125}
126
127/*
128** Set the i-th bit. Return 0 on success and an error code if
129** anything goes wrong.
drhdfe88ec2008-11-03 20:55:06 +0000130**
131** This routine might cause sub-bitmaps to be allocated. Failing
132** to get the memory needed to hold the sub-bitmap is the only
133** that can go wrong with an insert, assuming p and i are valid.
134**
135** The calling function must ensure that p is a valid Bitvec object
136** and that the value for "i" is within range of the Bitvec object.
137** Otherwise the behavior is undefined.
drhf5e7bb52008-02-18 14:47:33 +0000138*/
139int sqlite3BitvecSet(Bitvec *p, u32 i){
140 u32 h;
141 assert( p!=0 );
drh3088d592008-03-21 16:45:47 +0000142 assert( i>0 );
drhc5d0bd92008-04-14 01:00:57 +0000143 assert( i<=p->iSize );
drhf5e7bb52008-02-18 14:47:33 +0000144 if( p->iSize<=BITVEC_NBIT ){
145 i--;
146 p->u.aBitmap[i/8] |= 1 << (i&7);
147 return SQLITE_OK;
148 }
149 if( p->iDivisor ){
150 u32 bin = (i-1)/p->iDivisor;
151 i = (i-1)%p->iDivisor + 1;
152 if( p->u.apSub[bin]==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000153 sqlite3BeginBenignMalloc();
drhf5e7bb52008-02-18 14:47:33 +0000154 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
danielk19772d1d86f2008-06-20 14:59:51 +0000155 sqlite3EndBenignMalloc();
drhf5e7bb52008-02-18 14:47:33 +0000156 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
157 }
158 return sqlite3BitvecSet(p->u.apSub[bin], i);
159 }
160 h = BITVEC_HASH(i);
161 while( p->u.aHash[h] ){
162 if( p->u.aHash[h]==i ) return SQLITE_OK;
163 h++;
164 if( h==BITVEC_NINT ) h = 0;
165 }
166 p->nSet++;
167 if( p->nSet>=BITVEC_MXHASH ){
168 int j, rc;
169 u32 aiValues[BITVEC_NINT];
170 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
171 memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR);
172 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
drh3088d592008-03-21 16:45:47 +0000173 rc = sqlite3BitvecSet(p, i);
174 for(j=0; j<BITVEC_NINT; j++){
drhf5e7bb52008-02-18 14:47:33 +0000175 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
176 }
177 return rc;
178 }
179 p->u.aHash[h] = i;
180 return SQLITE_OK;
181}
182
183/*
184** Clear the i-th bit. Return 0 on success and an error code if
185** anything goes wrong.
186*/
187void sqlite3BitvecClear(Bitvec *p, u32 i){
188 assert( p!=0 );
drh3088d592008-03-21 16:45:47 +0000189 assert( i>0 );
drhf5e7bb52008-02-18 14:47:33 +0000190 if( p->iSize<=BITVEC_NBIT ){
191 i--;
192 p->u.aBitmap[i/8] &= ~(1 << (i&7));
193 }else if( p->iDivisor ){
194 u32 bin = (i-1)/p->iDivisor;
195 i = (i-1)%p->iDivisor + 1;
196 if( p->u.apSub[bin] ){
197 sqlite3BitvecClear(p->u.apSub[bin], i);
198 }
199 }else{
200 int j;
201 u32 aiValues[BITVEC_NINT];
202 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
203 memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT);
204 p->nSet = 0;
205 for(j=0; j<BITVEC_NINT; j++){
drh3088d592008-03-21 16:45:47 +0000206 if( aiValues[j] && aiValues[j]!=i ){
207 sqlite3BitvecSet(p, aiValues[j]);
208 }
drhf5e7bb52008-02-18 14:47:33 +0000209 }
210 }
211}
212
213/*
214** Destroy a bitmap object. Reclaim all memory used.
215*/
216void sqlite3BitvecDestroy(Bitvec *p){
217 if( p==0 ) return;
218 if( p->iDivisor ){
219 int i;
220 for(i=0; i<BITVEC_NPTR; i++){
221 sqlite3BitvecDestroy(p->u.apSub[i]);
222 }
223 }
224 sqlite3_free(p);
225}
drh3088d592008-03-21 16:45:47 +0000226
227#ifndef SQLITE_OMIT_BUILTIN_TEST
228/*
229** Let V[] be an array of unsigned characters sufficient to hold
230** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
231** Then the following macros can be used to set, clear, or test
232** individual bits within V.
233*/
234#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
235#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
236#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
237
238/*
239** This routine runs an extensive test of the Bitvec code.
240**
241** The input is an array of integers that acts as a program
242** to test the Bitvec. The integers are opcodes followed
243** by 0, 1, or 3 operands, depending on the opcode. Another
244** opcode follows immediately after the last operand.
245**
246** There are 6 opcodes numbered from 0 through 5. 0 is the
247** "halt" opcode and causes the test to end.
248**
249** 0 Halt and return the number of errors
250** 1 N S X Set N bits beginning with S and incrementing by X
251** 2 N S X Clear N bits beginning with S and incrementing by X
252** 3 N Set N randomly chosen bits
253** 4 N Clear N randomly chosen bits
254** 5 N S X Set N bits from S increment X in array only, not in bitvec
255**
256** The opcodes 1 through 4 perform set and clear operations are performed
257** on both a Bitvec object and on a linear array of bits obtained from malloc.
258** Opcode 5 works on the linear array only, not on the Bitvec.
259** Opcode 5 is used to deliberately induce a fault in order to
260** confirm that error detection works.
261**
262** At the conclusion of the test the linear array is compared
263** against the Bitvec object. If there are any differences,
264** an error is returned. If they are the same, zero is returned.
265**
266** If a memory allocation error occurs, return -1.
267*/
268int sqlite3BitvecBuiltinTest(int sz, int *aOp){
269 Bitvec *pBitvec = 0;
270 unsigned char *pV = 0;
271 int rc = -1;
272 int i, nx, pc, op;
273
274 /* Allocate the Bitvec to be tested and a linear array of
275 ** bits to act as the reference */
276 pBitvec = sqlite3BitvecCreate( sz );
277 pV = sqlite3_malloc( (sz+7)/8 + 1 );
278 if( pBitvec==0 || pV==0 ) goto bitvec_end;
279 memset(pV, 0, (sz+7)/8 + 1);
280
281 /* Run the program */
282 pc = 0;
283 while( (op = aOp[pc])!=0 ){
284 switch( op ){
285 case 1:
286 case 2:
287 case 5: {
288 nx = 4;
289 i = aOp[pc+2] - 1;
290 aOp[pc+2] += aOp[pc+3];
291 break;
292 }
293 case 3:
294 case 4:
295 default: {
296 nx = 2;
297 sqlite3_randomness(sizeof(i), &i);
298 break;
299 }
300 }
301 if( (--aOp[pc+1]) > 0 ) nx = 0;
302 pc += nx;
303 i = (i & 0x7fffffff)%sz;
304 if( (op & 1)!=0 ){
305 SETBIT(pV, (i+1));
306 if( op!=5 ){
307 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
308 }
309 }else{
310 CLEARBIT(pV, (i+1));
311 sqlite3BitvecClear(pBitvec, i+1);
312 }
313 }
314
315 /* Test to make sure the linear array exactly matches the
316 ** Bitvec object. Start with the assumption that they do
317 ** match (rc==0). Change rc to non-zero if a discrepancy
318 ** is found.
319 */
320 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
321 + sqlite3BitvecTest(pBitvec, 0);
322 for(i=1; i<=sz; i++){
323 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
324 rc = i;
325 break;
326 }
327 }
328
329 /* Free allocated structure */
330bitvec_end:
331 sqlite3_free(pV);
332 sqlite3BitvecDestroy(pBitvec);
333 return rc;
334}
335#endif /* SQLITE_OMIT_BUILTIN_TEST */