cleaning ScanAndOrder

This commit is contained in:
Eliot Horowitz
2011-08-01 17:51:13 -04:00
parent c7f7ba2098
commit d8d05fdc71
3 changed files with 106 additions and 70 deletions

View File

@@ -362,7 +362,7 @@ if has_option( "asio" ):
# mongod files - also files used in tools. present in dbtests, but not in mongos and not in client libs.
serverOnlyFiles = Split( "db/key.cpp db/btreebuilder.cpp util/logfile.cpp util/alignedbuilder.cpp db/mongommf.cpp db/dur.cpp db/durop.cpp db/dur_writetodatafiles.cpp db/dur_preplogbuffer.cpp db/dur_commitjob.cpp db/dur_recover.cpp db/dur_journal.cpp db/introspect.cpp db/btree.cpp db/clientcursor.cpp db/tests.cpp db/repl.cpp db/repl/rs.cpp db/repl/consensus.cpp db/repl/rs_initiate.cpp db/repl/replset_commands.cpp db/repl/manager.cpp db/repl/health.cpp db/repl/heartbeat.cpp db/repl/rs_config.cpp db/repl/rs_rollback.cpp db/repl/rs_sync.cpp db/repl/rs_initialsync.cpp db/oplog.cpp db/repl_block.cpp db/btreecursor.cpp db/cloner.cpp db/namespace.cpp db/cap.cpp db/matcher_covered.cpp db/dbeval.cpp db/restapi.cpp db/dbhelpers.cpp db/instance.cpp db/client.cpp db/database.cpp db/pdfile.cpp db/record.cpp db/cursor.cpp db/security.cpp db/queryoptimizer.cpp db/queryoptimizercursor.cpp db/extsort.cpp db/cmdline.cpp" )
serverOnlyFiles += [ "db/index.cpp" ] + Glob( "db/geo/*.cpp" ) + Glob( "db/ops/*.cpp" )
serverOnlyFiles += [ "db/index.cpp" , "db/scanandorder.cpp" ] + Glob( "db/geo/*.cpp" ) + Glob( "db/ops/*.cpp" )
serverOnlyFiles += [ "db/dbcommands.cpp" , "db/dbcommands_admin.cpp" ]
serverOnlyFiles += Glob( "db/commands/*.cpp" )

91
db/scanandorder.cpp Normal file
View File

@@ -0,0 +1,91 @@
/* scanandorder.cpp
Order results (that aren't already indexes and in order.)
*/
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../pch.h"
#include "scanandorder.h"
namespace mongo {
void ScanAndOrder::_add(BSONObj& k, BSONObj o, DiskLoc* loc) {
if (!loc) {
_best.insert(make_pair(k.getOwned(),o.getOwned()));
}
else {
BSONObjBuilder b;
b.appendElements(o);
b.append("$diskLoc", loc->toBSONObj());
_best.insert(make_pair(k.getOwned(), b.obj().getOwned()));
}
}
void ScanAndOrder::_addIfBetter(BSONObj& k, BSONObj o, BestMap::iterator i, DiskLoc* loc) {
/* todo : we don't correct _approxSize here. */
const BSONObj& worstBestKey = i->first;
int c = worstBestKey.woCompare(k, _order._spec.keyPattern);
if ( c > 0 ) {
// k is better, 'upgrade'
_best.erase(i);
_add(k, o, loc);
}
}
void ScanAndOrder::add(BSONObj o, DiskLoc* loc) {
assert( o.isValid() );
BSONObj k = _order.getKeyFromObject(o);
if ( k.isEmpty() ) {
return;
}
if ( (int) _best.size() < _limit ) {
_approxSize += k.objsize();
_approxSize += o.objsize();
/* note : adjust when bson return limit adjusts. note this limit should be a bit higher. */
uassert( 10128 , "too much data for sort() with no index. add an index or specify a smaller limit", _approxSize < 32 * 1024 * 1024 );
_add(k, o, loc);
return;
}
BestMap::iterator i;
assert( _best.end() != _best.begin() );
i = _best.end();
i--;
_addIfBetter(k, o, i, loc);
}
void ScanAndOrder::fill(BufBuilder& b, Projection *filter, int& nout ) const {
int n = 0;
int nFilled = 0;
for ( BestMap::const_iterator i = _best.begin(); i != _best.end(); i++ ) {
n++;
if ( n <= _startFrom )
continue;
const BSONObj& o = i->second;
fillQueryResultFromObj(b, filter, o);
nFilled++;
if ( nFilled >= _limit )
break;
uassert( 10129 , "too much data for sort() with no index", b.len() < 4000000 ); // appserver limit
}
nout = nFilled;
}
} // namespace mongo

