Skip to content

Commit

Permalink
SERVER-13635: hide NamespaceIndex behind Database
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed May 20, 2014
1 parent 2a0fd61 commit 3f1b15b
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 148 deletions.
119 changes: 109 additions & 10 deletions src/mongo/db/catalog/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,113 @@ namespace mongo {
return true;
}

void Database::getCollectionNamespaces( std::list<std::string>* out ) const {
_dbEntry->namespaceIndex().getCollectionNamespaces( out );
}

long long Database::getIndexSizeForCollection(Collection* coll,
BSONObjBuilder* details,
int scale ) {
if ( !coll )
return 0;

IndexCatalog::IndexIterator ii =
coll->getIndexCatalog()->getIndexIterator( true /*includeUnfinishedIndexes*/ );

long long totalSize = 0;

while ( ii.more() ) {
IndexDescriptor* d = ii.next();
string indNS = d->indexNamespace();
Collection* indColl = getCollection( indNS ); // XXX
if ( ! indColl ) {
log() << "error: have index descriptor [" << indNS
<< "] but no entry in the index collection." << endl;
continue;
}
totalSize += indColl->dataSize();
if ( details ) {
long long const indexSize = indColl->dataSize() / scale;
details->appendNumber( d->indexName() , indexSize );
}
}
return totalSize;
}

void Database::getStats( BSONObjBuilder* output, double scale ) {
bool empty = isEmpty() || getExtentManager()->numFiles() == 0;

list<string> collections;
if ( !empty )
getCollectionNamespaces( &collections );

long long ncollections = 0;
long long objects = 0;
long long size = 0;
long long storageSize = 0;
long long numExtents = 0;
long long indexes = 0;
long long indexSize = 0;

for (list<string>::const_iterator it = collections.begin(); it != collections.end(); ++it) {
const string ns = *it;

Collection* collection = getCollection( ns );
if ( !collection )
continue;

ncollections += 1;
objects += collection->numRecords();
size += collection->dataSize();

BSONObjBuilder temp;
storageSize += collection->getRecordStore()->storageSize( &temp );
numExtents += temp.obj()["numExtents"].numberInt(); // XXX

indexes += collection->getIndexCatalog()->numIndexesTotal();
indexSize += getIndexSizeForCollection(collection);
}

output->append ( "db" , _name );
output->appendNumber( "collections" , ncollections );
output->appendNumber( "objects" , objects );
output->append ( "avgObjSize" , objects == 0 ? 0 : double(size) / double(objects) );
output->appendNumber( "dataSize" , size / scale );
output->appendNumber( "storageSize" , storageSize / scale);
output->appendNumber( "numExtents" , numExtents );
output->appendNumber( "indexes" , indexes );
output->appendNumber( "indexSize" , indexSize / scale );
if ( !empty ) {
output->appendNumber( "fileSize" , fileSize() / scale );
output->appendNumber( "nsSizeMB", (int)_dbEntry->namespaceIndex().fileLength() / 1024 / 1024 );
}
else {
output->appendNumber( "fileSize" , 0 );
}

BSONObjBuilder dataFileVersion( output->subobjStart( "dataFileVersion" ) );
if ( !empty ) {
int major, minor;
getFileFormat( &major, &minor );
dataFileVersion.append( "major", major );
dataFileVersion.append( "minor", minor );
}
dataFileVersion.done();

if ( !empty ){
int freeListSize = 0;
int64_t freeListSpace = 0;
getExtentManager()->freeListStats( &freeListSize, &freeListSpace );

BSONObjBuilder extentFreeList( output->subobjStart( "extentFreeList" ) );
extentFreeList.append( "num", freeListSize );
extentFreeList.appendNumber( "totalSize",
static_cast<long long>( freeListSpace / scale ) );
extentFreeList.done();
}

}

