bool CheckEqual( const Pair1 &pair1, const Pair2 &pair2
               , typename boost::container::dtl::enable_if_c
                  <boost::container::dtl::is_pair<Pair1>::value &&
                   boost::container::dtl::is_pair<Pair2>::value
                  >::type* = 0)
{
   return CheckEqual(pair1.first, pair2.first) && CheckEqual(pair1.second, pair2.second);
}
Exemple #2
0
static void CheckIter(leveldb_iterator_t* iter,
                      const char* key, const char* val) {
  size_t len;
  const char* str;
  str = leveldb_iter_key(iter, &len);
  CheckEqual(key, str, len);
  str = leveldb_iter_value(iter, &len);
  CheckEqual(val, str, len);
}
PyObject *CListValue::Pyindex(PyObject *value)
{
	PyObject *result = NULL;

	CValue* checkobj = ConvertPythonToValue(value, "val = cList[i]: CValueList, ");
	if (checkobj==NULL)
		return NULL; /* ConvertPythonToValue sets the error */

	int numelem = GetCount();
	for (int i=0;i<numelem;i++)
	{
		CValue* elem = GetValue(i);
		if (checkobj==elem || CheckEqual(checkobj,elem))
		{
			result = PyLong_FromLong(i);
			break;
		}
	}
	checkobj->Release();

	if (result==NULL) {
		PyErr_SetString(PyExc_ValueError, "CList.index(x): x not in CListValue");
	}
	return result;

}
Exemple #4
0
// Callback from leveldb_writebatch_iterate()
static void CheckDel(void* ptr, const char* k, size_t klen)
{
    int* state = (int*) ptr;
    CheckCondition(*state == 2);
    CheckEqual("bar", k, klen);
    (*state)++;
}
bool ehgraph_transfer_node_info(ehgraph_t* dest, enode_t n1, ehgraph_t* src, enode_t n2) {
  bool bRet = false;

  do {
    CheckNotNULL(dest);
    CheckLessThan(n1, dest->size + dest->ptrdim);

    CheckNotNULL(src);
    CheckLessThan(n2, src->size + src->ptrdim);
    
    CheckEqual(src->nLinks, dest->nLinks);
    
    size_t nL = src->nLinks;
    enode_info_t* nDst = dest->info + n1;
    enode_info_t* nSrc = src->info + n2;
    
    nDst->outDoubleLink = nSrc->outDoubleLink;
    nDst->inDoubleLink = nSrc->inDoubleLink;
    
    for (size_t l = 0; l < nL; ++l) {
      nDst->links[l] = nSrc->links[l];
      nDst->superEdge[l] = nSrc->superEdge[l];
      
      for (size_t ld = 0; ld < nL; ++ld) {
	  nDst->isPredicate[l][ld] = nSrc->isPredicate[l][ld];
	  nDst->predicateTarget[l][ld] = nSrc->predicateTarget[l][ld];
      }
    }
    
    bRet = true;
  } while(0);

  return bRet;
}
bool ehgraph_transfer_edge(ehgraph_t* dest, enode_t n1, ehgraph_t* src, enode_t n2, size_t link) {
  bool bRet = false; 
  do {
    CheckNotNULL(dest);
    CheckNotNULL(src);
    CheckLessThan(n1, dest->size);
    CheckLessThan(n2, src->size);
    CheckEqual(src->nLinks, dest->nLinks);
    CheckLessThan(link, src->nLinks);

    size_t nL = src->nLinks;
    
    enode_info_t* nDst = &GET_NODE(dest, n1);
    enode_info_t* nSrc = &GET_NODE(src, n2);
    
    if (link == 0) 
      nDst->outDoubleLink = nSrc->outDoubleLink;
    
    if (1 == link)
      nDst->inDoubleLink = nSrc->inDoubleLink;
    

    nDst->links[link] = nSrc->links[link];
    nDst->superEdge[link] = nSrc->superEdge[link];
      
    for (size_t ld = 0; ld < nL; ++ld) {
      nDst->isPredicate[link][ld] = nSrc->isPredicate[link][ld];
      nDst->predicateTarget[link][ld] = nSrc->predicateTarget[link][ld];
    }

    bRet = true;
  } while(0);

  return bRet;
}
Exemple #7
0
// Callback from leveldb_writebatch_iterate()
static void CheckPut(void* ptr,
                     const char* k, size_t klen,
                     const char* v, size_t vlen) {
  int* state = (int*) ptr;
  CheckCondition(*state < 2);
  switch (*state) {
    case 0:
      CheckEqual("bar", k, klen);
      CheckEqual("b", v, vlen);
      break;
    case 1:
      CheckEqual("box", k, klen);
      CheckEqual("c", v, vlen);
      break;
  }
  (*state)++;
}
Exemple #8
0
static void CheckGet(
    leveldb_t* db,
    const leveldb_readoptions_t* options,
    const char* key,
    const char* expected) {
  char* err = NULL;
  size_t val_len;
  char* val;
  val = leveldb_get(db, options, key, strlen(key), &val_len, &err);
  CheckNoError(err);
  CheckEqual(expected, val, val_len);
  Free(&val);
}
Exemple #9
0
static void CheckGetCF(
    rocksdb_t* db,
    const rocksdb_readoptions_t* options,
    rocksdb_column_family_handle_t* handle,
    const char* key,
    const char* expected) {
  char* err = NULL;
  size_t val_len;
  char* val;
  val = rocksdb_get_cf(db, options, handle, key, strlen(key), &val_len, &err);
  CheckNoError(err);
  CheckEqual(expected, val, val_len);
  Free(&val);
}
bool CheckEqualIt( const T1 &i1, const T2 &i2, const C1 &c1, const C2 &c2 )
{
   bool c1end = i1 == c1.end();
   bool c2end = i2 == c2.end();
   if( c1end != c2end ){
      return false;
   }
   else if(c1end){
      return true;
   }
   else{
      return CheckEqual(*i1, *i2);
   }
}
bool CheckEqualContainers(MyShmCont *shmcont, MyStdCont *stdcont)
{
   if(shmcont->size() != stdcont->size())
      return false;

   typename MyShmCont::iterator itshm(shmcont->begin()), itshmend(shmcont->end());
   typename MyStdCont::iterator itstd(stdcont->begin());
   typename MyStdCont::size_type dist = (typename MyStdCont::size_type)std::distance(itshm, itshmend);
   if(dist != shmcont->size()){
      return false;
   }
   std::size_t i = 0;
   for(; itshm != itshmend; ++itshm, ++itstd, ++i){
      if(!CheckEqual(*itstd, *itshm))
         return false;
   }
   return true;
}
bool CheckEqualContainers(const MyBoostCont &boostcont, const MyStdCont &stdcont)
{
   if(boostcont.size() != stdcont.size())
      return false;

   typename MyBoostCont::const_iterator itboost(boostcont.begin()), itboostend(boostcont.end());
   typename MyStdCont::const_iterator itstd(stdcont.begin());
   typename MyStdCont::size_type dist = (typename MyStdCont::size_type)std::distance(itboost, itboostend);
   if(dist != boostcont.size()){
      return false;
   }
   std::size_t i = 0;
   for(; itboost != itboostend; ++itboost, ++itstd, ++i){
      if(!CheckEqual(*itstd, *itboost))
         return false;
   }
   return true;
}
bool CheckEqualContainers(const ContA &cont_a, const ContB &cont_b)
{
   if(cont_a.size() != cont_b.size())
      return false;

   typename ContA::const_iterator itcont_a(cont_a.begin()), itcont_a_end(cont_a.end());
   typename ContB::const_iterator itcont_b(cont_b.begin()), itcont_b_end(cont_b.end());;
   typename ContB::size_type dist = (typename ContB::size_type)boost::container::iterator_distance(itcont_a, itcont_a_end);
   if(dist != cont_a.size()){
      return false;
   }
   typename ContA::size_type dist2 = (typename ContA::size_type)boost::container::iterator_distance(itcont_b, itcont_b_end);
   if(dist2 != cont_b.size()){
      return false;
   }
   std::size_t i = 0;
   for(; itcont_a != itcont_a_end; ++itcont_a, ++itcont_b, ++i){
      if(!CheckEqual(*itcont_a, *itcont_b))
         return false;
   }
   return true;
}
Exemple #14
0
//__________________________________________________________________________________
_PMathObj _Constant::Raise (_PMathObj theObj)
{
    if (!theObj) {
        return nil;
    }

    _Parameter    base  = Value(),
                  expon = theObj->Value();

    if (base>0.0) {
        return    new  _Constant (exp (log(base)*(expon)));;
    } else {
        if (base<0.0)
            if (CheckEqual (expon, (long)expon)) {
                return new _Constant (((((long)expon)%2)?-1:1)*exp (log(-base)*(expon)));
            } else {
                _String errMsg ("An invalid base/exponent pair passed to ^");
                WarnError (errMsg.sData);
            }

        return     new _Constant (0.0);
    }
}
Exemple #15
0
PyObject *CListValue::Pycount(PyObject *value)
{
	int numfound = 0;

	CValue* checkobj = ConvertPythonToValue(value, ""); /* error ignored */

	if (checkobj==NULL) { /* in this case just return that there are no items in the list */
		PyErr_Clear();
		return PyLong_FromLong(0);
	}

	int numelem = GetCount();
	for (int i=0;i<numelem;i++)
	{
		CValue* elem = 			GetValue(i);
		if (checkobj==elem || CheckEqual(checkobj,elem))
		{
			numfound ++;
		}
	}
	checkobj->Release();

	return PyLong_FromLong(numfound);
}
Exemple #16
0
void			_LikelihoodFunction::PopulateConditionalProbabilities	(long index, char runMode, _Parameter* buffer, _SimpleList& scalers, long branchIndex, _SimpleList* branchValues)
// this function computes site probabilties for each rate class (or something else that involves iterating over rate classes)
// see run options below

