Skip to content

Commit

Permalink
JAVA-682: All MongoClient constructors now get default options from M…
Browse files Browse the repository at this point in the history
…ongoClientOptions instead of MongoOptions. So the job

of setting the default writeConcern is now delegated to MongoClientOptions instead of handled explicitly by MongoClient
  • Loading branch information
jyemin committed Nov 19, 2012
1 parent 55342ac commit a2ec2d9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
20 changes: 9 additions & 11 deletions src/main/com/mongodb/MongoClient.java
Expand Up @@ -53,6 +53,9 @@
* By default, all write operations will wait for acknowledgment by the server, as the default write concern is
* {@code WriteConcern.ACKNOWLEDGED}.
* <p>
* In general, users of this class will pick up all of the default options specified in {@code MongoClientOptions}. In
* particular, note that the default value of the connectionsPerHost option has been increased from 10 to 100.
* <p>
* Note: This class supersedes the {@code Mongo} class. While it extends {@code Mongo}, it differs from it in that
* the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its
* constructors accept instances of {@code MongoClientOptions} and {@code MongoClientURI}, which both also
Expand All @@ -73,8 +76,7 @@ public class MongoClient extends Mongo {
* @throws MongoException
*/
public MongoClient() throws UnknownHostException {
super();
setWriteConcern(WriteConcern.ACKNOWLEDGED);
this(new ServerAddress());
}

/**
Expand All @@ -85,8 +87,7 @@ public MongoClient() throws UnknownHostException {
* @throws MongoException
*/
public MongoClient(String host) throws UnknownHostException {
super(host);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
this(new ServerAddress(host));
}

/**
Expand All @@ -98,7 +99,7 @@ public MongoClient(String host) throws UnknownHostException {
* @throws MongoException
*/
public MongoClient(String host, MongoClientOptions options) throws UnknownHostException {
super(host, new MongoOptions(options));
this(new ServerAddress(host), options);
}

/**
Expand All @@ -110,8 +111,7 @@ public MongoClient(String host, MongoClientOptions options) throws UnknownHostEx
* @throws MongoException
*/
public MongoClient(String host, int port) throws UnknownHostException {
super(host, port);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
this(new ServerAddress(host, port));
}

/**
Expand All @@ -122,8 +122,7 @@ public MongoClient(String host, int port) throws UnknownHostException {
* @see com.mongodb.ServerAddress
*/
public MongoClient(ServerAddress addr) {
super(addr);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
this(addr, new MongoClientOptions.Builder().build());
}

/**
Expand Down Expand Up @@ -154,8 +153,7 @@ public MongoClient(ServerAddress addr, MongoClientOptions options) {
* @see com.mongodb.ServerAddress
*/
public MongoClient(List<ServerAddress> seeds) {
super(seeds);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
this(seeds, new MongoClientOptions.Builder().build());
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/com/mongodb/MongoClientOptions.java
Expand Up @@ -310,7 +310,7 @@ public String getDescription() {
* Those connections will be kept in a pool when idle.
* Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.
* <p>
* Default is 10.
* Default is 100.
* @return the maximum size of the connection pool per host
* @see MongoClientOptions#getThreadsAllowedToBlockForConnectionMultiplier()
*/
Expand Down
64 changes: 64 additions & 0 deletions src/main/com/mongodb/MongoOptions.java
Expand Up @@ -115,6 +115,70 @@ public WriteConcern getWriteConcern() {
}
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

final MongoOptions options = (MongoOptions) o;

if (autoConnectRetry != options.autoConnectRetry) return false;
if (connectTimeout != options.connectTimeout) return false;
if (connectionsPerHost != options.connectionsPerHost) return false;
if (cursorFinalizerEnabled != options.cursorFinalizerEnabled) return false;
if (fsync != options.fsync) return false;
if (j != options.j) return false;
if (maxAutoConnectRetryTime != options.maxAutoConnectRetryTime) return false;
if (maxWaitTime != options.maxWaitTime) return false;
if (safe != options.safe) return false;
if (slaveOk != options.slaveOk) return false;
if (socketKeepAlive != options.socketKeepAlive) return false;
if (socketTimeout != options.socketTimeout) return false;
if (threadsAllowedToBlockForConnectionMultiplier != options.threadsAllowedToBlockForConnectionMultiplier)
return false;
if (w != options.w) return false;
if (wtimeout != options.wtimeout) return false;
if (dbDecoderFactory != null ? !dbDecoderFactory.equals(options.dbDecoderFactory) : options.dbDecoderFactory != null)
return false;
if (dbEncoderFactory != null ? !dbEncoderFactory.equals(options.dbEncoderFactory) : options.dbEncoderFactory != null)
return false;
if (description != null ? !description.equals(options.description) : options.description != null) return false;
if (readPreference != null ? !readPreference.equals(options.readPreference) : options.readPreference != null)
return false;
if (socketFactory != null ? !socketFactory.equals(options.socketFactory) : options.socketFactory != null)
return false;
if (writeConcern != null ? !writeConcern.equals(options.writeConcern) : options.writeConcern != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = description != null ? description.hashCode() : 0;
result = 31 * result + connectionsPerHost;
result = 31 * result + threadsAllowedToBlockForConnectionMultiplier;
result = 31 * result + maxWaitTime;
result = 31 * result + connectTimeout;
result = 31 * result + socketTimeout;
result = 31 * result + (socketKeepAlive ? 1 : 0);
result = 31 * result + (autoConnectRetry ? 1 : 0);
result = 31 * result + (int) (maxAutoConnectRetryTime ^ (maxAutoConnectRetryTime >>> 32));
result = 31 * result + (slaveOk ? 1 : 0);
result = 31 * result + (readPreference != null ? readPreference.hashCode() : 0);
result = 31 * result + (dbDecoderFactory != null ? dbDecoderFactory.hashCode() : 0);
result = 31 * result + (dbEncoderFactory != null ? dbEncoderFactory.hashCode() : 0);
result = 31 * result + (safe ? 1 : 0);
result = 31 * result + w;
result = 31 * result + wtimeout;
result = 31 * result + (fsync ? 1 : 0);
result = 31 * result + (j ? 1 : 0);
result = 31 * result + (socketFactory != null ? socketFactory.hashCode() : 0);
result = 31 * result + (cursorFinalizerEnabled ? 1 : 0);
result = 31 * result + (writeConcern != null ? writeConcern.hashCode() : 0);
return result;
}

/**
* <p>The description for <code>Mongo</code> instances created with these options. This is used in various places like logging.</p>
*/
Expand Down
46 changes: 29 additions & 17 deletions src/test/com/mongodb/MongoClientTest.java
Expand Up @@ -26,42 +26,54 @@
public class MongoClientTest {
@Test
public void testConstructors() throws UnknownHostException {
MongoClientOptions customClientOptions = new MongoClientOptions.Builder().connectionsPerHost(500).build();
MongoOptions customOptions = new MongoOptions(customClientOptions);
MongoOptions defaultOptions = new MongoOptions(new MongoClientOptions.Builder().build());
MongoClient mc;

mc = new MongoClient();
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
Assert.assertEquals(new ServerAddress(), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient("localhost");
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient("127.0.0.1");
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient("localhost", new MongoClientOptions.Builder().build());
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient("127.0.0.1", customClientOptions);
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(customOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient("localhost", 27017);
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient("127.0.0.1", 27018);
Assert.assertEquals(new ServerAddress("127.0.0.1", 27018), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient(new ServerAddress("localhost"));
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient(new ServerAddress("127.0.0.1"));
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient(new ServerAddress("localhost"), new MongoClientOptions.Builder().build());
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient(new ServerAddress("127.0.0.1"), customClientOptions);
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(customOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient(Arrays.asList(new ServerAddress("localhost")));
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)));
Assert.assertEquals(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), mc.getAllAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient(Arrays.asList(new ServerAddress("localhost")), new MongoClientOptions.Builder().build());
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), customClientOptions);
Assert.assertEquals(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("127.0.0.1", 27018)), mc.getAllAddress());
Assert.assertEquals(customOptions, mc.getMongoOptions());
mc.close();

mc = new MongoClient(new MongoClientURI("mongodb://localhost"));
Assert.assertEquals(WriteConcern.ACKNOWLEDGED, mc.getWriteConcern());
mc = new MongoClient(new MongoClientURI("mongodb://127.0.0.1"));
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
mc.close();
}
}

0 comments on commit a2ec2d9

Please sign in to comment.