コード例 #1
0
ファイル: dbclient_rs.cpp プロジェクト: joegen/sipx-externals
    DBClientConnection* DBClientReplicaSet::selectNodeUsingTags(
            shared_ptr<ReadPreferenceSetting> readPref) {
        if (!shouldReevaluate() && checkLastHost(readPref.get())) {

            LOG( 3 ) << "dbclient_rs selecting compatible last used node " << _lastSlaveOkHost
                                << endl;

            return _lastSlaveOkConn.get();
        }

        ReplicaSetMonitorPtr monitor = _getMonitor();
        _lastSlaveOkHost = monitor->getHostOrRefresh(*readPref);

        if ( _lastSlaveOkHost.empty() ){

            LOG( 3 ) << "dbclient_rs no compatible node found" << endl;

            return NULL;
        }

        _lastReadPref = readPref;

        // Primary connection is special because it is the only connection that is
        // versioned in mongos. Therefore, we have to make sure that this object
        // maintains only one connection to the primary and use that connection
        // every time we need to talk to the primary.
        if (monitor->isPrimary(_lastSlaveOkHost)) {
            checkMaster();
            _lastSlaveOkConn = _master;
            _lastSlaveOkHost = _masterHost; // implied, but still assign just to be safe

            LOG( 3 ) << "dbclient_rs selecting primary node " << _lastSlaveOkHost << endl;

            return _master.get();
        }

        string errmsg;
        ConnectionString connStr(_lastSlaveOkHost);
        // Needs to perform a dynamic_cast because we need to set the replSet
        // callback. We should eventually not need this after we remove the
        // callback.
        DBClientConnection* newConn = dynamic_cast<DBClientConnection*>(
                connStr.connect(errmsg, _so_timeout));

        // Assert here instead of returning NULL since the contract of this method is such
        // that returning NULL means none of the nodes were good, which is not the case here.
        uassert(16532, str::stream() << "Failed to connect to " << _lastSlaveOkHost.toString(),
                newConn != NULL);

        _lastSlaveOkConn.reset(newConn);
        _lastSlaveOkConn->setReplSetClientCallback(this);
        _lastSlaveOkConn->setRunCommandHook(_runCommandHook);
        _lastSlaveOkConn->setPostRunCommandHook(_postRunCommandHook);

        _auth(_lastSlaveOkConn.get());

        LOG( 3 ) << "dbclient_rs selecting node " << _lastSlaveOkHost << endl;

        return _lastSlaveOkConn.get();
    }
コード例 #2
0
ファイル: odbcconf.cpp プロジェクト: zxycode008/odbc1
std::wstring odbcConf::generateConnStr()
{
	std::string connStr("");
	connStr += m_conn_param.driver;
	connStr += m_conn_param.server;
	connStr += m_conn_param.uid;
	connStr += m_conn_param.pwd;
	connStr += m_conn_param.database;
	connStr += m_conn_param._charset;
	const std::string &conStr2 = connStr;
	std::wstring wconn_Str = s2ws(conStr2);
	return wconn_Str;
}
コード例 #3
0
void read(std::string excelFile, int sheetIndex, bool header, std::string csvFile)
{
    clock_t t1 = clock();

    std::cout << "reading " << excelFile;

    if(FAILED(::CoInitialize(NULL))) return;

    _RecordsetPtr pSchema = NULL;
    _RecordsetPtr pRec = NULL;

    int cellCount = 0;

    try
    {
        _bstr_t connStr(makeConnStr(excelFile, header).c_str());

        TESTHR(pRec.CreateInstance(__uuidof(Recordset)));
        TESTHR(pRec->Open(sqlSelectSheet(connStr, sheetIndex).c_str(), connStr, adOpenStatic, adLockOptimistic, adCmdText));

        std::ofstream stream(csvFile.c_str());

        while(!pRec->adoEOF)
        {
            for(long i = 0; i < pRec->Fields->GetCount(); ++i)
            {
                if(i > 0) stream << ";";
                _variant_t v = pRec->Fields->GetItem(i)->Value;
                if(v.vt == VT_R8)
                    stream << v.dblVal;
                if(v.vt == VT_BSTR)
                    stream << (char*)(_bstr_t)v.bstrVal;
                ++cellCount;
            }
            stream << std::endl;
            pRec->MoveNext();
        }
    }
    catch(_com_error &e)
    {
        _bstr_t bstrDescription(e.Description());
        CharToOem(bstrDescription, bstrDescription);
        std::cout << bstrDescription << std::endl;
    }

    ::CoUninitialize();

    clock_t t2 = clock();
    double t = (double)(t2 - t1) / CLOCKS_PER_SEC;
    std::cout << ": " << t << " sec; " << cellCount / t << " cells/sec" << "; see " << csvFile << std::endl;
}
コード例 #4
0
ファイル: dbclient_rs.cpp プロジェクト: joegen/sipx-externals
    DBClientConnection * DBClientReplicaSet::checkMaster() {
        ReplicaSetMonitorPtr monitor = _getMonitor();
        HostAndPort h = monitor->getMasterOrUassert();

        if ( h == _masterHost && _master ) {
            // a master is selected.  let's just make sure connection didn't die
            if ( ! _master->isFailed() )
                return _master.get();

            monitor->failedHost( _masterHost );
            h = monitor->getMasterOrUassert(); // old master failed, try again.
        }

        _masterHost = h;

        ConnectionString connStr(_masterHost);

        string errmsg;
        DBClientConnection* newConn = NULL;

        try {
            // Needs to perform a dynamic_cast because we need to set the replSet
            // callback. We should eventually not need this after we remove the
            // callback.
            newConn = dynamic_cast<DBClientConnection*>(
                    connStr.connect(errmsg, _so_timeout));
        }
        catch (const AssertionException& ex) {
            errmsg = ex.toString();
        }

        if (newConn == NULL || !errmsg.empty()) {
            monitor->failedHost(_masterHost);
            uasserted(13639, str::stream() << "can't connect to new replica set master ["
                      << _masterHost.toString() << "]"
                      << (errmsg.empty()? "" : ", err: ") << errmsg);
        }

        _master.reset(newConn);
        _master->setReplSetClientCallback(this);
        _master->setRunCommandHook(_runCommandHook);
        _master->setPostRunCommandHook(_postRunCommandHook);

        _auth( _master.get() );
        return _master.get();
    }