// run mode can be one of the following

// _hyphyLFConditionProbsRawMatrixMode : simply   populate an M (number of rate classes) x S (number of site patterns) matrix of conditional likelihoods
//   : expected minimum dimension of buffer is M*S
//	 : scalers will have M*S entries laid out as S for rate class 0, S for rate class 1, .... S for rate class M-1

// _hyphyLFConditionProbsScaledMatrixMode : simply   populate an M (number of rate classes) x S (number of site patterns) and scale to the lowest multiplier
//   : expected minimum dimension of buffer is M*S
//	 : scalers will have S entries

// _hyphyLFConditionProbsWeightedSum : compute  a sum for each site using weighted by the probability of a given category
//   : expected minimum dimension of buffer is 2*S
//	 : scalers will have S entries
//   : **note that the behavior is different if there are HMM (or constant on partition) variables 
//	 : the size of the buffer is S*(N+1), where N is the cumulative number of categories in such variables for this partition
//   : the size of the scaler is also S*N
//	 : the code will behave as _hyphyLFConditionProbsScaledMatrixMode with all other category variables 
//   : summed CONDITIONED on the values of HMM/Constant on partition 

// _hyphyLFConditionProbsMaxProbClass : compute the category index of maximum probability  
//   : expected minimum dimension of buffer is 3*S -- the result goes into offset 0
//	 : scalers will have S entries 

// _hyphyLFConditionProbsClassWeights : compute the weight of each rate class index 
//   : expected minimum dimension of buffer is M
//	 : scalers will have no entries

// _hyphyLFConditionMPIIterate : compute conditional likelihoods of the partition using MPI
//	 : run mode effectively the same as _hyphyLFConditionProbsWeightedSum
{
	_List				*traversalPattern		= (_List*)categoryTraversalTemplate(index),
						*variables			    = (_List*)((*traversalPattern)(0)),
						*catWeigths				= nil;

	_SimpleList			*categoryCounts			= (_SimpleList*)((*traversalPattern)(1)),
						*categoryOffsets		= (_SimpleList*)((*traversalPattern)(2)),
						*hmmAndCOP				= (_SimpleList*)((*traversalPattern)(3)),
						categoryValues			(categoryCounts->lLength,0,0);
	
	long				totalSteps				= categoryOffsets->lData[0] * categoryCounts->lData[0],
						catCount				= variables->lLength-1,
						blockLength				= BlockLength(index),
						hmmCatSize				= hmmAndCOP->Element(-1),
						hmmCatCount				= hmmAndCOP->lLength?(totalSteps/hmmCatSize):0,
						currentHMMCat			= 1,
						arrayDim				;
	
	bool				isTrivial				= variables->lLength == 0,
						switchingHMM			= false;
	
	_CategoryVariable   *catVariable;
	
	
	switch (runMode)
	{
		case _hyphyLFConditionProbsRawMatrixMode:
			arrayDim = catCount*blockLength;
			break;
		case _hyphyLFConditionProbsClassWeights:
			arrayDim = 0;
			break;
		default:
			arrayDim = hmmCatCount?blockLength*hmmCatSize:blockLength;
	}
	
	if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionMPIIterate || runMode == _hyphyLFConditionProbsClassWeights)
	{
		if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionMPIIterate)
		{
			long upperBound = hmmCatCount?hmmAndCOP->Element(-1)*blockLength:blockLength;
			for (long r = 0; r < upperBound; r++)
				buffer[r] = 0.;
		}
		catWeigths = new _List;
	}
	else
		if (runMode == _hyphyLFConditionProbsMaxProbClass)
			for (long r = 0, r2 = 2*blockLength; r < blockLength; r++, r2++)
			{
				buffer[r] = 0.0; buffer[r2] = 0.0;
			}
	
	for					(long currentCat		= 0; currentCat <= catCount; currentCat++)
	{
		(catVariable = ((_CategoryVariable**)(variables->lData))[currentCat])->Refresh();
		catVariable->SetIntervalValue(0,true);
		if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionMPIIterate || runMode == _hyphyLFConditionProbsClassWeights)
			(*catWeigths) << catVariable->GetWeights();
	}
	
		
	scalers.Populate	(arrayDim,0,0);
	
#ifdef __HYPHYMPI__
	_GrowingVector * computedWeights = nil;
	if (runMode == _hyphyLFConditionMPIIterate)
	{
		computedWeights = new _GrowingVector;
	}
	long				mpiTasksSent = 0;
