blob: fd908f791b3fe247f681bff377ba5d4045e60993 [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.
drhf5e7bb52008-02-18 14:47:33 +000036*/
37#include "sqliteInt.h"
38
drh1feb7dd2008-11-19 18:30:29 +000039/* Size of the Bitvec structure in bytes. */
drhf6171e92010-08-05 11:56:01 +000040#define BITVEC_SZ 512
drh1feb7dd2008-11-19 18:30:29 +000041
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. */
drh1feb7dd2008-11-19 18:30:29 +000044#define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
45
46/* Type of the array "element" for the bitmap representation.
47** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
48** Setting this to the "natural word" size of your CPU may improve
49** performance. */
50#define BITVEC_TELEM u8
51/* Size, in bits, of the bitmap element. */
52#define BITVEC_SZELEM 8
53/* Number of elements in a bitmap array. */
54#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
55/* Number of bits in the bitmap array. */
56#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
57
58/* Number of u32 values in hash table. */
59#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
60/* Maximum number of entries in hash table before
61** sub-dividing and re-hashing. */
drhf5e7bb52008-02-18 14:47:33 +000062#define BITVEC_MXHASH (BITVEC_NINT/2)
drh1feb7dd2008-11-19 18:30:29 +000063/* Hashing function for the aHash representation.
64** Empirical testing showed that the *37 multiplier
65** (an arbitrary prime)in the hash function provided
66** no fewer collisions than the no-op *1. */
67#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
68
mlcreechdda5b682008-03-14 13:02:08 +000069#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
drhf5e7bb52008-02-18 14:47:33 +000070
drhf5e7bb52008-02-18 14:47:33 +000071
72/*
73** A bitmap is an instance of the following structure.
74**
mistachkin48864df2013-03-21 21:20:32 +000075** This bitmap records the existence of zero or more bits
drhf5e7bb52008-02-18 14:47:33 +000076** with values between 1 and iSize, inclusive.
77**
78** There are three possible representations of the bitmap.
79** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
80** bitmap. The least significant bit is bit 1.
81**
82** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
83** a hash table that will hold up to BITVEC_MXHASH distinct values.
84**
85** Otherwise, the value i is redirected into one of BITVEC_NPTR
86** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
87** handles up to iDivisor separate values of i. apSub[0] holds
88** values between 1 and iDivisor. apSub[1] holds values between
89** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
90** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
91** to hold deal with values between 1 and iDivisor.
92*/
93struct Bitvec {
drh1feb7dd2008-11-19 18:30:29 +000094 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
drh64f798d2009-04-01 23:49:04 +000095 u32 nSet; /* Number of bits that are set - only valid for aHash
96 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
97 ** this would be 125. */
drh1feb7dd2008-11-19 18:30:29 +000098 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
99 /* Should >=0 for apSub element. */
100 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
101 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
drhf5e7bb52008-02-18 14:47:33 +0000102 union {
drh1feb7dd2008-11-19 18:30:29 +0000103 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
drhf5e7bb52008-02-18 14:47:33 +0000104 u32 aHash[BITVEC_NINT]; /* Hash table representation */
105 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
106 } u;
107};
108
109/*
110** Create a new bitmap object able to handle bits between 0 and iSize,
111** inclusive. Return a pointer to the new object. Return NULL if
112** malloc fails.
113*/
114Bitvec *sqlite3BitvecCreate(u32 iSize){
115 Bitvec *p;
116 assert( sizeof(*p)==BITVEC_SZ );
117 p = sqlite3MallocZero( sizeof(*p) );
118 if( p ){
119 p->iSize = iSize;
120 }
121 return p;
122}
123
124/*
125** Check to see if the i-th bit is set. Return true or false.
126** If p is NULL (if the bitmap has not been created) or if
127** i is out of range, then return false.
128*/
drh82ef8772015-06-29 14:11:50 +0000129int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
130 assert( p!=0 );
drh1feb7dd2008-11-19 18:30:29 +0000131 i--;
drh234a93f2015-06-29 03:28:43 +0000132 if( i>=p->iSize ) return 0;
drh1feb7dd2008-11-19 18:30:29 +0000133 while( p->iDivisor ){
134 u32 bin = i/p->iDivisor;
135 i = i%p->iDivisor;
136 p = p->u.apSub[bin];
137 if (!p) {
138 return 0;
139 }
drhf5e7bb52008-02-18 14:47:33 +0000140 }
drh1feb7dd2008-11-19 18:30:29 +0000141 if( p->iSize<=BITVEC_NBIT ){
142 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
143 } else{
144 u32 h = BITVEC_HASH(i++);
drhf5e7bb52008-02-18 14:47:33 +0000145 while( p->u.aHash[h] ){
146 if( p->u.aHash[h]==i ) return 1;
drh7ee27b02009-07-25 17:33:25 +0000147 h = (h+1) % BITVEC_NINT;
drhf5e7bb52008-02-18 14:47:33 +0000148 }
149 return 0;
150 }
151}
drh82ef8772015-06-29 14:11:50 +0000152int sqlite3BitvecTest(Bitvec *p, u32 i){
153 return p!=0 && sqlite3BitvecTestNotNull(p,i);
154}
drhf5e7bb52008-02-18 14:47:33 +0000155
156/*
157** Set the i-th bit. Return 0 on success and an error code if
158** anything goes wrong.
drhdfe88ec2008-11-03 20:55:06 +0000159**
160** This routine might cause sub-bitmaps to be allocated. Failing
161** to get the memory needed to hold the sub-bitmap is the only
162** that can go wrong with an insert, assuming p and i are valid.
163**
164** The calling function must ensure that p is a valid Bitvec object
165** and that the value for "i" is within range of the Bitvec object.
166** Otherwise the behavior is undefined.
drhf5e7bb52008-02-18 14:47:33 +0000167*/
168int sqlite3BitvecSet(Bitvec *p, u32 i){
169 u32 h;
drh6aac11d2009-07-18 20:01:37 +0000170 if( p==0 ) return SQLITE_OK;
drh3088d592008-03-21 16:45:47 +0000171 assert( i>0 );
drhc5d0bd92008-04-14 01:00:57 +0000172 assert( i<=p->iSize );
drh1feb7dd2008-11-19 18:30:29 +0000173 i--;
174 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
175 u32 bin = i/p->iDivisor;
176 i = i%p->iDivisor;
drhf5e7bb52008-02-18 14:47:33 +0000177 if( p->u.apSub[bin]==0 ){
drhf5e7bb52008-02-18 14:47:33 +0000178 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
drhf5e7bb52008-02-18 14:47:33 +0000179 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
180 }
drh1feb7dd2008-11-19 18:30:29 +0000181 p = p->u.apSub[bin];
drhf5e7bb52008-02-18 14:47:33 +0000182 }
drh1feb7dd2008-11-19 18:30:29 +0000183 if( p->iSize<=BITVEC_NBIT ){
184 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
185 return SQLITE_OK;
186 }
187 h = BITVEC_HASH(i++);
188 /* if there wasn't a hash collision, and this doesn't */
189 /* completely fill the hash, then just add it without */
190 /* worring about sub-dividing and re-hashing. */
191 if( !p->u.aHash[h] ){
192 if (p->nSet<(BITVEC_NINT-1)) {
193 goto bitvec_set_end;
194 } else {
195 goto bitvec_set_rehash;
196 }
197 }
198 /* there was a collision, check to see if it's already */
199 /* in hash, if not, try to find a spot for it */
200 do {
drhf5e7bb52008-02-18 14:47:33 +0000201 if( p->u.aHash[h]==i ) return SQLITE_OK;
202 h++;
drh1feb7dd2008-11-19 18:30:29 +0000203 if( h>=BITVEC_NINT ) h = 0;
204 } while( p->u.aHash[h] );
205 /* we didn't find it in the hash. h points to the first */
206 /* available free spot. check to see if this is going to */
207 /* make our hash too "full". */
208bitvec_set_rehash:
drhf5e7bb52008-02-18 14:47:33 +0000209 if( p->nSet>=BITVEC_MXHASH ){
drh86a7a692008-11-11 15:48:48 +0000210 unsigned int j;
211 int rc;
drhe98c9042009-06-02 21:31:38 +0000212 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
213 if( aiValues==0 ){
214 return SQLITE_NOMEM;
215 }else{
216 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
217 memset(p->u.apSub, 0, sizeof(p->u.apSub));
218 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
219 rc = sqlite3BitvecSet(p, i);
220 for(j=0; j<BITVEC_NINT; j++){
221 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
222 }
223 sqlite3StackFree(0, aiValues);
224 return rc;
drhf5e7bb52008-02-18 14:47:33 +0000225 }
drhf5e7bb52008-02-18 14:47:33 +0000226 }
drh1feb7dd2008-11-19 18:30:29 +0000227bitvec_set_end:
228 p->nSet++;
drhf5e7bb52008-02-18 14:47:33 +0000229 p->u.aHash[h] = i;
230 return SQLITE_OK;
231}
232
233/*
drh1feb7dd2008-11-19 18:30:29 +0000234** Clear the i-th bit.
drhe98c9042009-06-02 21:31:38 +0000235**
236** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
237** that BitvecClear can use to rebuilt its hash table.
drhf5e7bb52008-02-18 14:47:33 +0000238*/
drhe98c9042009-06-02 21:31:38 +0000239void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
drh6aac11d2009-07-18 20:01:37 +0000240 if( p==0 ) return;
drh3088d592008-03-21 16:45:47 +0000241 assert( i>0 );
drh1feb7dd2008-11-19 18:30:29 +0000242 i--;
243 while( p->iDivisor ){
244 u32 bin = i/p->iDivisor;
245 i = i%p->iDivisor;
246 p = p->u.apSub[bin];
247 if (!p) {
248 return;
drhf5e7bb52008-02-18 14:47:33 +0000249 }
drh1feb7dd2008-11-19 18:30:29 +0000250 }
251 if( p->iSize<=BITVEC_NBIT ){
252 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
drhf5e7bb52008-02-18 14:47:33 +0000253 }else{
drh86a7a692008-11-11 15:48:48 +0000254 unsigned int j;
drhe98c9042009-06-02 21:31:38 +0000255 u32 *aiValues = pBuf;
256 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
257 memset(p->u.aHash, 0, sizeof(p->u.aHash));
drhf5e7bb52008-02-18 14:47:33 +0000258 p->nSet = 0;
259 for(j=0; j<BITVEC_NINT; j++){
drh1feb7dd2008-11-19 18:30:29 +0000260 if( aiValues[j] && aiValues[j]!=(i+1) ){
261 u32 h = BITVEC_HASH(aiValues[j]-1);
262 p->nSet++;
263 while( p->u.aHash[h] ){
264 h++;
265 if( h>=BITVEC_NINT ) h = 0;
266 }
267 p->u.aHash[h] = aiValues[j];
drh3088d592008-03-21 16:45:47 +0000268 }
drhf5e7bb52008-02-18 14:47:33 +0000269 }
270 }
271}
272
273/*
274** Destroy a bitmap object. Reclaim all memory used.
275*/
276void sqlite3BitvecDestroy(Bitvec *p){
277 if( p==0 ) return;
278 if( p->iDivisor ){
drh86a7a692008-11-11 15:48:48 +0000279 unsigned int i;
drhf5e7bb52008-02-18 14:47:33 +0000280 for(i=0; i<BITVEC_NPTR; i++){
281 sqlite3BitvecDestroy(p->u.apSub[i]);
282 }
283 }
284 sqlite3_free(p);
285}
drh3088d592008-03-21 16:45:47 +0000286
danielk1977bea2a942009-01-20 17:06:27 +0000287/*
288** Return the value of the iSize parameter specified when Bitvec *p
289** was created.
290*/
291u32 sqlite3BitvecSize(Bitvec *p){
292 return p->iSize;
293}
294
drh3088d592008-03-21 16:45:47 +0000295#ifndef SQLITE_OMIT_BUILTIN_TEST
296/*
297** Let V[] be an array of unsigned characters sufficient to hold
298** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
299** Then the following macros can be used to set, clear, or test
300** individual bits within V.
301*/
302#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
303#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
304#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
305
306/*
307** This routine runs an extensive test of the Bitvec code.
308**
309** The input is an array of integers that acts as a program
310** to test the Bitvec. The integers are opcodes followed
311** by 0, 1, or 3 operands, depending on the opcode. Another
312** opcode follows immediately after the last operand.
313**
314** There are 6 opcodes numbered from 0 through 5. 0 is the
315** "halt" opcode and causes the test to end.
316**
317** 0 Halt and return the number of errors
318** 1 N S X Set N bits beginning with S and incrementing by X
319** 2 N S X Clear N bits beginning with S and incrementing by X
320** 3 N Set N randomly chosen bits
321** 4 N Clear N randomly chosen bits
322** 5 N S X Set N bits from S increment X in array only, not in bitvec
323**
324** The opcodes 1 through 4 perform set and clear operations are performed
325** on both a Bitvec object and on a linear array of bits obtained from malloc.
326** Opcode 5 works on the linear array only, not on the Bitvec.
327** Opcode 5 is used to deliberately induce a fault in order to
328** confirm that error detection works.
329**
330** At the conclusion of the test the linear array is compared
331** against the Bitvec object. If there are any differences,
332** an error is returned. If they are the same, zero is returned.
333**
334** If a memory allocation error occurs, return -1.
335*/
336int sqlite3BitvecBuiltinTest(int sz, int *aOp){
337 Bitvec *pBitvec = 0;
338 unsigned char *pV = 0;
339 int rc = -1;
340 int i, nx, pc, op;
drhe98c9042009-06-02 21:31:38 +0000341 void *pTmpSpace;
drh3088d592008-03-21 16:45:47 +0000342
343 /* Allocate the Bitvec to be tested and a linear array of
344 ** bits to act as the reference */
345 pBitvec = sqlite3BitvecCreate( sz );
dan6809c962012-07-30 14:53:54 +0000346 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
drhf3cdcdc2015-04-29 16:50:28 +0000347 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
drhe98c9042009-06-02 21:31:38 +0000348 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
drh3088d592008-03-21 16:45:47 +0000349
drh6aac11d2009-07-18 20:01:37 +0000350 /* NULL pBitvec tests */
351 sqlite3BitvecSet(0, 1);
352 sqlite3BitvecClear(0, 1, pTmpSpace);
353
drh3088d592008-03-21 16:45:47 +0000354 /* Run the program */
355 pc = 0;
356 while( (op = aOp[pc])!=0 ){
357 switch( op ){
358 case 1:
359 case 2:
360 case 5: {
361 nx = 4;
362 i = aOp[pc+2] - 1;
363 aOp[pc+2] += aOp[pc+3];
364 break;
365 }
366 case 3:
367 case 4:
368 default: {
369 nx = 2;
370 sqlite3_randomness(sizeof(i), &i);
371 break;
372 }
373 }
374 if( (--aOp[pc+1]) > 0 ) nx = 0;
375 pc += nx;
376 i = (i & 0x7fffffff)%sz;
377 if( (op & 1)!=0 ){
378 SETBIT(pV, (i+1));
379 if( op!=5 ){
380 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
381 }
382 }else{
383 CLEARBIT(pV, (i+1));
drhe98c9042009-06-02 21:31:38 +0000384 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
drh3088d592008-03-21 16:45:47 +0000385 }
386 }
387
388 /* Test to make sure the linear array exactly matches the
389 ** Bitvec object. Start with the assumption that they do
390 ** match (rc==0). Change rc to non-zero if a discrepancy
391 ** is found.
392 */
393 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
drh64f798d2009-04-01 23:49:04 +0000394 + sqlite3BitvecTest(pBitvec, 0)
395 + (sqlite3BitvecSize(pBitvec) - sz);
drh3088d592008-03-21 16:45:47 +0000396 for(i=1; i<=sz; i++){
397 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
398 rc = i;
399 break;
400 }
401 }
402
403 /* Free allocated structure */
404bitvec_end:
drhe98c9042009-06-02 21:31:38 +0000405 sqlite3_free(pTmpSpace);
drh3088d592008-03-21 16:45:47 +0000406 sqlite3_free(pV);
407 sqlite3BitvecDestroy(pBitvec);
408 return rc;
409}
410#endif /* SQLITE_OMIT_BUILTIN_TEST */