Skip to content

Commit ebd698c

Browse files
committed
more simpification of net layer
1 parent 700da1a commit ebd698c

7 files changed

Lines changed: 105 additions & 202 deletions

File tree

src/main/com/mongodb/CommandResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void throwOnError()
5050

5151
static class CommandFailure extends MongoException {
5252
CommandFailure( CommandResult res , String msg ){
53-
super( MongoException._getCode( res ) , msg );
53+
super( ServerError._getCode( res ) , msg );
5454
}
5555
}
5656
}

src/main/com/mongodb/DBApiLayer.java

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,13 @@ public Iterator<DBObject> find( DBObject ref , DBObject fields , int numToSkip ,
274274
query.putObject( fields ); // fields to return
275275

276276

277-
Response raw = _connector.call( _db , this , query , 2 );
278-
SingleResult res = new SingleResult( _fullNameSpace , options , raw );
277+
Response res = _connector.call( _db , this , query , 2 );
279278

280-
if ( res._lst.size() != raw._num )
281-
throw new RuntimeException( "something is wrong have:" + res._lst.size() + " should have:" + raw._num );
282-
if ( res._lst.size() == 0 )
279+
if ( res.size() == 0 )
283280
return null;
284281

285-
if ( res._lst.size() == 1 ){
286-
BSONObject foo = res._lst.get(0);
282+
if ( res.size() == 1 ){
283+
BSONObject foo = res.get(0);
287284
Object err = foo.get( "$err" );
288285
if ( err != null )
289286
throw new MongoException( foo );
@@ -326,83 +323,27 @@ protected void createIndex( final DBObject keys, final DBObject options )
326323
final String _fullNameSpace;
327324
}
328325

329-
class SingleResult {
330-
331-
SingleResult( String fullNameSpace , int options , Response res ){
332-
_res = res;
333-
_fullNameSpace = fullNameSpace;
334-
_options = options;
335-
336-
if ( res._num == 0 )
337-
_lst = EMPTY;
338-
else if ( res._num < 3 )
339-
_lst = new LinkedList<DBObject>();
340-
else
341-
_lst = new ArrayList<DBObject>( res._num );
342-
343-
while ( res.more() ){
344-
_lst.add( res.next() );
345-
}
346-
}
347-
348-
boolean hasGetMore(){
349-
if ( _res._cursor <= 0 )
350-
return false;
351-
352-
if ( _res._num > 0 )
353-
return true;
354-
355-
if ( ( _options & Bytes.QUERYOPTION_TAILABLE ) == 0 )
356-
return false;
357-
358-
// have a tailable cursor
359-
if ( ( _res._flags & Bytes.RESULTFLAG_AWAITCAPABLE ) > 0 )
360-
return true;
361-
362-
try {
363-
System.out.println( "sleep" );
364-
Thread.sleep( 1000 );
365-
}
366-
catch ( Exception e ){}
367-
368-
return true;
369-
}
370-
371-
long cursor(){
372-
return _res._cursor;
373-
}
374-
375-
public String toString(){
376-
return _res.toString();
377-
}
378-
379-
final String _fullNameSpace;
380-
final Response _res;
381-
final int _options;
382-
final List<DBObject> _lst;
383-
}
384-
385326
class Result implements Iterator<DBObject> {
386327

387-
Result( MyCollection coll , SingleResult res , int numToReturn , int options ){
328+
Result( MyCollection coll , Response res , int numToReturn , int options ){
388329
init( res );
389330
_collection = coll;
390331
_numToReturn = numToReturn;
391332
_options = options;
392333
}
393334

394-
private void init( SingleResult res ){
395-
_totalBytes += res._res._len;
335+
private void init( Response res ){
336+
_totalBytes += res._len;
396337
_curResult = res;
397-
_cur = res._lst.iterator();
398-
_sizes.add( res._lst.size() );
338+
_cur = res.iterator();
339+
_sizes.add( res.size() );
399340
}
400341

401342
public DBObject next(){
402343
if ( _cur.hasNext() )
403344
return _cur.next();
404345

405-
if ( ! _curResult.hasGetMore() )
346+
if ( ! _curResult.hasGetMore( _options ) )
406347
throw new RuntimeException( "no more" );
407348

408349
_advance();
@@ -413,7 +354,7 @@ public boolean hasNext(){
413354
if ( _cur.hasNext() )
414355
return true;
415356

416-
if ( ! _curResult.hasGetMore() )
357+
if ( ! _curResult.hasGetMore( _options ) )
417358
return false;
418359

419360
_advance();
@@ -428,20 +369,14 @@ private void _advance(){
428369
OutMessage m = OutMessage.get( 2005 );
429370

430371
m.writeInt( 0 );
431-
m.writeCString( _curResult._fullNameSpace );
372+
m.writeCString( _collection._fullNameSpace );
432373
m.writeInt( _numToReturn ); // num to return
433374
m.writeLong( _curResult.cursor() );
434375

435376
try {
436-
Response raw = _connector.call( DBApiLayer.this , _collection , m );
377+
Response res = _connector.call( DBApiLayer.this , _collection , m );
437378
_numGetMores++;
438-
439-
SingleResult res = new SingleResult( _curResult._fullNameSpace , _options , raw );
440379
init( res );
441-
442-
if ( raw.more() || raw.bytesLeft() > 0 ){
443-
throw new RuntimeException( "uh oh" );
444-
}
445380
}
446381
catch ( MongoException me ){
447382
throw new MongoInternalException( "can't do getmore" , me );
@@ -474,7 +409,7 @@ List<Integer> getSizes(){
474409
return Collections.unmodifiableList( _sizes );
475410
}
476411

477-
SingleResult _curResult;
412+
Response _curResult;
478413
Iterator<DBObject> _cur;
479414
final MyCollection _collection;
480415
final int _numToReturn;

src/main/com/mongodb/DBCallback.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ DBObject dbget(){
104104
DBObject o = (DBObject)get();
105105
return o;
106106
}
107+
108+
public void reset(){
109+
_lastName = null;
110+
_lastArray = false;
111+
super.reset();
112+
}
107113

108114
private String _lastName;
109115
private boolean _lastArray = false;

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,15 @@ public Response call( DB db , DBCollection coll , OutMessage m , int retries )
167167

168168
try {
169169
Response res = port.call( m , coll );
170-
final MyDoneHook hook = new MyDoneHook( mp , port );
171-
res.addHook( hook );
172-
173-
String err = _getError( res.peek() );
170+
ServerError err = res.getError();
174171

175-
if ( err != null ){
176-
if ( "not master".equals( err ) ){
177-
if ( ! hook._done )
178-
mp.done( port );
179-
180-
_pickCurrent();
181-
if ( retries <= 0 ){
182-
throw new MongoException( "not talking to master and retries used up" );
183-
}
184-
185-
return call( db , coll , m , retries -1 );
172+
if ( err != null && err.isNotMasterError() ){
173+
_pickCurrent();
174+
if ( retries <= 0 ){
175+
throw new MongoException( "not talking to master and retries used up" );
186176
}
177+
178+
return call( db , coll , m , retries -1 );
187179
}
188180

189181
return res;
@@ -219,17 +211,6 @@ boolean _error( Throwable t )
219211
return true;
220212
}
221213

222-
String _getError( BSONObject obj ){
223-
if ( obj == null )
224-
return null;
225-
226-
Object err = obj.get( "$err" );
227-
if ( err == null )
228-
return null;
229-
230-
return err.toString();
231-
}
232-
233214
class MyPort {
234215

235216
DBPort get( boolean keep ){
@@ -433,27 +414,6 @@ boolean _isMaster( DBObject res ){
433414
throw new IllegalArgumentException( "invalid ismaster [" + x + "] : " + x.getClass().getName() );
434415
}
435416

436-
static class MyDoneHook implements Response.DoneHook {
437-
MyDoneHook( MyPort mp , DBPort dp ){
438-
_mp = mp;
439-
_dp = dp;
440-
}
441-
442-
public void done(){
443-
_mp.done( _dp );
444-
_done = true;
445-
}
446-
447-
public void error( IOException ioe ){
448-
_mp.error( ioe );
449-
_done = true;
450-
}
451-
452-
final MyPort _mp;
453-
final DBPort _dp;
454-
boolean _done = false;
455-
}
456-
457417
public void close(){
458418
_portHolder.close();
459419
}

src/main/com/mongodb/MongoException.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,7 @@ public MongoException( int code , String msg , Throwable t ){
4343
}
4444

4545
public MongoException( BSONObject o ){
46-
this( _getCode( o ) , _getMsg( o ) );
47-
}
48-
49-
static int _getCode( BSONObject o ){
50-
Object c = o.get( "code" );
51-
if ( c == null )
52-
c = o.get( "$code" );
53-
54-
if ( c == null )
55-
return -5;
56-
57-
return ((Number)c).intValue();
46+
this( ServerError._getCode( o ) , _getMsg( o ) );
5847
}
5948

6049
static String _getMsg( BSONObject o ){

0 commit comments

Comments
 (0)