#endif	
	
	for					(long pass = 0; pass < 1+(runMode == _hyphyLFConditionMPIIterate); pass++)
	{
		for					(long currentRateCombo  = 0; currentRateCombo < totalSteps; currentRateCombo++)
		{

			// setting each category variable to its appropriate value 
			
			_Parameter		 currentRateWeight = 1.;
			if (pass == 0)
			{
				if (!isTrivial)
				{
					long remainder = currentRateCombo % categoryCounts->lData[catCount];
					
					if (hmmCatCount)
					{
						currentHMMCat = currentRateCombo / hmmCatCount;
						switchingHMM = (currentRateCombo % hmmCatCount) == 0;
					}
					
					if (currentRateCombo && remainder  == 0)
					{
						categoryValues.lData[catCount] = 0;
						(((_CategoryVariable**)(variables->lData))[catCount])->SetIntervalValue(0);
						for (long uptick = catCount-1; uptick >= 0; uptick --)
						{
							categoryValues.lData[uptick]++;
							if (categoryValues.lData[uptick] == categoryCounts->lData[uptick])
							{
								categoryValues.lData[uptick] = 0;
								(((_CategoryVariable**)(variables->lData))[uptick])->SetIntervalValue(0);
							}
							else
							{
								(((_CategoryVariable**)(variables->lData))[uptick])->SetIntervalValue(categoryValues.lData[uptick]);
								break;
							}
						}
					}
					else
					{
						if (currentRateCombo)
						{
							categoryValues.lData[catCount]++;
							(((_CategoryVariable**)(variables->lData))[catCount])->SetIntervalValue(remainder);
						}
					}
				}
			
				if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionProbsClassWeights || runMode == _hyphyLFConditionMPIIterate)
				{
					for					(long currentCat		= hmmCatCount; currentCat <= catCount; currentCat++)
						currentRateWeight *= ((_Matrix**)catWeigths->lData)[currentCat]->theData[categoryValues.lData[currentCat]];
					
	#ifdef __HYPHYMPI__
					if (runMode == _hyphyLFConditionMPIIterate && pass == 0)
						computedWeights->Store(currentRateWeight);
	#endif				
					if (runMode == _hyphyLFConditionProbsClassWeights)
					{
						buffer [currentRateCombo] = currentRateWeight;
						continue;
					}
					else
						if (currentRateWeight == 0.0) // nothing to do, eh?
							continue;
	#ifdef __HYPHYMPI__
						else
						{
							if (runMode == _hyphyLFConditionMPIIterate)
							{
								SendOffToMPI (currentRateCombo);
								mpiTasksSent ++;
								continue;
							}
						}
	#endif						
				}
			}
			
			long useThisPartitonIndex = currentRateCombo;
			
#ifdef __HYPHYMPI__
			if (runMode == _hyphyLFConditionMPIIterate)
			{
				MPI_Status	   status;
				ReportMPIError(MPI_Recv (resTransferMatrix.theData, resTransferMatrix.GetSize(), MPI_DOUBLE, MPI_ANY_SOURCE , HYPHY_MPI_DATA_TAG, MPI_COMM_WORLD,&status),true);				
				useThisPartitonIndex = status.MPI_SOURCE-1;
				currentRateWeight    = computedWeights->theData[useThisPartitonIndex];
			}
#endif						
			
			// now that the categories are set we can proceed with the computing step
			long			 indexShifter					= blockLength * useThisPartitonIndex;
			long			 *siteCorrectors				= ((_SimpleList**)siteCorrections.lData)[index]->lLength?
															 (((_SimpleList**)siteCorrections.lData)[index]->lData) + indexShifter
															 :nil;
			

			if (runMode == _hyphyLFConditionProbsRawMatrixMode || runMode == _hyphyLFConditionProbsScaledMatrixMode) 
				// populate the matrix of conditionals and scaling factors
			{
				_Parameter	_hprestrict_ *bufferForThisCategory = buffer + indexShifter;

				ComputeBlock	(index, bufferForThisCategory, useThisPartitonIndex, branchIndex, branchValues);
				if (usedCachedResults)
				{
					bool saveFR = forceRecomputation;
					forceRecomputation = true;
					ComputeBlock	(index, bufferForThisCategory, useThisPartitonIndex, branchIndex, branchValues);
					forceRecomputation = saveFR;
				}
				
				if (runMode == _hyphyLFConditionProbsRawMatrixMode)
					for (long p = 0; p < blockLength; p++)
						scalers.lData[p+indexShifter] = siteCorrectors[p];
				else
				{
					if (siteCorrectors)
					{
						for (long r1 = 0; r1 < blockLength; r1++)
						{
							long scv			  = *siteCorrectors,
								 scalerDifference = scv-scalers.lData[r1];
							
							if (scalerDifference > 0) 
							// this class has a _bigger_ scaling factor than at least one other class
							// hence it needs to be scaled down (unless it's the first class)
							{
								if (useThisPartitonIndex==0) //(scalers.lData[r1] == -1)
									scalers.lData[r1] = scv;
								else
									bufferForThisCategory[r1] *= acquireScalerMultiplier (scalerDifference);
							}
							else
							{
								if (scalerDifference < 0) 
								// this class is a smaller scaling factor, i.e. its the biggest among all those
								// considered so far; all other classes need to be scaled down
								{							
									_Parameter scaled = acquireScalerMultiplier (-scalerDifference);
									for (long z = indexShifter+r1-blockLength; z >= 0; z-=blockLength)
										buffer[z] *= scaled;
									
									scalers.lData[r1] = scv;
								}
							}
							siteCorrectors++;
						}
					}
				}
			} 
			else
			{
				if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionProbsMaxProbClass || runMode == _hyphyLFConditionMPIIterate) 
				{
					//if (branchIndex>=0)
					//	((_TheTree*)LocateVar(theTrees.lData[index]))->AddBranchToForcedRecomputeList (branchIndex+((_TheTree*)LocateVar(theTrees.lData[index]))->GetLeafCount());
					
	#ifdef			__HYPHYMPI__
					if (runMode == _hyphyLFConditionMPIIterate)
					{
						long offset = resTransferMatrix.GetVDim();

						for (long k = 0; k < blockLength; k++)
						{
							buffer[blockLength+k] = resTransferMatrix.theData[k];
							siteCorrectors[k]     = resTransferMatrix.theData[k+offset];
						}
					}
					else
						
#endif
					
					ComputeBlock	(index, buffer + (hmmCatCount?hmmCatSize:1)*blockLength, useThisPartitonIndex, branchIndex, branchValues);
					
					if (runMode != _hyphyLFConditionMPIIterate && usedCachedResults)
					{
						bool saveFR = forceRecomputation;
						forceRecomputation = true;
						ComputeBlock	(index, buffer + (hmmCatCount?hmmCatSize:1)*blockLength, useThisPartitonIndex, branchIndex, branchValues);
						forceRecomputation = saveFR;
					}

					
					if (runMode == _hyphyLFConditionProbsWeightedSum || runMode == _hyphyLFConditionMPIIterate)
					{
						long lowerBound  = hmmCatCount?blockLength*currentHMMCat:0,
							 upperBound  = hmmCatCount?blockLength*(1+currentHMMCat):blockLength,
							 lowerBound2 = hmmCatCount?(hmmCatSize*blockLength):blockLength;
									
						
						for (long r1 = lowerBound, r2 = lowerBound2; r1 < upperBound; r1++,r2++)
						{
							if (siteCorrectors)
							{
								long scv = *siteCorrectors;
								
								if (scv < scalers.lData[r1]) // this class has a _smaller_ scaling factor
								{
									buffer[r1] = currentRateWeight * buffer[r2] + buffer[r1] * acquireScalerMultiplier (scalers.lData[r1] - scv);
									scalers.lData[r1] = scv;
								}
								else
								{
									if (scv > scalers.lData[r1]) // this is a _larger_ scaling factor
										buffer[r1] += currentRateWeight * buffer[r2] * acquireScalerMultiplier (scv - scalers.lData[r1]);							
									else // same scaling factors
										buffer[r1] += currentRateWeight * buffer[r2];
								}
								
								siteCorrectors++;
							}
							else
								buffer[r1] += currentRateWeight * buffer[r2];
							
						}		
					}
					else // runMode = _hyphyLFConditionProbsMaxProbClass
					{
						for (long r1 = blockLength*2, r2 = blockLength, r3 = 0; r3 < blockLength; r1++,r2++,r3++)
						{
							bool doChange = false;
							if (siteCorrectors)
							{
								long scv  = *siteCorrectors,
									 diff = scv - scalers.lData[r3];
								
								if (diff<0) // this has a _smaller_ scaling factor
								{
									_Parameter scaled = buffer[r1]*acquireScalerMultiplier (diff);
									if (buffer[r2] > scaled)
										doChange = true;
									else
										buffer[r1] = scaled;
									scalers.lData[r3] = scv;
								}
								else
								{
									if (diff>0) // this is a _larger_ scaling factor
										buffer[r2] *= acquireScalerMultiplier (-diff);		
									doChange = buffer[r2] > buffer[r1] && ! CheckEqual (buffer[r2],buffer[r1]);
								}
								
								siteCorrectors++;
							}
							else
								doChange = buffer[r2] > buffer[r1] && ! CheckEqual (buffer[r2],buffer[r1]);
							
							if (doChange)
							{
								buffer[r1]		   = buffer[r2];
								buffer[r3]         = useThisPartitonIndex;
							}
						}						
					}
				}
			}
#ifdef __HYPHYMPI__
			if (--mpiTasksSent == 0)
				break;
#endif	
		}
	}
#ifdef __HYPHYMPI__
	DeleteObject (computedWeights);
