Skip to content

Commit 0d03571

Browse files
committed
all write ops return WriteResult which can lazily call getlasterror JAVA-73
1 parent dbb00be commit 0d03571

File tree

8 files changed

+188
-48
lines changed

8 files changed

+188
-48
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,27 @@ class MyCollection extends DBCollection {
124124
public void doapply( DBObject o ){
125125
}
126126

127-
public void insert( DBObject o )
127+
public WriteResult insert( DBObject o )
128128
throws MongoException {
129-
insert( new DBObject[]{ o } );
129+
return insert( new DBObject[]{ o } );
130130
}
131131

132-
public void insert(DBObject[] arr)
132+
public WriteResult insert(DBObject[] arr)
133133
throws MongoException {
134-
insert(arr, true);
134+
return insert(arr, true);
135135
}
136136

137-
public void insert(List<DBObject> list)
137+
public WriteResult insert(List<DBObject> list)
138138
throws MongoException {
139-
insert(list.toArray(new DBObject[list.size()]) , true);
139+
return insert(list.toArray(new DBObject[list.size()]) , true);
140140
}
141141

142-
protected void insert(DBObject obj, boolean shouldApply )
142+
protected WriteResult insert(DBObject obj, boolean shouldApply )
143143
throws MongoException {
144-
insert( new DBObject[]{ obj } , shouldApply );
144+
return insert( new DBObject[]{ obj } , shouldApply );
145145
}
146146

147-
protected void insert(DBObject[] arr, boolean shouldApply )
147+
protected WriteResult insert(DBObject[] arr, boolean shouldApply )
148148
throws MongoException {
149149

150150
if ( SHOW ) {
@@ -164,6 +164,8 @@ protected void insert(DBObject[] arr, boolean shouldApply )
164164
}
165165
}
166166

167+
WriteResult last = null;
168+
167169
int cur = 0;
168170
while ( cur < arr.length ){
169171
OutMessage om = OutMessage.get( 2002 );
@@ -183,12 +185,13 @@ protected void insert(DBObject[] arr, boolean shouldApply )
183185
}
184186
}
185187

186-
_connector.say( _db , om , getWriteConcern() );
187-
188+
last = _connector.say( _db , om , getWriteConcern() );
188189
}
190+
191+
return last;
189192
}
190193

191-
public void remove( DBObject o )
194+
public WriteResult remove( DBObject o )
192195
throws MongoException {
193196

194197
if ( SHOW ) System.out.println( "remove: " + _fullNameSpace + " " + JSON.serialize( o ) );
@@ -209,7 +212,7 @@ public void remove( DBObject o )
209212

210213
om.putObject( o );
211214

212-
_connector.say( _db , om , getWriteConcern() );
215+
return _connector.say( _db , om , getWriteConcern() );
213216
}
214217

215218
void _cleanCursors()
@@ -262,17 +265,7 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
262265

263266
_cleanCursors();
264267

265-
OutMessage query = OutMessage.get( 2004 );
266-
267-
query.writeInt( options ); // options
268-
query.writeCString( _fullNameSpace );
269-
270-
query.writeInt( numToSkip );
271-
query.writeInt( batchSize );
272-
query.putObject( ref ); // ref
273-
if ( fields != null )
274-
query.putObject( fields ); // fields to return
275-
268+
OutMessage query = OutMessage.query( options , _fullNameSpace , numToSkip , batchSize , ref , fields );
276269

277270
Response res = _connector.call( _db , this , query , 2 );
278271

@@ -289,7 +282,7 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
289282
return new Result( this , res , batchSize , options );
290283
}
291284

