本文整理汇总了C++中ConnectionString::isValid方法的典型用法代码示例。如果您正苦于以下问题:C++ ConnectionString::isValid方法的具体用法?C++ ConnectionString::isValid怎么用?C++ ConnectionString::isValid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConnectionString
的用法示例。
在下文中一共展示了ConnectionString::isValid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: killOps
void killOps() {
if ( mongo::shellUtils::_nokillop || mongo::shellUtils::_allMyUris.size() == 0 )
return;
if ( atPrompt )
return;
sleepmillis(10); // give current op a chance to finish
for( map< string, set<string> >::const_iterator i = shellUtils::_allMyUris.begin(); i != shellUtils::_allMyUris.end(); ++i ) {
string errmsg;
ConnectionString cs = ConnectionString::parse( i->first, errmsg );
if (!cs.isValid()) continue;
boost::scoped_ptr<DBClientWithCommands> conn( cs.connect( errmsg ) );
if (!conn) continue;
const set<string>& uris = i->second;
BSONObj inprog = conn->findOne( "admin.$cmd.sys.inprog", Query() )["inprog"].embeddedObject().getOwned();
BSONForEach( op, inprog ) {
if ( uris.count( op["client"].String() ) ) {
ONCE if ( !autoKillOp ) {
cout << endl << "do you want to kill the current op(s) on the server? (y/n): ";
cout.flush();
char yn;
cin >> yn;
if ( yn != 'y' && yn != 'Y' )
return;
}
conn->findOne( "admin.$cmd.sys.killop", QUERY( "op"<< op["opid"] ) );
}
}
示例2: conn
inline boost::shared_ptr<DBClientBase> mongo_init(){
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return NULL;
}
std::string uri = "mongodb://localhost:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) {
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return NULL;
}
boost::shared_ptr<DBClientBase> conn(cs.connect(errmsg));
if (!conn) {
std::cout << "couldn't connect : " << errmsg << std::endl;
return NULL;
}
{
// clean up old data from any previous tests
BSONObjBuilder query;
conn->remove("test.people", query.obj());
}
//insert(conn.get(), "eliot", 15);
//insert(conn.get(), "sara", 23);
return conn;
}
示例3: appendAsCommand
void StartChunkCloneRequest::appendAsCommand(
BSONObjBuilder* builder,
const NamespaceString& nss,
const MigrationSessionId& sessionId,
const ConnectionString& configServerConnectionString,
const ConnectionString& fromShardConnectionString,
const ShardId& fromShardId,
const ShardId& toShardId,
const BSONObj& chunkMinKey,
const BSONObj& chunkMaxKey,
const BSONObj& shardKeyPattern,
const MigrationSecondaryThrottleOptions& secondaryThrottle) {
invariant(builder->asTempObj().isEmpty());
invariant(nss.isValid());
invariant(fromShardConnectionString.isValid());
builder->append(kRecvChunkStart, nss.ns());
sessionId.append(builder);
builder->append(kConfigServerConnectionString, configServerConnectionString.toString());
builder->append(kFromShardConnectionString, fromShardConnectionString.toString());
builder->append(kFromShardId, fromShardId.toString());
builder->append(kToShardId, toShardId.toString());
builder->append(kChunkMinKey, chunkMinKey);
builder->append(kChunkMaxKey, chunkMaxKey);
builder->append(kShardKeyPattern, shardKeyPattern);
secondaryThrottle.append(builder);
}
示例4: killOperationsOnAllConnections
void ConnectionRegistry::killOperationsOnAllConnections( bool withPrompt ) const {
Prompter prompter( "do you want to kill the current op(s) on the server?" );
mongo::mutex::scoped_lock lk( _mutex );
for( map<string,set<string> >::const_iterator i = _connectionUris.begin();
i != _connectionUris.end(); ++i ) {
string errmsg;
ConnectionString cs = ConnectionString::parse( i->first, errmsg );
if ( !cs.isValid() ) {
continue;
}
boost::scoped_ptr<DBClientWithCommands> conn( cs.connect( errmsg ) );
if ( !conn ) {
continue;
}
const set<string>& uris = i->second;
BSONObj inprog = conn->findOne( "admin.$cmd.sys.inprog", Query() )[ "inprog" ]
.embeddedObject().getOwned();
BSONForEach( op, inprog ) {
if ( uris.count( op[ "client" ].String() ) ) {
if ( !withPrompt || prompter.confirm() ) {
conn->findOne( "admin.$cmd.sys.killop", QUERY( "op"<< op[ "opid" ] ) );
}
else {
return;
}
}
}
}
}
示例5: mongo_connection_create
switch_status_t mongo_connection_create(DBClientBase **connection, const char *conn_str)
{
DBClientBase *conn = NULL;
string conn_string(conn_str), err_msg;
ConnectionString cs = ConnectionString::parse(conn_string, err_msg);
switch_status_t status = SWITCH_STATUS_FALSE;
if (!cs.isValid()) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't parse url: %s\n", err_msg.c_str());
return status;
}
try {
conn = cs.connect(err_msg);
} catch (DBException &e) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't connect to mongo [%s]: %s\n", conn_str, err_msg.c_str());
return status;
}
if (conn) {
*connection = conn;
status = SWITCH_STATUS_SUCCESS;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", conn_str);
}
return status;
}
示例6: run
virtual bool run(OperationContext* txn,
const string&,
BSONObj& cmdObj,
int,
string& errmsg,
BSONObjBuilder& result,
bool fromRepl) {
string fromhost = cmdObj.getStringField("fromhost");
if (fromhost.empty()) {
/* copy from self */
stringstream ss;
ss << "localhost:" << serverGlobalParams.port;
fromhost = ss.str();
}
BSONObj ret;
ConnectionString cs = ConnectionString::parse(fromhost, errmsg);
if (!cs.isValid()) {
return false;
}
authConn_.reset(cs.connect(errmsg));
if (!authConn_.get()) {
return false;
}
if (!authConn_->runCommand("admin", BSON("getnonce" << 1), ret)) {
errmsg = "couldn't get nonce " + ret.toString();
return false;
}
result.appendElements(ret);
return true;
}
示例7: main
int main(int argc, char* argv[]) {
if (argc > 2) {
std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl;
return EXIT_FAILURE;
}
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::string uri = argc == 2 ? argv[1] : "mongodb://localhost:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) {
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if (!conn) {
cout << "couldn't connect : " << errmsg << endl;
return EXIT_FAILURE;
}
try {
unsigned long long count = conn->count("test.foo");
cout << "count of exiting documents in collection test.foo : " << count << endl;
conn->remove("test.foo", BSONObj());
BSONObj o = BSON("hello"
<< "world");
conn->insert("test.foo", o);
string e = conn->getLastError();
if (!e.empty()) {
cout << "insert #1 failed: " << e << endl;
}
// make an index with a unique key constraint
conn->createIndex("test.foo", IndexSpec().addKeys(BSON("hello" << 1)).unique());
try {
conn->insert("test.foo", o); // will cause a dup key error on "hello" field
} catch (const OperationException&) {
// duplicate key error
}
cout << "we expect a dup key error here:" << endl;
cout << " " << conn->getLastErrorDetailed().toString() << endl;
} catch (DBException& e) {
cout << "caught DBException " << e.toString() << endl;
return 1;
}
return 0;
}
示例8: get
DBClientBase* MongoConnectionPool::get(const std::string& host) {
std::string errMsg;
ConnectionString cs = ConnectionString::parse(host, errMsg);
if(cs.isValid()) {
return get(cs);
}
return 0;
}
示例9: main
int main(int argc, char* argv[]) {
if ( argc > 2 ) {
std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl;
return EXIT_FAILURE;
}
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::string uri = argc == 2 ? argv[1] : "mongodb://localhost:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) {
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if ( !conn ) {
std::cout << "couldn't connect : " << errmsg << std::endl;
return EXIT_FAILURE;
}
conn->dropCollection("test.test");
// Don't run on MongoDB < 2.2
BSONObj cmdResult;
conn->runCommand("admin", BSON("buildinfo" << true), cmdResult);
std::vector<BSONElement> versionArray = cmdResult["versionArray"].Array();
if (versionArray[0].Int() < 2 || versionArray[1].Int() < 2)
return EXIT_SUCCESS;
conn->insert("test.test", BSON("x" << 0));
conn->insert("test.test", BSON("x" << 1));
conn->insert("test.test", BSON("x" << 1));
conn->insert("test.test", BSON("x" << 2));
conn->insert("test.test", BSON("x" << 2));
conn->insert("test.test", BSON("x" << 2));
std::auto_ptr<DBClientCursor> cursor = conn->aggregate("test.test",
BSON_ARRAY(
BSON("$match" << BSON("x" << GT << 0)) <<
BSON("$group" << BSON("_id" << "$x" << "count" << BSON("$sum" << 1)))
)
);
std::cout << "------- AGGREGATION -------" << std::endl;
while (cursor->more()) {
std::cout << cursor->next() << std::endl;
}
return EXIT_SUCCESS;
}
示例10: main
int main(int argc, char* argv[]) {
if ( argc > 2 ) {
std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl;
return EXIT_FAILURE;
}
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::string uri = argc == 2 ? argv[1] : "mongodb://localhost:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) {
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if ( !conn ) {
cout << "couldn't connect : " << errmsg << endl;
return EXIT_FAILURE;
}
try {
BSONObj o = BSON( "hello" << "world" );
cout << "dropping collection..." << endl;
conn->dropCollection("test.foo");
cout << "inserting..." << endl;
time_t start = time(0);
for( unsigned i = 0; i < 100000; i++ ) {
conn->insert("test.foo", o);
}
// wait until all operations applied
cout << "getlasterror returns: \"" << conn->getLastError() << '"' << endl;
time_t done = time(0);
time_t dt = done-start;
cout << dt << " seconds " << 100000/dt << " per second" << endl;
}
catch(DBException& e) {
cout << "caught DBException " << e.toString() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
示例11: main
int main( int argc, const char **argv ) {
const char *port = "27017";
if ( argc != 1 ) {
if ( argc != 3 ) {
std::cout << "need to pass port as second param" << endl;
return EXIT_FAILURE;
}
port = argv[ 2 ];
}
std::string errmsg;
ConnectionString cs = ConnectionString::parse(string("127.0.0.1:") + port, errmsg);
if (!cs.isValid()) {
cout << "error parsing url: " << errmsg << endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if (!conn) {
cout << "couldn't connect: " << errmsg << endl;
return EXIT_FAILURE;
}
BSONObj ret;
// clean up old data from any previous tests
conn->runCommand( "test", BSON("removeUsersFromDatabase" << 1), ret );
conn->runCommand( "test",
BSON( "createUser" << "eliot" <<
"pwd" << "bar" <<
"roles" << BSON_ARRAY("readWrite")),
ret);
errmsg.clear();
conn->auth(BSON("user" << "eliot" <<
"db" << "test" <<
"pwd" << "bar" <<
"mechanism" << "MONGODB-CR"));
try {
conn->auth(BSON("user" << "eliot" <<
"db" << "test" <<
"pwd" << "bars" << // incorrect password
"mechanism" << "MONGODB-CR"));
// Shouldn't get here.
cout << "Authentication with invalid password should have failed but didn't" << endl;
return EXIT_FAILURE;
} catch (const DBException& e) {
// expected
}
return EXIT_SUCCESS;
}
示例12: get
DBClientBase* DBConnectionPool::get(const string& host) {
DBClientBase * c = _get( host );
if ( c ){
onHandedOut( c );
return c;
}
string errmsg;
ConnectionString cs = ConnectionString::parse( host , errmsg );
uassert( 13071 , (string)"invalid hostname [" + host + "]" + errmsg , cs.isValid() );
c = cs.connect( errmsg );
uassert( 11002 , _name + ": connect failed " + host + " : " + errmsg , c );
return _finishCreate( host , c );
}
示例13: get
DBClientBase* DBConnectionPool::get(const string& host) {
DBClientBase * c = _get( host );
if ( c ) {
onHandedOut( c );
return c;
}
string errmsg;
ConnectionString cs = ConnectionString::parse( host , errmsg );
uassert( 13071 , (string)"invalid hostname [" + host + "]" + errmsg , cs.isValid() );
c = cs.connect( errmsg );
if ( ! c )
throw SocketException( SocketException::CONNECT_ERROR , host , 11002 , str::stream() << _name << " error: " << errmsg );
return _finishCreate( host , c );
}
示例14: main
int main( int argc , const char ** argv ) {
unsigned nThreads = 1;
bool print = false;
for ( int i=1; i<argc; i++ ) {
if ( mongoutils::str::equals( "--threads" , argv[i] ) ) {
nThreads = atoi( argv[++i] );
}
else if ( mongoutils::str::equals( "--print" , argv[1] ) ) {
print = true;
}
else {
cerr << "unknown option: " << argv[i] << endl;
return 1;
}
}
string errmsg;
ConnectionString cs = ConnectionString::parse( "foo/127.0.0.1" , errmsg );
if ( ! cs.isValid() ) {
cout << "error parsing url: " << errmsg << endl;
return 1;
}
DBClientReplicaSet * conn = (DBClientReplicaSet*)cs.connect( errmsg );
if ( ! conn ) {
cout << "error connecting: " << errmsg << endl;
return 2;
}
string collName = "test.rs1";
conn->dropCollection( collName );
vector<boost::shared_ptr<boost::thread> > threads;
for ( unsigned i=0; i<nThreads; i++ ) {
string errmsg;
threads.push_back( boost::shared_ptr<boost::thread>( new boost::thread( boost::bind( workerThread , collName , print , (DBClientReplicaSet*)cs.connect(errmsg) ) ) ) );
}
for ( unsigned i=0; i<threads.size(); i++ ) {
threads[i]->join();
}
}
示例15: main
int main( int argc, const char **argv ) {
if ( argc > 2 ) {
std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl;
return EXIT_FAILURE;
}
client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::string uri = argc == 2 ? argv[1] : "mongodb://localhost:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) {
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if ( !conn ) {
cout << "couldn't connect : " << errmsg << endl;
return EXIT_FAILURE;
}
try {
BSONObj cmdResult;
conn->runCommand("admin", BSON("buildinfo" << true), cmdResult);
if (cmdResult["versionArray"].Array()[1].Int() < 6)
return EXIT_SUCCESS;
insertGeoData(conn.get());
queryGeoData(conn.get());
conn->dropCollection(kDbCollectionName);
}
catch(const DBException& dbe) {
cout << "caught DBException " << dbe.toString() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}