summaryrefslogtreecommitdiff
path: root/db/commands/mr.h
diff options
context:
space:
mode:
Diffstat (limited to 'db/commands/mr.h')
-rw-r--r--db/commands/mr.h49
1 files changed, 36 insertions, 13 deletions
diff --git a/db/commands/mr.h b/db/commands/mr.h
index f505a45..3fa8146 100644
--- a/db/commands/mr.h
+++ b/db/commands/mr.h
@@ -50,12 +50,15 @@ namespace mongo {
class Reducer : boost::noncopyable {
public:
+ Reducer() : numReduces(0) {}
virtual ~Reducer() {}
virtual void init( State * state ) = 0;
virtual BSONObj reduce( const BSONList& tuples ) = 0;
/** this means its a final reduce, even if there is no finalizer */
virtual BSONObj finalReduce( const BSONList& tuples , Finalizer * finalizer ) = 0;
+
+ long long numReduces;
};
// ------------ js function implementations -----------
@@ -88,7 +91,7 @@ namespace mongo {
class JSMapper : public Mapper {
public:
- JSMapper( const BSONElement & code ) : _func( "map" , code ) {}
+ JSMapper( const BSONElement & code ) : _func( "_map" , code ) {}
virtual void map( const BSONObj& o );
virtual void init( State * state );
@@ -99,8 +102,8 @@ namespace mongo {
class JSReducer : public Reducer {
public:
- JSReducer( const BSONElement& code ) : _func( "reduce" , code ) {}
- virtual void init( State * state ) { _func.init( state ); }
+ JSReducer( const BSONElement& code ) : _func( "_reduce" , code ) {}
+ virtual void init( State * state );
virtual BSONObj reduce( const BSONList& tuples );
virtual BSONObj finalReduce( const BSONList& tuples , Finalizer * finalizer );
@@ -115,12 +118,11 @@ namespace mongo {
void _reduce( const BSONList& values , BSONObj& key , int& endSizeEstimate );
JSFunction _func;
-
};
class JSFinalizer : public Finalizer {
public:
- JSFinalizer( const BSONElement& code ) : _func( "finalize" , code ) {}
+ JSFinalizer( const BSONElement& code ) : _func( "_finalize" , code ) {}
virtual BSONObj finalize( const BSONObj& o );
virtual void init( State * state ) { _func.init( state ); }
private:
@@ -153,6 +155,7 @@ namespace mongo {
// options
bool verbose;
+ bool jsMode;
// query options
@@ -178,6 +181,13 @@ namespace mongo {
string outDB;
+ // max number of keys allowed in JS map before switching mode
+ long jsMaxKeys;
+ // ratio of duplicates vs unique keys before reduce is triggered in js mode
+ float reduceTriggerRatio;
+ // maximum size of map before it gets dumped to disk
+ long maxInMemSize;
+
enum { REPLACE , // atomically replace the collection
MERGE , // merge keys, override dups
REDUCE , // merge keys, reduce dups
@@ -225,6 +235,8 @@ namespace mongo {
* transfers in memory storage to temp collection
*/
void dumpToInc();
+ void insertToInc( BSONObj& o );
+ void _insertToInc( BSONObj& o );
// ------ reduce stage -----------
@@ -252,7 +264,7 @@ namespace mongo {
/**
* inserts with correct replication semantics
*/
- void insert( const string& ns , BSONObj& o );
+ void insert( const string& ns , const BSONObj& o );
// ------ simple accessors -----
@@ -263,27 +275,38 @@ namespace mongo {
const bool isOnDisk() { return _onDisk; }
- long long numEmits() const { return _numEmits; }
+ long long numEmits() const { if (_jsMode) return _scope->getNumberLongLong("_emitCt"); return _numEmits; }
+ long long numReduces() const { if (_jsMode) return _scope->getNumberLongLong("_redCt"); return _config.reducer->numReduces; }
+
+ bool jsMode() {return _jsMode;}
+ void switchMode(bool jsMode);
+ void bailFromJS();
+
+ const Config& _config;
+ DBDirectClient _db;
protected:
- void _insertToInc( BSONObj& o );
- static void _add( InMemory* im , const BSONObj& a , long& size, long& dupCount );
+ void _add( InMemory* im , const BSONObj& a , long& size );
scoped_ptr<Scope> _scope;
- const Config& _config;
bool _onDisk; // if the end result of this map reduce is disk or not
- DBDirectClient _db;
-
scoped_ptr<InMemory> _temp;
long _size; // bytes in _temp
long _dupCount; // number of duplicate key entries
long long _numEmits;
+
+ bool _jsMode;
+ ScriptingFunction _reduceAll;
+ ScriptingFunction _reduceAndEmit;
+ ScriptingFunction _reduceAndFinalize;
+ ScriptingFunction _reduceAndFinalizeAndInsert;
};
- BSONObj fast_emit( const BSONObj& args );
+ BSONObj fast_emit( const BSONObj& args, void* data );
+ BSONObj _bailFromJS( const BSONObj& args, void* data );
} // end mr namespace
}