292-
public void update( DBObject query , DBObject o , boolean upsert , boolean multi )
285+
public WriteResult update( DBObject query , DBObject o , boolean upsert , boolean multi )
293286
throws MongoException {
294287

295288
if ( SHOW ) System.out.println( "update: " + _fullNameSpace + " " + JSON.serialize( query ) );
@@ -306,7 +299,7 @@ public void update( DBObject query , DBObject o , boolean upsert , boolean multi
306299
om.putObject( query );
307300
om.putObject( o );
308301

309-
_connector.say( _db , om , getWriteConcern() );
302+
return _connector.say( _db , om , getWriteConcern() );
310303
}
311304

312305
protected void createIndex( final DBObject keys, final DBObject options )

src/main/com/mongodb/DBCollection.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ public abstract class DBCollection {
4343
* @param doc object to save
4444
* @dochub insert
4545
*/
46-
public abstract void insert(DBObject doc) throws MongoException;
46+
public abstract WriteResult insert(DBObject doc) throws MongoException;
4747

4848
/**
4949
* Saves an array of documents to the database.
5050
*
5151
* @param arr array of documents to save
5252
* @dochub insert
5353
*/
54-
public abstract void insert(DBObject[] arr) throws MongoException;
54+
public abstract WriteResult insert(DBObject[] arr) throws MongoException;
5555

5656
/**
5757
* Saves an array of documents to the database.
5858
*
5959
* @param list list of documents to save
6060
* @dochub insert
6161
*/
62-
public abstract void insert(List<DBObject> list) throws MongoException;
62+
public abstract WriteResult insert(List<DBObject> list) throws MongoException;
6363

6464
/**
6565
* Performs an update operation.
@@ -70,20 +70,20 @@ public abstract class DBCollection {
7070
* See http://www.mongodb.org/display/DOCS/Atomic+Operations
7171
* @dochub update
7272
*/
73-
public abstract void update( DBObject q , DBObject o , boolean upsert , boolean multi ) throws MongoException ;
73+
public abstract WriteResult update( DBObject q , DBObject o , boolean upsert , boolean multi ) throws MongoException ;
7474

7575
/**
7676
* @dochub update
7777
*/
78-
public void update( DBObject q , DBObject o ) throws MongoException {
79-
update( q , o , false , false );
78+
public WriteResult update( DBObject q , DBObject o ) throws MongoException {
79+
return update( q , o , false , false );
8080
}
8181

8282
/**
8383
* @dochub update
8484
*/
85-
public void updateMulti( DBObject q , DBObject o ) throws MongoException {
86-
update( q , o , false , true );
85+
public WriteResult updateMulti( DBObject q , DBObject o ) throws MongoException {
86+
return update( q , o , false , true );
8787
}
8888

8989
/** Adds any necessary fields to a given object before saving it to the collection.
@@ -95,7 +95,7 @@ public void updateMulti( DBObject q , DBObject o ) throws MongoException {
9595
* @param o the object that documents to be removed must match
9696
* @dochub remove
9797
*/
98-
public abstract void remove( DBObject o ) throws MongoException ;
98+
public abstract WriteResult remove( DBObject o ) throws MongoException ;
9999

100100
/** Finds an object.
101101
* @param ref query used to search
@@ -399,10 +399,10 @@ public final Object apply( DBObject jo , boolean ensureID ){
399399
* @param jo the <code>DBObject</code> to save
400400
* will add <code>_id</code> field to jo if needed
401401
*/
402-
public final void save( DBObject jo )
402+
public final WriteResult save( DBObject jo )
403403
throws MongoException {
404404
if ( checkReadOnly( true ) )
405-
return;
405+
return null;
406406

407407
_checkObject( jo , false , false );
408408

@@ -415,14 +415,13 @@ public final void save( DBObject jo )
415415
if ( DEBUG ) System.out.println( "saving new object" );
416416
if ( id != null && id instanceof ObjectId )
417417
((ObjectId)id).notNew();
418-
insert( jo );
419-
return;
418+
return insert( jo );
420419
}
421420

422421
if ( DEBUG ) System.out.println( "doing implicit upsert : " + jo.get( "_id" ) );
423422
DBObject q = new BasicDBObject();
424423
q.put( "_id" , id );
425-
update( q , jo , true , false );
424+
return update( q , jo , true , false );
426425
}
427426

428427
// ---- DB COMMANDS ----

src/main/com/mongodb/DBConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface DBConnector {
2929
public void requestDone();
3030
public void requestEnsureConnection();
3131

32-
public void say( DB db , OutMessage m , DB.WriteConcern concern ) throws MongoException;
32+
public WriteResult say( DB db , OutMessage m , DB.WriteConcern concern ) throws MongoException;
3333

3434
public Response call( DB db , DBCollection coll , OutMessage m ) throws MongoException;
3535
public Response call( DB db , DBCollection coll , OutMessage m , int retries ) throws MongoException;

src/main/com/mongodb/DBPort.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ void say( OutMessage msg )
6161
throws IOException {
6262
go( msg , null );
6363
}
64-
64+
6565
private synchronized Response go( OutMessage msg , DBCollection coll )
6666
throws IOException {
67-
67+
68+
_calls++;
69+
6870
if ( _socket == null )
6971
_open();
7072

@@ -86,6 +88,28 @@ private synchronized Response go( OutMessage msg , DBCollection coll )
8688
}
8789
}
8890

91+
synchronized BasicDBObject getLastError( DB db ){
92+
93+
OutMessage msg = OutMessage.query( 0 , db.getName() + ".$cmd" , 0 , -1 , new BasicDBObject( "getlasterror" , 1 ) , null );
94+
95+
try {
96+
Response res = go( msg , db.getCollection( "$cmd" ) );
97+
if ( res.size() != 1 )
98+
throw new MongoInternalException( "something is wrong. size:" + res.size() );
99+
return (BasicDBObject)res.get(0);
100+
}
101+
catch ( IOException ioe ){
102+
throw new MongoInternalException( "getlasterror failed: " + ioe.toString() , ioe );
103+
}
104+
}
105+
106+
synchronized BasicDBObject tryGetLastError( DB db , long last ){
107+
if ( last != _calls )
108+
return null;
109+
110+
return getLastError( db );
111+
}
112+
89113
public synchronized void ensureOpen()
90114
throws IOException {
91115

@@ -211,6 +235,7 @@ void checkAuth( DB db ){
211235
private boolean _inauth = false;
212236
private Map<DB,Boolean> _authed = Collections.synchronizedMap( new WeakHashMap<DB,Boolean>() );
213237
int _lastThread;
238+
long _calls = 0;
214239

215240
private static Logger _rootLogger = Logger.getLogger( "com.mongodb.port" );
216241
}

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ public void requestEnsureConnection(){
110110
_threadPort.get().requestEnsureConnection();
111111
}
112112

113-
void _checkWriteError( MyPort mp , DBPort port )
113+
WriteResult _checkWriteError( MyPort mp , DBPort port )
114114
throws MongoException {
115115

116116
DBObject e = _mongo.getDB( "admin" ).getLastError();
117117
mp.done( port );
118118

119119
Object foo = e.get( "err" );
120120
if ( foo == null )
121-
return;
121+
return new WriteResult( (BasicDBObject)e );
122122

123123
int code = -1;
124124
if ( e.get( "code" ) instanceof Number )
@@ -131,7 +131,7 @@ void _checkWriteError( MyPort mp , DBPort port )
131131
throw new MongoException( code , s );
132132
}
133133

134-
public void say( DB db , OutMessage m , DB.WriteConcern concern )
134+
public WriteResult say( DB db , OutMessage m , DB.WriteConcern concern )
135135
throws MongoException {
136136
MyPort mp = _threadPort.get();
137137
DBPort port = mp.get( true );
@@ -140,17 +140,18 @@ public void say( DB db , OutMessage m , DB.WriteConcern concern )
140140
try {
141141
port.say( m );
142142
if ( concern == DB.WriteConcern.STRICT ){
143-
_checkWriteError( mp , port );
143+
return _checkWriteError( mp , port );
144144
}
145145
else {
146146
mp.done( port );
147+
return new WriteResult( db , port );
147148
}
148149
}
149150
catch ( IOException ioe ){
150151
mp.error( ioe );
151152
_error( ioe );
152153
if ( concern == DB.WriteConcern.NONE )
153-
return;
154+
return new WriteResult( new BasicDBObject( "$err" , "NETWORK ERROR" ) );
154155
throw new MongoException.Network( "can't say something" , ioe );
155156
}
156157
catch ( MongoException me ){

src/main/com/mongodb/OutMessage.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ static OutMessage get( int op ){
4646
return m;
4747
}
4848

49+
static OutMessage query( int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields ){
50+
OutMessage out = get( 2004 );
51+
52+
out.writeInt( options );
53+
out.writeCString( ns );
54+
55+
out.writeInt( numToSkip );
56+
out.writeInt( batchSize );
57+
58+
out.putObject( query );
59+
if ( fields != null )
60+
out.putObject( fields );
61+
62+
return out;
63+
}
64+
4965
OutMessage(){
5066
set( _buffer );
5167
}

0 commit comments

Comments
 (0)