View File

@@ -22,6 +22,7 @@
#include "indexkey.h"
#include "queryutil.h"
#include "projection.h"
namespace mongo {
@@ -76,30 +77,9 @@ namespace mongo {
typedef multimap<BSONObj,BSONObj,BSONObjCmp> BestMap;
class ScanAndOrder {
void _add(BSONObj& k, BSONObj o, DiskLoc* loc) {
if (!loc) {
_best.insert(make_pair(k.getOwned(),o.getOwned()));
}
else {
BSONObjBuilder b;
b.appendElements(o);
b.append("$diskLoc", loc->toBSONObj());
_best.insert(make_pair(k.getOwned(), b.obj().getOwned()));
}
}
void _addIfBetter(BSONObj& k, BSONObj o, BestMap::iterator i, DiskLoc* loc) {
/* todo : we don't correct _approxSize here. */
const BSONObj& worstBestKey = i->first;
int c = worstBestKey.woCompare(k, _order._spec.keyPattern);
if ( c > 0 ) {
// k is better, 'upgrade'
_best.erase(i);
_add(k, o, loc);
}
}
public:
static const int MaxScanAndOrderBytes;
ScanAndOrder(int startFrom, int limit, BSONObj order, const FieldRangeSet &frs) :
_best( BSONObjCmp( order ) ),
_startFrom(startFrom), _order(order, frs) {
@@ -107,60 +87,25 @@ namespace mongo {
_approxSize = 0;
}
int size() const {
return _best.size();
}
int size() const { return _best.size(); }
void add(BSONObj o, DiskLoc* loc) {
assert( o.isValid() );
BSONObj k = _order.getKeyFromObject(o);
if ( k.isEmpty() ) {
return;
}
if ( (int) _best.size() < _limit ) {
_approxSize += k.objsize();
_approxSize += o.objsize();
/* note : adjust when bson return limit adjusts. note this limit should be a bit higher. */
uassert( 10128 , "too much data for sort() with no index. add an index or specify a smaller limit", _approxSize < 32 * 1024 * 1024 );
_add(k, o, loc);
return;
}
BestMap::iterator i;
assert( _best.end() != _best.begin() );
i = _best.end();
i--;
_addIfBetter(k, o, i, loc);
}
void _fill(BufBuilder& b, Projection *filter, int& nout, BestMap::iterator begin, BestMap::iterator end) {
int n = 0;
int nFilled = 0;
for ( BestMap::iterator i = begin; i != end; i++ ) {
n++;
if ( n <= _startFrom )
continue;
BSONObj& o = i->second;
fillQueryResultFromObj(b, filter, o);
nFilled++;
if ( nFilled >= _limit )
break;
uassert( 10129 , "too much data for sort() with no index", b.len() < 4000000 ); // appserver limit
}
nout = nFilled;
}
void add(BSONObj o, DiskLoc* loc);
/* scanning complete. stick the query result in b for n objects. */
void fill(BufBuilder& b, Projection *filter, int& nout) {
_fill(b, filter, nout, _best.begin(), _best.end());
}
void fill(BufBuilder& b, Projection *filter, int& nout ) const;
private:
void _add(BSONObj& k, BSONObj o, DiskLoc* loc);
void _addIfBetter(BSONObj& k, BSONObj o, BestMap::iterator i, DiskLoc* loc);
BestMap _best; // key -> full object
int _startFrom;
int _limit; // max to send back.
KeyType _order;
unsigned _approxSize;
};
} // namespace mongo