summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron <aaron@10gen.com>2009-04-02 17:29:51 -0400
committerAaron <aaron@10gen.com>2009-04-02 17:29:51 -0400
commit3c439e5fde5cd6ca40dac3fc48d550e6f4d03238 (patch)
tree501ecc5fc61043eed2fd55ea77a8f4f37164d18e
parent27653d55a0f29fa64177ea88e4cecdcc6523838e (diff)
downloadmongo-3c439e5fde5cd6ca40dac3fc48d550e6f4d03238.tar.gz
add datasize command
-rw-r--r--db/dbcommands.cpp51
-rw-r--r--jstests/datasize.js25
2 files changed, 75 insertions, 1 deletions
diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp
index 37e3d9ae173..6b92675939e 100644
--- a/db/dbcommands.cpp
+++ b/db/dbcommands.cpp
@@ -891,7 +891,8 @@ namespace mongo {
CmdMedianKey() : Command( "medianKey" ) {}
virtual bool slaveOk() { return true; }
virtual void help( stringstream &help ) const {
- help << " example: { medianKey:\"blog.posts\", keyPattern:{x:1}, min:{x:10}, max:{x:55} }";
+ help << " example: { medianKey:\"blog.posts\", keyPattern:{x:1}, min:{x:10}, max:{x:55} }\n"
+ "NOTE: This command may take awhile to run";
}
bool run(const char *dbname, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool fromRepl ){
const char *ns = jsobj.getStringField( "medianKey" );
@@ -924,6 +925,54 @@ namespace mongo {
}
} cmdMedianKey;
+ class CmdDatasize : public Command {
+ public:
+ CmdDatasize() : Command( "datasize" ) {}
+ virtual bool slaveOk() { return true; }
+ virtual void help( stringstream &help ) const {
+ help << " example: { medianKey:\"blog.posts\", keyPattern:{x:1}, min:{x:10}, max:{x:55} }\n"
+ "NOTE: This command may take awhile to run";
+ }
+ bool run(const char *dbname, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool fromRepl ){
+ const char *ns = jsobj.getStringField( "datasize" );
+ BSONObj min = jsobj.getObjectField( "min" );
+ BSONObj max = jsobj.getObjectField( "max" );
+ BSONObj keyPattern = jsobj.getObjectField( "keyPattern" );
+
+ auto_ptr< Cursor > c;
+ if ( min.isEmpty() && max.isEmpty() ) {
+ setClient( ns );
+ c = theDataFileMgr.findAll( ns );
+ } else if ( min.isEmpty() || max.isEmpty() ) {
+ errmsg = "only one of min or max specified";
+ return false;
+ } else {
+ const IndexDetails *id = indexDetailsForRange( ns, errmsg, min, max, keyPattern );
+ if ( id == 0 )
+ return false;
+ c.reset( new BtreeCursor( *id, min, max, 1 ) );
+ }
+
+ Timer t;
+ long long size = 0;
+ while( c->ok() ) {
+ size += c->current().objsize();
+ c->advance();
+ }
+ int ms = t.millis();
+ if ( ms > 100 ) {
+ if ( min.isEmpty() ) {
+ out() << "Finding size for ns: " << ns << " took " << ms << "ms." << endl;
+ } else {
+ out() << "Finding size for ns: " << ns << " between " << min << " and " << max << " took " << ms << "ms." << endl;
+ }
+ }
+
+ result.append( "size", (double)size );
+ return true;
+ }
+ } cmdDatasize;
+
extern map<string,Command*> *commands;
/* TODO make these all command objects -- legacy stuff here
diff --git a/jstests/datasize.js b/jstests/datasize.js
new file mode 100644
index 00000000000..402be703a04
--- /dev/null
+++ b/jstests/datasize.js
@@ -0,0 +1,25 @@
+f = db.jstests_datasize;
+f.drop();
+
+assert.eq( 0, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+f.save( {_id:'c'} );
+assert.eq( 16, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+f.save( {_id:'fg'} );
+assert.eq( 33, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+
+f.drop();
+f.ensureIndex( {_id:1} );
+assert.eq( 0, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+f.save( {_id:'c'} );
+assert.eq( 16, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+f.save( {_id:'fg'} );
+assert.eq( 33, db.runCommand( {datasize:"test.jstests_datasize"} ).size );
+
+assert.eq( 0, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'a'}} ).ok );
+
+assert.eq( 33, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'a'}, max:{_id:'z' }} ).size );
+assert.eq( 16, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'a'}, max:{_id:'d' }} ).size );
+assert.eq( 16, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'a'}, max:{_id:'d' }, keyPattern:{_id:1}} ).size );
+assert.eq( 17, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'d'}, max:{_id:'z' }, keyPattern:{_id:1}} ).size );
+
+assert.eq( 0, db.runCommand( {datasize:"test.jstests_datasize", min:{_id:'a'}, max:{_id:'d' }, keyPattern:{a:1}} ).ok );