Skip to content

Commit

Permalink
SERVER-6405: start moving delete code into collection structure
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Sep 7, 2013
1 parent c6d6dd5 commit 2dce8da
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/mongo/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ serverOnlyFiles = [ "db/curop.cpp",
"db/database.cpp",
"db/structure/collection.cpp",
"db/structure/collection_iterator.cpp",
"db/structure/record_store.cpp",
"db/database_holder.cpp",
"db/background.cpp",
"db/pdfile.cpp",
Expand Down
1 change: 1 addition & 0 deletions src/mongo/db/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "mongo/db/pdfile.h"
#include "mongo/db/query/internal_plans.h"
#include "mongo/db/ops/delete.h"
#include "mongo/db/structure/collection.h"

namespace mongo {

Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#include "mongo/db/namespace_details.h"
#include "mongo/db/storage/record.h"
#include "mongo/db/storage/extent_manager.h"
#include "mongo/db/structure/collection.h"

namespace mongo {

class CollectionTemp;
class Extent;
class DataFile;

Expand Down
8 changes: 4 additions & 4 deletions src/mongo/db/dbhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
#include "mongo/db/ops/update_request.h"
#include "mongo/db/ops/update_result.h"
#include "mongo/db/pagefault.h"
#include "mongo/db/pdfile.h"
#include "mongo/db/query_optimizer.h"
#include "mongo/db/query_runner.h"
#include "mongo/db/query/internal_plans.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/write_concern.h"
#include "mongo/db/structure/collection.h"
#include "mongo/s/d_logic.h"

namespace mongo {
Expand Down Expand Up @@ -389,12 +389,12 @@ namespace mongo {
break;
}
}

if ( callback )
callback->goingToDelete( obj );

logOp("d", ns.c_str(), obj["_id"].wrap(), 0, 0, fromMigrate);
theDataFileMgr.deleteRecord(ns.c_str() , rloc.rec(), rloc);
c.database()->getCollectionTemp( ns )->deleteDocument( rloc );
numDeleted++;
}

Expand Down
5 changes: 4 additions & 1 deletion src/mongo/db/ops/delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "mongo/db/query_optimizer.h"
#include "mongo/db/queryutil.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/structure/collection.h"
#include "mongo/util/stacktrace.h"

namespace mongo {
Expand Down Expand Up @@ -148,7 +149,9 @@ namespace mongo {
}
}

theDataFileMgr.deleteRecord(ns, rloc.rec(), rloc);
//theDataFileMgr.deleteRecord(ns, rloc.rec(), rloc);
currentClient.get()->database()->getCollectionTemp( ns )->deleteDocument( rloc );

nDeleted++;
if ( foundAllResults ) {
break;
Expand Down
43 changes: 41 additions & 2 deletions src/mongo/db/structure/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@

#include "mongo/db/structure/collection.h"

#include "mongo/db/clientcursor.h"
#include "mongo/db/database.h"
#include "mongo/db/namespace_details.h"
#include "mongo/db/storage/extent.h"
#include "mongo/db/storage/extent_manager.h"
#include "mongo/db/structure/collection_iterator.h"

#include "mongo/db/pdfile.h" // XXX-ERH
#include "mongo/db/index_update.h" // XXX-ERH

namespace mongo {

CollectionTemp::CollectionTemp( const StringData& fullNS,
NamespaceDetails* details,
Database* database ) {
_ns = fullNS.toString();
Database* database )
: _ns( fullNS ) {
_details = details;
_database = database;
_recordStore.init( this );
_magic = 1357924;
}

Expand All @@ -60,6 +65,40 @@ namespace mongo {
return new FlatIterator( this, start, dir );
}

BSONObj CollectionTemp::docFor( const DiskLoc& loc ) {
Record* rec = getExtentManager()->recordFor( loc );
return BSONObj::make( rec->accessed() );
}

void CollectionTemp::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;
}

if ( deletedId ) {
BSONObj doc = docFor( loc );
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 );

unindexRecord(_details, rec, loc, noWarn);

_recordStore.deallocRecord( loc, rec );

NamespaceDetailsTransient::get( _ns.ns().c_str() ).notifyOfWriteOp();

}


ExtentManager* CollectionTemp::getExtentManager() {
verify( ok() );
Expand Down
15 changes: 13 additions & 2 deletions src/mongo/db/structure/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "mongo/base/string_data.h"
#include "mongo/db/diskloc.h"
#include "mongo/db/exec/collection_scan_common.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/structure/record_store.h"

namespace mongo {

Expand All @@ -60,25 +62,34 @@ namespace mongo {

bool ok() const { return _magic == 1357924; }

StringData ns() const { return _ns; }
const NamespaceString& ns() const { return _ns; }

BSONObj docFor( const DiskLoc& loc );

CollectionIterator* getIterator( const DiskLoc& start, bool tailable,
const CollectionScanParams::Direction& dir) const;

void deleteDocument( const DiskLoc& loc,
bool cappedOK = false,
bool noWarn = false,
BSONObj* deletedId = 0 );

private:

ExtentManager* getExtentManager();
const ExtentManager* getExtentManager() const;

int _magic;

std::string _ns;
NamespaceString _ns;
NamespaceDetails* _details;
Database* _database;
RecordStore _recordStore;

friend class Database;
friend class FlatIterator;
friend class CappedIterator;
friend class RecordStore;
};

}
110 changes: 110 additions & 0 deletions src/mongo/db/structure/record_store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// record_store.cpp

/**
* Copyright (C) 2013 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#include "mongo/db/structure/record_store.h"

#include "mongo/db/storage/extent.h"
#include "mongo/db/structure/collection.h"

#include "mongo/db/pdfile.h" // XXX-ERH

namespace mongo {

RecordStore::RecordStore() {
_collection = NULL;
_details = NULL;
}

void RecordStore::init( CollectionTemp* collection) {
_collection = collection;
_details = _collection->_details;
_isSystemIndexes = _collection->ns().coll() == "system.indexes";
}

void RecordStore::deallocRecord( const DiskLoc& dl, Record* todelete ) {
ExtentManager* em = _collection->getExtentManager();

/* remove ourself from the record next/prev chain */
{
if ( todelete->prevOfs() != DiskLoc::NullOfs ) {
DiskLoc prev = em->getPrevRecordInExtent( dl );
Record* prevRecord = em->recordFor( prev );
getDur().writingInt( prevRecord->nextOfs() ) = todelete->nextOfs();
}

if ( todelete->nextOfs() != DiskLoc::NullOfs ) {
DiskLoc next = em->getNextRecord( dl );
Record* nextRecord = em->recordFor( next );
getDur().writingInt( nextRecord->prevOfs() ) = todelete->prevOfs();
}
}

/* remove ourself from extent pointers */
{
Extent *e = getDur().writing( todelete->myExtent(dl) );
if ( e->firstRecord == dl ) {
if ( todelete->nextOfs() == DiskLoc::NullOfs )
e->firstRecord.Null();
else
e->firstRecord.set(dl.a(), todelete->nextOfs() );
}
if ( e->lastRecord == dl ) {
if ( todelete->prevOfs() == DiskLoc::NullOfs )
e->lastRecord.Null();
else
e->lastRecord.set(dl.a(), todelete->prevOfs() );
}
}

/* add to the free list */
{
_details->incrementStats( -1 * todelete->netLength(), -1 );

if ( _isSystemIndexes ) {
/* temp: if in system.indexes, don't reuse, and zero out: we want to be
careful until validated more, as IndexDetails has pointers
to this disk location. so an incorrectly done remove would cause
a lot of problems.
*/
memset( getDur().writingPtr(todelete, todelete->lengthWithHeaders() ),
0, todelete->lengthWithHeaders() );
}
else {
DEV {
unsigned long long *p = reinterpret_cast<unsigned long long *>( todelete->data() );
*getDur().writing(p) = 0;
}
_details->addDeletedRec((DeletedRecord*)todelete, dl);
}
}

}

}
53 changes: 53 additions & 0 deletions src/mongo/db/structure/record_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// record_store.h

/**
* Copyright (C) 2013 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#pragma once

#include "mongo/db/namespace_details.h"

namespace mongo {

class CollectionTemp;

class RecordStore {
public:
RecordStore();

void init( CollectionTemp* collection );

void deallocRecord( const DiskLoc& dl, Record* todelete );

private:
CollectionTemp* _collection;
NamespaceDetails* _details;
bool _isSystemIndexes;
};

}

0 comments on commit 2dce8da

Please sign in to comment.