blob: 3a28a8004fb8d2e656c03de7fc72b08369b068b4 [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**
drh1feb7dd2008-11-19 18:30:29 +000037** @(#) $Id: bitvec.c,v 1.9 2008/11/19 18:30:35 shane Exp $
drhf5e7bb52008-02-18 14:47:33 +000038*/
39#include "sqliteInt.h"
40
drh1feb7dd2008-11-19 18:30:29 +000041/* Size of the Bitvec structure in bytes. */
drhf5e7bb52008-02-18 14:47:33 +000042#define BITVEC_SZ 512
drh1feb7dd2008-11-19 18:30:29 +000043
mlcreechdda5b682008-03-14 13:02:08 +000044/* Round the union size down to the nearest pointer boundary, since that's how
45** it will be aligned within the Bitvec struct. */
drh1feb7dd2008-11-19 18:30:29 +000046#define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
47
48/* Type of the array "element" for the bitmap representation.
49** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
50** Setting this to the "natural word" size of your CPU may improve
51** performance. */
52#define BITVEC_TELEM u8
53/* Size, in bits, of the bitmap element. */
54#define BITVEC_SZELEM 8
55/* Number of elements in a bitmap array. */
56#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
57/* Number of bits in the bitmap array. */
58#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
59
60/* Number of u32 values in hash table. */
61#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
62/* Maximum number of entries in hash table before
63** sub-dividing and re-hashing. */
drhf5e7bb52008-02-18 14:47:33 +000064#define BITVEC_MXHASH (BITVEC_NINT/2)
drh1feb7dd2008-11-19 18:30:29 +000065/* Hashing function for the aHash representation.
66** Empirical testing showed that the *37 multiplier
67** (an arbitrary prime)in the hash function provided
68** no fewer collisions than the no-op *1. */
69#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
70
mlcreechdda5b682008-03-14 13:02:08 +000071#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
drhf5e7bb52008-02-18 14:47:33 +000072
drhf5e7bb52008-02-18 14:47:33 +000073
74/*
75** A bitmap is an instance of the following structure.
76**
77** This bitmap records the existance of zero or more bits
78** with values between 1 and iSize, inclusive.
79**
80** There are three possible representations of the bitmap.
81** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
82** bitmap. The least significant bit is bit 1.
83**
84** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
85** a hash table that will hold up to BITVEC_MXHASH distinct values.
86**
87** Otherwise, the value i is redirected into one of BITVEC_NPTR
88** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
89** handles up to iDivisor separate values of i. apSub[0] holds
90** values between 1 and iDivisor. apSub[1] holds values between
91** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
92** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
93** to hold deal with values between 1 and iDivisor.
94*/
95struct Bitvec {
drh1feb7dd2008-11-19 18:30:29 +000096 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
97 u32 nSet; /* Number of bits that are set - only valid for aHash element */
98 /* Max nSet is BITVEC_NINT. For BITVEC_SZ of 512, this would be 125. */
99 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
100 /* Should >=0 for apSub element. */
101 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
102 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
drhf5e7bb52008-02-18 14:47:33 +0000103 union {
drh1feb7dd2008-11-19 18:30:29 +0000104 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
drhf5e7bb52008-02-18 14:47:33 +0000105 u32 aHash[BITVEC_NINT]; /* Hash table representation */
106 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
107 } u;
108};
109
110/*
111** Create a new bitmap object able to handle bits between 0 and iSize,
112** inclusive. Return a pointer to the new object. Return NULL if
113** malloc fails.
114*/
115Bitvec *sqlite3BitvecCreate(u32 iSize){
116 Bitvec *p;
117 assert( sizeof(*p)==BITVEC_SZ );
118 p = sqlite3MallocZero( sizeof(*p) );
119 if( p ){
120 p->iSize = iSize;
121 }
122 return p;
123}
124
125/*
126** Check to see if the i-th bit is set. Return true or false.
127** If p is NULL (if the bitmap has not been created) or if
128** i is out of range, then return false.
129*/
130int sqlite3BitvecTest(Bitvec *p, u32 i){
drhf5e7bb52008-02-18 14:47:33 +0000131 if( p==0 ) return 0;
drh3088d592008-03-21 16:45:47 +0000132 if( i>p->iSize || i==0 ) return 0;
drh1feb7dd2008-11-19 18:30:29 +0000133 i--;
134 while( p->iDivisor ){
135 u32 bin = i/p->iDivisor;
136 i = i%p->iDivisor;
137 p = p->u.apSub[bin];
138 if (!p) {
139 return 0;
140 }
drhf5e7bb52008-02-18 14:47:33 +0000141 }
drh1feb7dd2008-11-19 18:30:29 +0000142 if( p->iSize<=BITVEC_NBIT ){
143 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
144 } else{
145 u32 h = BITVEC_HASH(i++);
drhf5e7bb52008-02-18 14:47:33 +0000146 while( p->u.aHash[h] ){
147 if( p->u.aHash[h]==i ) return 1;
148 h++;
149 if( h>=BITVEC_NINT ) h = 0;
150 }
151 return 0;
152 }
153}
154
155/*
156** Set the i-th bit. Return 0 on success and an error code if
157** anything goes wrong.
drhdfe88ec2008-11-03 20:55:06 +0000158**
159** This routine might cause sub-bitmaps to be allocated. Failing
160** to get the memory needed to hold the sub-bitmap is the only
161** that can go wrong with an insert, assuming p and i are valid.
162**
163** The calling function must ensure that p is a valid Bitvec object
164** and that the value for "i" is within range of the Bitvec object.
165** Otherwise the behavior is undefined.
drhf5e7bb52008-02-18 14:47:33 +0000166*/
167int sqlite3BitvecSet(Bitvec *p, u32 i){
168 u32 h;
169 assert( p!=0 );
drh3088d592008-03-21 16:45:47 +0000170 assert( i>0 );
drhc5d0bd92008-04-14 01:00:57 +0000171 assert( i<=p->iSize );
drh1feb7dd2008-11-19 18:30:29 +0000172 i--;
173 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
174 u32 bin = i/p->iDivisor;
175 i = i%p->iDivisor;
drhf5e7bb52008-02-18 14:47:33 +0000176 if( p->u.apSub[bin]==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000177 sqlite3BeginBenignMalloc();
drhf5e7bb52008-02-18 14:47:33 +0000178 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
danielk19772d1d86f2008-06-20 14:59:51 +0000179 sqlite3EndBenignMalloc();
drhf5e7bb52008-02-18 14:47:33 +0000180 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
181 }
drh1feb7dd2008-11-19 18:30:29 +0000182 p = p->u.apSub[bin];
drhf5e7bb52008-02-18 14:47:33 +0000183 }
drh1feb7dd2008-11-19 18:30:29 +0000184 if( p->iSize<=BITVEC_NBIT ){
185 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
186 return SQLITE_OK;
187 }
188 h = BITVEC_HASH(i++);
189 /* if there wasn't a hash collision, and this doesn't */
190 /* completely fill the hash, then just add it without */
191 /* worring about sub-dividing and re-hashing. */
192 if( !p->u.aHash[h] ){
193 if (p->nSet<(BITVEC_NINT-1)) {
194 goto bitvec_set_end;
195 } else {
196 goto bitvec_set_rehash;
197 }
198 }
199 /* there was a collision, check to see if it's already */
200 /* in hash, if not, try to find a spot for it */
201 do {
drhf5e7bb52008-02-18 14:47:33 +0000202 if( p->u.aHash[h]==i ) return SQLITE_OK;
203 h++;
drh1feb7dd2008-11-19 18:30:29 +0000204 if( h>=BITVEC_NINT ) h = 0;
205 } while( p->u.aHash[h] );
206 /* we didn't find it in the hash. h points to the first */
207 /* available free spot. check to see if this is going to */
208 /* make our hash too "full". */
209bitvec_set_rehash:
drhf5e7bb52008-02-18 14:47:33 +0000210 if( p->nSet>=BITVEC_MXHASH ){
drh86a7a692008-11-11 15:48:48 +0000211 unsigned int j;
212 int rc;
drhf5e7bb52008-02-18 14:47:33 +0000213 u32 aiValues[BITVEC_NINT];
214 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
drh1feb7dd2008-11-19 18:30:29 +0000215 memset(p->u.apSub, 0, sizeof(aiValues));
drhf5e7bb52008-02-18 14:47:33 +0000216 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
drh3088d592008-03-21 16:45:47 +0000217 rc = sqlite3BitvecSet(p, i);
218 for(j=0; j<BITVEC_NINT; j++){
drhf5e7bb52008-02-18 14:47:33 +0000219 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
220 }
221 return rc;
222 }
drh1feb7dd2008-11-19 18:30:29 +0000223bitvec_set_end:
224 p->nSet++;
drhf5e7bb52008-02-18 14:47:33 +0000225 p->u.aHash[h] = i;
226 return SQLITE_OK;
227}
228
229/*
drh1feb7dd2008-11-19 18:30:29 +0000230** Clear the i-th bit.
drhf5e7bb52008-02-18 14:47:33 +0000231*/
232void sqlite3BitvecClear(Bitvec *p, u32 i){
233 assert( p!=0 );
drh3088d592008-03-21 16:45:47 +0000234 assert( i>0 );
drh1feb7dd2008-11-19 18:30:29 +0000235 i--;
236 while( p->iDivisor ){
237 u32 bin = i/p->iDivisor;
238 i = i%p->iDivisor;
239 p = p->u.apSub[bin];
240 if (!p) {
241 return;
drhf5e7bb52008-02-18 14:47:33 +0000242 }
drh1feb7dd2008-11-19 18:30:29 +0000243 }
244 if( p->iSize<=BITVEC_NBIT ){
245 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
drhf5e7bb52008-02-18 14:47:33 +0000246 }else{
drh86a7a692008-11-11 15:48:48 +0000247 unsigned int j;
drhf5e7bb52008-02-18 14:47:33 +0000248 u32 aiValues[BITVEC_NINT];
249 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
drh1feb7dd2008-11-19 18:30:29 +0000250 memset(p->u.aHash, 0, sizeof(aiValues));
drhf5e7bb52008-02-18 14:47:33 +0000251 p->nSet = 0;
252 for(j=0; j<BITVEC_NINT; j++){
drh1feb7dd2008-11-19 18:30:29 +0000253 if( aiValues[j] && aiValues[j]!=(i+1) ){
254 u32 h = BITVEC_HASH(aiValues[j]-1);
255 p->nSet++;
256 while( p->u.aHash[h] ){
257 h++;
258 if( h>=BITVEC_NINT ) h = 0;
259 }
260 p->u.aHash[h] = aiValues[j];
drh3088d592008-03-21 16:45:47 +0000261 }
drhf5e7bb52008-02-18 14:47:33 +0000262 }
263 }
264}
265
266/*
267** Destroy a bitmap object. Reclaim all memory used.
268*/
269void sqlite3BitvecDestroy(Bitvec *p){
270 if( p==0 ) return;
271 if( p->iDivisor ){
drh86a7a692008-11-11 15:48:48 +0000272 unsigned int i;
drhf5e7bb52008-02-18 14:47:33 +0000273 for(i=0; i<BITVEC_NPTR; i++){
274 sqlite3BitvecDestroy(p->u.apSub[i]);
275 }
276 }
277 sqlite3_free(p);
278}
drh3088d592008-03-21 16:45:47 +0000279
280#ifndef SQLITE_OMIT_BUILTIN_TEST
281/*
282** Let V[] be an array of unsigned characters sufficient to hold
283** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
284** Then the following macros can be used to set, clear, or test
285** individual bits within V.
286*/
287#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
288#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
289#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
290
291/*
292** This routine runs an extensive test of the Bitvec code.
293**
294** The input is an array of integers that acts as a program
295** to test the Bitvec. The integers are opcodes followed
296** by 0, 1, or 3 operands, depending on the opcode. Another
297** opcode follows immediately after the last operand.
298**
299** There are 6 opcodes numbered from 0 through 5. 0 is the
300** "halt" opcode and causes the test to end.
301**
302** 0 Halt and return the number of errors
303** 1 N S X Set N bits beginning with S and incrementing by X
304** 2 N S X Clear N bits beginning with S and incrementing by X
305** 3 N Set N randomly chosen bits
306** 4 N Clear N randomly chosen bits
307** 5 N S X Set N bits from S increment X in array only, not in bitvec
308**
309** The opcodes 1 through 4 perform set and clear operations are performed
310** on both a Bitvec object and on a linear array of bits obtained from malloc.
311** Opcode 5 works on the linear array only, not on the Bitvec.
312** Opcode 5 is used to deliberately induce a fault in order to
313** confirm that error detection works.
314**
315** At the conclusion of the test the linear array is compared
316** against the Bitvec object. If there are any differences,
317** an error is returned. If they are the same, zero is returned.
318**
319** If a memory allocation error occurs, return -1.
320*/
321int sqlite3BitvecBuiltinTest(int sz, int *aOp){
322 Bitvec *pBitvec = 0;
323 unsigned char *pV = 0;
324 int rc = -1;
325 int i, nx, pc, op;
326
327 /* Allocate the Bitvec to be tested and a linear array of
328 ** bits to act as the reference */
329 pBitvec = sqlite3BitvecCreate( sz );
330 pV = sqlite3_malloc( (sz+7)/8 + 1 );
331 if( pBitvec==0 || pV==0 ) goto bitvec_end;
332 memset(pV, 0, (sz+7)/8 + 1);
333
334 /* Run the program */
335 pc = 0;
336 while( (op = aOp[pc])!=0 ){
337 switch( op ){
338 case 1:
339 case 2:
340 case 5: {
341 nx = 4;
342 i = aOp[pc+2] - 1;
343 aOp[pc+2] += aOp[pc+3];
344 break;
345 }
346 case 3:
347 case 4:
348 default: {
349 nx = 2;
350 sqlite3_randomness(sizeof(i), &i);
351 break;
352 }
353 }
354 if( (--aOp[pc+1]) > 0 ) nx = 0;
355 pc += nx;
356 i = (i & 0x7fffffff)%sz;
357 if( (op & 1)!=0 ){
358 SETBIT(pV, (i+1));
359 if( op!=5 ){
360 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
361 }
362 }else{
363 CLEARBIT(pV, (i+1));
364 sqlite3BitvecClear(pBitvec, i+1);
365 }
366 }
367
368 /* Test to make sure the linear array exactly matches the
369 ** Bitvec object. Start with the assumption that they do
370 ** match (rc==0). Change rc to non-zero if a discrepancy
371 ** is found.
372 */
373 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
374 + sqlite3BitvecTest(pBitvec, 0);
375 for(i=1; i<=sz; i++){
376 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
377 rc = i;
378 break;
379 }
380 }
381
382 /* Free allocated structure */
383bitvec_end:
384 sqlite3_free(pV);
385 sqlite3BitvecDestroy(pBitvec);
386 return rc;
387}
388#endif /* SQLITE_OMIT_BUILTIN_TEST */