Skip to content

Commit ceff954

Browse files
committed
make Mongo a holder of DBs, not a DB
old: Mongo m = new Mongo( "1.1.1.1" , "foo" ) new: DB db = new Mongo( "1.1.1.1" ).getDB( "foo" ) JAVA-40
1 parent f2c18d3 commit ceff954

34 files changed

Lines changed: 262 additions & 292 deletions

build.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,26 @@
108108
</testng>
109109
</target>
110110

111+
<target name="examples" depends="compile">
112+
113+
<javac srcdir="examples"
114+
destdir="build/test"
115+
optimize="off"
116+
deprecation="off"
117+
source="1.5"
118+
encoding="ISO-8859-1"
119+
debug="on" >
120+
<classpath refid="classpath"/>
121+
</javac>
122+
123+
<java classname="QuickTour" >
124+
<classpath refid="classpath"/>
125+
</java>
126+
127+
<java classname="QuickTourAdmin" >
128+
<classpath refid="classpath"/>
129+
</java>
130+
131+
</target>
132+
111133
</project>

examples/QuickTour.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.mongodb.BasicDBObject;
44
import com.mongodb.DBObject;
55
import com.mongodb.DBCursor;
6-
import com.mongodb.MongoAdmin;
6+
import com.mongodb.DB;
77

