void Collection::deleteDocument( OperationContext* txn, const DiskLoc& loc, bool cappedOK, bool noWarn, BSONObj* deletedId ) { if ( isCapped() && !cappedOK ) { log() << "failing remove on a capped ns " << _ns << endl; uasserted( 10089, "cannot remove from a capped collection" ); return; } BSONObj doc = docFor( loc ); if ( deletedId ) { BSONElement e = doc["_id"]; if ( e.type() ) { *deletedId = e.wrap(); } } /* check if any cursors point to us. if so, advance them. */ _cursorCache.invalidateDocument(loc, INVALIDATION_DELETION); _indexCatalog.unindexRecord(txn, doc, loc, noWarn); _recordStore->deleteRecord( txn, loc ); _infoCache.notifyOfWriteOp(); }
void Collection::deleteDocument( const DiskLoc& loc, bool cappedOK, bool noWarn, BSONObj* deletedId ) { if ( _details->isCapped() && !cappedOK ) { log() << "failing remove on a capped ns " << _ns << endl; uasserted( 17115, "cannot remove from a capped collection" ); // XXX 10089 return; } BSONObj doc = docFor( loc ); if ( deletedId ) { BSONElement e = doc["_id"]; if ( e.type() ) { *deletedId = e.wrap(); } } /* check if any cursors point to us. if so, advance them. */ ClientCursor::aboutToDelete(_ns.ns(), _details, loc); Record* rec = getExtentManager()->recordFor( loc ); _indexCatalog.unindexRecord( doc, loc, noWarn); _recordStore.deallocRecord( loc, rec ); _infoCache.notifyOfWriteOp(); }
Status Collection::aboutToDeleteCapped( OperationContext* txn, const DiskLoc& loc ) { BSONObj doc = docFor( loc ); /* check if any cursors point to us. if so, advance them. */ _cursorCache.invalidateDocument(loc, INVALIDATION_DELETION); _indexCatalog.unindexRecord(txn, doc, loc, false); return Status::OK(); }
int64_t Collection::countTableScan( const MatchExpression* expression ) { scoped_ptr<RecordIterator> iterator( getIterator( DiskLoc(), false, CollectionScanParams::FORWARD ) ); int64_t count = 0; while ( !iterator->isEOF() ) { DiskLoc loc = iterator->getNext(); BSONObj obj = docFor( loc ); if ( expression->matchesBSON( obj ) ) count++; } return count; }
void Collection::_compactExtent(const DiskLoc diskloc, int extentNumber, MultiIndexBlock& indexesToInsertTo, const CompactOptions* compactOptions, CompactStats* stats ) { log() << "compact begin extent #" << extentNumber << " for namespace " << _ns << " " << diskloc; unsigned oldObjSize = 0; // we'll report what the old padding was unsigned oldObjSizeWithPadding = 0; Extent *e = getExtentManager()->getExtent( diskloc ); e->assertOk(); verify( e->validates(diskloc) ); { // the next/prev pointers within the extent might not be in order so we first // page the whole thing in sequentially log() << "compact paging in len=" << e->length/1000000.0 << "MB" << endl; Timer t; size_t length = e->length; touch_pages( reinterpret_cast<const char*>(e), length ); int ms = t.millis(); if( ms > 1000 ) log() << "compact end paging in " << ms << "ms " << e->length/1000000.0/t.seconds() << "MB/sec" << endl; } { log() << "compact copying records" << endl; long long datasize = 0; long long nrecords = 0; DiskLoc L = e->firstRecord; if( !L.isNull() ) { while( 1 ) { Record *recOld = _recordStore->recordFor(L); BSONObj objOld = docFor( L ); L = getExtentManager()->getNextRecordInExtent(L); if ( compactOptions->validateDocuments && !objOld.valid() ) { // object is corrupt! log() << "compact skipping corrupt document!"; stats->corruptDocuments++; } else { unsigned docSize = objOld.objsize(); nrecords++; oldObjSize += docSize; oldObjSizeWithPadding += recOld->netLength(); unsigned lenWHdr = docSize + Record::HeaderSize; unsigned lenWPadding = lenWHdr; switch( compactOptions->paddingMode ) { case CompactOptions::NONE: if ( details()->isUserFlagSet(NamespaceDetails::Flag_UsePowerOf2Sizes) ) lenWPadding = details()->quantizePowerOf2AllocationSpace(lenWPadding); break; case CompactOptions::PRESERVE: // if we are preserving the padding, the record should not change size lenWPadding = recOld->lengthWithHeaders(); break; case CompactOptions::MANUAL: lenWPadding = compactOptions->computeRecordSize(lenWPadding); if (lenWPadding < lenWHdr || lenWPadding > BSONObjMaxUserSize / 2 ) { lenWPadding = lenWHdr; } break; } CompactDocWriter writer( objOld, lenWPadding ); StatusWith<DiskLoc> status = _recordStore->insertRecord( &writer, 0 ); uassertStatusOK( status.getStatus() ); datasize += _recordStore->recordFor( status.getValue() )->netLength(); InsertDeleteOptions options; options.logIfError = false; options.dupsAllowed = true; // in compact we should be doing no checking indexesToInsertTo.insert( objOld, status.getValue(), options ); } if( L.isNull() ) { // we just did the very last record from the old extent. it's still pointed to // by the old extent ext, but that will be fixed below after this loop break; } // remove the old records (orphan them) periodically so our commit block doesn't get too large bool stopping = false; RARELY stopping = *killCurrentOp.checkForInterruptNoAssert() != 0; if( stopping || getDur().isCommitNeeded() ) { e->firstRecord.writing() = L; Record *r = _recordStore->recordFor(L); getDur().writingInt(r->prevOfs()) = DiskLoc::NullOfs; getDur().commitIfNeeded(); killCurrentOp.checkForInterrupt(); } } } // if !L.isNull() verify( details()->firstExtent() == diskloc ); verify( details()->lastExtent() != diskloc ); DiskLoc newFirst = e->xnext; details()->firstExtent().writing() = newFirst; getExtentManager()->getExtent( newFirst )->xprev.writing().Null(); getDur().writing(e)->markEmpty(); getExtentManager()->freeExtents( diskloc, diskloc ); getDur().commitIfNeeded(); { double op = 1.0; if( oldObjSize ) op = static_cast<double>(oldObjSizeWithPadding)/oldObjSize; log() << "compact finished extent #" << extentNumber << " containing " << nrecords << " documents (" << datasize/1000000.0 << "MB)" << " oldPadding: " << op << ' ' << static_cast<unsigned>(op*100.0)/100; } } }