Example #1
0
/*
 * The call interface to partition_select has one complicated looking feature:
 * you must pass in a "Real Index Calculation" function that will return an integer
 * corresponding to the actual partition index.  This is used to enable the same routine to
 * work with dense and compressed structures.  This function can just return the input
 * integer unmodified if using a dense array of values as input.
 * The arguments to realIndexCalc() should be:
 * 	int: 		the pivot index (returned from the pivot function)
 * 	char **:	the list of lists
 * 	size_t:		the number of lists
 * 	size_t *:	the width of each value in the list
 */
static int
partition_select (char **lists, size_t nlists, size_t *widths,
		int left, int right, int k,
		int (*compar)(const void *, const void *),
		int (*realIndexCalc)(const int, const char **, const size_t, const size_t *))
{
	int pivotIndex,pivotNewIndex,realIndex;
	char **tmp,*pvalue;
	int maxlen = right;

	/*
	 * Allocate memory for the temporary variables
	 */
	tmp = (char **)palloc(nlists*sizeof(char *));
	for (int i=0;i<nlists;i++)
	{
		tmp[i] = (void *)palloc(widths[i]);
	}
	pvalue = (char *)palloc(widths[0]);

	while (1)
	{
		pivotIndex = RANDOM_INT(left,right);
		pivotNewIndex = partition_pivot(lists,nlists,widths,
					left,right,pivotIndex,
					compar,
					tmp,pvalue);
		realIndex = realIndexCalc(pivotNewIndex,
				(const char **)lists,nlists,widths);
		int nextRealIndex = realIndexCalc(MIN(maxlen,pivotNewIndex+1),
	                                (const char **)lists,nlists,widths);
//		elog(NOTICE,"k,realIndex,nextRealIndex,pni=%d,%d,%d,%d",k,realIndex,nextRealIndex,
//				pivotNewIndex);
		if ((realIndex <= k) && (k < nextRealIndex ))
		{
			break;
		} else if (k < realIndex)
		{
			right = pivotNewIndex-1;
		} else
		{
			left = pivotNewIndex+1;
			if (left >= maxlen) 
			{
				pivotNewIndex = maxlen;
				break;
			}
		}
	}

	/*
	 * Free temporary variables
	 */
	for (int i=0;i<nlists;i++)
	{
		pfree(tmp[i]);
	}
	pfree(tmp);
	pfree(pvalue);

	return(pivotNewIndex); //This index is in the compressed coordinate system
}
/**
* This routine was taken from the benchmark manager, to create Dataobjects in code.
* It is used for the self test.    Depending on compile options, benchmark manager
* may not be included, so the relevant code is copied here to be always available.
*
* @see BenchmarkManager::createDataObject
*
*/
DataObjectRef CacheStrategyUtility::createDataObject(unsigned int numAttr)
{
	char name[128];
	char value[128];
	unsigned int r;

	unsigned char macaddr[6];
	macaddr[0] = (unsigned char) RANDOM_INT(255);
	macaddr[1] = (unsigned char) RANDOM_INT(255);
	macaddr[2] = (unsigned char) RANDOM_INT(255);
	macaddr[3] = (unsigned char) RANDOM_INT(255);
	macaddr[4] = (unsigned char) RANDOM_INT(255);
	macaddr[5] = (unsigned char) RANDOM_INT(255);
	
	unsigned char macaddr2[6];
	macaddr2[0] = (unsigned char) RANDOM_INT(255);
	macaddr2[1] = (unsigned char) RANDOM_INT(255);
	macaddr2[2] = (unsigned char) RANDOM_INT(255);
	macaddr2[3] = (unsigned char) RANDOM_INT(255);
	macaddr2[4] = (unsigned char) RANDOM_INT(255);
	macaddr2[5] = (unsigned char) RANDOM_INT(255);
	
	EthernetAddress addr(macaddr);
	EthernetAddress addr2(macaddr2);
	InterfaceRef localIface = Interface::create<EthernetInterface>(macaddr, "eth", addr, 0);		
	InterfaceRef remoteIface = Interface::create<EthernetInterface>(macaddr2, "eth2", addr2, 0);		
	DataObjectRef dObj = DataObject::create(NULL, 0, localIface, remoteIface);

	for (unsigned int i = 0; i < numAttr; i++) {
		int tries = 0;
		do {
			r = RANDOM_INT(32000);
			sprintf(name, "name");
			sprintf(value, "value %d", r);
			if (tries++ > 10) {
				HAGGLE_ERR("WARNING: Cannot generate unique attributes in data object... check attribute pool size!\n");
				break;
			}
		} while (dObj->getAttribute(name, value));

		dObj->addAttribute(name, value, 1); //r);
	}

	return dObj;
}