Skip to content

Commit

Permalink
JAVA-682: Added three new classes--MongoClient, MongoClientOptions, a…
Browse files Browse the repository at this point in the history
…nd MongoClientURI, all of which have a default write concern that waits

for acknowledgement of the write from the server.  MongoClient extends Mongo so that it's easy to switch to it just by changing which class
is constructed.  MongoClientOptions, unlike MongoOptions, is immutable and has a static inner Builder class for construction.
  • Loading branch information
jyemin committed Nov 15, 2012
1 parent 3984340 commit b4d0a6e
Show file tree
Hide file tree
Showing 13 changed files with 1,775 additions and 341 deletions.
7 changes: 5 additions & 2 deletions src/main/com/mongodb/Mongo.java
Expand Up @@ -64,8 +64,11 @@
* mongo.setWriteConcern(WriteConcern.SAFE);
* </pre>
*
* @see com.mongodb.ReadPreference
* @see com.mongodb.WriteConcern
* Note: This class has been superseded by {@code MongoClient}, and may be deprecated in a future release.
*
* @see MongoClient
* @see ReadPreference
* @see WriteConcern
*/
public class Mongo {

Expand Down
195 changes: 195 additions & 0 deletions src/main/com/mongodb/MongoClient.java
@@ -0,0 +1,195 @@
/**
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.mongodb;

import java.net.UnknownHostException;
import java.util.List;

/**
* A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance
* for the entire JVM.
* <p>
* The following are equivalent, and all connect to the local database running on the default port:
* <pre>
* MongoClient mongoClient1 = new MongoClient();
* MongoClient mongoClient1 = new MongoClient("localhost");
* MongoClient mongoClient2 = new MongoClient("localhost", 27017);
* MongoClient mongoClient4 = new MongoClient(new ServerAddress("localhost"));
* MongoClient mongoClient5 = new MongoClient(new ServerAddress("localhost"), new MongoClientOptions.Builder().build());
* </pre>
* <p>
* You can connect to a
* <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a> using the Java driver by passing
* a ServerAddress list to the MongoClient constructor. For example:
* <pre>
* MongoClient mongoClient = new MongoClient(Arrays.asList(
* new ServerAddress("localhost", 27017),
* new ServerAddress("localhost", 27018),
* new ServerAddress("localhost", 27019)));
* </pre>
* You can connect to a sharded cluster using the same constructor. MongoClient will auto-detect whether the servers are
* a list of replica set members or a list of mongos servers.
* <p>
* By default, all read and write operations will be made on the primary,
* but it's possible to read from secondaries by changing the read preference:
* <pre>
* mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
* </pre>
* By default, all write operations will wait for acknowledgment by the server, as the default write concern is
* {@code WriteConcern.ACKNOWLEDGED}.
* <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
* set the same default write concern.
*
* @see ReadPreference#primary()
* @see com.mongodb.WriteConcern#ACKNOWLEDGED
* @see MongoClientOptions
* @see MongoClientURI
* @since 2.10.0
*/
public class MongoClient extends Mongo {

/**
* Creates an instance based on a (single) mongodb node (localhost, default port).
*
* @throws UnknownHostException
* @throws MongoException
*/
public MongoClient() throws UnknownHostException {
super();
setWriteConcern(WriteConcern.ACKNOWLEDGED);
}

/**
* Creates a Mongo instance based on a (single) mongodb node.
*
* @param host server to connect to in format host[:port]
* @throws UnknownHostException if the database host cannot be resolved
* @throws MongoException
*/
public MongoClient(String host) throws UnknownHostException {
super(host);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
}

/**
* Creates a Mongo instance based on a (single) mongodb node (default port).
*
* @param host server to connect to in format host[:port]
* @param options default query options
* @throws UnknownHostException if the database host cannot be resolved
* @throws MongoException
*/
public MongoClient(String host, MongoClientOptions options) throws UnknownHostException {
super(host, new MongoOptions(options));
}

/**
* Creates a Mongo instance based on a (single) mongodb node.
*
* @param host the database's host address
* @param port the port on which the database is running
* @throws UnknownHostException if the database host cannot be resolved
* @throws MongoException
*/
public MongoClient(String host, int port) throws UnknownHostException {
super(host, port);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
}

/**
* Creates a Mongo instance based on a (single) mongodb node
*
* @param addr the database address
* @throws MongoException
* @see com.mongodb.ServerAddress
*/
public MongoClient(ServerAddress addr) {
super(addr);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
}

/**
* Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.
*
* @param addr the database address
* @param options default options
* @throws MongoException
* @see com.mongodb.ServerAddress
*/
public MongoClient(ServerAddress addr, MongoClientOptions options) {
super(addr, new MongoOptions(options));
}

/**
* Creates a Mongo based on a list of replica set members or a list of mongos.
* It will find all members (the master will be used by default). If you pass in a single server in the list,
* the driver will still function as if it is a replica set. If you have a standalone server,
* use the Mongo(ServerAddress) constructor.
* <p/>
* If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
* and automatically fail over to the next server if the closest is down.
*
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
* either be a list of mongod servers in the same replica set or a list of mongos servers in the same
* sharded cluster.
* @throws MongoException
* @see com.mongodb.ServerAddress
*/
public MongoClient(List<ServerAddress> seeds) {
super(seeds);
setWriteConcern(WriteConcern.ACKNOWLEDGED);
}


/**
* Creates a Mongo based on a list of replica set members or a list of mongos.
* It will find all members (the master will be used by default). If you pass in a single server in the list,
* the driver will still function as if it is a replica set. If you have a standalone server,
* use the Mongo(ServerAddress) constructor.
* <p/>
* If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
* and automatically fail over to the next server if the closest is down.
*
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
* either be a list of mongod servers in the same replica set or a list of mongos servers in the same
* sharded cluster.
* @param options default options
* @throws MongoException
* @see com.mongodb.ServerAddress
*/
public MongoClient(List<ServerAddress> seeds, MongoClientOptions options) {
super(seeds, new MongoOptions(options));
}


/**
* Creates a Mongo described by a URI.
* If only one address is used it will only connect to that node, otherwise it will discover all nodes.
* @param uri the URI
* @throws MongoException
* @throws UnknownHostException
* @see MongoURI
* @dochub connections
*/
public MongoClient(MongoClientURI uri) throws UnknownHostException {
super(new MongoURI(uri));
}
}

0 comments on commit b4d0a6e

Please sign in to comment.