Skip to content

Commit a2ec2d9

Browse files
committed
JAVA-682: All MongoClient constructors now get default options from MongoClientOptions instead of MongoOptions. So the job
of setting the default writeConcern is now delegated to MongoClientOptions instead of handled explicitly by MongoClient
1 parent 55342ac commit a2ec2d9

File tree

4 files changed

+103
-29
lines changed

4 files changed

+103
-29
lines changed

src/main/com/mongodb/MongoClient.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
* By default, all write operations will wait for acknowledgment by the server, as the default write concern is
5454
* {@code WriteConcern.ACKNOWLEDGED}.
5555
* <p>
56+
* In general, users of this class will pick up all of the default options specified in {@code MongoClientOptions}. In
57+
* particular, note that the default value of the connectionsPerHost option has been increased from 10 to 100.
58+
* <p>
5659
* Note: This class supersedes the {@code Mongo} class. While it extends {@code Mongo}, it differs from it in that
5760
* the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its
5861
* constructors accept instances of {@code MongoClientOptions} and {@code MongoClientURI}, which both also
@@ -73,8 +76,7 @@ public class MongoClient extends Mongo {
7376
* @throws MongoException
7477
*/
7578
public MongoClient() throws UnknownHostException {
76-
super();
77-
setWriteConcern(WriteConcern.ACKNOWLEDGED);
79+
this(new ServerAddress());
7880
}
7981

8082
/**
@@ -85,8 +87,7 @@ public MongoClient() throws UnknownHostException {
8587
* @throws MongoException
8688
*/
8789
public MongoClient(String host) throws UnknownHostException {
88-
super(host);
89-
setWriteConcern(WriteConcern.ACKNOWLEDGED);
90+
this(new ServerAddress(host));
9091
}
9192

9293
/**
@@ -98,7 +99,7 @@ public MongoClient(String host) throws UnknownHostException {
9899
* @throws MongoException
99100
*/
100101
public MongoClient(String host, MongoClientOptions options) throws UnknownHostException {
101-
super(host, new MongoOptions(options));
102+
this(new ServerAddress(host), options);
102103
}
103104

104105
/**
@@ -110,8 +111,7 @@ public MongoClient(String host, MongoClientOptions options) throws UnknownHostEx
110111
* @throws MongoException
111112
*/
112113
public MongoClient(String host, int port) throws UnknownHostException {
113-
super(host, port);
114-
setWriteConcern(WriteConcern.ACKNOWLEDGED);
114+
this(new ServerAddress(host, port));
115115
}
116116

117117
/**
@@ -122,8 +122,7 @@ public MongoClient(String host, int port) throws UnknownHostException {
122122
* @see com.mongodb.ServerAddress
123123
*/
124124
public MongoClient(ServerAddress addr) {
125-
super(addr);
126-
setWriteConcern(WriteConcern.ACKNOWLEDGED);
125+
this(addr, new MongoClientOptions.Builder().build());
127126
}
128127

129128
/**
@@ -154,8 +153,7 @@ public MongoClient(ServerAddress addr, MongoClientOptions options) {
154153
* @see com.mongodb.ServerAddress
155154
*/
156155
public MongoClient(List<ServerAddress> seeds) {
157-
super(seeds);
158-
setWriteConcern(WriteConcern.ACKNOWLEDGED);
156+
this(seeds, new MongoClientOptions.Builder().build());
159157
}
160158

161159

src/main/com/mongodb/MongoClientOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public String getDescription() {
310310
* Those connections will be kept in a pool when idle.
311311
* Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.
312312
* <p>
313-
* Default is 10.
313+
* Default is 100.
314314
* @return the maximum size of the connection pool per host
315315
* @see MongoClientOptions#getThreadsAllowedToBlockForConnectionMultiplier()
316316
*/

src/main/com/mongodb/MongoOptions.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,70 @@ public WriteConcern getWriteConcern() {
115115
}
116116
}
117117

118+
@Override
119+
public boolean equals(final Object o) {
120+
if (this == o) return true;
121+
if (o == null || getClass() != o.getClass()) return false;
122+
123+
final MongoOptions options = (MongoOptions) o;
124+
125+
if (autoConnectRetry != options.autoConnectRetry) return false;
126+
if (connectTimeout != options.connectTimeout) return false;
127+
if (connectionsPerHost != options.connectionsPerHost) return false;
128+
if (cursorFinalizerEnabled != options.cursorFinalizerEnabled) return false;
129+
if (fsync != options.fsync) return false;
130+
if (j != options.j) return false;
131+
if (maxAutoConnectRetryTime != options.maxAutoConnectRetryTime) return false;
132+
if (maxWaitTime != options.maxWaitTime) return false;
133+
if (safe != options.safe) return false;
134+
if (slaveOk != options.slaveOk) return false;
135+
if (socketKeepAlive != options.socketKeepAlive) return false;
136+
if (socketTimeout != options.socketTimeout) return false;
137+
if (threadsAllowedToBlockForConnectionMultiplier != options.threadsAllowedToBlockForConnectionMultiplier)
138+
return false;
139+
if (w != options.w) return false;
140+
if (wtimeout != options.wtimeout) return false;
141+
if (dbDecoderFactory != null ? !dbDecoderFactory.equals(options.dbDecoderFactory) : options.dbDecoderFactory != null)
142+
return false;
143+
if (dbEncoderFactory != null ? !dbEncoderFactory.equals(options.dbEncoderFactory) : options.dbEncoderFactory != null)
144+
return false;
145+
if (description != null ? !description.equals(options.description) : options.description != null) return false;
146+
if (readPreference != null ? !readPreference.equals(options.readPreference) : options.readPreference != null)
147+
return false;
148+
if (socketFactory != null ? !socketFactory.equals(options.socketFactory) : options.socketFactory != null)
149+
return false;
150+
if (writeConcern != null ? !writeConcern.equals(options.writeConcern) : options.writeConcern != null)
151+
return false;
152+
153+
return true;
154+
}
155+
156+
@Override
157+
public int hashCode() {
158+
int result = description != null ? description.hashCode() : 0;
159+
result = 31 * result + connectionsPerHost;
160+
result = 31 * result + threadsAllowedToBlockForConnectionMultiplier;
161+
result = 31 * result + maxWaitTime;
162+
result = 31 * result + connectTimeout;
163+
result = 31 * result + socketTimeout;
164+
result = 31 * result + (socketKeepAlive ? 1 : 0);
165+
result = 31 * result + (autoConnectRetry ? 1 : 0);
166+
result = 31 * result + (int) (maxAutoConnectRetryTime ^ (maxAutoConnectRetryTime >>> 32));
167+
result = 31 * result + (slaveOk ? 1 : 0);
168+
result = 31 * result + (readPreference != null ? readPreference.hashCode() : 0);
169+
result = 31 * result + (dbDecoderFactory != null ? dbDecoderFactory.hashCode() : 0);
170+
result = 31 * result + (dbEncoderFactory != null ? dbEncoderFactory.hashCode() : 0);
171+
result = 31 * result + (safe ? 1 : 0);
172+
result = 31 * result + w;
173+
result = 31 * result + wtimeout;
174+
result = 31 * result + (fsync ? 1 : 0);
175+
result = 31 * result + (j ? 1 : 0);
176+
result = 31 * result + (socketFactory != null ? socketFactory.hashCode() : 0);
177+
result = 31 * result + (cursorFinalizerEnabled ? 1 : 0);
178+
result = 31 * result + (writeConcern != null ? writeConcern.hashCode() : 0);
179+
return result;
180+
}
181+
118182
/**
119183
* <p>The description for <code>Mongo</code> instances created with these options. This is used in various places like logging.</p>
120184
*/

src/test/com/mongodb/MongoClientTest.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,54 @@
2626
public class MongoClientTest {
2727
@Test
2828
public void testConstructors() throws UnknownHostException {
29+
MongoClientOptions customClientOptions = new MongoClientOptions.Builder().connectionsPerHost(500).build();
30+
MongoOptions customOptions = new MongoOptions(customClientOptions);
31+
MongoOptions defaultOptions = new MongoOptions(new MongoClientOptions.Builder().build());
2932
MongoClient mc;
3033

3134
mc = new MongoClient();
32-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
35+
Assert.assertEquals(new ServerAddress(), mc.getAddress());
36+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
3337
mc.close();
3438

35-
mc = new MongoClient("localhost");
36-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
39+
mc = new MongoClient("127.0.0.1");
40+
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
41+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
3742
mc.close();
3843

39-
mc = new MongoClient("localhost", new MongoClientOptions.Builder().build());
40-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
44+
mc = new MongoClient("127.0.0.1", customClientOptions);
45+
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
46+
Assert.assertEquals(customOptions, mc.getMongoOptions());
4147
mc.close();
4248

43-
mc = new MongoClient("localhost", 27017);
44-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
49+
mc = new MongoClient("127.0.0.1", 27018);
50+
Assert.assertEquals(new ServerAddress("127.0.0.1", 27018), mc.getAddress());
51+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
4552
mc.close();
4653

47-
mc = new MongoClient(new ServerAddress("localhost"));
48-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
54+
mc = new MongoClient(new ServerAddress("127.0.0.1"));
55+
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
56+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
4957
mc.close();
5058

51-
mc = new MongoClient(new ServerAddress("localhost"), new MongoClientOptions.Builder().build());
52-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
59+
mc = new MongoClient(new ServerAddress("127.0.0.1"), customClientOptions);
60+
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
61+
Assert.assertEquals(customOptions, mc.getMongoOptions());
5362
mc.close();
5463

55-
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost")));
56-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
64+
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)));
65+
Assert.assertEquals(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), mc.getAllAddress());
66+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
5767
mc.close();
5868

59-
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost")), new MongoClientOptions.Builder().build());
60-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
69+
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), customClientOptions);
70+
Assert.assertEquals(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), mc.getAllAddress());
71+
Assert.assertEquals(customOptions, mc.getMongoOptions());
6172
mc.close();
6273

63-
mc = new MongoClient(new MongoClientURI("mongodb://localhost"));
64-
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
74+
mc = new MongoClient(new MongoClientURI("mongodb://127.0.0.1"));
75+
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
76+
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
6577
mc.close();
6678
}
6779
}

0 commit comments

Comments
 (0)