本文整理汇总了C++中BSONObj::toString方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::toString方法的具体用法?C++ BSONObj::toString怎么用?C++ BSONObj::toString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mergeChunks
bool mergeChunks( OperationContext* txn,
const NamespaceString& nss,
const BSONObj& minKey,
const BSONObj& maxKey,
const OID& epoch,
string* errMsg ) {
//
// Get sharding state up-to-date
//
ConnectionString configLoc = ConnectionString::parse( shardingState.getConfigServer(),
*errMsg );
if ( !configLoc.isValid() ){
warning() << *errMsg << endl;
return false;
}
//
// Get the distributed lock
//
ScopedDistributedLock collLock( configLoc, nss.ns() );
collLock.setLockMessage( stream() << "merging chunks in " << nss.ns() << " from "
<< minKey << " to " << maxKey );
Status acquisitionStatus = collLock.tryAcquire();
if (!acquisitionStatus.isOK()) {
*errMsg = stream() << "could not acquire collection lock for " << nss.ns()
<< " to merge chunks in [" << minKey << "," << maxKey << ")"
<< causedBy(acquisitionStatus);
warning() << *errMsg << endl;
return false;
}
//
// We now have the collection lock, refresh metadata to latest version and sanity check
//
ChunkVersion shardVersion;
Status status = shardingState.refreshMetadataNow(txn, nss.ns(), &shardVersion);
if ( !status.isOK() ) {
*errMsg = str::stream() << "could not merge chunks, failed to refresh metadata for "
<< nss.ns() << causedBy( status.reason() );
warning() << *errMsg << endl;
return false;
}
if ( epoch.isSet() && shardVersion.epoch() != epoch ) {
*errMsg = stream() << "could not merge chunks, collection " << nss.ns()
<< " has changed" << " since merge was sent" << "(sent epoch : "
<< epoch.toString()
<< ", current epoch : " << shardVersion.epoch().toString() << ")";
warning() << *errMsg << endl;
return false;
}
CollectionMetadataPtr metadata = shardingState.getCollectionMetadata( nss.ns() );
if ( !metadata || metadata->getKeyPattern().isEmpty() ) {
*errMsg = stream() << "could not merge chunks, collection " << nss.ns()
<< " is not sharded";
warning() << *errMsg << endl;
return false;
}
dassert( metadata->getShardVersion().equals( shardVersion ) );
if ( !metadata->isValidKey( minKey ) || !metadata->isValidKey( maxKey ) ) {
*errMsg = stream() << "could not merge chunks, the range "
<< rangeToString( minKey, maxKey ) << " is not valid"
<< " for collection " << nss.ns() << " with key pattern "
<< metadata->getKeyPattern();
warning() << *errMsg << endl;
return false;
}
//
// Get merged chunk information
//
ChunkVersion mergeVersion = metadata->getCollVersion();
mergeVersion.incMinor();
OwnedPointerVector<ChunkType> chunksToMerge;
ChunkType itChunk;
itChunk.setMin( minKey );
itChunk.setMax( minKey );
itChunk.setNS( nss.ns() );
//.........这里部分代码省略.........
示例2: parseAnObj
// PD_TRACE_DECLARE_FUNCTION ( SDB_CLSCATAMATCHER_PARSEANOBJ, "clsCatalogMatcher::parseAnObj" )
INT32 clsCatalogMatcher::parseAnObj( const BSONObj &matcher,
clsCatalogPredicateTree &predicateSet )
{
INT32 rc = SDB_OK;
PD_TRACE_ENTRY ( SDB_CLSCATAMATCHER_PARSEANOBJ ) ;
clsCatalogPredicateTree *pPredicateSet = NULL;
BOOLEAN isNew = FALSE ;
try
{
BSONObjIterator i( matcher ) ;
while ( i.more() )
{
BSONElement beTmp = i.next();
const CHAR *pFieldName = beTmp.fieldName() ;
if ( predicateSet.getLogicType() == CLS_CATA_LOGIC_OR )
{
pPredicateSet = SDB_OSS_NEW
clsCatalogPredicateTree( _shardingKey ) ;
PD_CHECK( pPredicateSet != NULL, SDB_OOM, error, PDERROR,
"malloc failed" ) ;
isNew = TRUE ;
}
else
{
pPredicateSet = &predicateSet ;
}
if ( MTH_OPERATOR_EYECATCHER == pFieldName[0] )
{
rc = parseLogicOp( beTmp, *pPredicateSet );
}
else
{
rc = parseCmpOp( beTmp, *pPredicateSet );
}
PD_RC_CHECK( rc, PDERROR, "Failed to parse the field(rc=%d)",
rc ) ;
if ( isNew )
{
predicateSet.addChild( pPredicateSet ) ;
}
if ( predicateSet.getLogicType() == CLS_CATA_LOGIC_OR &&
predicateSet.isUniverse() )
{
goto done ;
}
}
}
catch ( std::exception &e )
{
rc = SDB_INVALIDARG ;
PD_RC_CHECK( rc, PDERROR, "Failed to parse the matcher(%s), "
"occured unexpected error:%s",
matcher.toString( false, false ).c_str(),
e.what() ) ;
}
done:
PD_TRACE_EXITRC ( SDB_CLSCATAMATCHER_PARSEANOBJ, rc ) ;
return rc;
error:
if ( isNew )
{
SDB_OSS_DEL( pPredicateSet );
pPredicateSet = NULL;
}
goto done;
}
示例3: operator
void operator()( DBClientCursorBatchIterator &i ) {
mongolock l( true );
if ( context ) {
context->relocked();
}
while( i.moreInCurrentBatch() ) {
if ( n % 128 == 127 /*yield some*/ ) {
time_t now = time(0);
if( now - lastLog >= 60 ) {
// report progress
if( lastLog )
log() << "clone " << to_collection << ' ' << n << endl;
lastLog = now;
}
mayInterrupt( _mayBeInterrupted );
dbtempreleaseif t( _mayYield );
}
BSONObj tmp = i.nextSafe();
/* assure object is valid. note this will slow us down a little. */
if ( !tmp.valid() ) {
stringstream ss;
ss << "Cloner: skipping corrupt object from " << from_collection;
BSONElement e = tmp.firstElement();
try {
e.validate();
ss << " firstElement: " << e;
}
catch( ... ) {
ss << " firstElement corrupt";
}
out() << ss.str() << endl;
continue;
}
++n;
BSONObj js = tmp;
if ( isindex ) {
verify( strstr(from_collection, "system.indexes") );
js = fixindex(tmp);
storedForLater->push_back( js.getOwned() );
continue;
}
try {
theDataFileMgr.insertWithObjMod(to_collection, js);
if ( logForRepl )
logOp("i", to_collection, js);
getDur().commitIfNeeded();
}
catch( UserException& e ) {
log() << "warning: exception cloning object in " << from_collection << ' ' << e.what() << " obj:" << js.toString() << '\n';
}
RARELY if ( time( 0 ) - saveLast > 60 ) {
log() << n << " objects cloned so far from collection " << from_collection << endl;
saveLast = time( 0 );
}
}
}
示例4: runRollbackInsertFromOplog
static void runRollbackInsertFromOplog(const char* ns, BSONObj op) {
// handle add index case
if (mongoutils::str::endsWith(ns, ".system.indexes")) {
throw RollbackOplogException(str::stream() << "Not rolling back an add index on " << ns << ". Op: " << op.toString(false, true));
}
else {
// the rollback of a normal insert is to do the delete
runDeleteFromOplog(ns, op);
}
}
示例5: run
bool run( const string& dbname,
BSONObj& cmdObj,
int,
string& errmsg,
BSONObjBuilder& result,
bool ) {
string ns;
if ( !FieldParser::extract( cmdObj, nsField, &ns, &errmsg ) ) {
return false;
}
if ( ns.size() == 0 ) {
errmsg = "no namespace specified";
return false;
}
vector<BSONObj> bounds;
if ( !FieldParser::extract( cmdObj, boundsField, &bounds, &errmsg ) ) {
return false;
}
if ( bounds.size() == 0 ) {
errmsg = "no bounds were specified";
return false;
}
if ( bounds.size() != 2 ) {
errmsg = "only a min and max bound may be specified";
return false;
}
BSONObj minKey = bounds[0];
BSONObj maxKey = bounds[1];
if ( minKey.isEmpty() ) {
errmsg = "no min key specified";
return false;
}
if ( maxKey.isEmpty() ) {
errmsg = "no max key specified";
return false;
}
ShardPtr mergeShard = guessMergeShard( NamespaceString( ns ), minKey );
if ( !mergeShard ) {
errmsg = (string)"could not find shard for merge range starting at "
+ minKey.toString();
return false;
}
BSONObjBuilder remoteCmdObjB;
remoteCmdObjB.append( cmdObj[ MergeChunksPassCommand::nsField() ] );
remoteCmdObjB.append( cmdObj[ MergeChunksPassCommand::boundsField() ] );
remoteCmdObjB.append( MergeChunksPassCommand::configField(),
configServer.getPrimary().getAddress().toString() );
remoteCmdObjB.append( MergeChunksPassCommand::shardNameField(),
mergeShard->getName() );
BSONObj remoteResult;
// Throws, but handled at level above. Don't want to rewrap to preserve exception
// formatting.
ScopedDbConnection conn( mergeShard->getAddress() );
bool ok = conn->runCommand( "admin", remoteCmdObjB.obj(), remoteResult );
conn.done();
// Always refresh our chunks afterwards
refreshChunkCache( NamespaceString( ns ) );
result.appendElements( remoteResult );
return ok;
}
示例6: copy
/* copy the specified collection
isindex - if true, this is system.indexes collection, in which we do some transformation when copying.
*/
void Cloner::copy(const char *from_collection, const char *to_collection, bool isindex, bool logForRepl, bool masterSameProcess, bool slaveOk, Query query) {
auto_ptr<DBClientCursor> c;
{
dbtemprelease r;
c = conn->query( from_collection, query, 0, 0, 0, Option_NoCursorTimeout | ( slaveOk ? Option_SlaveOk : 0 ) );
}
list<BSONObj> storedForLater;
assert( c.get() );
long long n = 0;
time_t saveLast = time( 0 );
while ( 1 ) {
{
dbtemprelease r;
if ( !c->more() )
break;
}
BSONObj tmp = c->next();
/* assure object is valid. note this will slow us down a little. */
if ( !tmp.valid() ) {
stringstream ss;
ss << "skipping corrupt object from " << from_collection;
BSONElement e = tmp.firstElement();
try {
e.validate();
ss << " firstElement: " << e;
}
catch( ... ){
ss << " firstElement corrupt";
}
out() << ss.str() << endl;
continue;
}
++n;
BSONObj js = tmp;
if ( isindex ) {
assert( strstr(from_collection, "system.indexes") );
js = fixindex(tmp);
storedForLater.push_back( js.getOwned() );
continue;
}
try {
theDataFileMgr.insert(to_collection, js);
if ( logForRepl )
logOp("i", to_collection, js);
}
catch( UserException& e ) {
log() << "warning: exception cloning object in " << from_collection << ' ' << e.what() << " obj:" << js.toString() << '\n';
}
RARELY if ( time( 0 ) - saveLast > 60 ) {
log() << n << " objects cloned so far from collection " << from_collection << endl;
saveLast = time( 0 );
}
}
if ( storedForLater.size() ){
for ( list<BSONObj>::iterator i = storedForLater.begin(); i!=storedForLater.end(); i++ ){
BSONObj js = *i;
try {
theDataFileMgr.insert(to_collection, js);
if ( logForRepl )
logOp("i", to_collection, js);
}
catch( UserException& e ) {
log() << "warning: exception cloning object in " << from_collection << ' ' << e.what() << " obj:" << js.toString() << '\n';
}
}
}
}
示例7: initialize
void DocumentSourceOut::initialize() {
DBClientBase* conn = pExpCtx->mongoProcessInterface->directClient();
// Save the original collection options and index specs so we can check they didn't change
// during computation.
_originalOutOptions = pExpCtx->mongoProcessInterface->getCollectionOptions(_outputNs);
_originalIndexes = conn->getIndexSpecs(_outputNs.ns());
// Check if it's sharded or capped to make sure we have a chance of succeeding before we do all
// the work. If the collection becomes capped during processing, the collection options will
// have changed, and the $out will fail. If it becomes sharded during processing, the final
// rename will fail.
uassert(17017,
str::stream() << "namespace '" << _outputNs.ns()
<< "' is sharded so it can't be used for $out'",
!pExpCtx->mongoProcessInterface->isSharded(pExpCtx->opCtx, _outputNs));
uassert(17152,
str::stream() << "namespace '" << _outputNs.ns()
<< "' is capped so it can't be used for $out",
_originalOutOptions["capped"].eoo());
// We will write all results into a temporary collection, then rename the temporary collection
// to be the target collection once we are done.
_tempNs = NamespaceString(str::stream() << _outputNs.db() << ".tmp.agg_out."
<< aggOutCounter.addAndFetch(1));
// Create output collection, copying options from existing collection if any.
{
BSONObjBuilder cmd;
cmd << "create" << _tempNs.coll();
cmd << "temp" << true;
cmd.appendElementsUnique(_originalOutOptions);
BSONObj info;
bool ok = conn->runCommand(_outputNs.db().toString(), cmd.done(), info);
uassert(16994,
str::stream() << "failed to create temporary $out collection '" << _tempNs.ns()
<< "': "
<< info.toString(),
ok);
}
// copy indexes to _tempNs
for (std::list<BSONObj>::const_iterator it = _originalIndexes.begin();
it != _originalIndexes.end();
++it) {
MutableDocument index((Document(*it)));
index.remove("_id"); // indexes shouldn't have _ids but some existing ones do
index["ns"] = Value(_tempNs.ns());
BSONObj indexBson = index.freeze().toBson();
conn->insert(_tempNs.getSystemIndexesCollection(), indexBson);
BSONObj err = conn->getLastErrorDetailed();
uassert(16995,
str::stream() << "copying index for $out failed."
<< " index: "
<< indexBson
<< " error: "
<< err,
DBClientBase::getLastErrorString(err).empty());
}
_initialized = true;
}
示例8: atoi
/* ****************************************************************************
*
* mongoAttributesForEntityType -
*/
HttpStatusCode mongoAttributesForEntityType
(
std::string entityType,
EntityTypeAttributesResponse* responseP,
const std::string& tenant,
const std::vector<std::string>& servicePathV,
std::map<std::string, std::string>& uriParams
)
{
unsigned int offset = atoi(uriParams[URI_PARAM_PAGINATION_OFFSET].c_str());
unsigned int limit = atoi(uriParams[URI_PARAM_PAGINATION_LIMIT].c_str());
std::string detailsString = uriParams[URI_PARAM_PAGINATION_DETAILS];
bool details = (strcasecmp("on", detailsString.c_str()) == 0)? true : false;
// Setting the name of the entity type for the response
responseP->entityType.type = entityType;
LM_T(LmtMongo, ("Query Types Attribute for <%s>", entityType.c_str()));
LM_T(LmtPagination, ("Offset: %d, Limit: %d, Details: %s", offset, limit, (details == true)? "true" : "false"));
reqSemTake(__FUNCTION__, "query types attributes request");
DBClientBase* connection = getMongoConnection();
/* Compose query based on this aggregation command:
*
* FIXME P9: taking into account that type is no longer used as part of the attribute "key", not sure if the
* aggregation query below is fully correct
*
* db.runCommand({aggregate: "entities",
* pipeline: [ {$match: { "_id.type": "TYPE" , "_id.servicePath": /.../ } },
* {$project: {_id: 1, "attrs.name": 1, "attrs.type": 1} },
* {$unwind: "$attrs"},
* {$group: {_id: "$_id.type", attrs: {$addToSet: "$attrs"}} },
* {$unwind: "$attrs"},
* {$group: {_id: "$attrs" }},
* {$sort: {_id.name: 1, _id.type: 1} }
* ]
* })
*
*/
BSONObj result;
BSONObj cmd = BSON("aggregate" << COL_ENTITIES <<
"pipeline" << BSON_ARRAY(
BSON("$match" << BSON(C_ID_ENTITY << entityType << C_ID_SERVICEPATH << fillQueryServicePath(servicePathV))) <<
BSON("$project" << BSON("_id" << 1 << C_ATTR_NAME << 1 << C_ATTR_TYPE << 1)) <<
BSON("$unwind" << S_ATTRS) <<
BSON("$group" << BSON("_id" << CS_ID_ENTITY << "attrs" << BSON("$addToSet" << S_ATTRS))) <<
BSON("$unwind" << S_ATTRS) <<
BSON("$group" << BSON("_id" << S_ATTRS)) <<
BSON("$sort" << BSON(C_ID_NAME << 1 << C_ID_TYPE << 1))
)
);
LM_T(LmtMongo, ("runCommand() in '%s' database: '%s'", composeDatabaseName(tenant).c_str(), cmd.toString().c_str()));
mongoSemTake(__FUNCTION__, "aggregation command");
try
{
connection->runCommand(composeDatabaseName(tenant).c_str(), cmd, result);
mongoSemGive(__FUNCTION__, "aggregation command");
LM_I(("Database Operation Successful (%s)", cmd.toString().c_str()));
}
catch (const DBException& e)
{
mongoSemGive(__FUNCTION__, "aggregation command");
std::string err = std::string("database: ") + composeDatabaseName(tenant).c_str() +
" - command: " + cmd.toString() +
" - exception: " + e.what();
LM_E(("Database Error (%s)", err.c_str()));
responseP->statusCode.fill(SccReceiverInternalError, err);
reqSemGive(__FUNCTION__, "query types request");
return SccOk;
}
catch (...)
{
mongoSemGive(__FUNCTION__, "aggregation command");
std::string err = std::string("database: ") + composeDatabaseName(tenant).c_str() +
" - command: " + cmd.toString() +
" - exception: " + "generic";
LM_E(("Database Error (%s)", err.c_str()));
responseP->statusCode.fill(SccReceiverInternalError, err);
reqSemGive(__FUNCTION__, "query types request");
return SccOk;
}
/* Processing result to build response*/
LM_T(LmtMongo, ("aggregation result: %s", result.toString().c_str()));
std::vector<BSONElement> resultsArray = result.getField("result").Array();
/* See comment above in the other method regarding this strategy to implement pagination */
//.........这里部分代码省略.........
示例9: ossMemset
// PD_TRACE_DECLARE_FUNCTION ( SDB__IXMINXCB2, "_ixmIndexCB::_ixmIndexCB" )
_ixmIndexCB::_ixmIndexCB ( dmsExtentID extentID,
const BSONObj &infoObj,
UINT16 mbID ,
_dmsStorageIndex *pIndexSu,
_dmsContext *context )
{
SDB_ASSERT ( pIndexSu, "index su can't be NULL" ) ;
PD_TRACE_ENTRY ( SDB__IXMINXCB2 );
ixmIndexCBExtent *pExtent = NULL ;
_isInitialized = FALSE ;
dmsExtRW extRW ;
_pIndexSu = pIndexSu ;
_pContext = context ;
_extentID = extentID ;
_pageSize = _pIndexSu->pageSize() ;
_extent = (const ixmIndexCBExtent*)pIndexSu->beginFixedAddr ( extentID,
1 ) ;
extRW = pIndexSu->extent2RW( extentID, context->mbID() ) ;
pExtent = extRW.writePtr<ixmIndexCBExtent>( 0, _pageSize ) ;
if ( infoObj.objsize() + IXM_INDEX_CB_EXTENT_METADATA_SIZE >=
(UINT32)_pageSize )
{
PD_LOG ( PDERROR, "index object is too big: %s",
infoObj.toString().c_str() ) ;
goto error ;
}
pExtent->_type = IXM_EXTENT_TYPE_NONE ;
if ( !generateIndexType( infoObj, pExtent->_type ) )
{
goto error ;
}
pExtent->_flag = DMS_EXTENT_FLAG_INUSE ;
pExtent->_eyeCatcher [0] = IXM_EXTENT_CB_EYECATCHER0 ;
pExtent->_eyeCatcher [1] = IXM_EXTENT_CB_EYECATCHER1 ;
pExtent->_indexFlag = IXM_INDEX_FLAG_INVALID ;
pExtent->_mbID = mbID ;
pExtent->_version = DMS_EXTENT_CURRENT_V ;
pExtent->_logicID = DMS_INVALID_EXTENT ;
pExtent->_scanExtLID = DMS_INVALID_EXTENT ;
pExtent->_rootExtentID = DMS_INVALID_EXTENT ;
ossMemset( pExtent->_reserved, 0, sizeof( pExtent->_reserved ) ) ;
if ( !infoObj.hasField (DMS_ID_KEY_NAME) )
{
_IDToInsert oid ;
oid._oid.init() ;
*(INT32*)(((CHAR*)pExtent) +IXM_INDEX_CB_EXTENT_METADATA_SIZE) =
infoObj.objsize() + sizeof(_IDToInsert) ;
ossMemcpy ( ((CHAR*)pExtent) +
IXM_INDEX_CB_EXTENT_METADATA_SIZE +
sizeof(INT32),
(CHAR*)(&oid),
sizeof(_IDToInsert)) ;
ossMemcpy ( ((CHAR*)pExtent) +
IXM_INDEX_CB_EXTENT_METADATA_SIZE +
sizeof(INT32) +
sizeof(_IDToInsert),
infoObj.objdata()+sizeof(INT32),
infoObj.objsize()-sizeof(INT32) ) ;
}
else
{
ossMemcpy ( ((CHAR*)pExtent) +
IXM_INDEX_CB_EXTENT_METADATA_SIZE,
infoObj.objdata(),
infoObj.objsize() ) ;
}
_init() ;
done :
PD_TRACE_EXIT ( SDB__IXMINXCB2 );
return ;
error :
goto done ;
}
示例10: tailingQuery
void OplogReader::tailingQuery(const char* ns, const BSONObj& query) {
verify(!haveCursor());
LOG(2) << ns << ".find(" << query.toString() << ')' << endl;
cursor.reset(_conn->query(ns, query, 0, 0, nullptr, _tailingQueryOptions).release());
}
示例11: rangeToString
string CollectionMetadata::rangeToString( const BSONObj& inclusiveLower,
const BSONObj& exclusiveUpper ) const {
stringstream ss;
ss << "[" << inclusiveLower.toString() << ", " << exclusiveUpper.toString() << ")";
return ss.str();
}
示例12: mongoUpdateContextSubscription
/* ****************************************************************************
*
* 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 */
//.........这里部分代码省略.........
示例13: mongoUpdateContextAvailabilitySubscription
//.........这里部分代码省略.........
/* Entities (mandatory) */
BSONArrayBuilder entities;
for (unsigned int ix = 0; ix < requestP->entityIdVector.size(); ++ix) {
EntityId* en = requestP->entityIdVector.get(ix);
if (en->type == "") {
entities.append(BSON(CASUB_ENTITY_ID << en->id <<
CASUB_ENTITY_ISPATTERN << en->isPattern));
}
else {
entities.append(BSON(CASUB_ENTITY_ID << en->id <<
CASUB_ENTITY_TYPE << en->type <<
CASUB_ENTITY_ISPATTERN << en->isPattern));
}
}
newSub.append(CASUB_ENTITIES, entities.arr());
/* Attributes (always taken into account) */
BSONArrayBuilder attrs;
for (unsigned int ix = 0; ix < requestP->attributeList.size(); ++ix) {
attrs.append(requestP->attributeList.get(ix));
}
newSub.append(CASUB_ATTRS, attrs.arr());
/* Duration (optional) */
if (requestP->duration.isEmpty()) {
newSub.append(CASUB_EXPIRATION, sub.getField(CASUB_EXPIRATION).numberLong());
}
else {
long long expiration = getCurrentTime() + requestP->duration.parse();
newSub.append(CASUB_EXPIRATION, expiration);
LM_T(LmtMongo, ("New subscription expiration: %l", expiration));
}
/* Reference is not updatable, so it is appended directly */
newSub.append(CASUB_REFERENCE, STR_FIELD(sub, CASUB_REFERENCE));
int count = sub.hasField(CASUB_COUNT) ? sub.getIntField(CASUB_COUNT) : 0;
/* The hasField check is needed due to lastNotification/count could not be present in the original doc */
if (sub.hasField(CASUB_LASTNOTIFICATION)) {
newSub.append(CASUB_LASTNOTIFICATION, sub.getIntField(CASUB_LASTNOTIFICATION));
}
if (sub.hasField(CASUB_COUNT)) {
newSub.append(CASUB_COUNT, count);
}
/* Adding format to use in notifications */
newSub.append(CASUB_FORMAT, std::string(formatToString(inFormat)));
/* Update document in MongoDB */
BSONObj update = newSub.obj();
LM_T(LmtMongo, ("update() in '%s' collection _id '%s': %s}", getSubscribeContextAvailabilityCollectionName(tenant).c_str(),
requestP->subscriptionId.get().c_str(),
update.toString().c_str()));
try {
mongoSemTake(__FUNCTION__, "update in SubscribeContextAvailabilityCollection");
connection->update(getSubscribeContextAvailabilityCollectionName(tenant).c_str(), BSON("_id" << OID(requestP->subscriptionId.get())), update);
mongoSemGive(__FUNCTION__, "update in SubscribeContextAvailabilityCollection");
}
catch( const DBException &e ) {
mongoSemGive(__FUNCTION__, "update in SubscribeContextAvailabilityCollection (mongo db exception)");
reqSemGive(__FUNCTION__, "ngsi9 update subscription request (mongo db exception)");
responseP->errorCode.fill(SccReceiverInternalError,
std::string("collection: ") + getSubscribeContextAvailabilityCollectionName(tenant).c_str() +
" - update() _id: " + requestP->subscriptionId.get().c_str() +
" - update() doc: " + update.toString() +
" - exception: " + e.what());
return SccOk;
}
catch(...) {
mongoSemGive(__FUNCTION__, "update in SubscribeContextAvailabilityCollection (mongo generic exception)");
reqSemGive(__FUNCTION__, "ngsi9 update subscription request (mongo generic exception)");
responseP->errorCode.fill(SccReceiverInternalError,
std::string("collection: ") + getSubscribeContextAvailabilityCollectionName(tenant).c_str() +
" - update() _id: " + requestP->subscriptionId.get().c_str() +
" - update() doc: " + update.toString() +
" - exception: " + "generic");
return SccOk;
}
/* Send notifications for matching context registrations */
processAvailabilitySubscription(requestP->entityIdVector, requestP->attributeList, requestP->subscriptionId.get(), STR_FIELD(sub, CASUB_REFERENCE), inFormat, tenant);
/* Duration is an optional parameter, it is only added in the case they
* was used for update */
if (!requestP->duration.isEmpty()) {
responseP->duration = requestP->duration;
}
responseP->subscriptionId = requestP->subscriptionId;
reqSemGive(__FUNCTION__, "ngsi9 update subscription request");
return SccOk;
}
示例14: group
bool group( OperationContext* txn,
Database* db,
const std::string& ns,
const BSONObj& query,
BSONObj keyPattern,
const std::string& keyFunctionCode,
const std::string& reduceCode,
const char * reduceScope,
BSONObj initial,
const std::string& finalize,
string& errmsg,
BSONObjBuilder& result ) {
const string userToken = ClientBasic::getCurrent()->getAuthorizationSession()
->getAuthenticatedUserNamesToken();
auto_ptr<Scope> s = globalScriptEngine->getPooledScope(db->name(), "group" + userToken);
if ( reduceScope )
s->init( reduceScope );
s->setObject( "$initial" , initial , true );
s->exec( "$reduce = " + reduceCode , "$group reduce setup" , false , true , true , 100 );
s->exec( "$arr = [];" , "$group reduce setup 2" , false , true , true , 100 );
ScriptingFunction f = s->createFunction(
"function(){ "
" if ( $arr[n] == null ){ "
" next = {}; "
" Object.extend( next , $key ); "
" Object.extend( next , $initial , true ); "
" $arr[n] = next; "
" next = null; "
" } "
" $reduce( obj , $arr[n] ); "
"}" );
ScriptingFunction keyFunction = 0;
if ( keyFunctionCode.size() ) {
keyFunction = s->createFunction( keyFunctionCode.c_str() );
}
double keysize = keyPattern.objsize() * 3;
double keynum = 1;
Collection* collection = db->getCollection( txn, ns );
const WhereCallbackReal whereCallback(txn, StringData(db->name()));
map<BSONObj,int,BSONObjCmp> map;
list<BSONObj> blah;
if (collection) {
CanonicalQuery* cq;
if (!CanonicalQuery::canonicalize(ns, query, &cq, whereCallback).isOK()) {
uasserted(17212, "Can't canonicalize query " + query.toString());
return 0;
}
Runner* rawRunner;
if (!getRunner(txn,collection, cq, &rawRunner).isOK()) {
uasserted(17213, "Can't get runner for query " + query.toString());
return 0;
}
auto_ptr<Runner> runner(rawRunner);
const ScopedRunnerRegistration safety(runner.get());
BSONObj obj;
Runner::RunnerState state;
while (Runner::RUNNER_ADVANCED == (state = runner->getNext(&obj, NULL))) {
BSONObj key = getKey(obj , keyPattern , keyFunction , keysize / keynum,
s.get() );
keysize += key.objsize();
keynum++;
int& n = map[key];
if ( n == 0 ) {
n = map.size();
s->setObject( "$key" , key , true );
uassert(17203, "group() can't handle more than 20000 unique keys",
n <= 20000 );
}
s->setObject( "obj" , obj , true );
s->setNumber( "n" , n - 1 );
if ( s->invoke( f , 0, 0 , 0 , true ) ) {
throw UserException(17214,
(string)"reduce invoke failed: " + s->getError());
}
}
}
if (!finalize.empty()) {
s->exec( "$finalize = " + finalize , "$group finalize define" ,
false , true , true , 100 );
ScriptingFunction g = s->createFunction(
"function(){ "
" for(var i=0; i < $arr.length; i++){ "
" var ret = $finalize($arr[i]); "
//.........这里部分代码省略.........
示例15: make
// static
Status ParsedProjection::make(const BSONObj& spec,
const MatchExpression* const query,
ParsedProjection** out,
const MatchExpressionParser::WhereCallback& whereCallback) {
// Are we including or excluding fields? Values:
// -1 when we haven't initialized it.
// 1 when we're including
// 0 when we're excluding.
int include_exclude = -1;
// If any of these are 'true' the projection isn't covered.
bool include = true;
bool hasNonSimple = false;
bool hasDottedField = false;
bool includeID = true;
bool hasIndexKeyProjection = false;
bool wantGeoNearPoint = false;
bool wantGeoNearDistance = false;
// Until we see a positional or elemMatch operator we're normal.
ArrayOpType arrayOpType = ARRAY_OP_NORMAL;
BSONObjIterator it(spec);
while (it.more()) {
BSONElement e = it.next();
if (!e.isNumber() && !e.isBoolean()) {
hasNonSimple = true;
}
if (Object == e.type()) {
BSONObj obj = e.embeddedObject();
if (1 != obj.nFields()) {
return Status(ErrorCodes::BadValue, ">1 field in obj: " + obj.toString());
}
BSONElement e2 = obj.firstElement();
if (mongoutils::str::equals(e2.fieldName(), "$slice")) {
if (e2.isNumber()) {
// This is A-OK.
} else if (e2.type() == Array) {
BSONObj arr = e2.embeddedObject();
if (2 != arr.nFields()) {
return Status(ErrorCodes::BadValue, "$slice array wrong size");
}
BSONObjIterator it(arr);
// Skip over 'skip'.
it.next();
int limit = it.next().numberInt();
if (limit <= 0) {
return Status(ErrorCodes::BadValue, "$slice limit must be positive");
}
} else {
return Status(ErrorCodes::BadValue,
"$slice only supports numbers and [skip, limit] arrays");
}
} else if (mongoutils::str::equals(e2.fieldName(), "$elemMatch")) {
// Validate $elemMatch arguments and dependencies.
if (Object != e2.type()) {
return Status(ErrorCodes::BadValue,
"elemMatch: Invalid argument, object required.");
}
if (ARRAY_OP_POSITIONAL == arrayOpType) {
return Status(ErrorCodes::BadValue,
"Cannot specify positional operator and $elemMatch.");
}
if (mongoutils::str::contains(e.fieldName(), '.')) {
return Status(ErrorCodes::BadValue,
"Cannot use $elemMatch projection on a nested field.");
}
arrayOpType = ARRAY_OP_ELEM_MATCH;
// Create a MatchExpression for the elemMatch.
BSONObj elemMatchObj = e.wrap();
verify(elemMatchObj.isOwned());
// TODO: Is there a faster way of validating the elemMatchObj?
StatusWithMatchExpression swme =
MatchExpressionParser::parse(elemMatchObj, whereCallback);
if (!swme.isOK()) {
return swme.getStatus();
}
delete swme.getValue();
} else if (mongoutils::str::equals(e2.fieldName(), "$meta")) {
// Field for meta must be top level. We can relax this at some point.
if (mongoutils::str::contains(e.fieldName(), '.')) {
return Status(ErrorCodes::BadValue, "field for $meta cannot be nested");
}
// Make sure the argument to $meta is something we recognize.
// e.g. {x: {$meta: "textScore"}}
if (String != e2.type()) {
//.........这里部分代码省略.........