Files
mongo/db/extsort.h

151 lines
4.1 KiB
C
Raw Normal View History

// extsort.h
/**
* 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/>.
*/
#pragma once
2010-04-27 15:27:52 -04:00
#include "../pch.h"
#include "jsobj.h"
2010-11-04 09:07:28 -04:00
#include "namespace-inl.h"
#include "curop-inl.h"
#include "../util/array.h"
namespace mongo {
2009-09-24 11:57:45 -04:00
/**
2011-06-13 16:57:19 -04:00
for external (disk) sorting by BSONObj and attaching a value
*/
class BSONObjExternalSorter : boost::noncopyable {
public:
BSONObjExternalSorter( IndexInterface &i, const BSONObj & order = BSONObj() , long maxFileSize = 1024 * 1024 * 100 );
~BSONObjExternalSorter();
typedef pair<BSONObj,DiskLoc> Data;
2009-09-21 15:22:15 -04:00
private:
IndexInterface& _idxi;
static int _compare(IndexInterface& i, const Data& l, const Data& r, const Ordering& order) {
RARELY killCurrentOp.checkForInterrupt();
_compares++;
int x = i.keyCompare(l.first, r.first, order);
2011-06-13 16:57:19 -04:00
if ( x )
return x;
return l.second.compare( r.second );
}
class MyCmp {
public:
MyCmp( IndexInterface& i, BSONObj order = BSONObj() ) : _i(i), _order( Ordering::make(order) ) {}
2011-06-13 16:57:19 -04:00
bool operator()( const Data &l, const Data &r ) const {
return _compare(_i, l, r, _order) < 0;
2011-06-13 16:57:19 -04:00
};
private:
IndexInterface& _i;
const Ordering _order;
2011-06-13 16:57:19 -04:00
};
static IndexInterface *extSortIdxInterface;
static Ordering extSortOrder;
2011-06-13 16:57:19 -04:00
static int extSortComp( const void *lv, const void *rv ) {
DEV RARELY {
dbMutex.assertWriteLocked(); // must be as we use a global var
}
Data * l = (Data*)lv;
Data * r = (Data*)rv;
return _compare(*extSortIdxInterface, *l, *r, extSortOrder);
};
class FileIterator : boost::noncopyable {
public:
2009-09-21 15:22:15 -04:00
FileIterator( string file );
~FileIterator();
bool more();
2011-01-04 00:40:41 -05:00
Data next();
private:
2009-09-21 15:22:15 -04:00
MemoryMappedFile _file;
char * _buf;
2009-09-21 15:22:15 -04:00
char * _end;
};
2009-09-24 11:57:45 -04:00
public:
2011-01-04 00:40:41 -05:00
typedef FastArray<Data> InMemory;
2009-09-24 11:57:45 -04:00
2009-09-24 17:04:44 -04:00
class Iterator : boost::noncopyable {
public:
2011-01-04 00:40:41 -05:00
Iterator( BSONObjExternalSorter * sorter );
~Iterator();
bool more();
Data next();
2011-01-04 00:40:41 -05:00
private:
2009-09-24 11:57:45 -04:00
MyCmp _cmp;
vector<FileIterator*> _files;
vector< pair<Data,bool> > _stash;
2011-01-04 00:40:41 -05:00
InMemory * _in;
InMemory::iterator _it;
2011-01-04 00:40:41 -05:00
};
2011-01-04 00:40:41 -05:00
void add( const BSONObj& o , const DiskLoc & loc );
2011-01-04 00:40:41 -05:00
void add( const BSONObj& o , int a , int b ) {
add( o , DiskLoc( a , b ) );
}
/* call after adding values, and before fetching the iterator */
void sort();
2011-01-04 00:40:41 -05:00
auto_ptr<Iterator> iterator() {
uassert( 10052 , "not sorted" , _sorted );
2009-09-24 17:04:44 -04:00
return auto_ptr<Iterator>( new Iterator( this ) );
}
2011-01-04 00:40:41 -05:00
int numFiles() {
return _files.size();
}
2011-01-04 00:40:41 -05:00
long getCurSizeSoFar() { return _curSizeSoFar; }
void hintNumObjects( long long numObjects ) {
if ( numObjects < _arraySize )
_arraySize = (int)(numObjects + 100);
}
private:
void _sortInMem();
2011-01-04 00:40:41 -05:00
void sort( string file );
void finishMap();
2011-01-04 00:40:41 -05:00
BSONObj _order;
long _maxFilesize;
path _root;
2011-01-04 00:40:41 -05:00
int _arraySize;
InMemory * _cur;
long _curSizeSoFar;
2011-01-04 00:40:41 -05:00
list<string> _files;
bool _sorted;
static unsigned long long _compares;
};
}