0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/extsort.h

100 lines
2.5 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
#include "../stdafx.h"
#include "jsobj.h"
#include "namespace.h"
#include <map>
namespace mongo {
/**
for sorting by BSONObj and attaching a value
*/
class BSONObjExternalSorter : boost::noncopyable {
public:
typedef pair<BSONObj,DiskLoc> Data;
2009-09-21 21:22:15 +02:00
private:
class FileIterator : boost::noncopyable {
public:
2009-09-21 21:22:15 +02:00
FileIterator( string file );
~FileIterator();
bool more();
Data next();
private:
2009-09-21 21:22:15 +02:00
MemoryMappedFile _file;
char * _buf;
2009-09-21 21:22:15 +02:00
char * _end;
};
2009-09-21 21:22:15 +02:00
public:
class Iterator {
public:
Iterator( BSONObjExternalSorter * sorter );
~Iterator();
bool more();
Data next();
private:
BSONObjCmp _cmp;
vector<FileIterator*> _files;
vector< pair<Data,bool> > _stash;
};
BSONObjExternalSorter( const BSONObj & order = BSONObj() , long maxFileSize = 1024 * 1024 * 100 );
~BSONObjExternalSorter();
void add( const BSONObj& o , const DiskLoc & loc );
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();
Iterator iterator(){
uassert( "not sorted" , _sorted );
return Iterator( this );
}
private:
void sort( string file );
void finishMap();
BSONObj _order;
long _maxFilesize;
path _root;
multimap<BSONObj,DiskLoc,BSONObjCmp> * _map;
long _mapSizeSoFar;
long _largestObject;
list<string> _files;
bool _sorted;
};
}