Skip to content

Commit 8797fdb

Browse files
committed
some cleaning
1 parent 5b5296f commit 8797fdb

File tree

6 files changed

+62
-34
lines changed

6 files changed

+62
-34
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ void say( OutMessage msg )
6262

6363
private synchronized Response go( OutMessage msg , DBCollection coll )
6464
throws IOException {
65+
return go( msg , coll , false );
66+
}
67+
68+
private synchronized Response go( OutMessage msg , DBCollection coll , boolean forceReponse )
69+
throws IOException {
6570

6671
if ( _processingResponse ){
6772
if ( coll == null ){
@@ -88,7 +93,7 @@ private synchronized Response go( OutMessage msg , DBCollection coll )
8893
if ( _pool != null )
8994
_pool._everWorked = true;
9095

91-
if ( coll == null )
96+
if ( coll == null && ! forceReponse )
9297
return null;
9398

9499
_processingResponse = true;
@@ -132,6 +137,33 @@ synchronized CommandResult runCommand( DB db , DBObject cmd ) {
132137
return (CommandResult)res;
133138
}
134139

140+
synchronized DBObject findOne( String ns , DBObject q ){
141+
OutMessage msg = OutMessage.query( null , 0 , ns , 0 , -1 , q , null );
142+
143+
try {
144+
Response res = go( msg , null , true );
145+
if ( res.size() == 0 )
146+
return null;
147+
if ( res.size() > 1 )
148+
throw new MongoInternalException( "something is wrong. size:" + res.size() );
149+
return res.get(0);
150+
}
151+
catch ( IOException ioe ){
152+
throw new MongoInternalException( "DBPort.findOne failed" , ioe );
153+
}
154+
155+
}
156+
157+
synchronized CommandResult runCommand( String db , DBObject cmd ) {
158+
DBObject res = findOne( db + ".$cmd" , cmd );
159+
if ( res == null )
160+
throw new MongoInternalException( "something is wrong, no command result" );
161+
CommandResult cr = new CommandResult();
162+
cr.putAll( res );
163+
return cr;
164+
}
165+
166+
135167
synchronized CommandResult tryGetLastError( DB db , long last, WriteConcern concern){
136168
if ( last != _calls )
137169
return null;

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ class DBTCPConnector implements DBConnector {
3232

3333
public DBTCPConnector( Mongo m , ServerAddress addr )
3434
throws MongoException {
35-
_mongo = m;
3635
_portHolder = new DBPortPool.Holder( m._options );
3736
_checkAddress( addr );
3837

3938
_createLogger.info( addr.toString() );
4039

4140
if ( addr.isPaired() ){
4241
_allHosts = new ArrayList<ServerAddress>( addr.explode() );
43-
_rsStatus = new ReplicaSetStatus( m , _allHosts , this );
42+
_rsStatus = new ReplicaSetStatus( _allHosts );
4443
_createLogger.info( "switching to replica set mode : " + _allHosts + " -> " + _curMaster );
4544
}
4645
else {
@@ -58,12 +57,11 @@ public DBTCPConnector( Mongo m , ServerAddress ... all )
5857

5958
public DBTCPConnector( Mongo m , List<ServerAddress> all )
6059
throws MongoException {
61-
_mongo = m;
6260
_portHolder = new DBPortPool.Holder( m._options );
6361
_checkAddress( all );
6462

6563
_allHosts = new ArrayList<ServerAddress>( all ); // make a copy so it can't be modified
66-
_rsStatus = new ReplicaSetStatus( m , _allHosts , this );
64+
_rsStatus = new ReplicaSetStatus( _allHosts );
6765

6866
_createLogger.info( all + " -> " + _curMaster );
6967
}
@@ -353,7 +351,6 @@ public void close(){
353351
_rsStatus.close();
354352
}
355353

356-
final Mongo _mongo;
357354
private ServerAddress _curMaster;
358355
private DBPortPool _curPortPool;
359356
private DBPortPool.Holder _portHolder;

src/main/com/mongodb/OutMessage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static OutMessage query( Mongo m , int options , String ns , int numToSkip , int
4242

4343
OutMessage( Mongo m ){
4444
_mongo = m;
45-
_buffer = m._bufferPool.get();
45+
_buffer = _mongo == null ? new PoolOutputBuffer() : _mongo._bufferPool.get();
4646
set( _buffer );
4747
}
4848

@@ -166,11 +166,11 @@ byte[] toByteArray(){
166166
}
167167

168168
void doneWithMessage(){
169-
if ( _buffer != null ){
169+
if ( _buffer != null && _mongo != null )
170170
_mongo._bufferPool.done( _buffer );
171-
_buffer = null;
172-
_mongo = null;
173-
}
171+
172+
_buffer = null;
173+
_mongo = null;
174174
}
175175

176176
boolean hasOption( int option ){

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@ class ReplicaSetStatus {
2121

2222
static final Logger _rootLogger = Logger.getLogger( "com.mongodb.ReplicaSetStatus" );
2323

24-
ReplicaSetStatus( Mongo m , List<ServerAddress> initial , DBConnector connector ){
25-
_mongo = m;
24+
ReplicaSetStatus( List<ServerAddress> initial ){
2625

27-
if ( connector == null )
28-
_adminDB = m.getDB( "admin" );
29-
else
30-
_adminDB = new DBApiLayer( m , "admin" , connector );
31-
3226
_all = Collections.synchronizedList( new ArrayList<Node>() );
3327
for ( ServerAddress addr : initial ){
3428
_all.add( new Node( addr ) );
@@ -109,7 +103,7 @@ class Node {
109103
synchronized void update(){
110104
try {
111105
long start = System.currentTimeMillis();
112-
CommandResult res = _port.runCommand( _adminDB , _isMasterCmd );
106+
CommandResult res = _port.runCommand( "admin" , _isMasterCmd );
113107
_lastCheck = System.currentTimeMillis();
114108
_pingTime = _lastCheck - start;
115109

@@ -146,7 +140,7 @@ synchronized void update(){
146140
return;
147141

148142
try {
149-
DBObject config = _port.findOne( _mongo.getDB( "local" ) , "system.replset" , new BasicDBObject() );
143+
DBObject config = _port.findOne( "local.system.replset" , new BasicDBObject() );
150144
if ( config == null ){
151145
// probbaly a replica pair
152146
// TODO: add this in when pairs are really gone
@@ -316,8 +310,6 @@ void close(){
316310
_closed = true;
317311
}
318312

319-
final Mongo _mongo;
320-
final DB _adminDB;
321313

322314
final List<Node> _all;
323315
Updater _updater;
@@ -345,7 +337,7 @@ public static void main( String args[] )
345337

346338
Mongo m = new Mongo( addrs );
347339

348-
ReplicaSetStatus status = new ReplicaSetStatus( m , addrs , null );
340+
ReplicaSetStatus status = new ReplicaSetStatus( addrs );
349341
System.out.println( status.ensureMaster()._addr );
350342

351343
while ( true ){

src/main/com/mongodb/Response.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class Response {
2828

2929
Response( DBCollection collection , InputStream in, BSONDecoder decoder)
3030
throws IOException {
31-
_collection = collection;
3231

3332
byte[] b = new byte[36];
3433
Bits.readFully(in, b);
@@ -53,7 +52,7 @@ class Response {
5352
else
5453
_objects = new ArrayList<DBObject>( _num );
5554

56-
DBCallback c = DBCallback.FACTORY.create( _collection );
55+
DBCallback c = DBCallback.FACTORY.create( collection );
5756

5857
for ( int i=0; i<_num; i++ ){
5958
if ( user._toGo < 5 )
@@ -167,8 +166,6 @@ public String toString(){
167166
return "flags:" + _flags + " _cursor:" + _cursor + " _startingFrom:" + _startingFrom + " _num:" + _num ;
168167
}
169168

170-
final DBCollection _collection;
171-
172169
final int _len;
173170
final int _id;
174171
final int _responseTo;

src/test/com/mongodb/ReplSetTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
public class ReplSetTest {
88

9+
static void _sleep()
10+
throws InterruptedException {
11+
//Thread.sleep( 500 );
12+
}
13+
914
static class R extends Thread {
1015
R( ServerAddress a ){
1116
_a = a;
@@ -19,7 +24,7 @@ static class R extends Thread {
1924
public void run(){
2025
while ( true ){
2126
try {
22-
Thread.sleep( 500 );
27+
_sleep();
2328
_coll.findOne();
2429
}
2530
catch ( NullPointerException n ){
@@ -40,12 +45,16 @@ public void run(){
4045
public static void main( String args[] )
4146
throws Exception {
4247

48+
boolean rs = true;
49+
4350
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
44-
addrs.add( new ServerAddress( "localhost" , 27017 ) );
45-
addrs.add( new ServerAddress( "localhost" , 27018 ) );
46-
addrs.add( new ServerAddress( "localhost" , 27019 ) );
51+
if ( rs ){
52+
addrs.add( new ServerAddress( "localhost" , 27017 ) );
53+
addrs.add( new ServerAddress( "localhost" , 27018 ) );
54+
addrs.add( new ServerAddress( "localhost" , 27019 ) );
55+
}
4756

48-
Mongo m = new Mongo ( addrs );
57+
Mongo m = rs ? new Mongo( addrs ) : new Mongo();
4958
DB db = m.getDB( "test" );
5059
DBCollection c = db.getCollection( "foo" );
5160
c.insert( new BasicDBObject( "_id" , 17 ) );
@@ -56,9 +65,10 @@ public static void main( String args[] )
5665
}
5766

5867
while ( true ){
59-
Thread.sleep( 500 );
68+
_sleep();
6069
try {
61-
System.out.println( c.findOne() );
70+
DBObject x = c.findOne();
71+
//System.out.println( x );
6272
c.update( new BasicDBObject( "_id" , 17 ) , new BasicDBObject( "$inc" , new BasicDBObject( "x" , 1 ) ) );
6373
}
6474
catch ( Exception e ){

0 commit comments

Comments
 (0)