diff options
author | Eliot Horowitz <eliot@10gen.com> | 2014-05-20 10:26:12 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2014-05-20 14:05:57 -0400 |
commit | 3f1b15b8fa5e9a59030db37341163935b1df39ef (patch) | |
tree | e29b2b8e12ef51e30e393428c30fa60d411d9c05 /src | |
parent | 2a0fd61c1b0b390b7365e900a0b3e53118515423 (diff) | |
download | mongo-3f1b15b8fa5e9a59030db37341163935b1df39ef.tar.gz |
SERVER-13635: hide NamespaceIndex behind Database
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/catalog/database.cpp | 119 | ||||
-rw-r--r-- | src/mongo/db/catalog/database.h | 11 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_catalog_entry.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_create.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/commands/dbhash.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/db.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/dbcommands.cpp | 121 | ||||
-rw-r--r-- | src/mongo/db/index_rebuilder.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/query/stage_builder.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/repl/rs_initialsync.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/repl/rs_rollback.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/repl/sync.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/storage/mmap_v1/mmap_v1_engine.h | 2 | ||||
-rw-r--r-- | src/mongo/db/structure/catalog/namespace_index.cpp | 7 | ||||
-rw-r--r-- | src/mongo/db/structure/catalog/namespace_index.h | 2 | ||||
-rw-r--r-- | src/mongo/db/structure/collection_compact.cpp | 1 | ||||
-rw-r--r-- | src/mongo/tools/dump.cpp | 3 |
17 files changed, 134 insertions, 148 deletions
diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index d224e3a2a64..3af055c941b 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -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" ); @@ -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 && @@ -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(); } diff --git a/src/mongo/db/catalog/database.h b/src/mongo/db/catalog/database.h index 27b7aeacab0..02f6e3afe21 100644 --- a/src/mongo/db/catalog/database.h +++ b/src/mongo/db/catalog/database.h @@ -122,7 +122,7 @@ namespace mongo { */ bool isOk() const { return _magic == 781231; } - bool isEmpty(); + bool isEmpty() const; /** * total file size of Database in bytes @@ -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(); diff --git a/src/mongo/db/catalog/index_catalog_entry.cpp b/src/mongo/db/catalog/index_catalog_entry.cpp index 1b91c5710d0..9764eebdf21 100644 --- a/src/mongo/db/catalog/index_catalog_entry.cpp +++ b/src/mongo/db/catalog/index_catalog_entry.cpp @@ -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 { diff --git a/src/mongo/db/catalog/index_create.cpp b/src/mongo/db/catalog/index_create.cpp index a0c2fc30111..71c0cfe44a5 100644 --- a/src/mongo/db/catalog/index_create.cpp +++ b/src/mongo/db/catalog/index_create.cpp @@ -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" diff --git a/src/mongo/db/commands/dbhash.cpp b/src/mongo/db/commands/dbhash.cpp index df10255418e..0cfa4bcc598 100644 --- a/src/mongo/db/commands/dbhash.cpp +++ b/src/mongo/db/commands/dbhash.cpp @@ -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" @@ -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() ); diff --git a/src/mongo/db/db.cpp b/src/mongo/db/db.cpp index 44a5b3bf461..4cdc5fde00d 100644 --- a/src/mongo/db/db.cpp +++ b/src/mongo/db/db.cpp @@ -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" @@ -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) { diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp index 9661ccfc0ae..c94ec64140f 100644 --- a/src/mongo/db/dbcommands.cpp +++ b/src/mongo/db/dbcommands.cpp @@ -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" @@ -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(); @@ -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(); @@ -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" ) { @@ -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; @@ -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; @@ -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; } diff --git a/src/mongo/db/index_rebuilder.cpp b/src/mongo/db/index_rebuilder.cpp index 015a39b95cb..018a0cc512b 100644 --- a/src/mongo/db/index_rebuilder.cpp +++ b/src/mongo/db/index_rebuilder.cpp @@ -36,7 +36,6 @@ #include "mongo/db/instance.h" #include "mongo/db/pdfile.h" #include "mongo/db/repl/rs.h" -#include "mongo/db/structure/catalog/namespace_index.h" #include "mongo/db/operation_context_impl.h" #include "mongo/util/scopeguard.h" @@ -65,7 +64,7 @@ namespace mongo { dbName++) { Client::ReadContext ctx(*dbName); Database* db = ctx.ctx().db(); - db->namespaceIndex()->getNamespaces(collNames, /* onlyCollections */ true); + db->getCollectionNamespaces(&collNames); } checkNS(collNames); } diff --git a/src/mongo/db/query/stage_builder.cpp b/src/mongo/db/query/stage_builder.cpp index 47f5b8a92ad..0640590f063 100644 --- a/src/mongo/db/query/stage_builder.cpp +++ b/src/mongo/db/query/stage_builder.cpp @@ -49,7 +49,6 @@ #include "mongo/db/exec/skip.h" #include "mongo/db/exec/text.h" #include "mongo/db/index/fts_access_method.h" -#include "mongo/db/structure/catalog/namespace_details.h" #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" diff --git a/src/mongo/db/repl/rs_initialsync.cpp b/src/mongo/db/repl/rs_initialsync.cpp index 25626d83491..ba09cf6f9c1 100644 --- a/src/mongo/db/repl/rs_initialsync.cpp +++ b/src/mongo/db/repl/rs_initialsync.cpp @@ -43,7 +43,6 @@ #include "mongo/db/repl/repl_settings.h" // replSettings #include "mongo/db/repl/initial_sync.h" #include "mongo/db/operation_context_impl.h" -#include "mongo/db/structure/catalog/namespace_details.h" #include "mongo/util/mongoutils/str.h" namespace mongo { diff --git a/src/mongo/db/repl/rs_rollback.cpp b/src/mongo/db/repl/rs_rollback.cpp index 84e898dd997..1e5dc7dbe9e 100644 --- a/src/mongo/db/repl/rs_rollback.cpp +++ b/src/mongo/db/repl/rs_rollback.cpp @@ -42,7 +42,6 @@ #include "mongo/db/repl/oplog.h" #include "mongo/db/repl/rs.h" #include "mongo/db/operation_context_impl.h" -#include "mongo/db/structure/catalog/namespace_details.h" /* Scenarios * diff --git a/src/mongo/db/repl/sync.cpp b/src/mongo/db/repl/sync.cpp index 814ea19997c..6ad56531f80 100644 --- a/src/mongo/db/repl/sync.cpp +++ b/src/mongo/db/repl/sync.cpp @@ -34,7 +34,6 @@ #include "mongo/db/catalog/database.h" #include "mongo/db/client.h" #include "mongo/db/diskloc.h" -#include "mongo/db/structure/catalog/namespace_details.h" #include "mongo/db/pdfile.h" #include "mongo/db/repl/oplogreader.h" #include "mongo/db/operation_context_impl.h" diff --git a/src/mongo/db/storage/mmap_v1/mmap_v1_engine.h b/src/mongo/db/storage/mmap_v1/mmap_v1_engine.h index 09d8be4112f..d7a2b1981c8 100644 --- a/src/mongo/db/storage/mmap_v1/mmap_v1_engine.h +++ b/src/mongo/db/storage/mmap_v1/mmap_v1_engine.h @@ -28,6 +28,8 @@ * it in the license file. */ +#pragma once + #include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/db/storage/mmap_v1/mmap_v1_extent_manager.h" diff --git a/src/mongo/db/structure/catalog/namespace_index.cpp b/src/mongo/db/structure/catalog/namespace_index.cpp index 04eb462c06b..0776cc7cbab 100644 --- a/src/mongo/db/structure/catalog/namespace_index.cpp +++ b/src/mongo/db/structure/catalog/namespace_index.cpp @@ -115,12 +115,9 @@ namespace mongo { } } - void NamespaceIndex::getNamespaces( list<string>& tofill , bool onlyCollections ) const { - verify( onlyCollections ); // TODO: need to implement this - // need stdx::bind or something to make this less ugly - + void NamespaceIndex::getCollectionNamespaces( list<string>* tofill ) const { if ( _ht.get() ) - _ht->iterAll( namespaceGetNamespacesCallback , (void*)&tofill ); + _ht->iterAll( namespaceGetNamespacesCallback , (void*)tofill ); } void NamespaceIndex::maybeMkdir() const { diff --git a/src/mongo/db/structure/catalog/namespace_index.h b/src/mongo/db/structure/catalog/namespace_index.h index 8564887ff29..9601464520b 100644 --- a/src/mongo/db/structure/catalog/namespace_index.h +++ b/src/mongo/db/structure/catalog/namespace_index.h @@ -75,7 +75,7 @@ namespace mongo { bool allocated() const { return _ht.get() != 0; } - void getNamespaces( std::list<std::string>& tofill , bool onlyCollections = true ) const; + void getCollectionNamespaces( std::list<std::string>* tofill ) const; boost::filesystem::path path() const; diff --git a/src/mongo/db/structure/collection_compact.cpp b/src/mongo/db/structure/collection_compact.cpp index 903ef8aa184..95a9fafe1ab 100644 --- a/src/mongo/db/structure/collection_compact.cpp +++ b/src/mongo/db/structure/collection_compact.cpp @@ -40,7 +40,6 @@ #include "mongo/db/catalog/index_key_validate.h" #include "mongo/db/index/index_access_method.h" #include "mongo/db/kill_current_op.h" -#include "mongo/db/structure/catalog/namespace_details.h" #include "mongo/db/storage/extent.h" #include "mongo/db/storage/extent_manager.h" #include "mongo/db/storage/record.h" diff --git a/src/mongo/tools/dump.cpp b/src/mongo/tools/dump.cpp index e6a09ebc49e..4bf6a644aba 100644 --- a/src/mongo/tools/dump.cpp +++ b/src/mongo/tools/dump.cpp @@ -41,7 +41,6 @@ #include "mongo/db/db.h" #include "mongo/db/namespace_string.h" #include "mongo/db/catalog/collection.h" -#include "mongo/db/structure/catalog/namespace_index.h" #include "mongo/tools/mongodump_options.h" #include "mongo/tools/tool.h" #include "mongo/util/options_parser/option_section.h" @@ -335,7 +334,7 @@ public: Database * db = cx.ctx().db(); list<string> namespaces; - db->namespaceIndex()->getNamespaces( namespaces ); + db->getCollectionNamespaces( &namespaces ); boost::filesystem::path root = mongoDumpGlobalParams.outputDirectory; root /= dbname; |