88
import java.util.Set;
99
import java.util.List;
@@ -15,8 +15,10 @@ public static void main(String[] args) throws Exception {
1515
/*
1616
* connect to the local database server for the 'mydb' database
1717
*/
18-
Mongo db = new Mongo("127.0.0.1", "mydb");
18+
Mongo m = new Mongo();
1919

20+
DB db = m.getDB( "mydb" );
21+
2022
/*
2123
* Authenticate - optional
2224
*/

examples/QuickTourAdmin.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.mongodb.MongoAdmin;
1+
import com.mongodb.DB;
22
import com.mongodb.Mongo;
33
import com.mongodb.BasicDBObject;
44

@@ -9,14 +9,14 @@ public static void main(String[] args) throws Exception {
99
/*
1010
* connect to the local database server
1111
*/
12-
MongoAdmin admin = new MongoAdmin();
12+
Mongo m = new Mongo();
1313

1414
/*
1515
* Authenticate - optional
1616
*/
1717
// boolean auth = db.authenticate("foo", "bar");
1818

19-
for (String s : admin.getDatabaseNames()) {
19+
for (String s : m.getDatabaseNames()) {
2020
System.out.println(s);
2121
}
2222

@@ -25,25 +25,25 @@ public static void main(String[] args) throws Exception {
2525
* get a db
2626
*/
2727

28-
Mongo m = admin.getDatabase("com_mongodb_MongoAdmin");
28+
DB db = m.getDB("com_mongodb_MongoAdmin");
2929

3030
/*
3131
* do an insert so that the db will really be created. Calling getDB() doesn't really take any
3232
* action with the server
3333
*/
34-
m.getCollection("testcollection").insert(new BasicDBObject("i",1));
34+
db.getCollection("testcollection").insert(new BasicDBObject("i",1));
3535

36-
for (String s : admin.getDatabaseNames()) {
36+
for (String s : m.getDatabaseNames()) {
3737
System.out.println(s);
3838
}
3939

4040
/*
4141
* drop a database
4242
*/
4343

44-
admin.dropDatabase("com_mongodb_MongoAdmin");
44+
m.dropDatabase("com_mongodb_MongoAdmin");
4545

46-
for (String s : admin.getDatabaseNames()) {
46+
for (String s : m.getDatabaseNames()) {
4747
System.out.println(s);
4848
}
4949
}

src/main/com/mongodb/DB.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.util.*;
2222

23+
import com.mongodb.util.*;
24+
2325
public abstract class DB {
2426

2527
static enum WriteConcern { NONE, NORMAL, STRICT };
@@ -191,7 +193,92 @@ public WriteConcern getWriteConcern(){
191193
return _concern;
192194
}
193195

194-
196+
/**
197+
* Drops this database. Removes all data on disk. Use with caution.
198+
*/
199+
public void dropDatabase()
200+
throws MongoException {
201+
202+
BasicDBObject res = (BasicDBObject) command(new BasicDBObject("dropDatabase", 1));
203+
204+
if (res.getInt("ok") != 1) {
205+
throw new RuntimeException("Error - unable to drop database : " + res.toString());
206+
}
207+
}
208+
209+
210+
/**
211+
* Authenticates connection/db with given name and password
212+
*
213+
* @param username name of user for this database
214+
* @param passwd password of user for this database
215+
* @return true if authenticated, false otherwise
216+
*/
217+
public boolean authenticate(String username, String passwd)
218+
throws MongoException {
219+
220+
BasicDBObject res = (BasicDBObject) command(new BasicDBObject("getnonce", 1));
221+
222+
if (res.getInt("ok") != 1) {
223+
throw new MongoException("Error - unable to get nonce value for authentication.");
224+
}
225+
226+
String nonce = res.getString("nonce");
227+
228+
String auth = username + ":mongo:" + passwd;
229+
String key = nonce + username + Util.hexMD5(auth.getBytes());
230+
231+
BasicDBObject cmd = new BasicDBObject();
232+
233+
cmd.put("authenticate", 1);
234+
cmd.put("user", username);
235+
cmd.put("nonce", nonce);
236+
cmd.put("key", Util.hexMD5(key.getBytes()));
237+
238+
res = (BasicDBObject) command(cmd);
239+
240+
return res.getInt("ok") == 1;
241+
}
242+
243+
/**
244+
* Returns the last error that occurred since start of database or a call to <code>resetError()</code>
245+
*
246+
* The return object will look like
247+
*
248+
* <pre>
249+
* { err : errorMessage, nPrev : countOpsBack, ok : 1 }
250+
* </pre>
251+
*
252+
* The value for errormMessage will be null of no error has ocurred, or the message. The value of
253+
* countOpsBack will be the number of operations since the error occurred.
254+
*
255+
* Care must be taken to ensure that calls to getPreviousError go to the same connection as that
256+
* of the previous operation. See com.mongodb.Mongo.requestStart for more information.
257+
*
258+
* @return DBObject with error and status information
259+
*/
260+
public DBObject getPreviousError()
261+
throws MongoException {
262+
return command(new BasicDBObject("getpreverror", 1));
263+
}
264+
265+
/**
266+
* Resets the error memory for this database. Used to clear all errors such that getPreviousError()
267+
* will return no error.
268+
*/
269+
public void resetError()
270+
throws MongoException {
271+
command(new BasicDBObject("reseterror", 1));
272+
}
273+
274+
/**
275+
* For testing purposes only - this method forces an error to help test error handling
276+
*/
277+
public void forceError()
278+
throws MongoException {
279+
command(new BasicDBObject("forceerror", 1));
280+
}
281+
195282
final String _name;
196283
final Set<DBCollection> _seenCollections = new HashSet<DBCollection>();
197284

src/main/com/mongodb/DBAddress.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ List<DBAddress> getPairedAddresses(){
212212
}
213213
return l;
214214
}
215+
216+
public DBAddress getSister( String name ){
217+
try {
218+
return new DBAddress( _host , _port , name );
219+
}
220+
catch ( UnknownHostException uh ){
221+
throw new MongoInternalException( "shouldn't be possible" , uh );
222+
}
223+
}
215224

216225
final String _host;
217226
final int _port;

src/main/com/mongodb/DBApiLayer.java

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -74,90 +74,6 @@ String _removeRoot( String ns ){
7474
return ns.substring( _root.length() + 1 );
7575
}
7676

77-
/**
78-
* Authenticates connection/db with given name and password
79-
*
80-
* @param username name of user for this database
81-
* @param passwd password of user for this database
82-
* @return true if authenticated, false otherwise
83-
*/
84-
public boolean authenticate(String username, String passwd)
85-
throws MongoException {
86-
87-
BasicDBObject res = (BasicDBObject) command(new BasicDBObject("getnonce", 1));
88-
89-
if (res.getInt("ok") != 1) {
90-
throw new MongoException("Error - unable to get nonce value for authentication.");
91-
}
92-
93-
String nonce = res.getString("nonce");
94-
95-
String auth = username + ":mongo:" + passwd;
96-
String key = nonce + username + Util.hexMD5(auth.getBytes());
97-
98-
BasicDBObject cmd = new BasicDBObject();
99-
100-
cmd.put("authenticate", 1);
101-
cmd.put("user", username);
102-
cmd.put("nonce", nonce);
103-
cmd.put("key", Util.hexMD5(key.getBytes()));
104-
105-
res = (BasicDBObject) command(cmd);
106-
107-
return res.getInt("ok") == 1;
108-
}
109-
110-
/**
111-
* Returns the last error that occurred since start of database or a call to <code>resetError()</code>
112-
*
113-
* The return object will look like
114-
*
115-
* <pre>
116-
* { err : errorMessage, nPrev : countOpsBack, ok : 1 }
117-
* </pre>
118-
*
119-
* The value for errormMessage will be null of no error has ocurred, or the message. The value of
120-
* countOpsBack will be the number of operations since the error occurred.
121-
*
122-
* Care must be taken to ensure that calls to getPreviousError go to the same connection as that
123-
* of the previous operation. See com.mongodb.Mongo.requestStart for more information.
124-
*
125-
* @return DBObject with error and status information
126-
*/
127-
public DBObject getPreviousError()
128-
throws MongoException {
129-
return command(new BasicDBObject("getpreverror", 1));
130-
}
131-
132-
/**
133-
* Resets the error memory for this database. Used to clear all errors such that getPreviousError()
134-
* will return no error.
135-
*/
136-
public void resetError()
137-
throws MongoException {
138-
command(new BasicDBObject("reseterror", 1));
139-
}
140-
141-
/**
142-
* For testing purposes only - this method forces an error to help test error handling
143-
*/
144-
public void forceError()
145-
throws MongoException {
146-
command(new BasicDBObject("forceerror", 1));
147-
}
148-
149-
/**
150-
* Drops this database. Removes all data on disk. Use with caution.
151-
*/
152-
public void dropDatabase()
153-
throws MongoException {
154-
155-
BasicDBObject res = (BasicDBObject) command(new BasicDBObject("dropDatabase", 1));
156-
157-
if (res.getInt("ok") != 1) {
158-
throw new RuntimeException("Error - unable to drop database : " + res.toString());
159-
}
160-
}
16177

16278
/** Get a collection from a &lt;databaseName&gt;.&lt;collectionName&gt;.
16379
* If <code>fullNameSpace</code> does not contain any "."s, this will

0 commit comments

Comments
 (0)