本文整理汇总了C++中BatchItemRef类的典型用法代码示例。如果您正苦于以下问题:C++ BatchItemRef类的具体用法?C++ BatchItemRef怎么用?C++ BatchItemRef使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BatchItemRef类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: safeWrite
void DBClientSafeWriter::safeWrite( DBClientBase* conn,
const BatchItemRef& itemRef,
LastError* error ) {
const BatchedCommandRequest* request = itemRef.getRequest();
try {
// Default settings for checkShardVersion
const bool authoritative = false;
const int tryNum = 1;
// We need to set our version using setShardVersion, managed by checkShardVersionCB
versionManager.checkShardVersionCB( conn,
request->getTargetingNS(),
authoritative,
tryNum );
if ( request->getBatchType() == BatchedCommandRequest::BatchType_Insert ) {
conn->insert( request->getNS(),
request->getInsertRequest()->getDocumentsAt( itemRef.getItemIndex() ),
0 );
}
else if ( request->getBatchType() == BatchedCommandRequest::BatchType_Update ) {
const BatchedUpdateDocument* update =
request->getUpdateRequest()->getUpdatesAt( itemRef.getItemIndex() );
conn->update( request->getNS(),
update->getQuery(),
update->getUpdateExpr(),
update->getUpsert(),
update->getMulti() );
}
else {
dassert( request->getBatchType() == BatchedCommandRequest::BatchType_Delete );
const BatchedDeleteDocument* deleteDoc =
request->getDeleteRequest()->getDeletesAt( itemRef.getItemIndex() );
conn->remove( request->getNS(),
deleteDoc->getQuery(),
deleteDoc->getLimit() == 1 /*just one*/);
}
// Default GLE Options
const bool fsync = false;
const bool j = false;
const int w = 1;
const int wtimeout = 0;
BSONObj result = conn->getLastErrorDetailed( NamespaceString( request->getNS() ).db()
.toString(),
fsync,
j,
w,
wtimeout );
SafeWriter::fillLastError( result, error );
}
catch ( const DBException& ex ) {
error->raiseError( ex.getCode(), ex.toString().c_str() );
}
}
示例2: safeWrite
Status DBClientSafeWriter::safeWrite( DBClientBase* conn,
const BatchItemRef& itemRef,
const BSONObj& writeConcern,
BSONObj* gleResponse ) {
const BatchedCommandRequest* request = itemRef.getRequest();
try {
// Default settings for checkShardVersion
const bool authoritative = false;
const int tryNum = 1;
// We need to set our version using setShardVersion, managed by checkShardVersionCB
versionManager.checkShardVersionCB( conn,
request->getTargetingNS(),
authoritative,
tryNum );
if ( request->getBatchType() == BatchedCommandRequest::BatchType_Insert ) {
conn->insert( request->getNS(),
request->getInsertRequest()->getDocumentsAt( itemRef.getItemIndex() ),
0 );
}
else if ( request->getBatchType() == BatchedCommandRequest::BatchType_Update ) {
const BatchedUpdateDocument* update =
request->getUpdateRequest()->getUpdatesAt( itemRef.getItemIndex() );
conn->update( request->getNS(),
update->getQuery(),
update->getUpdateExpr(),
update->getUpsert(),
update->getMulti() );
}
else {
dassert( request->getBatchType() == BatchedCommandRequest::BatchType_Delete );
const BatchedDeleteDocument* deleteDoc =
request->getDeleteRequest()->getDeletesAt( itemRef.getItemIndex() );
conn->remove( request->getNS(),
deleteDoc->getQuery(),
deleteDoc->getLimit() == 1 /*just one*/);
}
const StringData& dbName = NamespaceString( request->getNS() ).db();
BSONObjBuilder gleCmdB;
gleCmdB.append( "getLastError", true );
gleCmdB.appendElements( writeConcern );
conn->runCommand( dbName.toString(), gleCmdB.obj(), *gleResponse );
}
catch ( const DBException& ex ) {
return ex.toStatus();
}
return Status::OK();
}
示例3: incOpStats
void WriteBatchExecutor::incOpStats( const BatchItemRef& currWrite ) {
if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Insert ) {
// No-op, for inserts we increment not on the op but once for each write
}
else if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Update ) {
_opCounters->gotUpdate();
}
else {
dassert( currWrite.getOpType() == BatchedCommandRequest::BatchType_Delete );
_opCounters->gotDelete();
}
}
示例4: incOpStats
void WriteBatchExecutor::incOpStats( const BatchItemRef& currWrite ) {
if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Insert ) {
_opCounters->gotInsert();
}
else if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Update ) {
_opCounters->gotUpdate();
}
else {
dassert( currWrite.getOpType() == BatchedCommandRequest::BatchType_Delete );
_opCounters->gotDelete();
}
}
示例5: execUpdate
void WriteBatchExecutor::execUpdate( const BatchItemRef& updateItem,
BSONObj* upsertedId,
WriteErrorDetail** error ) {
// Updates currently do a lot of the lock management internally
const BatchedCommandRequest& request = *updateItem.getRequest();
const NamespaceString nss( updateItem.getRequest()->getNS() );
// BEGIN CURRENT OP
scoped_ptr<CurOp> currentOp( beginCurrentOp( _client, updateItem ) );
incOpStats( updateItem );
WriteOpResult result;
{
///////////////////////////////////////////
Lock::DBWrite writeLock( nss.ns() );
///////////////////////////////////////////
// Check version once we're locked
if ( checkShardVersion( &shardingState, request, &result.error ) ) {
// Context once we're locked, to set more details in currentOp()
// TODO: better constructor?
Client::Context writeContext( nss.ns(),
storageGlobalParams.dbpath,
false /* don't check version */);
multiUpdate( updateItem, &result );
incWriteStats( updateItem, result.stats, result.error, currentOp.get() );
if ( !result.stats.upsertedID.isEmpty() ) {
*upsertedId = result.stats.upsertedID.getOwned();
}
}
}
// END CURRENT OP
finishCurrentOp( _client, currentOp.get(), result.error );
if ( result.error ) {
result.error->setIndex( updateItem.getItemIndex() );
*error = result.releaseError();
}
}
示例6: execRemove
void WriteBatchExecutor::execRemove( const BatchItemRef& removeItem,
WriteErrorDetail** error ) {
// Removes are similar to updates, but page faults are handled externally
// BEGIN CURRENT OP
scoped_ptr<CurOp> currentOp( beginCurrentOp( _client, removeItem ) );
incOpStats( removeItem );
WriteOpResult result;
// NOTE: Deletes will not fault outside the lock once any data has been written
PageFaultRetryableSection pageFaultSection;
while ( true ) {
try {
multiRemove( removeItem, &result );
break;
}
catch (PageFaultException& pfe) {
pfe.touch();
invariant(!result.getError());
continue;
}
fassertFailed(17429);
}
// END CURRENT OP
incWriteStats( removeItem, result.getStats(), result.getError(), currentOp.get() );
finishCurrentOp( _client, currentOp.get(), result.getError() );
if ( result.getError() ) {
result.getError()->setIndex( removeItem.getItemIndex() );
*error = result.releaseError();
}
}
示例7: singleCreateIndex
/**
* Perform a single index insert into a collection. Requires the index descriptor be
* preprocessed and the collection already has been created.
*
* Might fault or error, otherwise populates the result.
*/
static void singleCreateIndex( const BatchItemRef& insertItem,
const BSONObj& normalIndexDesc,
Collection* collection,
WriteOpResult* result ) {
const string& indexNS = insertItem.getRequest()->getNS();
Lock::assertWriteLocked( indexNS );
try {
Status status = collection->getIndexCatalog()->createIndex( normalIndexDesc, true );
if ( status.code() == ErrorCodes::IndexAlreadyExists ) {
result->stats.n = 0;
}
else if ( !status.isOK() ) {
result->error = toWriteError( status );
}
else {
logOp( "i", indexNS.c_str(), normalIndexDesc );
result->stats.n = 1;
}
}
catch ( const PageFaultException& ex ) {
// TODO: An actual data structure that's not an exception for this
result->fault = new PageFaultException( ex );
}
catch ( const DBException& ex ) {
result->error = toWriteError( ex.toStatus() );
}
}
示例8: execUpdate
void WriteBatchExecutor::execUpdate( const BatchItemRef& updateItem,
BSONObj* upsertedId,
WriteErrorDetail** error ) {
// BEGIN CURRENT OP
scoped_ptr<CurOp> currentOp( beginCurrentOp( _client, updateItem ) );
incOpStats( updateItem );
WriteOpResult result;
WriteUnitOfWork wunit(_txn->recoveryUnit());
multiUpdate( _txn, updateItem, &result );
wunit.commit();
if ( !result.getStats().upsertedID.isEmpty() ) {
*upsertedId = result.getStats().upsertedID;
}
// END CURRENT OP
incWriteStats( updateItem, result.getStats(), result.getError(), currentOp.get() );
finishCurrentOp( _txn, _client, currentOp.get(), result.getError() );
if ( result.getError() ) {
result.getError()->setIndex( updateItem.getItemIndex() );
*error = result.releaseError();
}
}
示例9: singleInsert
/**
* Perform a single insert into a collection. Requires the insert be preprocessed and the
* collection already has been created.
*
* Might fault or error, otherwise populates the result.
*/
static void singleInsert( const BatchItemRef& insertItem,
const BSONObj& normalInsert,
Collection* collection,
WriteOpResult* result ) {
const string& insertNS = insertItem.getRequest()->getNS();
Lock::assertWriteLocked( insertNS );
try {
// XXX - are we 100% sure that all !OK statuses do not write a document?
StatusWith<DiskLoc> status = collection->insertDocument( normalInsert, true );
if ( !status.isOK() ) {
result->error = toWriteError( status.getStatus() );
}
else {
logOp( "i", insertNS.c_str(), normalInsert );
getDur().commitIfNeeded();
result->stats.n = 1;
}
}
catch ( const PageFaultException& ex ) {
// TODO: An actual data structure that's not an exception for this
result->fault = new PageFaultException( ex );
}
catch ( const DBException& ex ) {
result->error = toWriteError( ex.toStatus() );
}
}
示例10: normalizeInsert
// Does preprocessing of inserts, special casing for indexes
// TODO: Simplify this when indexes aren't here anymore
static StatusWith<BSONObj> normalizeInsert( const BatchItemRef& insertItem ) {
if ( insertItem.getRequest()->isInsertIndexRequest() ) {
StatusWith<BSONObj> normalInsert = fixDocumentForInsert( insertItem.getDocument() );
if ( normalInsert.isOK() && insertItem.getDocument()["ns"].type() != String ) {
return StatusWith<BSONObj>( ErrorCodes::BadValue, "tried to create an index "
"without specifying namespace" );
}
else {
return normalInsert;
}
}
else {
return fixDocumentForInsert( insertItem.getDocument() );
}
}
示例11: multiRemove
/**
* Perform a remove operation, which might remove multiple documents. Dispatches to remove code
* currently to do most of this.
*
* Might fault or error, otherwise populates the result.
*/
static void multiRemove( OperationContext* txn,
const BatchItemRef& removeItem,
WriteOpResult* result ) {
const NamespaceString nss( removeItem.getRequest()->getNS() );
DeleteRequest request( nss );
request.setQuery( removeItem.getDelete()->getQuery() );
request.setMulti( removeItem.getDelete()->getLimit() != 1 );
request.setUpdateOpLog(true);
request.setGod( false );
DeleteExecutor executor( &request );
Status status = executor.prepare();
if ( !status.isOK() ) {
result->setError(toWriteError(status));
return;
}
///////////////////////////////////////////
Lock::DBWrite writeLock(txn->lockState(), nss.ns());
///////////////////////////////////////////
// Check version once we're locked
if (!checkShardVersion(txn, &shardingState, *removeItem.getRequest(), result)) {
// Version error
return;
}
// Context once we're locked, to set more details in currentOp()
// TODO: better constructor?
Client::Context writeContext( nss.ns(),
storageGlobalParams.dbpath,
false /* don't check version */);
try {
result->getStats().n = executor.execute(txn, writeContext.db());
}
catch ( const DBException& ex ) {
status = ex.toStatus();
if (ErrorCodes::isInterruption(status.code())) {
throw;
}
result->setError(toWriteError(status));
}
}
示例12: incWriteStats
void WriteBatchExecutor::incWriteStats( const BatchItemRef& currWrite,
const WriteOpStats& stats,
const WriteErrorDetail* error,
CurOp* currentOp ) {
if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Insert ) {
// We increment batch inserts like individual inserts
_opCounters->gotInsert();
_stats->numInserted += stats.n;
_le->nObjects = stats.n;
currentOp->debug().ninserted += stats.n;
}
else if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Update ) {
if ( stats.upsertedID.isEmpty() ) {
_stats->numMatched += stats.n;
_stats->numModified += stats.nModified;
}
else {
++_stats->numUpserted;
}
if ( !error ) {
_le->recordUpdate( stats.upsertedID.isEmpty() && stats.n > 0,
stats.n,
stats.upsertedID );
}
}
else {
dassert( currWrite.getOpType() == BatchedCommandRequest::BatchType_Delete );
_stats->numDeleted += stats.n;
if ( !error ) {
_le->recordDelete( stats.n );
}
currentOp->debug().ndeleted += stats.n;
}
// Errors reported in LastError are handled internally in write ops for now
// TODO: Move error reporting out of write op internals?
}
示例13: incWriteStats
void WriteBatchExecutor::incWriteStats( const BatchItemRef& currWrite,
const WriteOpStats& stats,
const WriteErrorDetail* error,
CurOp* currentOp ) {
if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Insert ) {
_stats->numInserted += stats.n;
_le->nObjects = stats.n;
currentOp->debug().ninserted += stats.n;
}
else if ( currWrite.getOpType() == BatchedCommandRequest::BatchType_Update ) {
if ( stats.upsertedID.isEmpty() ) {
_stats->numMatched += stats.n;
_stats->numModified += stats.nModified;
}
else {
++_stats->numUpserted;
}
if ( !error ) {
_le->recordUpdate( stats.upsertedID.isEmpty() && stats.n > 0,
stats.n,
stats.upsertedID );
}
}
else {
dassert( currWrite.getOpType() == BatchedCommandRequest::BatchType_Delete );
_stats->numDeleted += stats.n;
if ( !error ) {
_le->recordDelete( stats.n );
}
currentOp->debug().ndeleted += stats.n;
}
if (error && !_le->disabled) {
_le->raiseError(error->getErrCode(), error->getErrMessage().c_str());
}
}
示例14: multiRemove
/**
* Perform a remove operation, which might remove multiple documents. Dispatches to remove code
* currently to do most of this.
*
* Might fault or error, otherwise populates the result.
*/
static void multiRemove( const BatchItemRef& removeItem,
WriteOpResult* result ) {
Lock::assertWriteLocked( removeItem.getRequest()->getNS() );
try {
long long n = deleteObjects( removeItem.getRequest()->getNS(),
removeItem.getDelete()->getQuery(),
removeItem.getDelete()->getLimit() == 1, // justOne
true, // logOp
false // god
);
result->stats.n = n;
}
catch ( const PageFaultException& ex ) {
// TODO: An actual data structure that's not an exception for this
result->fault = new PageFaultException( ex );
}
catch ( const DBException& ex ) {
result->error = toWriteError( ex.toStatus() );
}
}
示例15: multiUpdate
static void multiUpdate( OperationContext* txn,
const BatchItemRef& updateItem,
WriteOpResult* result ) {
const NamespaceString nsString(updateItem.getRequest()->getNS());
UpdateRequest request(nsString);
request.setQuery(updateItem.getUpdate()->getQuery());
request.setUpdates(updateItem.getUpdate()->getUpdateExpr());
request.setMulti(updateItem.getUpdate()->getMulti());
request.setUpsert(updateItem.getUpdate()->getUpsert());
request.setUpdateOpLog(true);
UpdateLifecycleImpl updateLifecycle(true, request.getNamespaceString());
request.setLifecycle(&updateLifecycle);
UpdateExecutor executor(&request, &txn->getCurOp()->debug());
Status status = executor.prepare();
if (!status.isOK()) {
result->setError(toWriteError(status));
return;
}
///////////////////////////////////////////
Lock::DBWrite writeLock(txn->lockState(), nsString.ns());
///////////////////////////////////////////
if (!checkShardVersion(txn, &shardingState, *updateItem.getRequest(), result))
return;
Client::Context ctx( nsString.ns(),
storageGlobalParams.dbpath,
false /* don't check version */ );
try {
UpdateResult res = executor.execute(txn, ctx.db());
const long long numDocsModified = res.numDocsModified;
const long long numMatched = res.numMatched;
const BSONObj resUpsertedID = res.upserted;
// We have an _id from an insert
const bool didInsert = !resUpsertedID.isEmpty();
result->getStats().nModified = didInsert ? 0 : numDocsModified;
result->getStats().n = didInsert ? 1 : numMatched;
result->getStats().upsertedID = resUpsertedID;
}
catch (const DBException& ex) {
status = ex.toStatus();
if (ErrorCodes::isInterruption(status.code())) {
throw;
}
result->setError(toWriteError(status));
}
}