1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// parallel.cpp
#include "stdafx.h"
#include "parallel.h"
#include "connpool.h"
#include "../db/queryutil.h"
#include "../db/dbmessage.h"
#include "../s/util.h"
namespace mongo {
// -------- ClusteredCursor -----------
ClusteredCursor::ClusteredCursor( QueryMessage& q ){
_ns = q.ns;
_query = q.query.copy();
_options = q.queryOptions;
if ( q.fields.get() )
_fields = q.fields->getSpec();
_done = false;
}
ClusteredCursor::ClusteredCursor( const string& ns , const BSONObj& q , int options , const BSONObj& fields ){
_ns = ns;
_query = q.getOwned();
_options = options;
_fields = fields.getOwned();
_done = false;
}
ClusteredCursor::~ClusteredCursor(){
_done = true; // just in case
}
auto_ptr<DBClientCursor> ClusteredCursor::query( const string& server , int num , BSONObj extra ){
uassert( "cursor already done" , ! _done );
BSONObj q = _query;
if ( ! extra.isEmpty() ){
q = concatQuery( q , extra );
}
ScopedDbConnection conn( server );
checkShardVersion( conn.conn() , _ns );
log(5) << "ClusteredCursor::query server:" << server << " ns:" << _ns << " query:" << q << " num:" << num << " _fields:" << _fields << " options: " << _options << endl;
auto_ptr<DBClientCursor> cursor = conn->query( _ns.c_str() , q , num , 0 , ( _fields.isEmpty() ? 0 : &_fields ) , _options );
if ( cursor->hasResultFlag( QueryResult::ResultFlag_ShardConfigStale ) )
throw StaleConfigException( _ns , "ClusteredCursor::query" );
conn.done();
return cursor;
}
BSONObj ClusteredCursor::concatQuery( const BSONObj& query , const BSONObj& extraFilter ){
if ( ! query.hasField( "query" ) )
return _concatFilter( query , extraFilter );
BSONObjBuilder b;
BSONObjIterator i( query );
while ( i.more() ){
BSONElement e = i.next();
if ( strcmp( e.fieldName() , "query" ) ){
b.append( e );
continue;
}
b.append( "query" , _concatFilter( e.embeddedObjectUserCheck() , extraFilter ) );
}
return b.obj();
}
BSONObj ClusteredCursor::_concatFilter( const BSONObj& filter , const BSONObj& extra ){
BSONObjBuilder b;
b.appendElements( filter );
b.appendElements( extra );
return b.obj();
// TODO: should do some simplification here if possibl ideally
}
// -------- SerialServerClusteredCursor -----------
SerialServerClusteredCursor::SerialServerClusteredCursor( set<ServerAndQuery> servers , QueryMessage& q , int sortOrder) : ClusteredCursor( q ){
for ( set<ServerAndQuery>::iterator i = servers.begin(); i!=servers.end(); i++ )
_servers.push_back( *i );
if ( sortOrder > 0 )
sort( _servers.begin() , _servers.end() );
else if ( sortOrder < 0 )
sort( _servers.rbegin() , _servers.rend() );
_serverIndex = 0;
}
bool SerialServerClusteredCursor::more(){
if ( _current.get() && _current->more() )
return true;
if ( _serverIndex >= _servers.size() )
return false;
ServerAndQuery& sq = _servers[_serverIndex++];
_current = query( sq._server , 0 , sq._extra );
return _current->more();
}
BSONObj SerialServerClusteredCursor::next(){
uassert( "no more items" , more() );
return _current->next();
}
// -------- ParallelSortClusteredCursor -----------
ParallelSortClusteredCursor::ParallelSortClusteredCursor( set<ServerAndQuery> servers , QueryMessage& q , const BSONObj& sortKey ) : ClusteredCursor( q ) , _servers( servers ){
_numServers = servers.size();
_sortKey = sortKey.getOwned();
_cursors = new auto_ptr<DBClientCursor>[_numServers];
_nexts = new BSONObj[_numServers];
// TODO: parellize
int num = 0;
for ( set<ServerAndQuery>::iterator i = servers.begin(); i!=servers.end(); i++ ){
const ServerAndQuery& sq = *i;
_cursors[num++] = query( sq._server , 0 , sq._extra );
}
}
ParallelSortClusteredCursor::~ParallelSortClusteredCursor(){
delete [] _cursors;
delete [] _nexts;
}
bool ParallelSortClusteredCursor::more(){
for ( int i=0; i<_numServers; i++ ){
if ( ! _nexts[i].isEmpty() )
return true;
if ( _cursors[i].get() && _cursors[i]->more() )
return true;
}
return false;
}
BSONObj ParallelSortClusteredCursor::next(){
advance();
BSONObj best = BSONObj();
int bestFrom = -1;
for ( int i=0; i<_numServers; i++){
if ( _nexts[i].isEmpty() )
continue;
if ( best.isEmpty() ){
best = _nexts[i];
bestFrom = i;
continue;
}
int comp = best.woSortOrder( _nexts[i] , _sortKey );
if ( comp < 0 )
continue;
best = _nexts[i];
bestFrom = i;
}
uassert( "no more elements" , ! best.isEmpty() );
_nexts[bestFrom] = BSONObj();
return best;
}
void ParallelSortClusteredCursor::advance(){
for ( int i=0; i<_numServers; i++ ){
if ( ! _nexts[i].isEmpty() ){
// already have a good object there
continue;
}
if ( ! _cursors[i]->more() ){
// cursor is dead, oh well
continue;
}
_nexts[i] = _cursors[i]->next();
}
}
}
|