Skip to content

Commit c387d4a

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-java-driver into JAVA-458
Conflicts: src/test/com/mongodb/LazyDBObjectTest.java
2 parents 3f370a1 + c8b1048 commit c387d4a

34 files changed

Lines changed: 1046 additions & 620 deletions

build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ javac.source=1.5
2424
# If anyone wants to parse lib.version in ant, please do :-)
2525
#
2626

27-
lib.version=2.8.0-SNAPSHOT
27+
lib.version=2.8.0.BUILD-SNAPSHOT
2828

2929
lib.major=2
3030

31-
lib.minor=7
31+
lib.minor=8

src/main/META-INF/MANIFEST.MF

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: MongoDB
44
Bundle-SymbolicName: com.mongodb
5+
Bundle-ClassPath: mongo-java-driver-@VERSION@.jar
56
Bundle-Version: @VERSION@
67
Import-Package: javax.management, javax.net, javax.net.ssl
7-
Export-Package: com.mongodb, com.mongodb.io, com.mongodb.util, com.mongodb.gridfs, org.bson, org.bson.util, org.bson.types, org.bson.io
8+
Export-Package: com.mongodb;version="@VERSION@",
9+
com.mongodb.io;version="@VERSION@",
10+
com.mongodb.util;version="@VERSION@",
11+
com.mongodb.gridfs;version="@VERSION@",
12+
org.bson;version="@VERSION@",
13+
org.bson.util;version="@VERSION@",
14+
org.bson.types;version="@VERSION@",
15+
org.bson.io;version="@VERSION@"

src/main/META-INF/MANIFEST_BSON.MF

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: BSON
44
Bundle-SymbolicName: org.bson
5+
Bundle-ClassPath: bson-@VERSION@.jar
56
Bundle-Version: @VERSION@
6-
Export-Package: org.bson, org.bson.util, org.bson.types, org.bson.io
7+
Export-Package: org.bson;version="@VERSION@",
8+
org.bson.util;version="@VERSION@",
9+
org.bson.types;version="@VERSION@",
10+
org.bson.io;version="@VERSION@"

