//////////////////////////////////////////////////////////////////////////////////////////
///Creates the XML database file from a set of filepaths and returns number of new paths
int FileSniffer::createDatabase(std::set<std::string>& filelist)
{
	int count=0;
	try{
	XmlWriter writer;	//create new xml 
	writer.addDeclaration();
	writer.start("filelist");
	newfilesList.clear();
	for (std::set<std::string>::iterator it=filelist.begin(); it != filelist.end(); it++)	//add all paths in fileset to the xml
	{
		if(!isInExcluded(*it))
		{
			count++;
			XmlWriter childnode;
			childnode.start("filepath");	//add filepath tag for each path
			childnode.addBody(*it);		//add path
			childnode.end();				//add ending tag
			writer.addBody(childnode.xml());
			newfilesList.insert(*it);
		}
	}
	writer.end();
	std::ofstream myfile;	//create new file and write the xml in it
	myfile.open (databaseFilePath.c_str());
	myfile << writer.xml();
	myfile.close();
	StoreResult();
	}
	catch(std::exception ex)
    {
      std::cout << "\n  " << ex.what() << "\n\n";
    }
	return count;
}
//////////////////////////////////////////////////////////////
///Sniffs for new files and directories
void FileSniffer::sniffFiles(std::string path)
{
	NavWrapper filescanner;
	std::set<std::string> filelist;
	std::cout<<"Scanning for files and folders..."<<std::endl;
	try{
	filelist = filescanner.getFileDirList(path);	//gets a set of folder and file paths by recursively scanning specified path
//	if(!summary)
//		printList(filelist);
	std::cout<<"\nCompleted\n\n"<<filelist.size()<<" files found on the specified path"<<std::endl;
	if(filelist.size()==0)	//return if no paths in the set
		return;
	std::set<std::string>::iterator it;
	if(!fexists(databaseFilePath.c_str()))	//check if database file exists
	{
		std::cout<<"\nDatabase not found\n";
		newfilesList = filelist;
	/*	std::cout<<"Database not found, Creating new database and adding files to it..."<<std::endl;
		int count = createDatabase(filelist);	//create new database if not found
		if(filelist.size()!=0)
			std::cout<<std::endl<<count<<" file(s) and folder(s) added to the database"<<std::endl;*/
	}
	else
	{
		std::cout<<"\nLoading database into memory...\n";
		loadDatabase();	//load database into memory in form of a set
		std::cout<<"Completed";
		findNewFiles(filelist);	//find new files in the path set by comparing it with database
		StoreResult();	//store analysis results in xml file
	}
	}catch(std::exception ex)
	{
		std::cout << "\n  " << ex.what() << "\n\n";
	}
	
}
bool CMySQLQuery::Execute(MYSQL *mysql_connection)
{
    bool ret_val = false;
    char log_funcname[64];
    if (Unthreaded)
        sprintf(log_funcname, "CMySQLQuery::Execute");
    else
        sprintf(log_funcname, "CMySQLQuery::Execute[%s]", Callback.Name.c_str());

    CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "starting query execution");

    chrono::steady_clock::time_point query_exec = chrono::steady_clock::now();
    int query_error = mysql_real_query(mysql_connection, Query.c_str(), Query.length());
    chrono::steady_clock::duration query_exec_duration = chrono::steady_clock::now() - query_exec;

    if (query_error == 0)
    {
        unsigned int
        query_exec_time_milli = static_cast<unsigned int>(chrono::duration_cast<chrono::milliseconds>(query_exec_duration).count()),
        query_exec_time_micro = static_cast<unsigned int>(chrono::duration_cast<chrono::microseconds>(query_exec_duration).count());

        CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "query was successfully executed within %d.%d milliseconds", query_exec_time_milli, query_exec_time_micro-(query_exec_time_milli*1000));

        MYSQL_RES *mysql_result = mysql_store_result(mysql_connection); //this has to be here

        //why should we process the result if it won't and can't be used?
        if (Unthreaded || Callback.Name.length() > 0)
        {
            if (StoreResult(mysql_connection, mysql_result) == false)
                CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "an error occured while storing the result: (error #%d) \"%s\"", mysql_errno(mysql_connection), mysql_error(mysql_connection));
            else
            {
                Result->m_Query = Query;
                Result->m_ExecTime[UNIT_MILLISECONDS] = query_exec_time_milli;
                Result->m_ExecTime[UNIT_MICROSECONDS] = query_exec_time_micro;
            }
        }
        else  //no callback was specified
            CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "no callback specified, skipping result saving");

        if (mysql_result != NULL)
            mysql_free_result(mysql_result);

        ret_val = true;
    }
    else  //mysql_real_query failed
    {
        int error_id = mysql_errno(mysql_connection);
        string error_str(mysql_error(mysql_connection));

        CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "(error #%d) %s", error_id, error_str.c_str());

        if (!Unthreaded)
        {
            //forward OnQueryError(error_id, error[], callback[], query[], connectionHandle);
            //recycle these structures, change some data

            Orm.Object = NULL;
            Orm.Type = 0;

            while (Callback.Params.size() > 0)
                Callback.Params.pop();


            Callback.Params.push(static_cast<cell>(error_id));
            Callback.Params.push(error_str);
            Callback.Params.push(Callback.Name);
            Callback.Params.push(Query);
            Callback.Params.push(static_cast<cell>(Handle->GetID()));

            Callback.Name = "OnQueryError";

            CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "error will be triggered in OnQueryError");
        }
        ret_val = false;
    }

    if(Unthreaded == false) //decrease counter only if threaded query
        Handle->DecreaseQueryCounter();
    return ret_val;
}
Beispiel #4
0
static int negascout(struct SearchData *sd,
                     int alpha,
                     int beta,
                     const int depth,
                     int node_type
#if MP
                     ,int exclusiveP
#endif /* MP  */
                    ) {
    struct Position *p = sd->position;
    struct SearchStatus *st;
    int best = -INF;
    int bestm = M_NONE;
    int tmp;
    int talpha;
    int incheck;
    int lmove;
    int move;
    int extend = 0;
    int threat = FALSE;
    int reduce_extensions;
    int next_type;
    int was_futile = FALSE;
#if FUTILITY
    int is_futile;
    int optimistic = 0;
#endif

#if MP
    int deferred_cnt = 0;
    int deferred_list[MAX_DEFERRED];
    int deferred_depth[MAX_DEFERRED];
#endif

    EnterNode(sd);

    Nodes++;

    /* check for search termination */
    if (sd->master && TerminateSearch(sd)) {
        AbortSearch = TRUE;
        goto EXIT;
    }

    /* max search depth reached */
    if (sd->ply >= MaxDepth) goto EXIT;

    /*
     * Check for insufficent material or theoretical draw.
     */

    if ( /* InsufMat(p) || CheckDraw(p) || */  Repeated(p, FALSE)) {
        best = 0;
        goto EXIT;
    }

    /*
     * check extension
     */

    incheck = InCheck(p, p->turn);
    if (incheck && p->material[p->turn] > 0) {
        extend += CheckExtend(p);
        ChkExt++;
    }

    /*
     * Check the hashtable
     */

    st = sd->current;

    HTry++;
#if MP
    switch (ProbeHT(p->hkey, &tmp, depth, &(st->st_hashmove), &threat, sd->ply,
                    exclusiveP, sd->localHashTable))
#else
    switch (ProbeHT(p->hkey, &tmp, depth, &(st->st_hashmove), &threat, sd->ply))
#endif /* MP */
    {
    case ExactScore:
        HHit++;
        best = tmp;
        goto EXIT;
    case UpperBound:
        if (tmp <= alpha) {
            HHit++;
            best = tmp;
            goto EXIT;
        }
        break;
    case LowerBound:
        if (tmp >= beta) {
            HHit++;
            best = tmp;
            goto EXIT;
        }
        break;
    case Useless:
        threat = !incheck && MateThreat(p, OPP(p->turn));
        break;
#if MP
    case OnEvaluation:
        best = -ON_EVALUATION;
        goto EXIT;
#endif
    }

    /*
     * Probe EGTB
     */

    if (depth > EGTBDepth && ProbeEGTB(p, &tmp, sd->ply)) {
        best = tmp;
        goto EXIT;
    }

    /*
     * Probe recognizers
     */

    switch (ProbeRecognizer(p, &tmp)) {
    case ExactScore:
        best = tmp;
        goto EXIT;
    case LowerBound:
        if (tmp >= beta) {
            best = tmp;
            goto EXIT;
        }
        break;
    case UpperBound:
        if (tmp <= alpha) {
            best = tmp;
            goto EXIT;
        }
        break;
    }

#if NULLMOVE

    /*
     * Null move search.
     * See Christian Donninger, "Null Move and Deep Search"
     * ICCA Journal Volume 16, No. 3, pp. 137-143
     */

    if (!incheck && node_type == CutNode && !threat) {
        int next_depth;
        int nms;

        next_depth = depth - ReduceNullMove;

        if (next_depth > 0) {
            next_depth = depth - ReduceNullMoveDeep;
        }

        DoNull(p);
        if (next_depth < 0) {
            nms = -quies(sd, -beta, -beta+1, 0);
        } else {
#if MP
            nms = -negascout(sd, -beta, -beta+1, next_depth, AllNode, 0);
#else
            nms = -negascout(sd, -beta, -beta+1, next_depth, AllNode);
#endif
        }
        UndoNull(p);

        if (AbortSearch) goto EXIT;
        if (nms >= beta) {
            if (p->nonPawn[p->turn] >= Value[Queen]) {
                best = nms;
                goto EXIT;
            } else {
                if (next_depth < 0) {
                    nms = quies(sd, beta-1, beta, 0);
                } else {
#if MP
                    nms = negascout(sd, beta-1, beta, next_depth, CutNodeNoNull,
                                    0);
#else
                    nms = negascout(sd, beta-1, beta, next_depth,
                                    CutNodeNoNull);
#endif
                }

                if (nms >= beta) {
                    best = nms;
                    goto EXIT;
                } else {
                    extend += ExtendZugzwang;
                    ZZExt++;
                }
            }
        } else if (nms <= -CMLIMIT) {
            threat = TRUE;
        }
    }
#endif /* NULLMOVE */

    lmove = (p->actLog-1)->gl_Move;
    reduce_extensions = (sd->ply > 2*sd->depth);
    talpha = alpha;

    switch (node_type) {
    case AllNode:
        next_type = CutNode;
        break;
    case CutNode:
    case CutNodeNoNull:
        next_type = AllNode;
        break;
    default:
        next_type = PVNode;
        break;
    }

#if FUTILITY
    is_futile = !incheck && !threat && alpha < CMLIMIT && alpha > -CMLIMIT;
    if (is_futile) {
        if (p->turn == White) {
            optimistic = MaterialBalance(p) + MaxPos;
        } else {
            optimistic = -MaterialBalance(p) + MaxPos;
        }
    }
#endif /* FUTILITY */

    /*
     * Internal iterative deepening. If we do not have a move, we try
     * a shallow search to find a good candidate.
     */

    if (depth > 2*OnePly && ((alpha + 1) != beta) && !LegalMove(p, st->st_hashmove)) {
        int useless;
#if MP
        useless = negascout(sd, alpha, beta, depth-2*OnePly, PVNode, 0);
#else
        useless = negascout(sd, alpha, beta, depth-2*OnePly, PVNode);
#endif
        st->st_hashmove = sd->pv_save[sd->ply+1];
    }

    /*
     * Search all legal moves
     */

    while ((move = incheck ? NextEvasion(sd) : NextMove(sd)) != M_NONE) {
        int next_depth = extend;

        if (move & M_CANY && !MayCastle(p, move)) continue;

        /*
         * recapture extension
         */

        if ((move & M_CAPTURE) && (lmove & M_CAPTURE) &&
                M_TO(move) == M_TO(lmove) &&
                IsRecapture(p->piece[M_TO(move)], (p->actLog-1)->gl_Piece)) {
            RCExt += 1;
            next_depth += ExtendRecapture[TYPE(p->piece[M_TO(move)])];
        }

        /*
         * passed pawn push extension
         */

        if (TYPE(p->piece[M_FROM(move)]) == Pawn &&
                p->nonPawn[OPP(p->turn)] <= Value[Queen]) {

            int to = M_TO(move);

            if (((p->turn == White && to >= a7)
                    || (p->turn == Black && to <= h2))
                    && IsPassed(p, to, p->turn) && SwapOff(p, move) >= 0) {
                next_depth += ExtendPassedPawn;
                PPExt += 1;
            }
        }

        /*
         * limit extensions to sensible range.
         */

        if (reduce_extensions) next_depth /= 2;

        next_depth += depth - OnePly;

#if FUTILITY

        /*
         * Futility cutoffs
         */

        if (is_futile) {
            if (next_depth < 0 && !IsCheckingMove(p, move)) {
                tmp = optimistic + ScoreMove(p, move);
                if (tmp <= alpha) {
                    if (tmp > best) {
                        best = tmp;
                        bestm = move;
                        was_futile = TRUE;
                    }
                    continue;
                }
            }
#if EXTENDED_FUTILITY

            /*
             * Extended futility cutoffs and limited razoring.
             * See Ernst A. Heinz, "Extended Futility Pruning"
             * ICCA Journal Volume 21, No. 2, pp 75-83
             */

            else if (next_depth >= 0 && next_depth < OnePly
                     && !IsCheckingMove(p, move)) {
                tmp = optimistic + ScoreMove(p, move) + (3*Value[Pawn]);
                if (tmp <= alpha) {
                    if (tmp > best) {
                        best = tmp;
                        bestm = move;
                        was_futile = TRUE;
                    }
                    continue;
                }
            }
#if RAZORING
            else if (next_depth >= OnePly && next_depth < 2*OnePly
                     && !IsCheckingMove(p, move)) {
                tmp = optimistic + ScoreMove(p, move) + (6*Value[Pawn]);
                if (tmp <= alpha) {
                    next_depth -= OnePly;
                }
            }
#endif /* RAZORING */
#endif /* EXTENDED_FUTILITY */
        }

#endif /* FUTILITY */

        DoMove(p, move);
        if (InCheck(p, OPP(p->turn))) {
            UndoMove(p, move);
        } else {
            /*
             * Check extension
             */

            if (p->material[p->turn] > 0 && InCheck(p, p->turn)) {
                next_depth += (reduce_extensions) ?
                              ExtendInCheck>>1 : ExtendInCheck;
            }

            /*
             * Recursively search this position. If depth is exhausted, use
             * quies, otherwise use negascout.
             */

            if (next_depth < 0) {
                tmp = -quies(sd, -beta, -talpha, 0);
            } else if (bestm != M_NONE && !was_futile) {
#if MP
                tmp = -negascout(sd, -talpha-1, -talpha, next_depth, next_type,
                                 bestm != M_NONE);
                if (tmp != ON_EVALUATION && tmp > talpha && tmp < beta) {
                    tmp = -negascout(sd, -beta, -tmp, next_depth,
                                     node_type == PVNode ? PVNode : AllNode,
                                     bestm != M_NONE);
                }
#else
                tmp = -negascout(sd, -talpha-1, -talpha, next_depth, next_type);
                if (tmp > talpha && tmp < beta) {
                    tmp = -negascout(sd, -beta, -tmp, next_depth,
                                     node_type == PVNode ? PVNode : AllNode);
                }
#endif /* MP */
            } else {
#if MP
                tmp = -negascout(sd, -beta, -talpha, next_depth, next_type,
                                 bestm != M_NONE);
#else
                tmp = -negascout(sd, -beta, -talpha, next_depth, next_type);
#endif /* MP */
            }

            UndoMove(p, move);

            if (AbortSearch) goto EXIT;

#if MP
            if (tmp == ON_EVALUATION) {

                /*
                 * This child is ON_EVALUATION. Remember move and
                 * depth.
                 */

                deferred_list[deferred_cnt] = move;
                deferred_depth[deferred_cnt] = next_depth;
                deferred_cnt++;

            } else {
#endif /* MP */

                /*
                 * beta cutoff, enter move in Killer/Countermove table
                 */

                if (tmp >= beta) {
                    if (!(move & M_TACTICAL)) {
                        PutKiller(sd, move);
                        sd->counterTab[p->turn][lmove & 4095] = move;
                    }
                    StoreResult(sd, tmp, alpha, beta, move, depth, threat);
                    best = tmp;
                    goto EXIT;
                }

                /*
                 * Improvement on best move to date
                 */

                if (tmp > best) {
                    best = tmp;
                    bestm = move;
                    was_futile = FALSE;

                    if (best > talpha) {
                        talpha = best;
                    }
                }

                next_type = CutNode;
#if MP
            }
#endif /* MP */
        }
    }