예제 #1
0
파일: pDB.cpp 프로젝트: weyrick/corvus
//template <typename LTYPE>
void pDB::list_query(pStringRef query, RowList &result) const {
    sqlite3_stmt *stmt;
    if (trace_) {
        std::cerr << "TRACE: " << query.str() << "\n";
    }
    int rc = sqlite3_prepare_v2(db_, query.str().c_str(), -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        std::cerr << "sqlite error: " << query.str() << "\n";
        std::cerr << sqlite3_errmsg(db_) << "\n";
        exit(1);
    }
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        //typename LTYPE::value_type f;
        RowList::value_type f;
        for (int i = 0; i < sqlite3_column_count(stmt); i++) {
            pStringRef key = sqlite3_column_name(stmt, i);
            pStringRef val;
            char *cVal = (char*)sqlite3_column_text(stmt, i);
            if (cVal)
                val = cVal;
            f.set(key, val);
        }
        result.push_back(f);
    }
    if (trace_) {
        std::cerr << "TRACE: " << result.size() << " rows returned\n";
    }
    sqlite3_finalize(stmt);
}
예제 #2
0
void ml::TakeLabelCensus(const RowList & Examples,
                         map<int, size_t> & LabelCensus, size_t LabelAttributes,
                         int LCValue)
{
    
    ml::GenericTakeLabelCensus<RowList::const_iterator, int>
        (Examples.begin(), Examples.end(), LabelCensus, LabelAttributes, 
            LCValue);
}
예제 #3
0
QString MyPrint::printOnlyTable(QSqlQueryModel *model, RowList colHeaders, int groupCols)
{
    QString body;
    QStringList colls;
    body.append("<table border='1' class = 'mine'><tr>");
    RowList::const_iterator i = colHeaders.constBegin();
    while (i != colHeaders.constEnd()) {
        colls.append(QString::number(i.key()));
        body.append("<th>" + i.value() + "</th>\n");
        ++i;
    }
    body.append("</tr>\n");
    while (model->canFetchMore())
        model->fetchMore();
    QMap<int,QString> groupTitles; // заголовки групп groupTitles(номер колонки, название группы)
    QMap<int,int> groupCounts; // количество элементов в группе groupCounts(номер колонки, кол-во)
    for (int index = 0; index < model->rowCount(); index++)
    {
        body.append("<tr>\n");
        for (int listId = 0; listId < colls.count(); listId++)
        {
            if(listId < groupCols) // если колонки нужно сгруппировать(если это групируемая колонка)
            {
                if(groupTitles.value(listId) != model->record(index).value(colls[listId].toInt()).toString())
                {
                    groupTitles[listId] = model->record(index).value(colls[listId].toInt()).toString();
                    body.append("<td class='grHead' colspan = '"+QString::number(colls.count()-listId)+"'><b>");
                    body.append(groupTitles[listId]);
                    body.append("</b></td>\n</tr>\n<tr>\n");
                    if(listId == groupCols-1)
                        for(int i=0; i < listId + 1; i++)
                            body.append("<td>1</td>\n");
                    else
                        for(int i=0; i < listId + 1; i++)
                            body.append("<td></td>\n");
                    groupCounts[listId] = 1;
                }
                else
                {
                    groupCounts[listId] = groupCounts[listId] + 1;
                    body.append("<td>" + QString::number(groupCounts[listId]) + "</td>\n");
                }
            }
            else
            {
                body.append("<td>");
                body.append(model->record(index).value(colls[listId].toInt()).toString());
                body.append("</td>\n");
            }
        }
        body.append("</tr>\n");
    }
    body.append("</table>");
    return body;
}
예제 #4
0
void ml::GetRowList(RowList & l, Matrix & Features, 
        const std::vector<double> & Labels)
{
    l.clear();
    if (Features.rows() != Labels.size())
    {
        throw ArgumentException("Labels don't match Features given.");
    }
    for (int i = 0; i < Features.rows(); i++)
    {
        l.push_back(make_pair(&Features[i], Labels[i]));
    }
}
RowList RelationFilterProxyModel::getCurrentSourceModelRowList() const
{
  RowList list;

  if(columnCount() < 1){
    return list;
  }
  const int n = rowCount();
  for(int row = 0; row < n; ++row){
    const auto idx = index(row, 0);
    list.append( mapToSource(idx).row() );
  }

  return list;
}
예제 #6
0
void ml::TakeFeatureCensus(const RowList & Examples,
        map<size_t, map<double, size_t> > & FeatureCensus,
        const std::vector< std::pair<size_t, double> > & FeatureAttributes)

