summaryrefslogtreecommitdiff
path: root/db/extsort.h
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-09-16 15:26:18 -0400
committerEliot Horowitz <eliot@10gen.com>2009-09-16 15:26:18 -0400
commit03404026b9cb3174905b0114dc73fc9264a8c741 (patch)
tree978872441ecc4475c277679778b90dfbcee7076a /db/extsort.h
parentd5b26ce6b5682d9191fa44dcbc77e77f7633a246 (diff)
downloadmongo-03404026b9cb3174905b0114dc73fc9264a8c741.tar.gz
external srot for BSONObj and DiskLoc SERVER-291
Diffstat (limited to 'db/extsort.h')
-rw-r--r--db/extsort.h98
1 files changed, 98 insertions, 0 deletions
diff --git a/db/extsort.h b/db/extsort.h
new file mode 100644
index 00000000000..6b2511a7c0c
--- /dev/null
+++ b/db/extsort.h
@@ -0,0 +1,98 @@
+// 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;
+
+ class FileIterator : boost::noncopyable {
+ public:
+ FileIterator( string file , int bufSize );
+ ~FileIterator();
+ bool more();
+ Data next();
+ private:
+ int _fd;
+ char * _buf;
+ long _read;
+ long _length;
+ };
+
+ 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 ) );
+ }
+
+ 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;
+ };
+}