int paraseUserStockCfg(BSONObj obj,std::vector<user_stock_cfg>& uscarray) { std::string uid = obj.getStringField("Uid"); std::vector<BSONElement> elements = obj["stocks"].Array(); std::vector<BSONElement>::iterator it = elements.begin(); int count = 0; for(; it != elements.end(); ++it) { BSONObj stock = it->Obj(); user_stock_cfg usc; usc.uid = uid; usc.stock = stock.getStringField("stock"); usc.bulletin = stock.getIntField("bulletin"); if(stock.getField("max_price").ok()) usc.max_price = stock.getField("max_price").Double(); else usc.max_price = -0.111; if(stock.getField("min_price").ok()) usc.min_price = stock.getField("min_price").Double(); else usc.min_price = -0.111; usc.run = stock.getIntField("run"); uscarray.push_back(usc); count++; } return count ; }
/********************************************************** *see readFromDB in FileRec, similar structure * ***********************************************************/ void VersionRec::readFromDB(mongo::DBClientConnection& conn, string versionID) { auto_ptr<mongo::DBClientCursor> cursor = conn.query("fileRecords.FileVersion", MONGO_QUERY("_id" << mongo::OID(versionID))); if (cursor->more()) { //convert to VersionRec BSONObj record = cursor->next(); this->versionid = versionID; this->tmpname = record.getStringField("Tempname"); this->filehash = record.getStringField("filehash"); this->length = record.getIntField("length"); this->modifytime.tv_nsec = record.getField("Mtnsec").numberLong(); this->modifytime.tv_sec = record.getField("mtsec").numberLong(); this->versionnumber = record.getIntField("Version"); //similar to the comments collection in readfromDB in FileRec vector<BSONElement> hashes(record.getField("Blktable").Array()); for (vector<BSONElement>::iterator it = hashes.begin(); it != hashes.end(); ++it) { BSONObj blockdata = it->Obj(); BSONElement blocknum = blockdata.getField("Blknum"); BSONElement blockhash = blockdata.getField("hash"); VersionDiffBlock tmp; tmp.blockNo = blocknum.Int(); tmp.blockHash = blockhash.String(); changesAppend(tmp); } } else{ cout << "could not find version " << versionID << endl; } }
void parseMobileToken(BSONObj obj,mobile_apptypes& app) { app.token = obj.getStringField("Token"); if(obj.getField("incrementID").ok()) app.incrementID = obj.getField("incrementID").Long(); else app.incrementID = -1; app.osversion = obj.getStringField("osversion"); app.comment = obj.getStringField("comment"); std::vector<BSONElement> elements = obj["apptypes"].Array(); std::vector<BSONElement>::iterator it = elements.begin(); for(; it != elements.end(); ++it) { BSONObj appobj = it->Obj(); apptypeinfo appinfo; appinfo.apptype = appobj.getIntField("apptype"); if(appobj.getField("flag").ok()) appinfo.flag = appobj.getIntField("flag"); else appinfo.flag = 0; appinfo.lastuid = appobj.getStringField("lastuid"); if(appobj.getField("lasttime").ok()) appinfo.lasttime = appobj.getField("lasttime").Long(); else appinfo.lasttime = 0; appinfo.appversion = appobj.getStringField("appversion"); app.apptypes.push_back(appinfo); } }
/********************************************************** *reads from db and converts bson to FileRec object * ***********************************************************/ void FileRec::readFromDB(mongo::DBClientConnection& conn, string filename) { boost::filesystem::path p(filename); //get filename from path string file(p.filename().c_str()); auto_ptr<mongo::DBClientCursor> cursor = conn.query("fileRecords.Filerec", MONGO_QUERY("filename" << file)); if (cursor->more()) { BSONObj record = cursor->next(); //get data from db and store in the FileRec this->filename = record.getStringField("filename"); this->tempname = record.getStringField("Tempname"); this->recentHash = record.getStringField("curhash"); this->origHash = record.getStringField("ovhash"); this->length = record.getIntField("length"); this->versionCount = record.getIntField("nversions"); this->modifytime.tv_nsec = record.getField("Mtnsec").numberLong(); this->modifytime.tv_sec = record.getField("mtsec").numberLong(); this->refNum = record.getIntField("currentversion"); vector<BSONElement> hashes(record.getField("FileBlkHashes").Array()); for (vector<BSONElement>::iterator it = hashes.begin(); it != hashes.end(); ++it) { appendBlock((*it).String()); } //comments is an array of objects so it takes a bit of nesting to convert vector<BSONElement> array = record["comments"].Array(); //convert to array for (vector<BSONElement>::iterator ar = array.begin(); ar != array.end(); ++ar) { BSONObj commentdata = ar->Obj(); //store object at array[x] into BSONObj BSONElement version = commentdata.getField("version"); //convert BSONElement commentdb = commentdata.getField("comment"); comment data; data.comment = commentdb.String(); data.version = version.Int(); appendComment(data); } if (record.hasElement("versionrec")) { //again an array of objects vector<BSONElement> array = record["versionrec"].Array(); for (vector<BSONElement>::iterator it = array.begin(); it != array.end(); ++it) { BSONObj versionRecord = it->Obj(); BSONElement id = versionRecord.getField("id"); appendVersion(id.String()); } } } }
/** * @param args - [ name, byte index ] * In this initial implementation, all bits in the specified byte are flipped. */ BSONObj fuzzFile(const BSONObj& args, void* data) { uassert( 13619, "fuzzFile takes 2 arguments", args.nFields() == 2 ); scoped_ptr< File > f( new File() ); f->open( args.getStringField( "0" ) ); uassert( 13620, "couldn't open file to fuzz", !f->bad() && f->is_open() ); char c; f->read( args.getIntField( "1" ), &c, 1 ); c = ~c; f->write( args.getIntField( "1" ), &c, 1 ); return undefinedReturn; // f close is implicit }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string fromhost = cmdObj.getStringField("from"); if ( fromhost.empty() ) { errmsg = "missing from spec"; return false; } string collection = cmdObj.getStringField("cloneCollection"); if ( collection.empty() ) { errmsg = "missing cloneCollection spec"; return false; } BSONObj query = cmdObj.getObjectField("query"); if ( query.isEmpty() ) query = BSONObj(); BSONElement copyIndexesSpec = cmdObj.getField("copyindexes"); bool copyIndexes = copyIndexesSpec.isBoolean() ? copyIndexesSpec.boolean() : true; // Will not be used if doesn't exist. int logSizeMb = cmdObj.getIntField( "logSizeMb" ); /* replication note: we must logOp() not the command, but the cloned data -- if the slave were to clone it would get a different point-in-time and not match. */ setClient( collection.c_str() ); log() << "cloneCollection. db:" << ns << " collection:" << collection << " from: " << fromhost << " query: " << query << " logSizeMb: " << logSizeMb << ( copyIndexes ? "" : ", not copying indexes" ) << endl; Cloner c; long long cursorId; if ( !c.startCloneCollection( fromhost.c_str(), collection.c_str(), query, errmsg, !fromRepl, copyIndexes, logSizeMb, cursorId ) ) return false; return c.finishCloneCollection( fromhost.c_str(), collection.c_str(), query, cursorId, errmsg); }
bool CCentralizeZbxEventProcessor::ExecuteEarlyProcess(BSONObj boRecord) { auto_ptr<DBClientCursor> ptrTriggerDataCursor; BSONObj boTriggerRecord; int iPriority; iPriority = 0; Query queryCondition = QUERY("triggerid" << boRecord["triggerid"] << "zabbix_server_id" << boRecord["zabbix_server_id"]); m_pTriggerLogController->Find(ptrTriggerDataCursor, queryCondition); if (ptrTriggerDataCursor->more()) { boTriggerRecord = ptrTriggerDataCursor->nextSafe(); } /*-- Check Trigger Info --*/ if (boTriggerRecord.isEmpty()) { return false; } iPriority = boTriggerRecord.hasField("priority") ? boTriggerRecord.getIntField("priority") : 0; if (5 != iPriority) { return false; } return true; }
virtual bool run(OperationContext* txn, const string& dbname , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool) { string coll = cmdObj[ "captrunc" ].valuestrsafe(); uassert( 13416, "captrunc must specify a collection", !coll.empty() ); NamespaceString nss( dbname, coll ); int n = cmdObj.getIntField( "n" ); bool inc = cmdObj.getBoolField( "inc" ); // inclusive range? Client::WriteContext ctx(txn, nss.ns() ); Collection* collection = ctx.getCollection(); massert( 13417, "captrunc collection not found or empty", collection); RecordId end; { boost::scoped_ptr<PlanExecutor> exec(InternalPlanner::collectionScan(txn, nss.ns(), collection, InternalPlanner::BACKWARD)); // We remove 'n' elements so the start is one past that for( int i = 0; i < n + 1; ++i ) { PlanExecutor::ExecState state = exec->getNext(NULL, &end); massert( 13418, "captrunc invalid n", PlanExecutor::ADVANCED == state); } } WriteUnitOfWork wuow(txn); collection->temp_cappedTruncateAfter( txn, end, inc ); wuow.commit(); return true; }
std::vector<post> StorageEngine::getposts(int number) { std::vector<post> posts; collection = "posts"; update_namespace(); //if it's empty, return nothing. try { if(con.count(namespacestr) == 0) return posts; Query q("{ }"); auto_ptr<DBClientCursor> cursor = con.query(namespacestr,q.sort("timestamp",-1)); while(cursor->more()) { BSONObj record = cursor->next(); post newpost; newpost.title = record.getStringField("title"); newpost.body = record.getStringField("body"); newpost.timestamp = record.getIntField("timestamp"); newpost.oid = getoid(record); posts.push_back(newpost); } } catch( DBException &e ) { cout << "caught " << e.what() << endl; exit(0); } return posts; }
virtual bool run(OperationContext* opCtx, const string& dbname, const BSONObj& cmdObj, BSONObjBuilder& result) { const NamespaceString fullNs = CommandHelpers::parseNsCollectionRequired(dbname, cmdObj); if (!fullNs.isValid()) { return CommandHelpers::appendCommandStatus( result, {ErrorCodes::InvalidNamespace, str::stream() << "collection name " << fullNs.ns() << " is not valid"}); } int n = cmdObj.getIntField("n"); bool inc = cmdObj.getBoolField("inc"); // inclusive range? if (n <= 0) { return CommandHelpers::appendCommandStatus( result, {ErrorCodes::BadValue, "n must be a positive integer"}); } // Lock the database in mode IX and lock the collection exclusively. AutoGetCollection autoColl(opCtx, fullNs, MODE_IX, MODE_X); Collection* collection = autoColl.getCollection(); if (!collection) { return CommandHelpers::appendCommandStatus( result, {ErrorCodes::NamespaceNotFound, str::stream() << "collection " << fullNs.ns() << " does not exist"}); } if (!collection->isCapped()) { return CommandHelpers::appendCommandStatus( result, {ErrorCodes::IllegalOperation, "collection must be capped"}); } RecordId end; { // Scan backwards through the collection to find the document to start truncating from. // We will remove 'n' documents, so start truncating from the (n + 1)th document to the // end. auto exec = InternalPlanner::collectionScan( opCtx, fullNs.ns(), collection, PlanExecutor::NO_YIELD, InternalPlanner::BACKWARD); for (int i = 0; i < n + 1; ++i) { PlanExecutor::ExecState state = exec->getNext(nullptr, &end); if (PlanExecutor::ADVANCED != state) { return CommandHelpers::appendCommandStatus( result, {ErrorCodes::IllegalOperation, str::stream() << "invalid n, collection contains fewer than " << n << " documents"}); } } } collection->cappedTruncateAfter(opCtx, end, inc); return true; }
TEST(QueryTest, Sort) { Query q; q.sort(BSON("a" << 1)); ASSERT_TRUE(q.isComplex()); BSONObj sort = q.getSort(); ASSERT_TRUE(sort.hasField("a")); ASSERT_EQUALS(sort.getIntField("a"), 1); }
unsigned long long DBClientWithCommands::count(const string &_ns, BSONObj query) { NamespaceString ns(_ns); BSONObj cmd = BSON( "count" << ns.coll << "query" << query ); BSONObj res; if( !runCommand(ns.db.c_str(), cmd, res) ) uasserted(string("count fails:") + res.toString()); return res.getIntField("n"); }
/** * SERVER-13001 - mixed sharded cluster could return nModified * (servers >= 2.6) or not (servers <= 2.4). If any call does * not return nModified we cannot report a valid final count. */ void WriteResult::_setModified(const BSONObj& result) { int nModified = result.getIntField("nModified"); if (_hasModifiedCount && nModified >= 0) _nModified += nModified; else _hasModifiedCount = false; }
// static std::string WorkingSetCommon::toStatusString(const BSONObj& obj) { if (!isValidStatusMemberObject(obj)) { Status unknownStatus(ErrorCodes::UnknownError, "no details available"); return unknownStatus.toString(); } Status status(ErrorCodes::fromInt(obj.getIntField("code")), obj.getStringField("errmsg")); return status.toString(); }
enum QueryOptions DBClientWithCommands::availableOptions() { if ( !_haveCachedAvailableOptions ) { BSONObj ret; if ( runCommand( "admin", BSON( "availablequeryoptions" << 1 ), ret ) ) { _cachedAvailableOptions = ( enum QueryOptions )( ret.getIntField( "options" ) ); } _haveCachedAvailableOptions = true; } return _cachedAvailableOptions; }
virtual bool run(OperationContext* txn, const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result) { const std::string fullNs = parseNsCollectionRequired(dbname, cmdObj); int n = cmdObj.getIntField("n"); bool inc = cmdObj.getBoolField("inc"); // inclusive range? if (n <= 0) { return appendCommandStatus(result, {ErrorCodes::BadValue, "n must be a positive integer"}); } OldClientWriteContext ctx(txn, fullNs); Collection* collection = ctx.getCollection(); if (!collection) { return appendCommandStatus( result, {ErrorCodes::NamespaceNotFound, str::stream() << "collection " << fullNs << " does not exist"}); } if (!collection->isCapped()) { return appendCommandStatus(result, {ErrorCodes::IllegalOperation, "collection must be capped"}); } RecordId end; { // Scan backwards through the collection to find the document to start truncating from. // We will remove 'n' documents, so start truncating from the (n + 1)th document to the // end. std::unique_ptr<PlanExecutor> exec(InternalPlanner::collectionScan( txn, fullNs, collection, PlanExecutor::YIELD_MANUAL, InternalPlanner::BACKWARD)); for (int i = 0; i < n + 1; ++i) { PlanExecutor::ExecState state = exec->getNext(nullptr, &end); if (PlanExecutor::ADVANCED != state) { return appendCommandStatus(result, {ErrorCodes::IllegalOperation, str::stream() << "invalid n, collection contains fewer than " << n << " documents"}); } } } collection->temp_cappedTruncateAfter(txn, end, inc); return true; }
/** * @brief getGeneratedCertList * retrieve certificates list * @return */ QVariantList Database::getGeneratedCertList(){ auto_ptr<DBClientCursor> cursor = con.query("ssldashboard.generatedcerts", Query()); QVariantList listOfCerts; while (cursor->more()){ QVariantMap certElement; BSONObj currentObj =cursor->next(); certElement.insert(CERT_START_DATE ,currentObj.getField(CERT_START_DATE).date().toString().data()); certElement.insert(CERT_END_DATE ,currentObj.getField(CERT_END_DATE).date().toString().data()); certElement.insert(CERT_RECORD_DATE_FIELD ,currentObj.getField(CERT_RECORD_DATE_FIELD).date().toString().data()); certElement.insert(CERT_IS_CA ,currentObj.getBoolField(CERT_IS_CA)); certElement.insert(CERT_COMMON_NAME ,currentObj.getStringField(CERT_COMMON_NAME)); certElement.insert(CERT_SEQ_NUM ,currentObj.getIntField(CERT_SEQ_NUM)); certElement.insert(CERT_SIGN_BY_SERIAL ,currentObj.getIntField(CERT_SIGN_BY_SERIAL)); listOfCerts << certElement; } return listOfCerts; }
int CMongodbModel::GetIntFieldValue(BSONObj boRecord, string strFieldName, int iExceptionValue) { int iValue = iExceptionValue; if (boRecord.hasField(strFieldName)) { try { iValue = boRecord.getIntField(strFieldName.c_str()); } catch(exception& ex) {} } return iValue; }
void calc_sizes() { BSONObj emptyObj = BSONObj(); numMembers = new map<int,int>(); auto_ptr<DBClientCursor> cursor = connection->query("memprof_datasets.stdlib_groups", emptyObj); while( cursor->more() ) { BSONObj o = cursor->next(); int id = o.getIntField("_id"); int size = dfs_size_for(id); connection->update("memprof_datasets.stdlib_groups", BSON("_id" << id), BSON("$set" << BSON("size" << size))); } delete numMembers; }
void StorageEngine::getUser(User &u,std::string cookie_id) { collection = "cookies"; update_namespace(); auto_ptr<DBClientCursor> cursor = con.query(namespacestr,QUERY("cid" << cookie_id)); if(cursor->more()) { BSONObj record = cursor->next(); string oid = record.getStringField("uid"); collection = "users"; update_namespace(); auto_ptr<DBClientCursor> cursor_b = con.query(namespacestr,getoid(oid)); if(cursor_b->more()) { BSONObj rec = cursor_b->next(); u.username = rec.getStringField("UserName"); u.is_admin = rec.getIntField("is_admin"); } else { cout << "The user mentioned in the cookie does not exist!" << endl; } } }
post StorageEngine::getpost(std::string oid) { post newpost; collection = "posts"; update_namespace(); auto_ptr<DBClientCursor> cursor = con.query(namespacestr,getoid(oid)); if(cursor->more()) { BSONObj record = cursor->next(); newpost.title = record.getStringField("title"); newpost.body = record.getStringField("body"); newpost.timestamp = record.getIntField("timestamp"); newpost.oid = getoid(record); } else { newpost.title = "Post Not Found"; stringstream pb; pb << "The post you requested (" << oid << ") was not found."; newpost.body = pb.str(); } return newpost; }
virtual bool run(const string& dbname , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string fromhost = cmdObj.getStringField("from"); if ( fromhost.empty() ) { errmsg = "missing from spec"; return false; } string collection = cmdObj.getStringField("startCloneCollection"); if ( collection.empty() ) { errmsg = "missing startCloneCollection spec"; return false; } BSONObj query = cmdObj.getObjectField("query"); if ( query.isEmpty() ) query = BSONObj(); BSONElement copyIndexesSpec = cmdObj.getField("copyindexes"); bool copyIndexes = copyIndexesSpec.isBoolean() ? copyIndexesSpec.boolean() : true; // Will not be used if doesn't exist. int logSizeMb = cmdObj.getIntField( "logSizeMb" ); /* replication note: we must logOp() not the command, but the cloned data -- if the slave were to clone it would get a different point-in-time and not match. */ Client::Context ctx(collection); log() << "startCloneCollection. db:" << dbname << " collection:" << collection << " from: " << fromhost << " query: " << query << endl; Cloner c; long long cursorId; bool res = c.startCloneCollection( fromhost.c_str(), collection.c_str(), query, errmsg, !fromRepl, copyIndexes, logSizeMb, cursorId ); if ( res ) { BSONObjBuilder b; b << "fromhost" << fromhost; b << "collection" << collection; b << "query" << query; b.appendDate( "cursorId", cursorId ); BSONObj token = b.done(); result << "finishToken" << token; } return res; }
/** * @brief get_user_list * retrieve user list * @return */ QVariantList Database::get_user_list(){ auto_ptr<DBClientCursor> cursor = con.query("ssldashboard.users", Query()); QVariantList userList; while (cursor->more()){ QVariantMap userElement; BSONObj currentObj =cursor->next(); userElement.insert(USER_USERNAME ,currentObj.getStringField(USER_USERNAME)); userElement.insert(USER_ROLE ,currentObj.getIntField(USER_ROLE)); userElement.insert(USER_CREATION ,currentObj.getField(USER_CREATION).date().toString().data()); userElement.insert(USER_LAST_LOGIN ,currentObj.getField(USER_LAST_LOGIN).date().toString().data()); userList << userElement; } return userList; }
virtual bool run(const string& dbname , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool) { string coll = cmdObj[ "captrunc" ].valuestrsafe(); uassert( 13416, "captrunc must specify a collection", !coll.empty() ); NamespaceString nss( dbname, coll ); int n = cmdObj.getIntField( "n" ); bool inc = cmdObj.getBoolField( "inc" ); // inclusive range? Client::WriteContext ctx( nss.ns() ); Collection* collection = ctx.ctx().db()->getCollection( nss.ns() ); massert( 13417, "captrunc collection not found or empty", collection); boost::scoped_ptr<Runner> runner(InternalPlanner::collectionScan(nss.ns(), InternalPlanner::BACKWARD)); DiskLoc end; // We remove 'n' elements so the start is one past that for( int i = 0; i < n + 1; ++i ) { Runner::RunnerState state = runner->getNext(NULL, &end); massert( 13418, "captrunc invalid n", Runner::RUNNER_ADVANCED == state); } collection->temp_cappedTruncateAfter( end, inc ); return true; }
virtual bool run(const string& dbname, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string logCollection = cmdObj.getStringField( "logCollection" ); if ( logCollection.empty() ) { errmsg = "missing logCollection spec"; return false; } bool start = !cmdObj.getField( "start" ).eoo(); bool validateComplete = !cmdObj.getField( "validateComplete" ).eoo(); if ( start ? validateComplete : !validateComplete ) { errmsg = "Must specify exactly one of start:1 or validateComplete:1"; return false; } int logSizeMb = cmdObj.getIntField( "logSizeMb" ); NamespaceDetailsTransient &t = NamespaceDetailsTransient::get_w( logCollection.c_str() ); if ( start ) { if ( t.cllNS().empty() ) { if ( logSizeMb == INT_MIN ) { t.cllStart(); } else { t.cllStart( logSizeMb ); } } else { errmsg = "Log already started for ns: " + logCollection; return false; } } else { if ( t.cllNS().empty() ) { errmsg = "No log to validateComplete for ns: " + logCollection; return false; } else { if ( !t.cllValidateComplete() ) { errmsg = "Oplog failure, insufficient space allocated"; return false; } } } log() << "started logCollection with cmd obj: " << cmdObj << endl; return true; }
void plumage::stats::processSubmitterStats(ODSMongodbOps* ops, Date_t& ts) { dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processSubmitterStats called...\n"); DBClientConnection* conn = ops->m_db_conn; conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 )); auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Submitter" ) ); conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "ts" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "sn" << 1 )); while( cursor->more() ) { BSONObj p = cursor->next(); // write record to submitter samples BSONObjBuilder bob; DATE(ts,ts); STRING(sn,ATTR_NAME); STRING(ma,ATTR_MACHINE); INTEGER(jr,ATTR_RUNNING_JOBS); // TODO: weird...HeldJobs isn't always there in the raw submitter ad int h = p.getIntField(ATTR_HELD_JOBS); h = (h>0) ? h : 0; bob.append("jh",h); INTEGER(ji,ATTR_IDLE_JOBS); conn->insert(DB_STATS_SAMPLES_SUB,bob.obj()); } }
/** * @brief getNextUserSequenceNum * get new incremented user sequential number * @return */ int Database::getNextUserSequenceNum(){ con.update("ssldashboard.userSeqNum", QUERY("_id"<<"userId"), BSON("$inc"<<BSON("seq"<<1)), true, true); auto_ptr<DBClientCursor> cursor = con.query("ssldashboard.userSeqNum", BSON("_id"<<"userId")); if (cursor->more()){ BSONObj currentObj =cursor->next(); if (currentObj.hasField("seq") && currentObj.getField("seq").isNumber()){ return currentObj.getIntField("seq"); } } else{ BSONObjBuilder box; box.append("_id","userId"); box.append("seq",0); BSONObj seqNum = box.obj(); con.insert("ssldashboard.userSeqNum", seqNum); } string err = con.getLastError(); if( !err.empty()){ cerr << err.data() << endl; return -1; } return -1; }
inline bool DBClientWithCommands::isOk(const BSONObj& o) { return o.getIntField("ok") == 1; }
/* **************************************************************************** * * mongoUpdateContextSubscription - */ HttpStatusCode mongoUpdateContextSubscription(UpdateContextSubscriptionRequest* requestP, UpdateContextSubscriptionResponse* responseP, Format inFormat, const std::string& tenant) { reqSemTake(__FUNCTION__, "ngsi10 update subscription request"); LM_T(LmtMongo, ("Update Context Subscription")); DBClientBase* connection = getMongoConnection(); /* Look for document */ BSONObj sub; try { OID id = OID(requestP->subscriptionId.get()); mongoSemTake(__FUNCTION__, "findOne in SubscribeContextCollection"); sub = connection->findOne(getSubscribeContextCollectionName(tenant).c_str(), BSON("_id" << id)); mongoSemGive(__FUNCTION__, "findOne in SubscribeContextCollection"); LM_I(("Database Operation Successful (findOne _id: %s)", id.toString().c_str())); } catch (const AssertionException &e) { /* This happens when OID format is wrong */ // FIXME P4: this checking should be done at the parsing stage, without progressing to // mongoBackend. For the moment we can leave this here, but we should remove it in the future // (old issue #95) mongoSemGive(__FUNCTION__, "findOne in SubscribeContextCollection (mongo assertion exception)"); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (mongo assertion exception)"); responseP->subscribeError.errorCode.fill(SccContextElementNotFound); LM_W(("Bad Input (invalid OID format)")); return SccOk; } catch (const DBException &e) { mongoSemGive(__FUNCTION__, "findOne in SubscribeContextCollection (mongo db exception)"); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (mongo db exception)"); responseP->subscribeError.errorCode.fill(SccReceiverInternalError, std::string("collection: ") + getSubscribeContextCollectionName(tenant).c_str() + " - findOne() _id: " + requestP->subscriptionId.get() + " - exception: " + e.what()); LM_E(("Database Error (%s)", responseP->subscribeError.errorCode.details.c_str())); return SccOk; } catch (...) { mongoSemGive(__FUNCTION__, "findOne in SubscribeContextCollection (mongo generic exception)"); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (mongo generic exception)"); responseP->subscribeError.errorCode.fill(SccReceiverInternalError, std::string("collection: ") + getSubscribeContextCollectionName(tenant).c_str() + " - findOne() _id: " + requestP->subscriptionId.get() + " - exception: " + "generic"); LM_E(("Database Error (%s)", responseP->subscribeError.errorCode.details.c_str())); return SccOk; } if (sub.isEmpty()) { responseP->subscribeError.errorCode.fill(SccContextElementNotFound); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (no subscriptions found)"); return SccOk; } /* We start with an empty BSONObjBuilder and process requestP for all the fields that can * be updated. I don't like too much this strategy (I would have preferred to start with * a copy of the original document, then modify as neded, but this doesn't seem to be easy * using the API provide by the Mongo C++ driver) * * FIXME: a better implementation strategy could be doing an findAndModify() query to do the * update, so detecting if the document was not found, instead of using findOne() + update() * with $set operation. One operations to MongoDb. vs two operations. */ BSONObjBuilder newSub; /* Entities, attribute list and reference are not updatable, so they are appended directly */ newSub.appendArray(CSUB_ENTITIES, sub.getField(CSUB_ENTITIES).Obj()); newSub.appendArray(CSUB_ATTRS, sub.getField(CSUB_ATTRS).Obj()); newSub.append(CSUB_REFERENCE, STR_FIELD(sub, CSUB_REFERENCE)); /* Duration update */ if (requestP->duration.isEmpty()) { newSub.append(CSUB_EXPIRATION, sub.getField(CSUB_EXPIRATION).numberLong()); } else { long long expiration = getCurrentTime() + requestP->duration.parse(); newSub.append(CSUB_EXPIRATION, expiration); LM_T(LmtMongo, ("New subscription expiration: %l", expiration)); } /* Restriction update */ // FIXME: Restrictions not implemented yet /* Throttling update */ if (!requestP->throttling.isEmpty()) { /* Throttling equal to 0 removes throttling */ long long throttling = requestP->throttling.parse(); if (throttling != 0) { newSub.append(CSUB_THROTTLING, throttling); } } else { /* The hasField check is needed due to Throttling could not be present in the original doc */ if (sub.hasField(CSUB_THROTTLING)) { newSub.append(CSUB_THROTTLING, sub.getField(CSUB_THROTTLING).numberLong()); } } /* Notify conditions */ bool notificationDone = false; if (requestP->notifyConditionVector.size() == 0) { newSub.appendArray(CSUB_CONDITIONS, sub.getField(CSUB_CONDITIONS).embeddedObject()); } else { /* Destroy any previous ONTIMEINTERVAL thread */ getNotifier()->destroyOntimeIntervalThreads(requestP->subscriptionId.get()); /* Build conditions array (including side-effect notifications and threads creation) * In order to do so, we have to create and EntityIdVector and AttributeList from sub * document, given the processConditionVector() signature */ EntityIdVector enV = subToEntityIdVector(sub); AttributeList attrL = subToAttributeList(sub); BSONArray conds = processConditionVector(&requestP->notifyConditionVector, enV, attrL, requestP->subscriptionId.get(), C_STR_FIELD(sub, CSUB_REFERENCE), ¬ificationDone, inFormat, tenant); newSub.appendArray(CSUB_CONDITIONS, conds); /* Remove EntityIdVector and AttributeList dynamic memory */ enV.release(); attrL.release(); } int count = sub.hasField(CSUB_COUNT) ? sub.getIntField(CSUB_COUNT) : 0; /* Last notification */ if (notificationDone) { newSub.append(CSUB_LASTNOTIFICATION, getCurrentTime()); newSub.append(CSUB_COUNT, count + 1); } else { /* The hasField check is needed due to lastNotification/count could not be present in the original doc */ if (sub.hasField(CSUB_LASTNOTIFICATION)) { newSub.append(CSUB_LASTNOTIFICATION, sub.getIntField(CSUB_LASTNOTIFICATION)); } if (sub.hasField(CSUB_COUNT)) { newSub.append(CSUB_COUNT, count); } } /* Adding format to use in notifications */ newSub.append(CSUB_FORMAT, std::string(formatToString(inFormat))); /* Update document in MongoDB */ BSONObj update = newSub.obj(); try { LM_T(LmtMongo, ("update() in '%s' collection _id '%s': %s}", getSubscribeContextCollectionName(tenant).c_str(), requestP->subscriptionId.get().c_str(), update.toString().c_str())); mongoSemTake(__FUNCTION__, "update in SubscribeContextCollection"); connection->update(getSubscribeContextCollectionName(tenant).c_str(), BSON("_id" << OID(requestP->subscriptionId.get())), update); mongoSemGive(__FUNCTION__, "update in SubscribeContextCollection"); LM_I(("Database Operation Successful (update _id: %s, %s)", requestP->subscriptionId.get().c_str(), update.toString().c_str())); } catch (const DBException &e) { mongoSemGive(__FUNCTION__, "update in SubscribeContextCollection (mongo db exception)"); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (mongo db exception)"); responseP->subscribeError.errorCode.fill(SccReceiverInternalError, std::string("collection: ") + getSubscribeContextCollectionName(tenant).c_str() + " - update() _id: " + requestP->subscriptionId.get().c_str() + " - update() doc: " + update.toString() + " - exception: " + e.what()); LM_E(("Database Error (%s)", responseP->subscribeError.errorCode.details.c_str())); return SccOk; } catch (...) { mongoSemGive(__FUNCTION__, "update in SubscribeContextCollection (mongo generic exception)"); reqSemGive(__FUNCTION__, "ngsi10 update subscription request (mongo generic exception)"); responseP->subscribeError.errorCode.fill(SccReceiverInternalError, std::string("collection: ") + getSubscribeContextCollectionName(tenant).c_str() + " - update() _id: " + requestP->subscriptionId.get().c_str() + " - update() doc: " + update.toString() + " - exception: " + "generic"); LM_E(("Database Error (%s)", responseP->subscribeError.errorCode.details.c_str())); return SccOk; } /* Duration and throttling are optional parameters, they are only added in the case they * was used for update */ if (!requestP->duration.isEmpty()) { responseP->subscribeResponse.duration = requestP->duration; } if (!requestP->throttling.isEmpty()) { responseP->subscribeResponse.throttling = requestP->throttling; } responseP->subscribeResponse.subscriptionId = requestP->subscriptionId; reqSemGive(__FUNCTION__, "ngsi10 update subscription request"); return SccOk; }
int main( int argc, const char **argv ) { const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) throw -12; port = argv[ 2 ]; } DBClientConnection conn; string errmsg; if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { cout << "couldn't connect : " << errmsg << endl; throw -11; } const char * ns = "test.test1"; conn.dropCollection(ns); // clean up old data from any previous tests conn.remove( ns, BSONObj() ); assert( conn.findOne( ns , BSONObj() ).isEmpty() ); // test insert conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) ); assert( ! conn.findOne( ns , BSONObj() ).isEmpty() ); // test remove conn.remove( ns, BSONObj() ); assert( conn.findOne( ns , BSONObj() ).isEmpty() ); // insert, findOne testing conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) ); { BSONObj res = conn.findOne( ns , BSONObj() ); assert( strstr( res.getStringField( "name" ) , "eliot" ) ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); assert( 1 == res.getIntField( "num" ) ); } // cursor conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) ); { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 2 ); } { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 1 ); } { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 0 ); } // update { BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj(); conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after ); res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() ); conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after ); res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( strstr( res.getStringField( "name" ) , "eliot" ) ); assert( strstr( res.getStringField( "name2" ) , "h" ) ); assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() ); // upsert conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 ); assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() ); } { // ensure index assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); } { // hint related tests assert( conn.findOne(ns, "{}")["name"].str() == "sara" ); assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" ); assert( conn.getLastError() == "" ); // nonexistent index test bool asserted = false; try { conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")); } catch ( ... ){ asserted = true; } assert( asserted ); //existing index assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") ); // run validate assert( conn.validate( ns ) ); } { // timestamp test const char * tsns = "test.tstest1"; conn.dropCollection( tsns ); { mongo::BSONObjBuilder b; b.appendTimestamp( "ts" ); conn.insert( tsns , b.obj() ); } mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() ); Date_t oldTime = out["ts"].timestampTime(); unsigned int oldInc = out["ts"].timestampInc(); { mongo::BSONObjBuilder b1; b1.append( out["_id"] ); mongo::BSONObjBuilder b2; b2.append( out["_id"] ); b2.appendTimestamp( "ts" ); conn.update( tsns , b1.obj() , b2.obj() ); } BSONObj found = conn.findOne( tsns , mongo::BSONObj() ); cout << "old: " << out << "\nnew: " << found << endl; assert( ( oldTime < found["ts"].timestampTime() ) || ( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) ); } { // check that killcursors doesn't affect last error assert( conn.getLastError().empty() ); BufBuilder b; b.appendNum( (int)0 ); // reserved b.appendNum( (int)-1 ); // invalid # of cursors triggers exception b.appendNum( (int)-1 ); // bogus cursor id Message m; m.setData( dbKillCursors, b.buf(), b.len() ); // say() is protected in DBClientConnection, so get superclass static_cast< DBConnector* >( &conn )->say( m ); assert( conn.getLastError().empty() ); } { list<string> l = conn.getDatabaseNames(); for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ cout << "db name : " << *i << endl; } l = conn.getCollectionNames( "test" ); for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ cout << "coll name : " << *i << endl; } } cout << "client test finished!" << endl; }