src/main/com/mongodb/CommandResult.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
public class CommandResult extends BasicDBObject {
2626

2727
CommandResult(ServerAddress srv) {
28-
super();
28+
this(null, srv);
29+
}
30+
31+
CommandResult(DBObject cmd, ServerAddress srv) {
32+
if (srv == null) {
33+
throw new IllegalArgumentException("server address is null");
34+
}
35+
_cmd = cmd;
2936
_host = srv;
3037
//so it is shown in toString/debug
3138
put("serverUsed", srv.toString());
@@ -41,7 +48,7 @@ public boolean ok(){
4148
throw new IllegalArgumentException( "'ok' should never be null..." );
4249

4350
if ( o instanceof Boolean )
44-
return ((Boolean)o).booleanValue();
51+
return (Boolean) o;
4552

4653
if ( o instanceof Number )
4754
return ((Number)o).intValue() == 1;
@@ -66,10 +73,16 @@ public String getErrorMessage(){
6673
*/
6774
public MongoException getException(){
6875
if ( !ok() ) {
69-
String cmdName = _cmd.keySet().iterator().next();
70-
71-
StringBuilder buf = new StringBuilder( "command failed [" );
72-
buf.append( "command failed [" ).append( cmdName ).append( "] " );
76+
StringBuilder buf = new StringBuilder();
77+
78+
String cmdName;
79+
if (_cmd != null) {
80+
cmdName = _cmd.keySet().iterator().next();
81+
buf.append( "command failed [" ).append( cmdName ).append( "]: " );
82+
} else {
83+
buf.append( "operation failed: ");
84+
}
85+
7386
buf.append( toString() );
7487

7588
return new CommandFailure( this , buf.toString() );
@@ -126,8 +139,8 @@ public ServerAddress getServerUsed() {
126139
return _host;
127140
}
128141

129-
DBObject _cmd;
130-
ServerAddress _host = null;
142+
private final DBObject _cmd;
143+
private final ServerAddress _host;
131144
private static final long serialVersionUID = 1L;
132145

133146
static class CommandFailure extends MongoException {

src/main/com/mongodb/DB.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
163163

164164
DBObject res = i.next();
165165
ServerAddress sa = (i instanceof Result) ? ((Result) i).getServerAddress() : null;
166-
CommandResult cr = new CommandResult(sa);
166+
CommandResult cr = new CommandResult(cmd, sa);
167167
cr.putAll( res );
168-
cr._cmd = cmd;
169168
return cr;
170169
}
171170

src/main/com/mongodb/DBApiLayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private void init( Response res ){
388388
_numFetched += res.size();
389389

390390
if ( ( res._flags & Bytes.RESULTFLAG_CURSORNOTFOUND ) > 0 ){
391-
throw new MongoException.CursorNotFound();
391+
throw new MongoException.CursorNotFound(res._cursor, res.serverUsed());
392392
}
393393

394394
if (res._cursor != 0 && _limit > 0 && _limit - _numFetched <= 0) {

src/main/com/mongodb/DBCursor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.mongodb;
2020

21+
import java.io.Closeable;
2122
import java.util.*;
2223

2324
import com.mongodb.DBApiLayer.Result;
@@ -48,7 +49,7 @@
4849
*
4950
* @dochub cursors
5051
*/
51-
public class DBCursor implements Iterator<DBObject> , Iterable<DBObject> {
52+
public class DBCursor implements Iterator<DBObject> , Iterable<DBObject>, Closeable {
5253

5354
/**
5455
* Initializes a new database cursor

src/main/com/mongodb/DBPort.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@
1818

1919
package com.mongodb;
2020

21+
import com.mongodb.util.ThreadUtil;
22+
2123
import java.io.BufferedInputStream;
2224
import java.io.IOException;
2325
import java.io.InputStream;
2426
import java.io.OutputStream;
2527
import java.net.InetSocketAddress;
2628
import java.net.Socket;
27-
import java.util.Collections;
2829
import java.util.Map;
29-
import java.util.WeakHashMap;
30-
import java.util.concurrent.*;
30+
import java.util.concurrent.ConcurrentHashMap;
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333

34-
import com.mongodb.util.ThreadUtil;
35-
3634
/**
3735
* represents a Port to the database, which is effectively a single connection to a server
3836
* Methods implemented at the port level should throw the raw exceptions like IOException,
@@ -56,7 +54,7 @@ public DBPort( ServerAddress addr ){
5654
this( addr , null , new MongoOptions() );
5755
}
5856

59-
DBPort( ServerAddress addr , DBPortPool pool , MongoOptions options ){
57+
DBPort( ServerAddress addr, DBPortPool pool, MongoOptions options ){
6058
_options = options;
6159
_sa = addr;
6260
_addr = addr.getSocketAddress();
@@ -118,7 +116,7 @@ private synchronized Response go( OutMessage msg , DBCollection coll , boolean f
118116
try {
119117
msg.prepare();
120118
msg.pipe( _out );
121-
119+
122120
if ( _pool != null )
123121
_pool._everWorked = true;
124122

@@ -156,15 +154,10 @@ synchronized private Response findOne( String ns , DBObject q ) throws IOExcepti
156154

157155
synchronized CommandResult runCommand( DB db , DBObject cmd ) throws IOException {
158156
Response res = findOne( db , "$cmd" , cmd );
159-
return convertToCR( res );
160-
}
161-
162-
synchronized CommandResult runCommand( String db , DBObject cmd ) throws IOException {
163-
Response res = findOne( db + ".$cmd" , cmd );
164-
return convertToCR( res );
157+
return convertToCommandResult(cmd, res);
165158
}
166159

167-
private CommandResult convertToCR(Response res) {
160+
private CommandResult convertToCommandResult(DBObject cmd, Response res) {
168161
if ( res.size() == 0 )
169162
return null;
170163
if ( res.size() > 1 )
@@ -174,7 +167,7 @@ private CommandResult convertToCR(Response res) {
174167
if ( data == null )
175168
throw new MongoInternalException( "something is wrong, no command result" );
176169

177-
CommandResult cr = new CommandResult(res.serverUsed());
170+
CommandResult cr = new CommandResult(cmd, res.serverUsed());
178171
cr.putAll( data );
179172
return cr;
180173
}
@@ -217,7 +210,7 @@ boolean _open()
217210
try {
218211
_socket = _options.socketFactory.createSocket();
219212
_socket.connect( _addr , _options.connectTimeout );
220-
213+
221214
_socket.setTcpNoDelay( ! USE_NAGLE );
222215
_socket.setKeepAlive( _options.socketKeepAlive );
223216
_socket.setSoTimeout( _options.socketTimeout );

src/main/com/mongodb/MongoException.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,34 @@ public static class CursorNotFound extends MongoException {
128128

129129
private static final long serialVersionUID = -4415279469780082174L;
130130

131-
CursorNotFound(){
132-
super( -5 , "cursor not found on server" );
131+
private final long cursorId;
132+
private final ServerAddress serverAddress;
133+
134+
/**
135+
*
136+
* @param cursorId
137+
* @param serverAddress
138+
*/
139+
CursorNotFound(long cursorId, ServerAddress serverAddress){
140+
super( -5 , "cursor " + cursorId + " not found on server " + serverAddress );
141+
this.cursorId = cursorId;
142+
this.serverAddress = serverAddress;
143+
}
144+
145+
/**
146+
* Get the cursor id that wasn't found.
147+
* @return
148+
*/
149+
public long getCursorId() {
150+
return cursorId;
151+
}
152+
153+
/**
154+
* The server address where the cursor is.
155+
* @return
156+
*/
157+
public ServerAddress getServerAddress() {
158+
return serverAddress;
133159
}
134160
}
135161

src/main/com/mongodb/WriteConcern.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public WriteConcern( int w , int wtimeout , boolean fsync , boolean j ){
169169
* @param j whether writes should wait for a journaling group commit
170170
* @param continueOnInsertError if batch inserts should continue after the first error
171171
*/
172-
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j, boolean continueOnInsertError){
172+
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j, boolean continueOnInsertError) {
173173
_w = w;
174174
_wtimeout = wtimeout;
175175
_fsync = fsync;
@@ -216,6 +216,10 @@ public WriteConcern( String w , int wtimeout , boolean fsync, boolean j ){
216216
* @return
217217
*/
218218
public WriteConcern( String w , int wtimeout , boolean fsync, boolean j, boolean continueOnInsertError ){
219+
if (w == null) {
220+
throw new IllegalArgumentException("w can not be null");
221+
}
222+
219223
_w = w;
220224
_wtimeout = wtimeout;
221225
_fsync = fsync;
@@ -243,7 +247,7 @@ public BasicDBObject getCommand(){
243247

244248
/**
245249
* Sets the w value (the write strategy)
246-
* @param wValue
250+
* @param w
247251
*/
248252
public void setWObject(Object w) {
249253
if ( ! (w instanceof Integer) && ! (w instanceof String) )
@@ -351,12 +355,29 @@ public String toString(){
351355
}
352356

353357
@Override
354-
public boolean equals( Object o ){
355-
if ( this == o ) return true;
356-
if ( o == null || getClass() != o.getClass() ) return false;
358+
public boolean equals(Object o) {
359+
if (this == o) return true;
360+
if (o == null || getClass() != o.getClass()) return false;
357361

358362
WriteConcern that = (WriteConcern) o;
359-
return _fsync == that._fsync && _w == that._w && _wtimeout == that._wtimeout && _j == that._j && _continueOnErrorForInsert == that._continueOnErrorForInsert;
363+
364+
if (_continueOnErrorForInsert != that._continueOnErrorForInsert) return false;
365+
if (_fsync != that._fsync) return false;
366+
if (_j != that._j) return false;
367+
if (_wtimeout != that._wtimeout) return false;
368+
if (!_w.equals(that._w)) return false;
369+
370+
return true;
371+
}
372+
373+
@Override
374+
public int hashCode() {
375+
int result = _w.hashCode();
376+
result = 31 * result + _wtimeout;
377+
result = 31 * result + (_fsync ? 1 : 0);
378+
result = 31 * result + (_j ? 1 : 0);
379+
result = 31 * result + (_continueOnErrorForInsert ? 1 : 0);
380+
return result;
360381
}
361382

362383
/**

0 commit comments

Comments
 (0)