Status Database::dropCollection( OperationContext* txn, const StringData& fullns ) {
LOG(1) << "dropCollection: " << fullns << endl;
massertNamespaceNotIndex( fullns, "dropCollection" );
Expand Down Expand Up @@ -624,7 +731,7 @@ namespace mongo {
const CollectionOptions& options,
bool allocateDefaultSpace,
bool createIdIndex ) {
massert( 17399, "collection already exists", _dbEntry->namespaceIndex().details( ns ) == NULL );
massert( 17399, "collection already exists", getCollection( ns ) == NULL );
massertNamespaceNotIndex( ns, "createCollection" );

if ( serverGlobalParams.configsvr &&
Expand Down Expand Up @@ -742,15 +849,7 @@ namespace mongo {
return _dbEntry->getExtentManager();
}

const NamespaceIndex* Database::namespaceIndex() const {
return &_dbEntry->namespaceIndex();
}

NamespaceIndex* Database::namespaceIndex() {
return &_dbEntry->namespaceIndex();
}

bool Database::isEmpty() {
bool Database::isEmpty() const {
return !_dbEntry->namespaceIndex().allocated();
}

Expand Down
11 changes: 8 additions & 3 deletions src/mongo/db/catalog/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace mongo {
*/
bool isOk() const { return _magic == 781231; }

bool isEmpty();
bool isEmpty() const;

/**
* total file size of Database in bytes
Expand Down Expand Up @@ -153,8 +153,13 @@ namespace mongo {
int getProfilingLevel() const { return _profile; }
const char* getProfilingNS() const { return _profileName.c_str(); }

const NamespaceIndex* namespaceIndex() const;
NamespaceIndex* namespaceIndex();
void getCollectionNamespaces( std::list<std::string>* out ) const;

void getStats( BSONObjBuilder* output, double scale = 1 );

long long getIndexSizeForCollection( Collection* collections,
BSONObjBuilder* details = NULL,
int scale = 1 );

// TODO: do not think this method should exist, so should try and encapsulate better
MmapV1ExtentManager* getExtentManager();
Expand Down
1 change: 0 additions & 1 deletion src/mongo/db/catalog/index_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "mongo/db/index/index_access_method.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/structure/catalog/namespace_details.h"
#include "mongo/db/structure/head_manager.h"

namespace mongo {
Expand Down
2 changes: 0 additions & 2 deletions src/mongo/db/catalog/index_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/rs.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/structure/catalog/index_details.h"
#include "mongo/db/structure/catalog/namespace_details.h"
#include "mongo/util/processinfo.h"
#include "mongo/util/progress_meter.h"

Expand Down
3 changes: 1 addition & 2 deletions src/mongo/db/commands/dbhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "mongo/db/commands.h"
#include "mongo/db/catalog/database.h"
#include "mongo/db/query/internal_plans.h"
#include "mongo/db/structure/catalog/namespace_index.h"
#include "mongo/util/md5.hpp"
#include "mongo/util/timer.h"

Expand Down Expand Up @@ -148,7 +147,7 @@ namespace mongo {
Client::ReadContext ctx(ns);
Database* db = ctx.ctx().db();
if ( db )
db->namespaceIndex()->getNamespaces( colls );
db->getCollectionNamespaces( &colls );
colls.sort();

result.appendNumber( "numCollections" , (long long)colls.size() );
Expand Down
3 changes: 1 addition & 2 deletions src/mongo/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
#include "mongo/db/storage/mmap_v1/dur.h"
#include "mongo/db/storage/mmap_v1/mmap_v1_extent_manager.h"
#include "mongo/db/storage_options.h"
#include "mongo/db/structure/catalog/namespace_index.h"
#include "mongo/db/ttl.h"
#include "mongo/platform/process_id.h"
#include "mongo/s/d_writeback.h"
Expand Down Expand Up @@ -328,7 +327,7 @@ namespace mongo {
}

list<string> collections;
db->namespaceIndex()->getNamespaces( collections );
db->getCollectionNamespaces( &collections );

// for each collection, ensure there is a $_id_ index
for (list<string>::iterator i = collections.begin(); i != collections.end(); ++i) {
Expand Down
121 changes: 8 additions & 113 deletions src/mongo/db/dbcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include "mongo/db/storage/mmap_v1/mmap_v1_extent_manager.h"
#include "mongo/db/storage/record.h"
#include "mongo/db/catalog/collection_catalog_entry.h"
#include "mongo/db/structure/catalog/namespace_details.h"
#include "mongo/db/write_concern.h"
#include "mongo/s/d_logic.h"
#include "mongo/s/d_writeback.h"
Expand Down Expand Up @@ -171,7 +170,7 @@ namespace mongo {
const BSONObj& cmdObj) {
invariant(db);
std::list<std::string> collections;
db->namespaceIndex()->getNamespaces(collections, true /* onlyCollections */);
db->getCollectionNamespaces(&collections);

std::vector<BSONObj> allKilledIndexes;
for (std::list<std::string>::iterator it = collections.begin();
Expand Down Expand Up @@ -257,7 +256,7 @@ namespace mongo {
const BSONObj& cmdObj) {
invariant(db);
std::list<std::string> collections;
db->namespaceIndex()->getNamespaces(collections, true /* onlyCollections */);
db->getCollectionNamespaces(&collections);

std::vector<BSONObj> allKilledIndexes;
for (std::list<std::string>::iterator it = collections.begin();
Expand Down Expand Up @@ -1017,39 +1016,6 @@ namespace mongo {

} cmdDatasize;

namespace {
long long getIndexSizeForCollection(string db, string ns, BSONObjBuilder* details=NULL, int scale = 1 ) {
Lock::assertAtLeastReadLocked(ns);
Client::Context ctx( ns );

Collection* coll = ctx.db()->getCollection( ns );
if ( !coll )
return 0;

IndexCatalog::IndexIterator ii =
coll->getIndexCatalog()->getIndexIterator( true /*includeUnfinishedIndexes*/ );

long long totalSize = 0;

while ( ii.more() ) {
IndexDescriptor* d = ii.next();
string indNS = d->indexNamespace();
Collection* indColl = ctx.db()->getCollection( indNS );
if ( ! indColl ) {
log() << "error: have index descriptor [" << indNS
<< "] but no entry in the index collection." << endl;
continue;
}
totalSize += indColl->dataSize();
if ( details ) {
long long const indexSize = indColl->dataSize() / scale;
details->appendNumber( d->indexName() , indexSize );
}
}
return totalSize;
}
}

class CollectionStats : public Command {
public:
CollectionStats() : Command( "collStats", false, "collstats" ) {
Expand All @@ -1074,8 +1040,8 @@ namespace mongo {
bool run(OperationContext* txn, const string& dbname, BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl ) {
const string ns = dbname + "." + jsobj.firstElement().valuestr();
Client::ReadContext cx( ns );

Collection* collection = cx.ctx().db()->getCollection( ns );
Database* db = cx.ctx().db();
Collection* collection = db->getCollection( ns );
if ( !collection ) {
errmsg = "Collection [" + ns + "] not found.";
return false;
Expand Down Expand Up @@ -1114,7 +1080,9 @@ namespace mongo {
collection->getRecordStore()->appendCustomStats( &result, scale );

BSONObjBuilder indexSizes;
result.appendNumber( "totalIndexSize" , getIndexSizeForCollection(dbname, ns, &indexSizes, scale) / scale );
result.appendNumber( "totalIndexSize" , db->getIndexSizeForCollection(collection,
&indexSizes,
scale) / scale );
result.append("indexSizes", indexSizes.obj());

return true;
Expand Down Expand Up @@ -1277,84 +1245,11 @@ namespace mongo {
}

const string ns = parseNs(dbname, jsobj);
list<string> collections;

Client::ReadContext ctx(ns);
Database* d = ctx.ctx().db();

if ( d && ( d->isEmpty() || d->getExtentManager()->numFiles() == 0 ) )
d = NULL;

if ( d )
d->namespaceIndex()->getNamespaces( collections );

long long ncollections = 0;
long long objects = 0;
long long size = 0;
long long storageSize = 0;
long long numExtents = 0;
long long indexes = 0;
long long indexSize = 0;

for (list<string>::const_iterator it = collections.begin(); it != collections.end(); ++it) {
const string ns = *it;

Collection* collection = d->getCollection( ns );
if ( !collection ) {
errmsg = "missing ns: ";
errmsg += ns;
return false;
}

ncollections += 1;
objects += collection->numRecords();
size += collection->dataSize();

BSONObjBuilder temp;
storageSize += collection->getRecordStore()->storageSize( &temp );
numExtents += temp.obj()["numExtents"].numberInt(); // XXX

indexes += collection->getIndexCatalog()->numIndexesTotal();
indexSize += getIndexSizeForCollection(dbname, ns);
}

result.append ( "db" , dbname );
result.appendNumber( "collections" , ncollections );
result.appendNumber( "objects" , objects );
result.append ( "avgObjSize" , objects == 0 ? 0 : double(size) / double(objects) );
result.appendNumber( "dataSize" , size / scale );
result.appendNumber( "storageSize" , storageSize / scale);
result.appendNumber( "numExtents" , numExtents );
result.appendNumber( "indexes" , indexes );
result.appendNumber( "indexSize" , indexSize / scale );
if ( d ) {
result.appendNumber( "fileSize" , d->fileSize() / scale );
result.appendNumber( "nsSizeMB", (int) d->namespaceIndex()->fileLength() / 1024 / 1024 );
}
else {
result.appendNumber( "fileSize" , 0 );
}

BSONObjBuilder dataFileVersion( result.subobjStart( "dataFileVersion" ) );
if ( d ) {
int major, minor;
d->getFileFormat( &major, &minor );
dataFileVersion.append( "major", major );
dataFileVersion.append( "minor", minor );
}
dataFileVersion.done();

if ( d ){
int freeListSize = 0;
int64_t freeListSpace = 0;
d->getExtentManager()->freeListStats( &freeListSize, &freeListSpace );

BSONObjBuilder extentFreeList( result.subobjStart( "extentFreeList" ) );
extentFreeList.append( "num", freeListSize );
extentFreeList.appendNumber( "totalSize",
static_cast<long long>( freeListSpace / scale ) );
extentFreeList.done();
}
d->getStats( &result, scale );

return true;
}
Expand Down
Loading

0 comments on commit 3f1b15b

Please sign in to comment.