コード例 #5
0
void write(std::string filename)
{
    clock_t t1 = clock();

    std::cout << "writing " << filename;

    DeleteFileA(filename.c_str());

    if(FAILED(::CoInitialize(NULL))) return;

    _ConnectionPtr pCon = NULL;
    _CommandPtr pCmd = NULL;
    _RecordsetPtr pRec = NULL;

    try
    {
        _bstr_t connStr(makeConnStr(filename).c_str());

        TESTHR(pCon.CreateInstance(__uuidof(Connection)));
        TESTHR(pCon->Open(connStr, "", "", NULL));

        TESTHR(pCmd.CreateInstance(__uuidof(Command)));
        pCmd->ActiveConnection = pCon;
        pCmd->CommandText = "CREATE TABLE MySheet(A int, B varchar, C int, D int, E int, F int, G int, H int, I int, J varchar)";
        pCmd->Execute(NULL, NULL, adCmdText);

        TESTHR(pRec.CreateInstance(__uuidof(Recordset)));
        pRec->Open("SELECT * FROM MySheet", _variant_t((IDispatch*)pCon), adOpenKeyset, adLockOptimistic, adCmdText);

        for(int i = 0; i < writeRows; ++i)
        {
            TESTHR(pRec->AddNew());

            char str[11] = {0}; for(int j = 0; j < 10; ++j) str[j] = 'a' + (rand() % 26);

            pRec->Fields->GetItem("A")->Value = _variant_t(i);
            pRec->Fields->GetItem("B")->Value = _variant_t(str);
            pRec->Fields->GetItem("C")->Value = _variant_t(i);
            pRec->Fields->GetItem("D")->Value = _variant_t(i);
            pRec->Fields->GetItem("E")->Value = _variant_t(i);
            pRec->Fields->GetItem("F")->Value = _variant_t(i);
            pRec->Fields->GetItem("G")->Value = _variant_t(i);
            pRec->Fields->GetItem("H")->Value = _variant_t(i);
            pRec->Fields->GetItem("I")->Value = _variant_t(i);
            pRec->Fields->GetItem("J")->Value = _variant_t(str);
        }
        TESTHR(pRec->Update());
        TESTHR(pRec->Close());
    }
    catch(_com_error &e)
    {
        _bstr_t bstrDescription(e.Description());
        CharToOem(bstrDescription, bstrDescription);
        std::cout << bstrDescription << std::endl;
    }

    if(pCon != 0 && pCon->State == adStateOpen) pCon->Close();
    ::CoUninitialize();

    clock_t t2 = clock();
    double t = (double)(t2 - t1) / CLOCKS_PER_SEC;
    std::wcout << ": " << t << " sec; " << writeRows * 10 / t << " cells/sec" << std::endl;
}