#endif	
	DeleteObject (catWeigths);
}
Exemple #17
0
int main(int argc, char** argv) {
  rocksdb_t* db;
  rocksdb_comparator_t* cmp;
  rocksdb_cache_t* cache;
  rocksdb_env_t* env;
  rocksdb_options_t* options;
  rocksdb_block_based_table_options_t* table_options;
  rocksdb_readoptions_t* roptions;
  rocksdb_writeoptions_t* woptions;
  char* err = NULL;
  int run = -1;

  snprintf(dbname, sizeof(dbname),
           "%s/rocksdb_c_test-%d",
           GetTempDir(),
           ((int) geteuid()));

  snprintf(dbbackupname, sizeof(dbbackupname),
           "%s/rocksdb_c_test-%d-backup",
           GetTempDir(),
           ((int) geteuid()));

  StartPhase("create_objects");
  cmp = rocksdb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  env = rocksdb_create_default_env();
  cache = rocksdb_cache_create_lru(100000);

  options = rocksdb_options_create();
  rocksdb_options_set_comparator(options, cmp);
  rocksdb_options_set_error_if_exists(options, 1);
  rocksdb_options_set_env(options, env);
  rocksdb_options_set_info_log(options, NULL);
  rocksdb_options_set_write_buffer_size(options, 100000);
  rocksdb_options_set_paranoid_checks(options, 1);
  rocksdb_options_set_max_open_files(options, 10);
  table_options = rocksdb_block_based_options_create();
  rocksdb_block_based_options_set_block_cache(table_options, cache);
  rocksdb_options_set_block_based_table_factory(options, table_options);

  rocksdb_options_set_compression(options, rocksdb_no_compression);
  rocksdb_options_set_compression_options(options, -14, -1, 0, 0);
  int compression_levels[] = {rocksdb_no_compression, rocksdb_no_compression,
                              rocksdb_no_compression, rocksdb_no_compression};
  rocksdb_options_set_compression_per_level(options, compression_levels, 4);

  roptions = rocksdb_readoptions_create();
  rocksdb_readoptions_set_verify_checksums(roptions, 1);
  rocksdb_readoptions_set_fill_cache(roptions, 0);

  woptions = rocksdb_writeoptions_create();
  rocksdb_writeoptions_set_sync(woptions, 1);

  StartPhase("destroy");
  rocksdb_destroy_db(options, dbname, &err);
  Free(&err);

  StartPhase("open_error");
  rocksdb_open(options, dbname, &err);
  CheckCondition(err != NULL);
  Free(&err);

  StartPhase("open");
  rocksdb_options_set_create_if_missing(options, 1);
  db = rocksdb_open(options, dbname, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", NULL);

  StartPhase("put");
  rocksdb_put(db, woptions, "foo", 3, "hello", 5, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("backup_and_restore");
  {
    rocksdb_destroy_db(options, dbbackupname, &err);
    CheckNoError(err);

    rocksdb_backup_engine_t *be = rocksdb_backup_engine_open(options, dbbackupname, &err);
    CheckNoError(err);

    rocksdb_backup_engine_create_new_backup(be, db, &err);
    CheckNoError(err);

    // need a change to trigger a new backup
    rocksdb_delete(db, woptions, "does-not-exist", 14, &err);
    CheckNoError(err);

    rocksdb_backup_engine_create_new_backup(be, db, &err);
    CheckNoError(err);

    const rocksdb_backup_engine_info_t* bei = rocksdb_backup_engine_get_backup_info(be);
    CheckCondition(rocksdb_backup_engine_info_count(bei) > 1);
    rocksdb_backup_engine_info_destroy(bei);

    rocksdb_backup_engine_purge_old_backups(be, 1, &err);
    CheckNoError(err);

    bei = rocksdb_backup_engine_get_backup_info(be);
    CheckCondition(rocksdb_backup_engine_info_count(bei) == 1);
    rocksdb_backup_engine_info_destroy(bei);

    rocksdb_delete(db, woptions, "foo", 3, &err);
    CheckNoError(err);

    rocksdb_close(db);

    rocksdb_destroy_db(options, dbname, &err);
    CheckNoError(err);

    rocksdb_restore_options_t *restore_options = rocksdb_restore_options_create();
    rocksdb_restore_options_set_keep_log_files(restore_options, 0);
    rocksdb_backup_engine_restore_db_from_latest_backup(be, dbname, dbname, restore_options, &err);
    CheckNoError(err);
    rocksdb_restore_options_destroy(restore_options);

    rocksdb_options_set_error_if_exists(options, 0);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    rocksdb_options_set_error_if_exists(options, 1);

    CheckGet(db, roptions, "foo", "hello");

    rocksdb_backup_engine_close(be);
  }

  StartPhase("compactall");
  rocksdb_compact_range(db, NULL, 0, NULL, 0);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("compactrange");
  rocksdb_compact_range(db, "a", 1, "z", 1);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("writebatch");
  {
    rocksdb_writebatch_t* wb = rocksdb_writebatch_create();
    rocksdb_writebatch_put(wb, "foo", 3, "a", 1);
    rocksdb_writebatch_clear(wb);
    rocksdb_writebatch_put(wb, "bar", 3, "b", 1);
    rocksdb_writebatch_put(wb, "box", 3, "c", 1);
    rocksdb_writebatch_delete(wb, "bar", 3);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "hello");
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    int pos = 0;
    rocksdb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
    CheckCondition(pos == 3);
    rocksdb_writebatch_destroy(wb);
  }

  StartPhase("writebatch_vectors");
  {
    rocksdb_writebatch_t* wb = rocksdb_writebatch_create();
    const char* k_list[2] = { "z", "ap" };
    const size_t k_sizes[2] = { 1, 2 };
    const char* v_list[3] = { "x", "y", "z" };
    const size_t v_sizes[3] = { 1, 1, 1 };
    rocksdb_writebatch_putv(wb, 2, k_list, k_sizes, 3, v_list, v_sizes);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "zap", "xyz");
    rocksdb_writebatch_delete(wb, "zap", 3);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "zap", NULL);
    rocksdb_writebatch_destroy(wb);
  }

  StartPhase("writebatch_rep");
  {
    rocksdb_writebatch_t* wb1 = rocksdb_writebatch_create();
    rocksdb_writebatch_put(wb1, "baz", 3, "d", 1);
    rocksdb_writebatch_put(wb1, "quux", 4, "e", 1);
    rocksdb_writebatch_delete(wb1, "quux", 4);
    size_t repsize1 = 0;
    const char* rep = rocksdb_writebatch_data(wb1, &repsize1);
    rocksdb_writebatch_t* wb2 = rocksdb_writebatch_create_from(rep, repsize1);
    CheckCondition(rocksdb_writebatch_count(wb1) ==
                   rocksdb_writebatch_count(wb2));
    size_t repsize2 = 0;
    CheckCondition(
        memcmp(rep, rocksdb_writebatch_data(wb2, &repsize2), repsize1) == 0);
    rocksdb_writebatch_destroy(wb1);
    rocksdb_writebatch_destroy(wb2);
  }

  StartPhase("iter");
  {
    rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(rocksdb_iter_valid(iter));
    CheckIter(iter, "box", "c");
    rocksdb_iter_next(iter);
    CheckIter(iter, "foo", "hello");
    rocksdb_iter_prev(iter);
    CheckIter(iter, "box", "c");
    rocksdb_iter_prev(iter);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_last(iter);
    CheckIter(iter, "foo", "hello");
    rocksdb_iter_seek(iter, "b", 1);
    CheckIter(iter, "box", "c");
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);
  }

  StartPhase("multiget");
  {
    const char* keys[3] = { "box", "foo", "notfound" };
    const size_t keys_sizes[3] = { 3, 3, 8 };
    char* vals[3];
    size_t vals_sizes[3];
    char* errs[3];
    rocksdb_multi_get(db, roptions, 3, keys, keys_sizes, vals, vals_sizes, errs);

    int i;
    for (i = 0; i < 3; i++) {
      CheckEqual(NULL, errs[i], 0);
      switch (i) {
      case 0:
        CheckEqual("c", vals[i], vals_sizes[i]);
        break;
      case 1:
        CheckEqual("hello", vals[i], vals_sizes[i]);
        break;
      case 2:
        CheckEqual(NULL, vals[i], vals_sizes[i]);
        break;
      }
      Free(&vals[i]);
    }
  }

  StartPhase("approximate_sizes");
  {
    int i;
    int n = 20000;
    char keybuf[100];
    char valbuf[100];
    uint64_t sizes[2];
    const char* start[2] = { "a", "k00000000000000010000" };
    size_t start_len[2] = { 1, 21 };
    const char* limit[2] = { "k00000000000000010000", "z" };
    size_t limit_len[2] = { 21, 1 };
    rocksdb_writeoptions_set_sync(woptions, 0);
    for (i = 0; i < n; i++) {
      snprintf(keybuf, sizeof(keybuf), "k%020d", i);
      snprintf(valbuf, sizeof(valbuf), "v%020d", i);
      rocksdb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
                  &err);
      CheckNoError(err);
    }
    rocksdb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
    CheckCondition(sizes[0] > 0);
    CheckCondition(sizes[1] > 0);
  }

  StartPhase("property");
  {
    char* prop = rocksdb_property_value(db, "nosuchprop");
    CheckCondition(prop == NULL);
    prop = rocksdb_property_value(db, "rocksdb.stats");
    CheckCondition(prop != NULL);
    Free(&prop);
  }

  StartPhase("snapshot");
  {
    const rocksdb_snapshot_t* snap;
    snap = rocksdb_create_snapshot(db);
    rocksdb_delete(db, woptions, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_readoptions_set_snapshot(roptions, snap);
    CheckGet(db, roptions, "foo", "hello");
    rocksdb_readoptions_set_snapshot(roptions, NULL);
    CheckGet(db, roptions, "foo", NULL);
    rocksdb_release_snapshot(db, snap);
  }

  StartPhase("repair");
  {
    // If we do not compact here, then the lazy deletion of
    // files (https://reviews.facebook.net/D6123) would leave
    // around deleted files and the repair process will find
    // those files and put them back into the database.
    rocksdb_compact_range(db, NULL, 0, NULL, 0);
    rocksdb_close(db);
    rocksdb_options_set_create_if_missing(options, 0);
    rocksdb_options_set_error_if_exists(options, 0);
    rocksdb_repair_db(options, dbname, &err);
    CheckNoError(err);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", NULL);
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    rocksdb_options_set_create_if_missing(options, 1);
    rocksdb_options_set_error_if_exists(options, 1);
  }

  StartPhase("filter");
  for (run = 0; run < 2; run++) {
    // First run uses custom filter, second run uses bloom filter
    CheckNoError(err);
    rocksdb_filterpolicy_t* policy;
    if (run == 0) {
      policy = rocksdb_filterpolicy_create(
          NULL, FilterDestroy, FilterCreate, FilterKeyMatch, NULL, FilterName);
    } else {
      policy = rocksdb_filterpolicy_create_bloom(10);
    }

    rocksdb_block_based_options_set_filter_policy(table_options, policy);

    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_block_based_table_factory(options, table_options);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
    CheckNoError(err);
    rocksdb_compact_range(db, NULL, 0, NULL, 0);

    fake_filter_result = 1;
    CheckGet(db, roptions, "foo", "foovalue");
    CheckGet(db, roptions, "bar", "barvalue");
    if (phase == 0) {
      // Must not find value when custom filter returns false
      fake_filter_result = 0;
      CheckGet(db, roptions, "foo", NULL);
      CheckGet(db, roptions, "bar", NULL);
      fake_filter_result = 1;

      CheckGet(db, roptions, "foo", "foovalue");
      CheckGet(db, roptions, "bar", "barvalue");
    }
    // Reset the policy
    rocksdb_block_based_options_set_filter_policy(table_options, NULL);
    rocksdb_options_set_block_based_table_factory(options, table_options);
  }

  StartPhase("compaction_filter");
  {
    rocksdb_options_t* options_with_filter = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(options_with_filter, 1);
    rocksdb_compactionfilter_t* cfilter;
    cfilter = rocksdb_compactionfilter_create(NULL, CFilterDestroy,
                                              CFilterFilter, CFilterName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options_with_filter, dbname, &err);
    rocksdb_options_set_compaction_filter(options_with_filter, cfilter);
    db = CheckCompaction(db, options_with_filter, roptions, woptions);

    rocksdb_options_set_compaction_filter(options_with_filter, NULL);
    rocksdb_compactionfilter_destroy(cfilter);
    rocksdb_options_destroy(options_with_filter);
  }

  StartPhase("compaction_filter_factory");
  {
    rocksdb_options_t* options_with_filter_factory = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(options_with_filter_factory, 1);
    rocksdb_compactionfilterfactory_t* factory;
    factory = rocksdb_compactionfilterfactory_create(
        NULL, CFilterFactoryDestroy, CFilterCreate, CFilterFactoryName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options_with_filter_factory, dbname, &err);
    rocksdb_options_set_compaction_filter_factory(options_with_filter_factory,
                                                  factory);
    db = CheckCompaction(db, options_with_filter_factory, roptions, woptions);

    rocksdb_options_set_compaction_filter_factory(
        options_with_filter_factory, NULL);
    rocksdb_options_destroy(options_with_filter_factory);
  }

  StartPhase("merge_operator");
  {
    rocksdb_mergeoperator_t* merge_operator;
    merge_operator = rocksdb_mergeoperator_create(
        NULL, MergeOperatorDestroy, MergeOperatorFullMerge,
        MergeOperatorPartialMerge, NULL, MergeOperatorName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_merge_operator(options, merge_operator);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "foovalue");
    rocksdb_merge(db, woptions, "foo", 3, "barvalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "fake");

    // Merge of a non-existing value
    rocksdb_merge(db, woptions, "bar", 3, "barvalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "bar", "fake");

  }

  StartPhase("columnfamilies");
  {
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    CheckNoError(err)

    rocksdb_options_t* db_options = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(db_options, 1);
    db = rocksdb_open(db_options, dbname, &err);
    CheckNoError(err)
    rocksdb_column_family_handle_t* cfh;
    cfh = rocksdb_create_column_family(db, db_options, "cf1", &err);
    rocksdb_column_family_handle_destroy(cfh);
    CheckNoError(err);
    rocksdb_close(db);

    size_t cflen;
    char** column_fams = rocksdb_list_column_families(db_options, dbname, &cflen, &err);
    CheckNoError(err);
    CheckEqual("default", column_fams[0], 7);
    CheckEqual("cf1", column_fams[1], 3);
    CheckCondition(cflen == 2);
    rocksdb_list_column_families_destroy(column_fams, cflen);

    rocksdb_options_t* cf_options = rocksdb_options_create();

    const char* cf_names[2] = {"default", "cf1"};
    const rocksdb_options_t* cf_opts[2] = {cf_options, cf_options};
    rocksdb_column_family_handle_t* handles[2];
    db = rocksdb_open_column_families(db_options, dbname, 2, cf_names, cf_opts, handles, &err);
    CheckNoError(err);

    rocksdb_put_cf(db, woptions, handles[1], "foo", 3, "hello", 5, &err);
    CheckNoError(err);

    CheckGetCF(db, roptions, handles[1], "foo", "hello");

    rocksdb_delete_cf(db, woptions, handles[1], "foo", 3, &err);
    CheckNoError(err);

    CheckGetCF(db, roptions, handles[1], "foo", NULL);

    rocksdb_writebatch_t* wb = rocksdb_writebatch_create();
    rocksdb_writebatch_put_cf(wb, handles[1], "baz", 3, "a", 1);
    rocksdb_writebatch_clear(wb);
    rocksdb_writebatch_put_cf(wb, handles[1], "bar", 3, "b", 1);
    rocksdb_writebatch_put_cf(wb, handles[1], "box", 3, "c", 1);
    rocksdb_writebatch_delete_cf(wb, handles[1], "bar", 3);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGetCF(db, roptions, handles[1], "baz", NULL);
    CheckGetCF(db, roptions, handles[1], "bar", NULL);
    CheckGetCF(db, roptions, handles[1], "box", "c");
    rocksdb_writebatch_destroy(wb);

    const char* keys[3] = { "box", "box", "barfooxx" };
    const rocksdb_column_family_handle_t* get_handles[3] = { handles[0], handles[1], handles[1] };
    const size_t keys_sizes[3] = { 3, 3, 8 };
    char* vals[3];
    size_t vals_sizes[3];
    char* errs[3];
    rocksdb_multi_get_cf(db, roptions, get_handles, 3, keys, keys_sizes, vals, vals_sizes, errs);

    int i;
    for (i = 0; i < 3; i++) {
      CheckEqual(NULL, errs[i], 0);
      switch (i) {
      case 0:
        CheckEqual(NULL, vals[i], vals_sizes[i]); // wrong cf
        break;
      case 1:
        CheckEqual("c", vals[i], vals_sizes[i]); // bingo
        break;
      case 2:
        CheckEqual(NULL, vals[i], vals_sizes[i]); // normal not found
        break;
      }
      Free(&vals[i]);
    }

    rocksdb_iterator_t* iter = rocksdb_create_iterator_cf(db, roptions, handles[1]);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(rocksdb_iter_valid(iter));

    for (i = 0; rocksdb_iter_valid(iter) != 0; rocksdb_iter_next(iter)) {
      i++;
    }
    CheckCondition(i == 1);
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);

    rocksdb_column_family_handle_t* iters_cf_handles[2] = { handles[0], handles[1] };
    rocksdb_iterator_t* iters_handles[2];
    rocksdb_create_iterators(db, roptions, iters_cf_handles, iters_handles, 2, &err);
    CheckNoError(err);

    iter = iters_handles[0];
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_destroy(iter);

    iter = iters_handles[1];
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(rocksdb_iter_valid(iter));

    for (i = 0; rocksdb_iter_valid(iter) != 0; rocksdb_iter_next(iter)) {
      i++;
    }
    CheckCondition(i == 1);
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);

    rocksdb_drop_column_family(db, handles[1], &err);
    CheckNoError(err);
    for (i = 0; i < 2; i++) {
      rocksdb_column_family_handle_destroy(handles[i]);
    }
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_destroy(db_options);
    rocksdb_options_destroy(cf_options);
  }

  StartPhase("prefix");
  {
    // Create new database
    rocksdb_options_set_allow_mmap_reads(options, 1);
    rocksdb_options_set_prefix_extractor(options, rocksdb_slicetransform_create_fixed_prefix(3));
    rocksdb_options_set_hash_skip_list_rep(options, 5000, 4, 4);
    rocksdb_options_set_plain_table_factory(options, 4, 10, 0.75, 16);

    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);

    rocksdb_put(db, woptions, "foo1", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo2", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo3", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar1", 4, "bar", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar2", 4, "bar", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar3", 4, "bar", 3, &err);
    CheckNoError(err);

    rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);
    CheckCondition(!rocksdb_iter_valid(iter));

    rocksdb_iter_seek(iter, "bar", 3);
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    CheckCondition(rocksdb_iter_valid(iter));

    CheckIter(iter, "bar1", "bar");
    rocksdb_iter_next(iter);
    CheckIter(iter, "bar2", "bar");
    rocksdb_iter_next(iter);
    CheckIter(iter, "bar3", "bar");
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);

    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
  }

  StartPhase("cuckoo_options");
  {
    rocksdb_cuckoo_table_options_t* cuckoo_options;
    cuckoo_options = rocksdb_cuckoo_options_create();
    rocksdb_cuckoo_options_set_hash_ratio(cuckoo_options, 0.5);
    rocksdb_cuckoo_options_set_max_search_depth(cuckoo_options, 200);
    rocksdb_cuckoo_options_set_cuckoo_block_size(cuckoo_options, 10);
    rocksdb_cuckoo_options_set_identity_as_first_hash(cuckoo_options, 1);
    rocksdb_cuckoo_options_set_use_module_hash(cuckoo_options, 0);
    rocksdb_options_set_cuckoo_table_factory(options, cuckoo_options);

    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);

    rocksdb_cuckoo_options_destroy(cuckoo_options);
  }

  StartPhase("iterate_upper_bound");
  {
    // Create new empty database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    CheckNoError(err);

    rocksdb_options_set_prefix_extractor(options, NULL);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);

    rocksdb_put(db, woptions, "a",    1, "0",    1, &err); CheckNoError(err);
    rocksdb_put(db, woptions, "foo",  3, "bar",  3, &err); CheckNoError(err);
    rocksdb_put(db, woptions, "foo1", 4, "bar1", 4, &err); CheckNoError(err);
    rocksdb_put(db, woptions, "g1",   2, "0",    1, &err); CheckNoError(err);

    // testing basic case with no iterate_upper_bound and no prefix_extractor
    {
       rocksdb_readoptions_set_iterate_upper_bound(roptions, NULL, 0);
       rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);

       rocksdb_iter_seek(iter, "foo", 3);
       CheckCondition(rocksdb_iter_valid(iter));
       CheckIter(iter, "foo", "bar");

       rocksdb_iter_next(iter);
       CheckCondition(rocksdb_iter_valid(iter));
       CheckIter(iter, "foo1", "bar1");

       rocksdb_iter_next(iter);
       CheckCondition(rocksdb_iter_valid(iter));
       CheckIter(iter, "g1", "0");

       rocksdb_iter_destroy(iter);
    }

    // testing iterate_upper_bound and forward iterator
    // to make sure it stops at bound
    {
       // iterate_upper_bound points beyond the last expected entry
       rocksdb_readoptions_set_iterate_upper_bound(roptions, "foo2", 4);

       rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);

       rocksdb_iter_seek(iter, "foo", 3);
       CheckCondition(rocksdb_iter_valid(iter));
       CheckIter(iter, "foo", "bar");

       rocksdb_iter_next(iter);
       CheckCondition(rocksdb_iter_valid(iter));
       CheckIter(iter, "foo1", "bar1");

       rocksdb_iter_next(iter);
       // should stop here...
       CheckCondition(!rocksdb_iter_valid(iter));

       rocksdb_iter_destroy(iter);
    }
  }

  StartPhase("cleanup");
  rocksdb_close(db);
  rocksdb_options_destroy(options);
  rocksdb_block_based_options_destroy(table_options);
  rocksdb_readoptions_destroy(roptions);
  rocksdb_writeoptions_destroy(woptions);
  rocksdb_cache_destroy(cache);
  rocksdb_comparator_destroy(cmp);
  rocksdb_env_destroy(env);

  fprintf(stderr, "PASS\n");
  return 0;
}
Exemple #18
0
//__________________________________________________________________________________
_PMathObj _Constant::LNot ()
{
    return new _Constant (CheckEqual(theValue, 0.0));
}
Exemple #19
0
void         TrainModelNN   (_String* model, _String* matrix)
{
    _String         errMsg;

    long            modelIdx = modelNames.Find(model);

    _Parameter      verbI;

    checkParameter (VerbosityLevelString, verbI, 0.0);

    char            buffer [128];

    if (modelIdx < 0) {
        errMsg = *model & " did not refer to an existring model";
    } else {
        _Variable*    boundsMatrix              =   FetchVar  (LocateVarByName (*matrix));

        if (boundsMatrix && (boundsMatrix->ObjectClass() == MATRIX)) {
            _Matrix  *    bmatrix               =   (_Matrix*)      boundsMatrix->GetValue ();

            if (bmatrix->IsAStringMatrix() && (bmatrix->GetVDim () == 3)) {
                _Variable*    modelMatrix           =   LocateVar       (modelMatrixIndices.lData[modelIdx]);
                _SimpleList   modelVariableList;
                {
                    _AVLList mvla (&modelVariableList);
                    modelMatrix->ScanForVariables       (mvla, true);
                    mvla.ReorderList();
                }

                if (bmatrix->GetHDim () == modelVariableList.lLength) {
                    // now map model variables to bounds matrix
                    _SimpleList          variableMap;
                    _String             *myName;

                    for (long k = 0; k < modelVariableList.lLength; k++) {
                        myName = ((_FString*)bmatrix->GetFormula(k,0)->Compute())->theString;
                        long      vID    = LocateVarByName (*myName);

                        if (vID < 0) {
                            break;
                        }

                        vID = variableNames.GetXtra (vID);
                        vID = modelVariableList.Find(vID);

                        if (vID < 0) {
                            break;
                        }

                        variableMap << vID;
                    }

                    if (variableMap.lLength == modelVariableList.lLength) {
                        _Matrix     vBounds (variableMap.lLength,2, false, true);

                        long        k2 = 0;

                        for (; k2 < variableMap.lLength; k2++) {
                            _Parameter lb = ((_FString*)bmatrix->GetFormula(k2,1)->Compute())->theString->toNum(),
                                       ub = ((_FString*)bmatrix->GetFormula(k2,2)->Compute())->theString->toNum();

                            if ( ub>lb || k2) {
                                vBounds.Store (k2,0,lb);
                                vBounds.Store (k2,1,ub);
                                if (ub<=lb && vBounds (k2-1,0) <= vBounds (k2-1,1) && (!CheckEqual(vBounds (k2-1,0),0.0) || !CheckEqual(vBounds (k2-1,1),1.0))) {
                                    break;
                                }
                            }

                        }
                        if (k2 == modelVariableList.lLength) {
                            // set up the sampling now
                            _String             fName       = ProcessLiteralArgument (&ModelNNFile,nil);
                            FILE*               nnFile      = doFileOpen (fName.getStr(), "w");
                            if (nnFile) {
                                _Matrix*            modelMatrix = (_Matrix*) LocateVar(modelMatrixIndices.lData[modelIdx])->GetValue();

                                _Parameter          mainSteps,
                                                    checkSteps,
                                                    errorTerm,
                                                    loopMax,
                                                    hiddenNodes,
                                                    absError,
                                                    nn1,
                                                    nn2;

                                long                fullDimension = modelMatrix->GetHDim() * modelMatrix->GetVDim();


                                checkParameter      (ModelNNTrainingSteps,      mainSteps,      10000.0);
                                checkParameter      (ModelNNVerificationSample, checkSteps,     500.0);
                                checkParameter      (ModelNNPrecision,          errorTerm,      0.01);
                                checkParameter      (ModelNNTrainingSteps,      loopMax,        10);
                                checkParameter      (ModelNNHiddenNodes,        hiddenNodes,    5);
                                checkParameter      (ModelNNLearningRate,       nn1,            .3);
                                checkParameter      (ModelNNPersistenceRate,    nn2,            .1);

                                Net**               matrixNet = new Net* [fullDimension] ;

                                for (long i = 0; i < fullDimension; i++) {
                                    checkPointer (matrixNet [i] = new Net (variableMap.lLength,(long)hiddenNodes,1,errorTerm,nn1,nn2,100,200,true));
                                    //matrixNet[i]->verbose = true;
                                }

                                checkPointer        (matrixNet);

                                _List               tIn,
                                                    tOut;

                                FILE*               varSamples = doFileOpen ("variableSamples.out", "w");

                                fprintf (varSamples, "%s" ,LocateVar(modelVariableList.lData[0])->GetName()->getStr());
                                for (long vc = 1; vc < modelVariableList.lLength; vc++) {
                                    fprintf (varSamples, ",%s" ,LocateVar(modelVariableList.lData[variableMap.lData[vc]])->GetName()->getStr());
                                }

                                fprintf (varSamples, "\n");

                                for (long itCount = 0; itCount < loopMax; itCount ++) {
                                    if (verbI > 5) {
                                        snprintf (buffer, sizeof(buffer), "\nNeural Network Pass %ld. Building a training set...\n", itCount);
                                        BufferToConsole (buffer);
                                    }

                                    while   (tIn.countitems() < mainSteps) {
                                        NNMatrixSampler     (0, vBounds, modelVariableList, variableMap, modelMatrix, tIn, tOut);
                                    }

                                    _Matrix             inData (mainSteps, variableMap.lLength, false, true);
                                    _Parameter          *md = inData.theData;

                                    for (long matrixC = 0; matrixC < mainSteps; matrixC++) {
                                        _Parameter  *   ed = ((_Matrix*)tIn (matrixC))->theData;
                                        fprintf (varSamples, "\n%g",*ed);
                                        *md = *ed;
                                        ed++;
                                        md++;
                                        for (long entryC = 1; entryC < variableMap.lLength; entryC++, ed++, md++) {
                                            *md = *ed;
                                            fprintf (varSamples, ",%g", *md);
                                        }
                                    }

                                    tIn.Clear();

                                    if (verbI > 5) {
                                        BufferToConsole ( "Done Building Training Set. Training...\n");
                                    }

                                    long lastDone = 0;

                                    for (long cellCount = 0; cellCount < fullDimension; cellCount++) {
                                        Net* thisCell = matrixNet[cellCount];
                                        _Matrix outVector (mainSteps, 1, false, true);

                                        for (long oc = 0; oc < mainSteps; oc++) {
                                            outVector.theData[oc] = ((_Matrix*)tOut(oc))->theData[cellCount];
                                        }

                                        thisCell->studyAll (inData.theData, outVector.theData, mainSteps);

                                        long    nowDone = (cellCount+1)*100./fullDimension;
                                        if (nowDone > lastDone) {
                                            snprintf (buffer, sizeof(buffer),"%ld%% done\n", lastDone = nowDone);
                                            BufferToConsole (buffer);
                                        }
                                    }
                                    tOut.Clear();

                                    if (verbI > 5) {
                                        BufferToConsole ( "Done Training. Resampling...\n");
                                    }

                                    _PMathObj  tObj = _Constant(0).Time();
                                    _Parameter time1 = tObj->Value(),
                                               time2;

                                    while   (tIn.countitems() < checkSteps) {
                                        NNMatrixSampler     (0, vBounds, modelVariableList, variableMap, modelMatrix, tIn, tOut);
                                    }

                                    absError = 0.0;

                                    DeleteObject (tObj);
                                    tObj = _Constant(0).Time();
                                    time2 = tObj->Value();

                                    if (verbI > 5) {
                                        snprintf (buffer, sizeof(buffer),"Done Resampling in %g seconds. Computing Error...\n", time2-time1);
                                        BufferToConsole (buffer);
                                    }

                                    _Parameter  maxValT,
                                                maxValE;

                                    for (long verCount = 0; verCount < checkSteps; verCount++) {
                                        _Parameter*  inData     = ((_Matrix*)tIn(verCount))->theData,
                                                     *  outData  = ((_Matrix*)tOut(verCount))->theData;

                                        for (long cellCount = 0; cellCount < fullDimension; cellCount++) {
                                            Net         *thisCell = matrixNet[cellCount];

                                            _Parameter estVal     = thisCell->eval(inData)[0],
                                                       trueVal      = outData[cellCount],
                                                       localError;

                                            localError = estVal-trueVal;

                                            if (localError < 0) {
                                                localError = -localError;
                                            }

                                            if (absError < localError) {
                                                maxValT  = trueVal;
                                                maxValE  = estVal;
                                                absError = localError;
                                            }
                                        }
                                    }

                                    DeleteObject (tObj);
                                    tObj = _Constant(0).Time();
                                    time1 = tObj->Value();
                                    DeleteObject (tObj);


                                    if (verbI > 5) {
                                        snprintf (buffer, sizeof(buffer), "Done Error Checking in %g seconds. Got max abs error %g on the pair %g %g\n", time1-time2, absError, maxValT, maxValE);
                                        BufferToConsole (buffer);
                                    }
                                    if (absError <= errorTerm) {
                                        break;
                                    }
                                }

                                if (absError > errorTerm) {
                                    ReportWarning (_String("Couldn't achive desired precision in TrainModelNN. Achieved error of ") & absError);
                                }
                                fclose  (varSamples);
                                fprintf (nnFile,"{{\n\"%s\"", LocateVar(modelVariableList.lData[0])->GetName()->getStr());
                                _Matrix newBounds (modelVariableList.lLength, 2, false, true);
                                if (vBounds(0,0)>vBounds(0,1)) {
                                    newBounds.Store (variableMap.lData[0],0,0.);
                                    newBounds.Store (variableMap.lData[0],1,1.);
                                } else {
                                    newBounds.Store (variableMap.lData[0],0,vBounds(0,0));
                                    newBounds.Store (variableMap.lData[0],1,vBounds(0,1));
                                }
                                for (long varCounter = 1; varCounter < modelVariableList.lLength; varCounter ++) {
                                    fprintf (nnFile,",\n\"%s\"", LocateVar(modelVariableList.lData[varCounter])->GetName()->getStr());
                                    if (vBounds(varCounter,0)>vBounds(varCounter,1)) {
                                        newBounds.Store (variableMap.lData[varCounter],0,0.);
                                        newBounds.Store (variableMap.lData[varCounter],1,1.);
                                    } else {
                                        newBounds.Store (variableMap.lData[varCounter],0,vBounds(varCounter,0));
                                        newBounds.Store (variableMap.lData[varCounter],1,vBounds(varCounter,1));
                                    }
                                }

                                fprintf (nnFile,"\n}}\n");
                                newBounds.toFileStr (nnFile);


                                for (long i2 = 0; i2 < fullDimension; i2++) {
                                    matrixNet[i2]->save(nnFile);
                                    delete matrixNet [i2];
                                }

                                fclose (nnFile);
                                delete              matrixNet;
                            } else {
                                errMsg = _String ("Failed to open ") & fName & " for writing";
                            }
                        } else {
                            errMsg = _String ("Invalid variable bounds in row ") & (k2+1) & " of the bounds matrix";
                        }
                    } else {
                        errMsg = *myName & " was not one of the model parameters";
                    }

                } else {
                    errMsg = *matrix & " must be a have the same number of rows as the number of model parameters";
                }

            } else {
                errMsg = *matrix & " must be a string matrix with 3 columns";
            }
        } else {
            errMsg = *matrix & " was not the identifier of a valid matrix variable";
        }



    }

    if (errMsg.sLength) {
        errMsg = errMsg & _String(" in call to TrainModelNN.");
        WarnError (errMsg);
    }
}
Exemple #20
0
int main(int argc, char** argv) {
  rocksdb_t* db;
  rocksdb_comparator_t* cmp;
  rocksdb_cache_t* cache;
  rocksdb_env_t* env;
  rocksdb_options_t* options;
  rocksdb_readoptions_t* roptions;
  rocksdb_writeoptions_t* woptions;
  char* err = NULL;
  int run = -1;

  snprintf(dbname, sizeof(dbname),
           "%s/rocksdb_c_test-%d",
           GetTempDir(),
           ((int) geteuid()));

  StartPhase("create_objects");
  cmp = rocksdb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  env = rocksdb_create_default_env();
  cache = rocksdb_cache_create_lru(100000);

  options = rocksdb_options_create();
  rocksdb_options_set_comparator(options, cmp);
  rocksdb_options_set_error_if_exists(options, 1);
  rocksdb_options_set_cache(options, cache);
  rocksdb_options_set_env(options, env);
  rocksdb_options_set_info_log(options, NULL);
  rocksdb_options_set_write_buffer_size(options, 100000);
  rocksdb_options_set_paranoid_checks(options, 1);
  rocksdb_options_set_max_open_files(options, 10);
  rocksdb_options_set_block_size(options, 1024);
  rocksdb_options_set_block_restart_interval(options, 8);
  rocksdb_options_set_compression(options, rocksdb_no_compression);
  rocksdb_options_set_compression_options(options, -14, -1, 0);
  int compression_levels[] = {rocksdb_no_compression, rocksdb_no_compression,
                              rocksdb_no_compression, rocksdb_no_compression};
  rocksdb_options_set_compression_per_level(options, compression_levels, 4);

  roptions = rocksdb_readoptions_create();
  rocksdb_readoptions_set_verify_checksums(roptions, 1);
  rocksdb_readoptions_set_fill_cache(roptions, 0);

  woptions = rocksdb_writeoptions_create();
  rocksdb_writeoptions_set_sync(woptions, 1);

  StartPhase("destroy");
  rocksdb_destroy_db(options, dbname, &err);
  Free(&err);

  StartPhase("open_error");
  db = rocksdb_open(options, dbname, &err);
  CheckCondition(err != NULL);
  Free(&err);

  StartPhase("open");
  rocksdb_options_set_create_if_missing(options, 1);
  db = rocksdb_open(options, dbname, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", NULL);

  StartPhase("put");
  rocksdb_put(db, woptions, "foo", 3, "hello", 5, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("compactall");
  rocksdb_compact_range(db, NULL, 0, NULL, 0);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("compactrange");
  rocksdb_compact_range(db, "a", 1, "z", 1);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("writebatch");
  {
    rocksdb_writebatch_t* wb = rocksdb_writebatch_create();
    rocksdb_writebatch_put(wb, "foo", 3, "a", 1);
    rocksdb_writebatch_clear(wb);
    rocksdb_writebatch_put(wb, "bar", 3, "b", 1);
    rocksdb_writebatch_put(wb, "box", 3, "c", 1);
    rocksdb_writebatch_delete(wb, "bar", 3);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "hello");
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    int pos = 0;
    rocksdb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
    CheckCondition(pos == 3);
    rocksdb_writebatch_destroy(wb);
  }

  StartPhase("writebatch_rep");
  {
    rocksdb_writebatch_t* wb1 = rocksdb_writebatch_create();
    rocksdb_writebatch_put(wb1, "baz", 3, "d", 1);
    rocksdb_writebatch_put(wb1, "quux", 4, "e", 1);
    rocksdb_writebatch_delete(wb1, "quux", 4);
    size_t repsize1 = 0;
    const char* rep = rocksdb_writebatch_data(wb1, &repsize1);
    rocksdb_writebatch_t* wb2 = rocksdb_writebatch_create_from(rep, repsize1);
    CheckCondition(rocksdb_writebatch_count(wb1) ==
                   rocksdb_writebatch_count(wb2));
    size_t repsize2 = 0;
    CheckCondition(
        memcmp(rep, rocksdb_writebatch_data(wb2, &repsize2), repsize1) == 0);
    rocksdb_writebatch_destroy(wb1);
    rocksdb_writebatch_destroy(wb2);
  }

  StartPhase("iter");
  {
    rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(rocksdb_iter_valid(iter));
    CheckIter(iter, "box", "c");
    rocksdb_iter_next(iter);
    CheckIter(iter, "foo", "hello");
    rocksdb_iter_prev(iter);
    CheckIter(iter, "box", "c");
    rocksdb_iter_prev(iter);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_last(iter);
    CheckIter(iter, "foo", "hello");
    rocksdb_iter_seek(iter, "b", 1);
    CheckIter(iter, "box", "c");
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);
  }

  StartPhase("approximate_sizes");
  {
    int i;
    int n = 20000;
    char keybuf[100];
    char valbuf[100];
    uint64_t sizes[2];
    const char* start[2] = { "a", "k00000000000000010000" };
    size_t start_len[2] = { 1, 21 };
    const char* limit[2] = { "k00000000000000010000", "z" };
    size_t limit_len[2] = { 21, 1 };
    rocksdb_writeoptions_set_sync(woptions, 0);
    for (i = 0; i < n; i++) {
      snprintf(keybuf, sizeof(keybuf), "k%020d", i);
      snprintf(valbuf, sizeof(valbuf), "v%020d", i);
      rocksdb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
                  &err);
      CheckNoError(err);
    }
    rocksdb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
    CheckCondition(sizes[0] > 0);
    CheckCondition(sizes[1] > 0);
  }

  StartPhase("property");
  {
    char* prop = rocksdb_property_value(db, "nosuchprop");
    CheckCondition(prop == NULL);
    prop = rocksdb_property_value(db, "rocksdb.stats");
    CheckCondition(prop != NULL);
    Free(&prop);
  }

  StartPhase("snapshot");
  {
    const rocksdb_snapshot_t* snap;
    snap = rocksdb_create_snapshot(db);
    rocksdb_delete(db, woptions, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_readoptions_set_snapshot(roptions, snap);
    CheckGet(db, roptions, "foo", "hello");
    rocksdb_readoptions_set_snapshot(roptions, NULL);
    CheckGet(db, roptions, "foo", NULL);
    rocksdb_release_snapshot(db, snap);
  }

  StartPhase("repair");
  {
    // If we do not compact here, then the lazy deletion of
    // files (https://reviews.facebook.net/D6123) would leave
    // around deleted files and the repair process will find
    // those files and put them back into the database.
    rocksdb_compact_range(db, NULL, 0, NULL, 0);
    rocksdb_close(db);
    rocksdb_options_set_create_if_missing(options, 0);
    rocksdb_options_set_error_if_exists(options, 0);
    rocksdb_repair_db(options, dbname, &err);
    CheckNoError(err);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", NULL);
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    rocksdb_options_set_create_if_missing(options, 1);
    rocksdb_options_set_error_if_exists(options, 1);
  }

  StartPhase("filter");
  for (run = 0; run < 2; run++) {
    // First run uses custom filter, second run uses bloom filter
    CheckNoError(err);
    rocksdb_filterpolicy_t* policy;
    if (run == 0) {
      policy = rocksdb_filterpolicy_create(
          NULL, FilterDestroy, FilterCreate, FilterKeyMatch, NULL, FilterName);
    } else {
      policy = rocksdb_filterpolicy_create_bloom(10);
    }

    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_filter_policy(options, policy);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
    CheckNoError(err);
    rocksdb_compact_range(db, NULL, 0, NULL, 0);

    fake_filter_result = 1;
    CheckGet(db, roptions, "foo", "foovalue");
    CheckGet(db, roptions, "bar", "barvalue");
    if (phase == 0) {
      // Must not find value when custom filter returns false
      fake_filter_result = 0;
      CheckGet(db, roptions, "foo", NULL);
      CheckGet(db, roptions, "bar", NULL);
      fake_filter_result = 1;

      CheckGet(db, roptions, "foo", "foovalue");
      CheckGet(db, roptions, "bar", "barvalue");
    }
    rocksdb_options_set_filter_policy(options, NULL);
    rocksdb_filterpolicy_destroy(policy);
  }

  StartPhase("compaction_filter");
  {
    rocksdb_options_t* options = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(options, 1);
    rocksdb_compactionfilter_t* cfilter;
    cfilter = rocksdb_compactionfilter_create(NULL, CFilterDestroy,
                                              CFilterFilter, CFilterName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_compaction_filter(options, cfilter);
    db = CheckCompaction(db, options, roptions, woptions);

    rocksdb_options_set_compaction_filter(options, NULL);
    rocksdb_compactionfilter_destroy(cfilter);
    rocksdb_options_destroy(options);
  }

  StartPhase("compaction_filter_factory");
  {
    rocksdb_options_t* options = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(options, 1);
    rocksdb_compactionfilterfactory_t* factory;
    factory = rocksdb_compactionfilterfactory_create(
        NULL, CFilterFactoryDestroy, CFilterCreate, CFilterFactoryName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_compaction_filter_factory(options, factory);
    db = CheckCompaction(db, options, roptions, woptions);

    rocksdb_options_set_compaction_filter_factory(options, NULL);
    rocksdb_options_destroy(options);
  }

  StartPhase("merge_operator");
  {
    rocksdb_mergeoperator_t* merge_operator;
    merge_operator = rocksdb_mergeoperator_create(
        NULL, MergeOperatorDestroy, MergeOperatorFullMerge,
        MergeOperatorPartialMerge, NULL, MergeOperatorName);
    // Create new database
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_set_merge_operator(options, merge_operator);
    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "foovalue");
    rocksdb_merge(db, woptions, "foo", 3, "barvalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "fake");

    // Merge of a non-existing value
    rocksdb_merge(db, woptions, "bar", 3, "barvalue", 8, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "bar", "fake");

  }

  StartPhase("columnfamilies");
  {
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    CheckNoError(err)

    rocksdb_options_t* db_options = rocksdb_options_create();
    rocksdb_options_set_create_if_missing(db_options, 1);
    db = rocksdb_open(db_options, dbname, &err);
    CheckNoError(err)
    rocksdb_column_family_handle_t* cfh;
    cfh = rocksdb_create_column_family(db, db_options, "cf1", &err);
    rocksdb_column_family_handle_destroy(cfh);
    CheckNoError(err);
    rocksdb_close(db);

    size_t cflen;
    char** column_fams = rocksdb_list_column_families(db_options, dbname, &cflen, &err);
    CheckNoError(err);
    // TODO column_families vals seg fault
    CheckEqual("default", column_fams[0], 7);
    CheckEqual("cf1", column_fams[1], 3);
    CheckCondition(cflen == 2);
    rocksdb_list_column_families_destroy(column_fams, cflen);

    rocksdb_options_t* cf_options = rocksdb_options_create();

    const char* cf_names[2] = {"default", "cf1"};
    const rocksdb_options_t* cf_opts[2] = {cf_options, cf_options};
    rocksdb_column_family_handle_t* handles[2];
    db = rocksdb_open_column_families(db_options, dbname, 2, cf_names, cf_opts, handles, &err);
    CheckNoError(err);

    rocksdb_put_cf(db, woptions, handles[1], "foo", 3, "hello", 5, &err);
    CheckNoError(err);

    CheckGetCF(db, roptions, handles[1], "foo", "hello");

    rocksdb_delete_cf(db, woptions, handles[1], "foo", 3, &err);
    CheckNoError(err);

    CheckGetCF(db, roptions, handles[1], "foo", NULL);

    rocksdb_writebatch_t* wb = rocksdb_writebatch_create();
    rocksdb_writebatch_put_cf(wb, handles[1], "baz", 3, "a", 1);
    rocksdb_writebatch_clear(wb);
    rocksdb_writebatch_put_cf(wb, handles[1], "bar", 3, "b", 1);
    rocksdb_writebatch_put_cf(wb, handles[1], "box", 3, "c", 1);
    rocksdb_writebatch_delete_cf(wb, handles[1], "bar", 3);
    rocksdb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGetCF(db, roptions, handles[1], "baz", NULL);
    CheckGetCF(db, roptions, handles[1], "bar", NULL);
    CheckGetCF(db, roptions, handles[1], "box", "c");
    rocksdb_writebatch_destroy(wb);

    rocksdb_iterator_t* iter = rocksdb_create_iterator_cf(db, roptions, handles[1]);
    CheckCondition(!rocksdb_iter_valid(iter));
    rocksdb_iter_seek_to_first(iter);
    CheckCondition(rocksdb_iter_valid(iter));

    int i;
    for (i = 0; rocksdb_iter_valid(iter) != 0; rocksdb_iter_next(iter)) {
      i++;
    }
    CheckCondition(i == 1);
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);

    rocksdb_drop_column_family(db, handles[1], &err);
    CheckNoError(err);
    for (i = 0; i < 2; i++) {
      rocksdb_column_family_handle_destroy(handles[i]);
    }
    rocksdb_close(db);
    rocksdb_destroy_db(options, dbname, &err);
    rocksdb_options_destroy(db_options);
    rocksdb_options_destroy(cf_options);
  }

  StartPhase("prefix");
  {
    // Create new database
    rocksdb_filterpolicy_t* policy = rocksdb_filterpolicy_create_bloom(10);
    rocksdb_options_set_filter_policy(options, policy);
    rocksdb_options_set_prefix_extractor(options, rocksdb_slicetransform_create_fixed_prefix(3));
    rocksdb_options_set_hash_skip_list_rep(options, 5000, 4, 4);
    rocksdb_options_set_plain_table_factory(options, 4, 10, 0.75, 16);

    db = rocksdb_open(options, dbname, &err);
    CheckNoError(err);

    rocksdb_put(db, woptions, "foo1", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo2", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "foo3", 4, "foo", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar1", 4, "bar", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar2", 4, "bar", 3, &err);
    CheckNoError(err);
    rocksdb_put(db, woptions, "bar3", 4, "bar", 3, &err);
    CheckNoError(err);

    rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions);
    CheckCondition(!rocksdb_iter_valid(iter));

    rocksdb_iter_seek(iter, "bar", 3);
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    CheckCondition(rocksdb_iter_valid(iter));

    CheckIter(iter, "bar1", "bar");
    rocksdb_iter_next(iter);
    CheckIter(iter, "bar2", "bar");
    rocksdb_iter_next(iter);
    CheckIter(iter, "bar3", "bar");
    rocksdb_iter_get_error(iter, &err);
    CheckNoError(err);
    rocksdb_iter_destroy(iter);
    rocksdb_filterpolicy_destroy(policy);
  }


  StartPhase("cleanup");
  rocksdb_close(db);
  rocksdb_options_destroy(options);
  rocksdb_readoptions_destroy(roptions);
  rocksdb_writeoptions_destroy(woptions);
  rocksdb_cache_destroy(cache);
  rocksdb_comparator_destroy(cmp);
  rocksdb_env_destroy(env);

  fprintf(stderr, "PASS\n");
  return 0;
}