{
    typedef typename std::map<double,size_t>::iterator CensusIterator;
    FeatureCensus.clear();
    
    // set non-nominal census to expected, the count to 0, and 
    // otherwise zero-out the nominal census.
    for (int i = 0; i < FeatureAttributes.size(); i++)
    {
        if (FeatureAttributes[i].first != 0)
        {
            for (int j = 0; j < FeatureAttributes[i].first; j++)
            {
                FeatureCensus[i][(double)j] = 0u;
            }
        }
    }

    for (RowListCIterator Row = Examples.begin(); 
            Row != Examples.end();
            Row++)
    {
        for (int i = 0; i < FeatureAttributes.size(); i++)
        {
            if (FeatureAttributes[i].first == 0)
            {
                continue;
            }
            if (Row->first->at(i) == UNKNOWN_VALUE)
            {
                FeatureCensus[i][FeatureAttributes[i].second]++;
            }
            else
            {
                FeatureCensus[i][Row->first->at(i)]++;
            }
        }
    }
}
//StopWatch timer;
DMLPackageProcessor::DMLResult
UpdatePackageProcessor::processPackage(dmlpackage::CalpontDMLPackage& cpackage)
{
    SUMMARY_INFO("UpdatePackageProcessor::processPackage");

    std::string err;
    DMLResult result;
    result.result = NO_ERROR;
    result.rowCount = 0;
    BRM::TxnID txnid;
    // set-up the transaction
    txnid.id  = cpackage.get_TxnID();
    txnid.valid = true;
    fSessionID = cpackage.get_SessionID();
    VERBOSE_INFO("Processing Update DML Package...");
    TablelockData* tablelockData = TablelockData::makeTablelockData(fSessionID);
    uint64_t uniqueId = 0;

    //Bug 5070. Added exception handling
    try
    {
        uniqueId = fDbrm->getUnique64();
    }
    catch (std::exception& ex)
    {
        logging::Message::Args args;
        logging::Message message(9);
        args.add(ex.what());
        message.format(args);
        result.result = UPDATE_ERROR;
        result.message = message;
        fSessionManager.rolledback(txnid);
        return result;
    }
    catch ( ... )
    {
        logging::Message::Args args;
        logging::Message message(9);
        args.add("Unknown error occured while getting unique number.");
        message.format(args);
        result.result = UPDATE_ERROR;
        result.message = message;
        fSessionManager.rolledback(txnid);
        return result;
    }

    uint64_t tableLockId = 0;
    boost::shared_ptr<CalpontSystemCatalog> systemCatalogPtr = CalpontSystemCatalog::makeCalpontSystemCatalog(fSessionID);
    CalpontSystemCatalog::TableName tableName;
    // get the table object from the package
    DMLTable* tablePtr =  cpackage.get_Table();
    tableName.schema = tablePtr->get_SchemaName();
    tableName.table = tablePtr->get_TableName();
    fWEClient->addQueue(uniqueId);
    execplan::CalpontSystemCatalog::ROPair roPair;

//#ifdef PROFILE
//	StopWatch timer;
//#endif
    try
    {
        LoggingID logid( DMLLoggingId, fSessionID, txnid.id);
        logging::Message::Args args1;
        logging::Message msg(1);
        args1.add("Start SQL statement: ");
        ostringstream oss;
        oss << cpackage.get_SQLStatement() << "|" << tablePtr->get_SchemaName() << "|";
        args1.add(oss.str());
        msg.format( args1 );
        logging::Logger logger(logid.fSubsysID);
        logger.logMessage(LOG_TYPE_DEBUG, msg, logid);

        VERBOSE_INFO("The table name is:");
        VERBOSE_INFO(tablePtr->get_TableName());

        if (0 != tablePtr)
        {
            // get the row(s) from the table
            RowList rows = tablePtr->get_RowList();

            if (rows.size() == 0)
            {
                SUMMARY_INFO("No row to update!");
                fWEClient->removeQueue(uniqueId);
                return result;
            }

            roPair = systemCatalogPtr->tableRID(tableName);
            tableLockId = tablelockData->getTablelockId(roPair.objnum); //check whether this table is locked already for this session

            if (tableLockId == 0)
            {
                //cout << "tablelock is not found in cache, getting from dbrm" << endl;
                uint32_t  processID = ::getpid();
                int32_t   txnId = txnid.id;
                int32_t sessionId = fSessionID;
                std::string  processName("DMLProc");
                int i = 0;
                OamCache* oamcache = OamCache::makeOamCache();
                std::vector<int> pmList = oamcache->getModuleIds();
                std::vector<uint32_t> pms;

                for (unsigned i = 0; i < pmList.size(); i++)
                {
                    pms.push_back((uint32_t)pmList[i]);
                }

                try
                {
                    tableLockId = fDbrm->getTableLock(pms, roPair.objnum, &processName, &processID, &sessionId, &txnId, BRM::LOADING );
                }
                catch (std::exception&)
                {
                    throw std::runtime_error(IDBErrorInfo::instance()->errorMsg(ERR_HARD_FAILURE));
                }

                if ( tableLockId  == 0 )
                {
                    int waitPeriod = 10;
                    int sleepTime = 100; // sleep 100 milliseconds between checks
                    int numTries = 10;  // try 10 times per second
                    waitPeriod = Config::getWaitPeriod();
                    numTries = 	waitPeriod * 10;
                    struct timespec rm_ts;

                    rm_ts.tv_sec = sleepTime / 1000;
                    rm_ts.tv_nsec = sleepTime % 1000 * 1000000;

                    for (; i < numTries; i++)
                    {
#ifdef _MSC_VER
                        Sleep(rm_ts.tv_sec * 1000);
#else
                        struct timespec abs_ts;

                        do
                        {
                            abs_ts.tv_sec = rm_ts.tv_sec;
                            abs_ts.tv_nsec = rm_ts.tv_nsec;
                        }
                        while (nanosleep(&abs_ts, &rm_ts) < 0);

#endif

                        try
                        {
                            processID = ::getpid();
                            txnId = txnid.id;
                            sessionId = fSessionID;
                            processName = "DMLProc";
                            tableLockId = fDbrm->getTableLock(pms, roPair.objnum, &processName, &processID, &sessionId, &txnId, BRM::LOADING );
                        }
                        catch (std::exception&)
                        {
                            throw std::runtime_error(IDBErrorInfo::instance()->errorMsg(ERR_HARD_FAILURE));
                        }

                        if (tableLockId > 0)
                            break;
                    }

                    if (i >= numTries) //error out
                    {
                        result.result = UPDATE_ERROR;
                        logging::Message::Args args;
                        string strOp("update");
                        args.add(strOp);
                        args.add(processName);
                        args.add((uint64_t)processID);
                        args.add(sessionId);
                        throw std::runtime_error(IDBErrorInfo::instance()->errorMsg(ERR_TABLE_LOCKED, args));
                    }
                }
            }

            //cout << " tablelock is obtained with id " << tableLockId << endl;
            tablelockData->setTablelock(roPair.objnum, tableLockId);
            //@Bug 4491 start AI sequence for autoincrement column
            const CalpontSystemCatalog::RIDList ridList = systemCatalogPtr->columnRIDs(tableName);
            CalpontSystemCatalog::RIDList::const_iterator rid_iterator = ridList.begin();
            CalpontSystemCatalog::ColType colType;

            while (rid_iterator != ridList.end())
            {
                // If user hit ctrl+c in the mysql console, this will be true.
                if (fRollbackPending)
                {
                    result.result = JOB_CANCELED;
                    break;
                }

                CalpontSystemCatalog::ROPair roPair = *rid_iterator;
                colType = systemCatalogPtr->colType(roPair.objnum);

                if (colType.autoincrement)
                {
                    try
                    {
                        uint64_t nextVal = systemCatalogPtr->nextAutoIncrValue(tableName);
                        fDbrm->startAISequence(roPair.objnum, nextVal, colType.colWidth, colType.colDataType);
                        break; //Only one autoincrement column per table
                    }
                    catch (std::exception& ex)
                    {
                        result.result = UPDATE_ERROR;
                        throw std::runtime_error(ex.what());
                    }
                }

                ++rid_iterator;
            }

            uint64_t  rowsProcessed = 0;

            if (!fRollbackPending)
            {
                rowsProcessed = fixUpRows(cpackage, result, uniqueId, roPair.objnum);
            }

            //@Bug 4994 Cancelled job is not error
            if (result.result == JOB_CANCELED)
                throw std::runtime_error("Query execution was interrupted");

            if ((result.result != 0) && (result.result != DMLPackageProcessor::IDBRANGE_WARNING))
                throw std::runtime_error(result.message.msg());

            result.rowCount = rowsProcessed;

            // Log the update statement.
            LoggingID logid( DMLLoggingId, fSessionID, txnid.id);
            logging::Message::Args args1;
            logging::Message msg(1);
            args1.add("End SQL statement");
            msg.format( args1 );
            logging::Logger logger(logid.fSubsysID);
            logger.logMessage(LOG_TYPE_DEBUG, msg, logid);
            logging::logDML(cpackage.get_SessionID(), txnid.id, cpackage.get_SQLStatement(), cpackage.get_SchemaName());
        }
    }
    catch (std::exception& ex)
    {
        cerr << "UpdatePackageProcessor::processPackage:" << ex.what() << endl;

        if (result.result == 0)
        {
            result.result = UPDATE_ERROR;
        }

        result.message = Message(ex.what());
        result.rowCount = 0;
        LoggingID logid( DMLLoggingId, fSessionID, txnid.id);
        logging::Message::Args args1;
        logging::Message msg(1);
        args1.add("End SQL statement with error");
        msg.format( args1 );
        logging::Logger logger(logid.fSubsysID);
        logger.logMessage(LOG_TYPE_DEBUG, msg, logid);
    }
    catch (...)
    {
        cerr << "UpdatePackageProcessor::processPackage: caught unknown exception!" << endl;
        logging::Message::Args args;
        logging::Message message(7);
        args.add("Update Failed: ");
        args.add("encountered unkown exception");
        args.add("");
        args.add("");
        message.format(args);

        result.result = UPDATE_ERROR;
        result.message = message;
        result.rowCount = 0;
        LoggingID logid( DMLLoggingId, fSessionID, txnid.id);
        logging::Message::Args args1;
        logging::Message msg(1);
        args1.add("End SQL statement with error");
        msg.format( args1 );
        logging::Logger logger(logid.fSubsysID);
        logger.logMessage(LOG_TYPE_DEBUG, msg, logid);
    }

    // timer.finish();
    //@Bug 1886,2870 Flush VM cache only once per statement. send to all PMs.
    //WriteEngineWrapper writeEngine;
    std::map<uint32_t, uint32_t> oids;
    int rc = 0;

    if (result.result == NO_ERROR || result.result == IDBRANGE_WARNING)
    {
        if ((rc = flushDataFiles(NO_ERROR, oids, uniqueId, txnid, roPair.objnum)) != NO_ERROR)
        {
            cerr << "UpdatePackageProcessor::processPackage: write data to disk failed" << endl;

            if (!fRollbackPending)
            {
                logging::Message::Args args;
                logging::Message message(7);
                args.add("Update Failed: ");
                args.add("error when writing data to disk");
                args.add("");
                args.add("");
                message.format(args);

                result.result = UPDATE_ERROR;
                result.message = message;
                result.rowCount = 0;
            }

            rc = endTransaction(uniqueId, txnid, false);
        }
        else
        {
            if (fRollbackPending)
                rc = endTransaction(uniqueId, txnid, false);
            else
                rc = endTransaction(uniqueId, txnid, true);

            if (( rc != NO_ERROR) && (!fRollbackPending))
            {
                logging::Message::Args args;
                logging::Message message(7);
                args.add("Update Failed: ");
                args.add("error when cleaning up data files");
                args.add("");
                args.add("");
                message.format(args);

                result.result = UPDATE_ERROR;
                result.message = message;
                result.rowCount = 0;

            }
        }
    }
    else
    {
        //@Bug 4563. Always flush. error is already set
        rc = flushDataFiles(result.result, oids, uniqueId, txnid, roPair.objnum);
        rc = endTransaction(uniqueId, txnid, false);
    }

    //timer.finish();

    /*	if (result.result != IDBRANGE_WARNING)
    		flushDataFiles(result.result, oids, uniqueId, txnid);
    	else
    		flushDataFiles(0, oids, uniqueId, txnid);
    */
    if (fRollbackPending)
    {
        result.result = JOB_CANCELED;
        logging::Message::Args args1;
        args1.add("Query execution was interrupted");
        result.message.format(args1);
    }

    fWEClient->removeQueue(uniqueId);
    VERBOSE_INFO("Finished Processing Update DML Package");
    return result;
}