Beispiel #1
0
/*
** Locate a user function given a name, a number of arguments and a flag
** indicating whether the function prefers UTF-16 over UTF-8.  Return a
** pointer to the FuncDef structure that defines that function, or return
** NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed.  When createFlag is true
** and the nArg parameter is -1, then only a function that accepts
** any number of arguments will be returned.
**
** If createFlag is false and nArg is -1, then the first valid
** function found is returned.  A function is valid if either xFunc
** or xStep is non-zero.
**
** If createFlag is false, then a function with the required name and
** number of arguments may be returned even if the eTextRep flag does not
** match that requested.
*/
FuncDef *sqlite3FindFunction(
  sqlite3 *db,       /* An open database */
  const char *zName, /* Name of the function.  Not null-terminated */
  int nName,         /* Number of characters in the name */
  int nArg,          /* Number of arguments.  -1 means any number */
  u8 enc,            /* Preferred text encoding */
  int createFlag     /* Create new entry if true and does not otherwise exist */
){
  FuncDef *p;         /* Iterator variable */
  FuncDef *pBest = 0; /* Best match found so far */
  int bestScore = 0;  /* Score of best match */
  int h;              /* Hash value */


  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  if( nArg<-1 ) nArg = -1;
  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);

  /* First search for a match amongst the application-defined functions.
  */
  p = functionSearch(&db->aFunc, h, zName, nName);
  while( p ){
    int score = matchQuality(p, nArg, enc);
    if( score>bestScore ){
      pBest = p;
      bestScore = score;
    }
    p = p->pNext;
  }

  /* If no match is found, search the built-in functions.
  **
  ** Except, if createFlag is true, that means that we are trying to
  ** install a new function.  Whatever FuncDef structure is returned will
  ** have fields overwritten with new information appropriate for the
  ** new function.  But the FuncDefs for built-in functions are read-only.
  ** So we must not search for built-ins when creating a new function.
  */ 
  if( !createFlag && !pBest ){
    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
    p = functionSearch(pHash, h, zName, nName);
    while( p ){
      int score = matchQuality(p, nArg, enc);
      if( score>bestScore ){
        pBest = p;
        bestScore = score;
      }
      p = p->pNext;
    }
  }

  /* If the createFlag parameter is true and the search did not reveal an
  ** exact match for the name, number of arguments and encoding, then add a
  ** new entry to the hash table and return it.
  */
  if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && 
      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
    pBest->zName = (char *)&pBest[1];
    pBest->nArg = (u16)nArg;
    pBest->iPrefEnc = enc;
    memcpy(pBest->zName, zName, nName);
    pBest->zName[nName] = 0;
    sqlite3FuncDefInsert(&db->aFunc, pBest);
  }

  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
    return pBest;
  }
  return 0;
}
Beispiel #2
0
/*
** Locate a user function given a name, a number of arguments and a flag
** indicating whether the function prefers UTF-16 over UTF-8.  Return a
** pointer to the FuncDef structure that defines that function, or return
** NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed.
**
** If nArg is -2, then the first valid function found is returned.  A
** function is valid if xSFunc is non-zero.  The nArg==(-2)
** case is used to see if zName is a valid function name for some number
** of arguments.  If nArg is -2, then createFlag must be 0.
**
** If createFlag is false, then a function with the required name and
** number of arguments may be returned even if the eTextRep flag does not
** match that requested.
*/
FuncDef *sqlite3FindFunction(
  sqlite3 *db,       /* An open database */
  const char *zName, /* Name of the function.  zero-terminated */
  int nArg,          /* Number of arguments.  -1 means any number */
  u8 enc,            /* Preferred text encoding */
  u8 createFlag      /* Create new entry if true and does not otherwise exist */
){
  FuncDef *p;         /* Iterator variable */
  FuncDef *pBest = 0; /* Best match found so far */
  int bestScore = 0;  /* Score of best match */
  int h;              /* Hash value */
  int nName;          /* Length of the name */

  assert( nArg>=(-2) );
  assert( nArg>=(-1) || createFlag==0 );
  nName = sqlite3Strlen30(zName);

  /* First search for a match amongst the application-defined functions.
  */
  p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
  while( p ){
    int score = matchQuality(p, nArg, enc);
    if( score>bestScore ){
      pBest = p;
      bestScore = score;
    }
    p = p->pNext;
  }

  /* If no match is found, search the built-in functions.
  **
  ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
  ** functions even if a prior app-defined function was found.  And give
  ** priority to built-in functions.
  **
  ** Except, if createFlag is true, that means that we are trying to
  ** install a new function.  Whatever FuncDef structure is returned it will
  ** have fields overwritten with new information appropriate for the
  ** new function.  But the FuncDefs for built-in functions are read-only.
  ** So we must not search for built-ins when creating a new function.
  */ 
  if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
    bestScore = 0;
    h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
    p = functionSearch(h, zName);
    while( p ){
      int score = matchQuality(p, nArg, enc);
      if( score>bestScore ){
        pBest = p;
        bestScore = score;
      }
      p = p->pNext;
    }
  }

  /* If the createFlag parameter is true and the search did not reveal an
  ** exact match for the name, number of arguments and encoding, then add a
  ** new entry to the hash table and return it.
  */
  if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
    FuncDef *pOther;
    pBest->zName = (const char*)&pBest[1];
    pBest->nArg = (u16)nArg;
    pBest->funcFlags = enc;
    memcpy((char*)&pBest[1], zName, nName+1);
    pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
    if( pOther==pBest ){
      sqlite3DbFree(db, pBest);
      sqlite3OomFault(db);
      return 0;
    }else{
      pBest->pNext = pOther;
    }
  }

  if( pBest && (pBest->xSFunc || createFlag) ){
    return pBest;
  }
  return 0;
}
Beispiel #3
0
void Query::performQuery()
{
	vector<int> impNodes;
	
	topKImpNodes(G, (*pOrthinfolist), params.n_imp, impNodes);
	debug(46, "got important nodes\n");
	debug(47, "got important nodes2\n");
	DBMatchMap dbmap;

	Filter filter;
	filter.setDatabase(db);
	filter.setQueryParams(params);
	filter.setGraph(G, pOrthinfolist);
	filter.setTablePrefix(tablePrefix);

	vector<Neighborhood> nbs;
	getAllNeighborhoods(G, (*pOrthinfolist), nbs);
	debug(46, "got all neighbors2\n");
	debug(47, "got all neighbors\n");
	for(int i=0; i<params.n_imp; i++)	
	{
		filter.probeIndex(nbs[impNodes[i]], dbmap);
	}
	vector<Gid_Num_Score> gnsvec;
	DBMatchMap::iterator iter;
	int allhits=0;
	debug(46, "after filtering\n");
	debug(47, "after filtering2\n");
	for(iter=dbmap.begin(); iter!=dbmap.end(); iter++)
	{
		Gid_Num_Score gns;
		gns.dbgid=iter->first;
		gns.numMatches=iter->second->size();
		gns.score=0;
		if(gns.numMatches==params.n_imp)
			allhits++;
		for(int i=0; i<gns.numMatches; i++)
			gns.score+=(*(iter->second))[i]->minscore;
		gnsvec.push_back(gns);
		//debug(41, "dbgid: "<<gns.dbgid<<endl);
		//debug(41, "num Node matches: "<<gns.numMatches<<endl);
		//debug(41, "Total min score: "<<gns.score<<endl);
	}
	
	int dbnum=max(params.top_k, allhits);
	std::sort(gnsvec.begin(), gnsvec.end(), orderGidNumScore);
	
	//clean the hash_map
	for(unsigned int i=dbnum; i<gnsvec.size(); i++)
	{
		//debug(41, "remove dbgid= "<< gnsvec[i].dbgid<<endl);
		removeFromMatchMap(gnsvec[i].dbgid, dbmap);
	}	

	debug(46, "before the actual match \n");
	debug(47, "before the actual match \n");
	
	//now is the match algorithm
	for(iter=dbmap.begin(); iter!=dbmap.end(); iter++)
	{
		//call mcb_matching to find the best match for the important nodes
		debug(41, "match dbgid = "<<iter->first<<"\n");
		OrthologInfoList orthinfolist_db;
		
		HashGraph* DBG=readGraphFromDB(db, tablePrefix, iter->first, orthinfolist_db);
		
		GraphMatch* gm=new GraphMatch;
		gm->dbgid=iter->first;
		
		mcb_matching(DBG, *(iter->second), gm);
		debug(46, "gm->dbgid"<<gm->dbgid<<endl);
		debug(47, "gm->dbgid2 = 2 = "<<gm->dbgid<<endl);
		debug(46, "before growMatch\n");
		 debug(47, "before growMatch2\n");
		debug(46, "DBG->n()"<<DBG->n()<<endl);
		debug(47, "DBG->n()"<<DBG->n()<<endl);
		debug(46, "orthinfolist_db[0].size()"<<orthinfolist_db[0].size()<<endl);
		debug(46, "gm->mappings.size()"<<gm->mappings.size()<<endl);
		 debug(47, "orthinfolist_db[0].size()"<<orthinfolist_db[0].size()<<endl);
                debug(47, "gm->mappings.size()"<<gm->mappings.size()<<endl);
			
		growMatch(nbs, DBG, &orthinfolist_db, gm);
		gm->score=matchQuality(DBG, gm);
		mlist.push_back(gm);
	
		delete DBG;
		debug(46, "grow match succeeds\n");
		debug(47, "grow match succeeds\n");
	}	
}