Blame src/bitvec.c

Packit 87b942
/*
Packit 87b942
** 2008 February 16
Packit 87b942
**
Packit 87b942
** The author disclaims copyright to this source code.  In place of
Packit 87b942
** a legal notice, here is a blessing:
Packit 87b942
**
Packit 87b942
**    May you do good and not evil.
Packit 87b942
**    May you find forgiveness for yourself and forgive others.
Packit 87b942
**    May you share freely, never taking more than you give.
Packit 87b942
**
Packit 87b942
*************************************************************************
Packit 87b942
** This file implements an object that represents a fixed-length
Packit 87b942
** bitmap.  Bits are numbered starting with 1.
Packit 87b942
**
Packit 87b942
** A bitmap is used to record which pages of a database file have been
Packit 87b942
** journalled during a transaction, or which pages have the "dont-write"
Packit 87b942
** property.  Usually only a few pages are meet either condition.
Packit 87b942
** So the bitmap is usually sparse and has low cardinality.
Packit 87b942
** But sometimes (for example when during a DROP of a large table) most
Packit 87b942
** or all of the pages in a database can get journalled.  In those cases, 
Packit 87b942
** the bitmap becomes dense with high cardinality.  The algorithm needs 
Packit 87b942
** to handle both cases well.
Packit 87b942
**
Packit 87b942
** The size of the bitmap is fixed when the object is created.
Packit 87b942
**
Packit 87b942
** All bits are clear when the bitmap is created.  Individual bits
Packit 87b942
** may be set or cleared one at a time.
Packit 87b942
**
Packit 87b942
** Test operations are about 100 times more common that set operations.
Packit 87b942
** Clear operations are exceedingly rare.  There are usually between
Packit 87b942
** 5 and 500 set operations per Bitvec object, though the number of sets can
Packit 87b942
** sometimes grow into tens of thousands or larger.  The size of the
Packit 87b942
** Bitvec object is the number of pages in the database file at the
Packit 87b942
** start of a transaction, and is thus usually less than a few thousand,
Packit 87b942
** but can be as large as 2 billion for a really big database.
Packit 87b942
*/
Packit 87b942
#include "sqliteInt.h"
Packit 87b942
Packit 87b942
/* Size of the Bitvec structure in bytes. */
Packit 87b942
#define BITVEC_SZ        512
Packit 87b942
Packit 87b942
/* Round the union size down to the nearest pointer boundary, since that's how 
Packit 87b942
** it will be aligned within the Bitvec struct. */
Packit 87b942
#define BITVEC_USIZE \
Packit 87b942
    (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
Packit 87b942
Packit 87b942
/* Type of the array "element" for the bitmap representation. 
Packit 87b942
** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
Packit 87b942
** Setting this to the "natural word" size of your CPU may improve
Packit 87b942
** performance. */
Packit 87b942
#define BITVEC_TELEM     u8
Packit 87b942
/* Size, in bits, of the bitmap element. */
Packit 87b942
#define BITVEC_SZELEM    8
Packit 87b942
/* Number of elements in a bitmap array. */
Packit 87b942
#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
Packit 87b942
/* Number of bits in the bitmap array. */
Packit 87b942
#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
Packit 87b942
Packit 87b942
/* Number of u32 values in hash table. */
Packit 87b942
#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
Packit 87b942
/* Maximum number of entries in hash table before 
Packit 87b942
** sub-dividing and re-hashing. */
Packit 87b942
#define BITVEC_MXHASH    (BITVEC_NINT/2)
Packit 87b942
/* Hashing function for the aHash representation.
Packit 87b942
** Empirical testing showed that the *37 multiplier 
Packit 87b942
** (an arbitrary prime)in the hash function provided 
Packit 87b942
** no fewer collisions than the no-op *1. */
Packit 87b942
#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
Packit 87b942
Packit 87b942
#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
Packit 87b942
Packit 87b942
Packit 87b942
/*
Packit 87b942
** A bitmap is an instance of the following structure.
Packit 87b942
**
Packit 87b942
** This bitmap records the existence of zero or more bits
Packit 87b942
** with values between 1 and iSize, inclusive.
Packit 87b942
**
Packit 87b942
** There are three possible representations of the bitmap.
Packit 87b942
** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
Packit 87b942
** bitmap.  The least significant bit is bit 1.
Packit 87b942
**
Packit 87b942
** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
Packit 87b942
** a hash table that will hold up to BITVEC_MXHASH distinct values.
Packit 87b942
**
Packit 87b942
** Otherwise, the value i is redirected into one of BITVEC_NPTR
Packit 87b942
** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
Packit 87b942
** handles up to iDivisor separate values of i.  apSub[0] holds
Packit 87b942
** values between 1 and iDivisor.  apSub[1] holds values between
Packit 87b942
** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
Packit 87b942
** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
Packit 87b942
** to hold deal with values between 1 and iDivisor.
Packit 87b942
*/
Packit 87b942
struct Bitvec {
Packit 87b942
  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
Packit 87b942
  u32 nSet;       /* Number of bits that are set - only valid for aHash
Packit 87b942
                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
Packit 87b942
                  ** this would be 125. */
Packit 87b942
  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
Packit 87b942
                  /* Should >=0 for apSub element. */
Packit 87b942
                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
Packit 87b942
                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
Packit 87b942
  union {
Packit 87b942
    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
Packit 87b942
    u32 aHash[BITVEC_NINT];      /* Hash table representation */
Packit 87b942
    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
Packit 87b942
  } u;
Packit 87b942
};
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Create a new bitmap object able to handle bits between 0 and iSize,
Packit 87b942
** inclusive.  Return a pointer to the new object.  Return NULL if 
Packit 87b942
** malloc fails.
Packit 87b942
*/
Packit 87b942
Bitvec *sqlite3BitvecCreate(u32 iSize){
Packit 87b942
  Bitvec *p;
Packit 87b942
  assert( sizeof(*p)==BITVEC_SZ );
Packit 87b942
  p = sqlite3MallocZero( sizeof(*p) );
Packit 87b942
  if( p ){
Packit 87b942
    p->iSize = iSize;
Packit 87b942
  }
Packit 87b942
  return p;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Check to see if the i-th bit is set.  Return true or false.
Packit 87b942
** If p is NULL (if the bitmap has not been created) or if
Packit 87b942
** i is out of range, then return false.
Packit 87b942
*/
Packit 87b942
int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
Packit 87b942
  assert( p!=0 );
Packit 87b942
  i--;
Packit 87b942
  if( i>=p->iSize ) return 0;
Packit 87b942
  while( p->iDivisor ){
Packit 87b942
    u32 bin = i/p->iDivisor;
Packit 87b942
    i = i%p->iDivisor;
Packit 87b942
    p = p->u.apSub[bin];
Packit 87b942
    if (!p) {
Packit 87b942
      return 0;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
  if( p->iSize<=BITVEC_NBIT ){
Packit 87b942
    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
Packit 87b942
  } else{
Packit 87b942
    u32 h = BITVEC_HASH(i++);
Packit 87b942
    while( p->u.aHash[h] ){
Packit 87b942
      if( p->u.aHash[h]==i ) return 1;
Packit 87b942
      h = (h+1) % BITVEC_NINT;
Packit 87b942
    }
Packit 87b942
    return 0;
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
int sqlite3BitvecTest(Bitvec *p, u32 i){
Packit 87b942
  return p!=0 && sqlite3BitvecTestNotNull(p,i);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Set the i-th bit.  Return 0 on success and an error code if
Packit 87b942
** anything goes wrong.
Packit 87b942
**
Packit 87b942
** This routine might cause sub-bitmaps to be allocated.  Failing
Packit 87b942
** to get the memory needed to hold the sub-bitmap is the only
Packit 87b942
** that can go wrong with an insert, assuming p and i are valid.
Packit 87b942
**
Packit 87b942
** The calling function must ensure that p is a valid Bitvec object
Packit 87b942
** and that the value for "i" is within range of the Bitvec object.
Packit 87b942
** Otherwise the behavior is undefined.
Packit 87b942
*/
Packit 87b942
int sqlite3BitvecSet(Bitvec *p, u32 i){
Packit 87b942
  u32 h;
Packit 87b942
  if( p==0 ) return SQLITE_OK;
Packit 87b942
  assert( i>0 );
Packit 87b942
  assert( i<=p->iSize );
Packit 87b942
  i--;
Packit 87b942
  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
Packit 87b942
    u32 bin = i/p->iDivisor;
Packit 87b942
    i = i%p->iDivisor;
Packit 87b942
    if( p->u.apSub[bin]==0 ){
Packit 87b942
      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
Packit 87b942
      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
Packit 87b942
    }
Packit 87b942
    p = p->u.apSub[bin];
Packit 87b942
  }
Packit 87b942
  if( p->iSize<=BITVEC_NBIT ){
Packit 87b942
    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
Packit 87b942
    return SQLITE_OK;
Packit 87b942
  }
Packit 87b942
  h = BITVEC_HASH(i++);
Packit 87b942
  /* if there wasn't a hash collision, and this doesn't */
Packit 87b942
  /* completely fill the hash, then just add it without */
Packit 87b942
  /* worring about sub-dividing and re-hashing. */
Packit 87b942
  if( !p->u.aHash[h] ){
Packit 87b942
    if (p->nSet<(BITVEC_NINT-1)) {
Packit 87b942
      goto bitvec_set_end;
Packit 87b942
    } else {
Packit 87b942
      goto bitvec_set_rehash;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
  /* there was a collision, check to see if it's already */
Packit 87b942
  /* in hash, if not, try to find a spot for it */
Packit 87b942
  do {
Packit 87b942
    if( p->u.aHash[h]==i ) return SQLITE_OK;
Packit 87b942
    h++;
Packit 87b942
    if( h>=BITVEC_NINT ) h = 0;
Packit 87b942
  } while( p->u.aHash[h] );
Packit 87b942
  /* we didn't find it in the hash.  h points to the first */
Packit 87b942
  /* available free spot. check to see if this is going to */
Packit 87b942
  /* make our hash too "full".  */
Packit 87b942
bitvec_set_rehash:
Packit 87b942
  if( p->nSet>=BITVEC_MXHASH ){
Packit 87b942
    unsigned int j;
Packit 87b942
    int rc;
Packit 87b942
    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
Packit 87b942
    if( aiValues==0 ){
Packit 87b942
      return SQLITE_NOMEM_BKPT;
Packit 87b942
    }else{
Packit 87b942
      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
Packit 87b942
      memset(p->u.apSub, 0, sizeof(p->u.apSub));
Packit 87b942
      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
Packit 87b942
      rc = sqlite3BitvecSet(p, i);
Packit 87b942
      for(j=0; j
Packit 87b942
        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
Packit 87b942
      }
Packit 87b942
      sqlite3StackFree(0, aiValues);
Packit 87b942
      return rc;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
bitvec_set_end:
Packit 87b942
  p->nSet++;
Packit 87b942
  p->u.aHash[h] = i;
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Clear the i-th bit.
Packit 87b942
**
Packit 87b942
** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
Packit 87b942
** that BitvecClear can use to rebuilt its hash table.
Packit 87b942
*/
Packit 87b942
void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
Packit 87b942
  if( p==0 ) return;
Packit 87b942
  assert( i>0 );
Packit 87b942
  i--;
Packit 87b942
  while( p->iDivisor ){
Packit 87b942
    u32 bin = i/p->iDivisor;
Packit 87b942
    i = i%p->iDivisor;
Packit 87b942
    p = p->u.apSub[bin];
Packit 87b942
    if (!p) {
Packit 87b942
      return;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
  if( p->iSize<=BITVEC_NBIT ){
Packit 87b942
    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
Packit 87b942
  }else{
Packit 87b942
    unsigned int j;
Packit 87b942
    u32 *aiValues = pBuf;
Packit 87b942
    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
Packit 87b942
    memset(p->u.aHash, 0, sizeof(p->u.aHash));
Packit 87b942
    p->nSet = 0;
Packit 87b942
    for(j=0; j
Packit 87b942
      if( aiValues[j] && aiValues[j]!=(i+1) ){
Packit 87b942
        u32 h = BITVEC_HASH(aiValues[j]-1);
Packit 87b942
        p->nSet++;
Packit 87b942
        while( p->u.aHash[h] ){
Packit 87b942
          h++;
Packit 87b942
          if( h>=BITVEC_NINT ) h = 0;
Packit 87b942
        }
Packit 87b942
        p->u.aHash[h] = aiValues[j];
Packit 87b942
      }
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Destroy a bitmap object.  Reclaim all memory used.
Packit 87b942
*/
Packit 87b942
void sqlite3BitvecDestroy(Bitvec *p){
Packit 87b942
  if( p==0 ) return;
Packit 87b942
  if( p->iDivisor ){
Packit 87b942
    unsigned int i;
Packit 87b942
    for(i=0; i
Packit 87b942
      sqlite3BitvecDestroy(p->u.apSub[i]);
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
  sqlite3_free(p);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Return the value of the iSize parameter specified when Bitvec *p
Packit 87b942
** was created.
Packit 87b942
*/
Packit 87b942
u32 sqlite3BitvecSize(Bitvec *p){
Packit 87b942
  return p->iSize;
Packit 87b942
}
Packit 87b942
Packit 87b942
#ifndef SQLITE_UNTESTABLE
Packit 87b942
/*
Packit 87b942
** Let V[] be an array of unsigned characters sufficient to hold
Packit 87b942
** up to N bits.  Let I be an integer between 0 and N.  0<=I
Packit 87b942
** Then the following macros can be used to set, clear, or test
Packit 87b942
** individual bits within V.
Packit 87b942
*/
Packit 87b942
#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
Packit 87b942
#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
Packit 87b942
#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
Packit 87b942
Packit 87b942
/*
Packit 87b942
** This routine runs an extensive test of the Bitvec code.
Packit 87b942
**
Packit 87b942
** The input is an array of integers that acts as a program
Packit 87b942
** to test the Bitvec.  The integers are opcodes followed
Packit 87b942
** by 0, 1, or 3 operands, depending on the opcode.  Another
Packit 87b942
** opcode follows immediately after the last operand.
Packit 87b942
**
Packit 87b942
** There are 6 opcodes numbered from 0 through 5.  0 is the
Packit 87b942
** "halt" opcode and causes the test to end.
Packit 87b942
**
Packit 87b942
**    0          Halt and return the number of errors
Packit 87b942
**    1 N S X    Set N bits beginning with S and incrementing by X
Packit 87b942
**    2 N S X    Clear N bits beginning with S and incrementing by X
Packit 87b942
**    3 N        Set N randomly chosen bits
Packit 87b942
**    4 N        Clear N randomly chosen bits
Packit 87b942
**    5 N S X    Set N bits from S increment X in array only, not in bitvec
Packit 87b942
**
Packit 87b942
** The opcodes 1 through 4 perform set and clear operations are performed
Packit 87b942
** on both a Bitvec object and on a linear array of bits obtained from malloc.
Packit 87b942
** Opcode 5 works on the linear array only, not on the Bitvec.
Packit 87b942
** Opcode 5 is used to deliberately induce a fault in order to
Packit 87b942
** confirm that error detection works.
Packit 87b942
**
Packit 87b942
** At the conclusion of the test the linear array is compared
Packit 87b942
** against the Bitvec object.  If there are any differences,
Packit 87b942
** an error is returned.  If they are the same, zero is returned.
Packit 87b942
**
Packit 87b942
** If a memory allocation error occurs, return -1.
Packit 87b942
*/
Packit 87b942
int sqlite3BitvecBuiltinTest(int sz, int *aOp){
Packit 87b942
  Bitvec *pBitvec = 0;
Packit 87b942
  unsigned char *pV = 0;
Packit 87b942
  int rc = -1;
Packit 87b942
  int i, nx, pc, op;
Packit 87b942
  void *pTmpSpace;
Packit 87b942
Packit 87b942
  /* Allocate the Bitvec to be tested and a linear array of
Packit 87b942
  ** bits to act as the reference */
Packit 87b942
  pBitvec = sqlite3BitvecCreate( sz );
Packit 87b942
  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
Packit 87b942
  pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
Packit 87b942
  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
Packit 87b942
Packit 87b942
  /* NULL pBitvec tests */
Packit 87b942
  sqlite3BitvecSet(0, 1);
Packit 87b942
  sqlite3BitvecClear(0, 1, pTmpSpace);
Packit 87b942
Packit 87b942
  /* Run the program */
Packit 87b942
  pc = 0;
Packit 87b942
  while( (op = aOp[pc])!=0 ){
Packit 87b942
    switch( op ){
Packit 87b942
      case 1:
Packit 87b942
      case 2:
Packit 87b942
      case 5: {
Packit 87b942
        nx = 4;
Packit 87b942
        i = aOp[pc+2] - 1;
Packit 87b942
        aOp[pc+2] += aOp[pc+3];
Packit 87b942
        break;
Packit 87b942
      }
Packit 87b942
      case 3:
Packit 87b942
      case 4: 
Packit 87b942
      default: {
Packit 87b942
        nx = 2;
Packit 87b942
        sqlite3_randomness(sizeof(i), &i);
Packit 87b942
        break;
Packit 87b942
      }
Packit 87b942
    }
Packit 87b942
    if( (--aOp[pc+1]) > 0 ) nx = 0;
Packit 87b942
    pc += nx;
Packit 87b942
    i = (i & 0x7fffffff)%sz;
Packit 87b942
    if( (op & 1)!=0 ){
Packit 87b942
      SETBIT(pV, (i+1));
Packit 87b942
      if( op!=5 ){
Packit 87b942
        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
Packit 87b942
      }
Packit 87b942
    }else{
Packit 87b942
      CLEARBIT(pV, (i+1));
Packit 87b942
      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* Test to make sure the linear array exactly matches the
Packit 87b942
  ** Bitvec object.  Start with the assumption that they do
Packit 87b942
  ** match (rc==0).  Change rc to non-zero if a discrepancy
Packit 87b942
  ** is found.
Packit 87b942
  */
Packit 87b942
  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
Packit 87b942
          + sqlite3BitvecTest(pBitvec, 0)
Packit 87b942
          + (sqlite3BitvecSize(pBitvec) - sz);
Packit 87b942
  for(i=1; i<=sz; i++){
Packit 87b942
    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
Packit 87b942
      rc = i;
Packit 87b942
      break;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* Free allocated structure */
Packit 87b942
bitvec_end:
Packit 87b942
  sqlite3_free(pTmpSpace);
Packit 87b942
  sqlite3_free(pV);
Packit 87b942
  sqlite3BitvecDestroy(pBitvec);
Packit 87b942
  return rc;
Packit 87b942
}
Packit 87b942
#endif /* SQLITE_UNTESTABLE */