void run() { const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); OperationContext& opCtx = *opCtxPtr; OldClientWriteContext ctx(&opCtx, ns()); DBDirectClient db(&opCtx); db.insert(ns(), BSON("x" << 1 << "y" << 2)); db.insert(ns(), BSON("x" << 2 << "y" << 2)); Collection* collection = ctx.getCollection(); ASSERT(collection); IndexCatalog* indexCatalog = collection->getIndexCatalog(); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(&opCtx)); // _id index ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); ASSERT_EQUALS(ErrorCodes::DuplicateKey, dbtests::createIndex(&opCtx, ns(), BSON("y" << 1), true)); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(&opCtx)); ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); ASSERT_OK(dbtests::createIndex(&opCtx, ns(), BSON("x" << 1), true)); ASSERT_EQUALS(2, indexCatalog->numIndexesReady(&opCtx)); ASSERT_EQUALS(2U, db.getIndexSpecs(ns()).size()); }
void run() { OperationContextNoop txn; Client::WriteContext ctx(&txn, ns()); db.insert(ns(), BSON("x" << 1 << "y" << 2)); db.insert(ns(), BSON("x" << 2 << "y" << 2)); Collection* collection = ctx.ctx().db()->getCollection( &txn, ns() ); ASSERT( collection ); IndexCatalog* indexCatalog = collection->getIndexCatalog(); ASSERT_EQUALS(1, indexCatalog->numIndexesReady()); // _id index ASSERT_EQUALS(1U, db.count("test.system.indexes")); // test.buildindex // test.buildindex_$id // test.system.indexes ASSERT_EQUALS(3U, db.count("test.system.namespaces")); db.ensureIndex(ns(), BSON("y" << 1), true); ASSERT_EQUALS(1, indexCatalog->numIndexesReady()); ASSERT_EQUALS(1U, db.count("test.system.indexes")); ASSERT_EQUALS(3U, db.count("test.system.namespaces")); db.ensureIndex(ns(), BSON("x" << 1), true); ASSERT_EQUALS(2, indexCatalog->numIndexesReady()); ASSERT_EQUALS(2U, db.count("test.system.indexes")); ASSERT_EQUALS(4U, db.count("test.system.namespaces")); }
void run() { OperationContextImpl txn; Client::WriteContext ctx(&txn, ns()); DBDirectClient db(&txn); db.insert(ns(), BSON("x" << 1 << "y" << 2)); db.insert(ns(), BSON("x" << 2 << "y" << 2)); Collection* collection = ctx.ctx().db()->getCollection( &txn, ns() ); ASSERT( collection ); IndexCatalog* indexCatalog = collection->getIndexCatalog(); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(&txn)); // _id index ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); db.ensureIndex(ns(), BSON("y" << 1), true); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(&txn)); ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); db.ensureIndex(ns(), BSON("x" << 1), true); ctx.commit(); ASSERT_EQUALS(2, indexCatalog->numIndexesReady(&txn)); ASSERT_EQUALS(2U, db.getIndexSpecs(ns()).size()); }
Status appendCollectionStorageStats(OperationContext* opCtx, const NamespaceString& nss, const BSONObj& param, BSONObjBuilder* result) { int scale = 1; if (param["scale"].isNumber()) { scale = param["scale"].numberInt(); if (scale < 1) { return {ErrorCodes::BadValue, "scale has to be >= 1"}; } } else if (param["scale"].trueValue()) { return {ErrorCodes::BadValue, "scale has to be a number >= 1"}; } bool verbose = param["verbose"].trueValue(); AutoGetCollectionForReadCommand ctx(opCtx, nss); Collection* collection = ctx.getCollection(); // Will be set if present if (!ctx.getDb() || !collection) { result->appendNumber("size", 0); result->appendNumber("count", 0); result->appendNumber("storageSize", 0); result->append("nindexes", 0); result->appendNumber("totalIndexSize", 0); result->append("indexDetails", BSONObj()); result->append("indexSizes", BSONObj()); std::string errmsg = !(ctx.getDb()) ? "Database [" + nss.db().toString() + "] not found." : "Collection [" + nss.toString() + "] not found."; return {ErrorCodes::NamespaceNotFound, errmsg}; } long long size = collection->dataSize(opCtx) / scale; result->appendNumber("size", size); long long numRecords = collection->numRecords(opCtx); result->appendNumber("count", numRecords); if (numRecords) result->append("avgObjSize", collection->averageObjectSize(opCtx)); RecordStore* recordStore = collection->getRecordStore(); result->appendNumber( "storageSize", static_cast<long long>(recordStore->storageSize(opCtx, result, verbose ? 1 : 0)) / scale); recordStore->appendCustomStats(opCtx, result, scale); IndexCatalog* indexCatalog = collection->getIndexCatalog(); result->append("nindexes", indexCatalog->numIndexesReady(opCtx)); BSONObjBuilder indexDetails; IndexCatalog::IndexIterator i = indexCatalog->getIndexIterator(opCtx, false); while (i.more()) { const IndexDescriptor* descriptor = i.next(); IndexAccessMethod* iam = indexCatalog->getIndex(descriptor); invariant(iam); BSONObjBuilder bob; if (iam->appendCustomStats(opCtx, &bob, scale)) { indexDetails.append(descriptor->indexName(), bob.obj()); } } result->append("indexDetails", indexDetails.obj()); BSONObjBuilder indexSizes; long long indexSize = collection->getIndexSize(opCtx, &indexSizes, scale); result->appendNumber("totalIndexSize", indexSize / scale); result->append("indexSizes", indexSizes.obj()); return Status::OK(); }