Ejemplo n.º 1
0
int main(){
	int x=3;
	printf("%d\n",ispowof2(x));

	x=4;
	printf("%d\n",ispowof2(x));
	return 0;
}
Ejemplo n.º 2
0
/*
 * Create a CdbHash for this session.
 *
 * CdbHash maintains the following information about the hash.
 * In here we set the variables that should not change in the scope of the newly created
 * CdbHash, these are:
 *
 * 1 - number of segments in Greenplum Database.
 * 2 - hashingalgorithm used.
 * 3 - reduction method.
 *
 * The hash value itself will be initialized for every tuple in cdbhashinit()
 */
CdbHash *
makeCdbHash(int numsegs, CdbHashAlg algorithm)
{
	CdbHash    *h;

	assert(numsegs > 0);		/* verify number of segments is legal. */
	assert(algorithm == HASH_FNV_1); /* make sure everybody uses same algorithm */

	/* Create a pointer to a CdbHash that includes the hash properties */
	h = palloc(sizeof(CdbHash));

	/*
	 * set this hash session characteristics.
	 */
	h->hash = 0;
	h->numsegs = numsegs;
	h->hashalg = algorithm;

	if (h->hashalg == HASH_FNV_1)
		h->hashfn = &fnv1_32_buf;
	else if (h->hashalg == HASH_FNV_1A)
		h->hashfn = &fnv1a_32_buf;

	/*
	 * set the reduction algorithm: If num_segs is power of 2 use bit mask,
	 * else use lazy mod (h mod n)
	 */
	if (ispowof2(numsegs))
	{
		h->reducealg = REDUCE_BITMASK;
	}
	else
	{
		h->reducealg = REDUCE_LAZYMOD;
	}

	/*
	 * if we distribute into a relation with an empty partitioning policy, 
	 * we will round robin the tuples starting off from this index. Note that 
	 * the random number is created one per makeCdbHash. This means that commands 
	 * that create a cdbhash object only once for all tuples (like COPY, 
	 * INSERT-INTO-SELECT) behave more like a round-robin distribution, while 
	 * commands that create a cdbhash per row (like INSERT) behave more like a 
	 * random distribution.
	 */
	h->rrindex = cdb_randint(0, UPPER_VAL);
		
	ereport(DEBUG4,
	  (errmsg("CDBHASH started using algorithm %d into %d segment databases",
			  h->hashalg,
			  h->numsegs)));

	return h;
}