summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/compact.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/commands/compact.cpp')
-rw-r--r--src/mongo/db/commands/compact.cpp204
1 files changed, 106 insertions, 98 deletions
diff --git a/src/mongo/db/commands/compact.cpp b/src/mongo/db/commands/compact.cpp
index e0c935a3f19..a71357cb53a 100644
--- a/src/mongo/db/commands/compact.cpp
+++ b/src/mongo/db/commands/compact.cpp
@@ -51,126 +51,134 @@
namespace mongo {
- using std::string;
- using std::stringstream;
-
- class CompactCmd : public Command {
- public:
- virtual bool isWriteCommandForConfigServer() const { return false; }
- virtual bool adminOnly() const { return false; }
- virtual bool slaveOk() const { return true; }
- virtual bool maintenanceMode() const { return true; }
- virtual void addRequiredPrivileges(const std::string& dbname,
- const BSONObj& cmdObj,
- std::vector<Privilege>* out) {
- ActionSet actions;
- actions.addAction(ActionType::compact);
- out->push_back(Privilege(parseResourcePattern(dbname, cmdObj), actions));
- }
- virtual void help( stringstream& help ) const {
- help << "compact collection\n"
- "warning: this operation locks the database and is slow. you can cancel with killOp()\n"
+using std::string;
+using std::stringstream;
+
+class CompactCmd : public Command {
+public:
+ virtual bool isWriteCommandForConfigServer() const {
+ return false;
+ }
+ virtual bool adminOnly() const {
+ return false;
+ }
+ virtual bool slaveOk() const {
+ return true;
+ }
+ virtual bool maintenanceMode() const {
+ return true;
+ }
+ virtual void addRequiredPrivileges(const std::string& dbname,
+ const BSONObj& cmdObj,
+ std::vector<Privilege>* out) {
+ ActionSet actions;
+ actions.addAction(ActionType::compact);
+ out->push_back(Privilege(parseResourcePattern(dbname, cmdObj), actions));
+ }
+ virtual void help(stringstream& help) const {
+ help << "compact collection\n"
+ "warning: this operation locks the database and is slow. you can cancel with "
+ "killOp()\n"
"{ compact : <collection_name>, [force:<bool>], [validate:<bool>],\n"
" [paddingFactor:<num>], [paddingBytes:<num>] }\n"
" force - allows to run on a replica set primary\n"
- " validate - check records are noncorrupt before adding to newly compacting extents. slower but safer (defaults to true in this version)\n";
+ " validate - check records are noncorrupt before adding to newly compacting "
+ "extents. slower but safer (defaults to true in this version)\n";
+ }
+ CompactCmd() : Command("compact") {}
+
+ virtual bool run(OperationContext* txn,
+ const string& db,
+ BSONObj& cmdObj,
+ int,
+ string& errmsg,
+ BSONObjBuilder& result) {
+ const std::string nsToCompact = parseNsCollectionRequired(db, cmdObj);
+
+ repl::ReplicationCoordinator* replCoord = repl::getGlobalReplicationCoordinator();
+ if (replCoord->getMemberState().primary() && !cmdObj["force"].trueValue()) {
+ errmsg =
+ "will not run compact on an active replica set primary as this is a slow blocking "
+ "operation. use force:true to force";
+ return false;
}
- CompactCmd() : Command("compact") { }
-
- virtual bool run(OperationContext* txn,
- const string& db,
- BSONObj& cmdObj,
- int,
- string& errmsg,
- BSONObjBuilder& result) {
- const std::string nsToCompact = parseNsCollectionRequired(db, cmdObj);
-
- repl::ReplicationCoordinator* replCoord = repl::getGlobalReplicationCoordinator();
- if (replCoord->getMemberState().primary() && !cmdObj["force"].trueValue()) {
- errmsg = "will not run compact on an active replica set primary as this is a slow blocking operation. use force:true to force";
- return false;
- }
- NamespaceString ns(nsToCompact);
- if ( !ns.isNormal() ) {
- errmsg = "bad namespace name";
- return false;
- }
+ NamespaceString ns(nsToCompact);
+ if (!ns.isNormal()) {
+ errmsg = "bad namespace name";
+ return false;
+ }
- if ( ns.isSystem() ) {
- // items in system.* cannot be moved as there might be pointers to them
- // i.e. system.indexes entries are pointed to from NamespaceDetails
- errmsg = "can't compact a system namespace";
- return false;
- }
+ if (ns.isSystem()) {
+ // items in system.* cannot be moved as there might be pointers to them
+ // i.e. system.indexes entries are pointed to from NamespaceDetails
+ errmsg = "can't compact a system namespace";
+ return false;
+ }
- CompactOptions compactOptions;
+ CompactOptions compactOptions;
- if ( cmdObj["preservePadding"].trueValue() ) {
- compactOptions.paddingMode = CompactOptions::PRESERVE;
- if ( cmdObj.hasElement( "paddingFactor" ) ||
- cmdObj.hasElement( "paddingBytes" ) ) {
- errmsg = "cannot mix preservePadding and paddingFactor|paddingBytes";
+ if (cmdObj["preservePadding"].trueValue()) {
+ compactOptions.paddingMode = CompactOptions::PRESERVE;
+ if (cmdObj.hasElement("paddingFactor") || cmdObj.hasElement("paddingBytes")) {
+ errmsg = "cannot mix preservePadding and paddingFactor|paddingBytes";
+ return false;
+ }
+ } else if (cmdObj.hasElement("paddingFactor") || cmdObj.hasElement("paddingBytes")) {
+ compactOptions.paddingMode = CompactOptions::MANUAL;
+ if (cmdObj.hasElement("paddingFactor")) {
+ compactOptions.paddingFactor = cmdObj["paddingFactor"].Number();
+ if (compactOptions.paddingFactor < 1 || compactOptions.paddingFactor > 4) {
+ errmsg = "invalid padding factor";
return false;
}
}
- else if ( cmdObj.hasElement( "paddingFactor" ) || cmdObj.hasElement( "paddingBytes" ) ) {
- compactOptions.paddingMode = CompactOptions::MANUAL;
- if ( cmdObj.hasElement("paddingFactor") ) {
- compactOptions.paddingFactor = cmdObj["paddingFactor"].Number();
- if ( compactOptions.paddingFactor < 1 ||
- compactOptions.paddingFactor > 4 ){
- errmsg = "invalid padding factor";
- return false;
- }
- }
- if ( cmdObj.hasElement("paddingBytes") ) {
- compactOptions.paddingBytes = cmdObj["paddingBytes"].numberInt();
- if ( compactOptions.paddingBytes < 0 ||
- compactOptions.paddingBytes > ( 1024 * 1024 ) ) {
- errmsg = "invalid padding bytes";
- return false;
- }
+ if (cmdObj.hasElement("paddingBytes")) {
+ compactOptions.paddingBytes = cmdObj["paddingBytes"].numberInt();
+ if (compactOptions.paddingBytes < 0 ||
+ compactOptions.paddingBytes > (1024 * 1024)) {
+ errmsg = "invalid padding bytes";
+ return false;
}
}
+ }
- if ( cmdObj.hasElement("validate") )
- compactOptions.validateDocuments = cmdObj["validate"].trueValue();
-
+ if (cmdObj.hasElement("validate"))
+ compactOptions.validateDocuments = cmdObj["validate"].trueValue();
- ScopedTransaction transaction(txn, MODE_IX);
- AutoGetDb autoDb(txn, db, MODE_X);
- Database* const collDB = autoDb.getDb();
- Collection* collection = collDB ? collDB->getCollection(ns) : NULL;
- // If db/collection does not exist, short circuit and return.
- if ( !collDB || !collection ) {
- errmsg = "namespace does not exist";
- return false;
- }
+ ScopedTransaction transaction(txn, MODE_IX);
+ AutoGetDb autoDb(txn, db, MODE_X);
+ Database* const collDB = autoDb.getDb();
+ Collection* collection = collDB ? collDB->getCollection(ns) : NULL;
- OldClientContext ctx(txn, ns);
- BackgroundOperation::assertNoBgOpInProgForNs(ns.ns());
+ // If db/collection does not exist, short circuit and return.
+ if (!collDB || !collection) {
+ errmsg = "namespace does not exist";
+ return false;
+ }
- if ( collection->isCapped() ) {
- errmsg = "cannot compact a capped collection";
- return false;
- }
+ OldClientContext ctx(txn, ns);
+ BackgroundOperation::assertNoBgOpInProgForNs(ns.ns());
- log() << "compact " << ns << " begin, options: " << compactOptions.toString();
+ if (collection->isCapped()) {
+ errmsg = "cannot compact a capped collection";
+ return false;
+ }
- StatusWith<CompactStats> status = collection->compact( txn, &compactOptions );
- if ( !status.isOK() )
- return appendCommandStatus( result, status.getStatus() );
+ log() << "compact " << ns << " begin, options: " << compactOptions.toString();
- if ( status.getValue().corruptDocuments > 0 )
- result.append("invalidObjects", status.getValue().corruptDocuments );
+ StatusWith<CompactStats> status = collection->compact(txn, &compactOptions);
+ if (!status.isOK())
+ return appendCommandStatus(result, status.getStatus());
- log() << "compact " << ns << " end";
+ if (status.getValue().corruptDocuments > 0)
+ result.append("invalidObjects", status.getValue().corruptDocuments);
- return true;
- }
- };
- static CompactCmd compactCmd;
+ log() << "compact " << ns << " end";
+ return true;
+ }
+};
+static CompactCmd compactCmd;
}