Skip to content

Commit c2a691f

Browse files
committed
JAVA-342: Converting array to List instead of vice versa using Arrays.asList(T ...), to avoid double copy
1 parent ba9ff84 commit c2a691f

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bson.types.ObjectId;
2424

2525
import java.util.ArrayList;
26+
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.HashMap;
2829
import java.util.Iterator;
@@ -196,28 +197,28 @@ public void drop(){
196197
super.drop();
197198
}
198199

199-
public WriteResult insert(DBObject[] arr, com.mongodb.WriteConcern concern, DBEncoder encoder ){
200+
public WriteResult insert(List<DBObject> list, com.mongodb.WriteConcern concern, DBEncoder encoder ){
200201

201202
if (concern == null) {
202203
throw new IllegalArgumentException("Write concern can not be null");
203204
}
204205

205-
return insert( arr, true, concern, encoder );
206+
return insert(list, true, concern, encoder);
206207
}
207208

208-
protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.WriteConcern concern, DBEncoder encoder ){
209+
protected WriteResult insert(List<DBObject> list, boolean shouldApply , com.mongodb.WriteConcern concern, DBEncoder encoder ){
209210

210211
if (encoder == null)
211212
encoder = DefaultDBEncoder.FACTORY.create();
212213

213214
if ( willTrace() ) {
214-
for (DBObject o : arr) {
215+
for (DBObject o : list) {
215216
trace( "save: " + _fullNameSpace + " " + JSON.serialize( o ) );
216217
}
217218
}
218219

219220
if ( shouldApply ){
220-
for (DBObject o : arr) {
221+
for (DBObject o : list) {
221222
apply(o);
222223
_checkObject(o, false, false);
223224
Object id = o.get("_id");
@@ -231,12 +232,12 @@ protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.W
231232

232233
int cur = 0;
233234
int maxsize = _mongo.getMaxBsonObjectSize();
234-
while ( cur < arr.length ) {
235+
while ( cur < list.size() ) {
235236

236237
OutMessage om = OutMessage.insert( this , encoder, concern );
237238

238-
for ( ; cur<arr.length; cur++ ){
239-
DBObject o = arr[cur];
239+
for ( ; cur < list.size(); cur++ ){
240+
DBObject o = list.get(cur);
240241
om.putObject( o );
241242

242243
// limit for batch insert is 4 x maxbson on server, use 2 x to be safe
@@ -341,7 +342,7 @@ public void createIndex( final DBObject keys, final DBObject options, DBEncoder
341342
MyCollection idxs = DBApiLayer.this.doGetCollection( "system.indexes" );
342343
//query first, maybe we should do an update w/upsert? -- need to test performance and lock behavior
343344
if ( idxs.findOne( full ) == null )
344-
idxs.insert( new DBObject[] { full }, false, WriteConcern.SAFE, encoder );
345+
idxs.insert(Arrays.asList(full), false, WriteConcern.SAFE, encoder);
345346
}
346347

347348
final String _fullNameSpace;

src/main/com/mongodb/DBCollection.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package com.mongodb;
2020

2121
// Mongo
22+
2223
import org.bson.types.ObjectId;
2324

2425
import java.util.ArrayList;
26+
import java.util.Arrays;
2527
import java.util.Collections;
2628
import java.util.HashMap;
2729
import java.util.HashSet;
@@ -69,7 +71,9 @@ public WriteResult insert(DBObject[] arr , WriteConcern concern ){
6971
* @throws MongoException
7072
* @dochub insert
7173
*/
72-
public abstract WriteResult insert(DBObject[] arr , WriteConcern concern, DBEncoder encoder);
74+
public WriteResult insert(DBObject[] arr , WriteConcern concern, DBEncoder encoder) {
75+
return insert(Arrays.asList(arr), concern, encoder);
76+
}
7377

7478
/**
7579
* Inserts a document into the database.
@@ -83,7 +87,7 @@ public WriteResult insert(DBObject[] arr , WriteConcern concern ){
8387
* @dochub insert
8488
*/
8589
public WriteResult insert(DBObject o , WriteConcern concern ){
86-
return insert( new DBObject[]{ o } , concern );
90+
return insert( Arrays.asList(o) , concern );
8791
}
8892

8993
/**
@@ -140,9 +144,22 @@ public WriteResult insert(List<DBObject> list ){
140144
* @dochub insert
141145
*/
142146
public WriteResult insert(List<DBObject> list, WriteConcern concern ){
143-
return insert( list.toArray( new DBObject[list.size()] ) , concern );
147+
return insert(list, concern, getDBEncoder() );
144148
}
145149

150+
/**
151+
* Saves document(s) to the database.
152+
* if doc doesn't have an _id, one will be added
153+
* you can get the _id that was added from doc after the insert
154+
*
155+
* @param list list of documents to save
156+
* @param concern the write concern
157+
* @return
158+
* @throws MongoException
159+
* @dochub insert
160+
*/
161+
public abstract WriteResult insert(List<DBObject> list, WriteConcern concern, DBEncoder encoder);
162+
146163
/**
147164
* Performs an update operation.
148165
* @param q search query for old object to update

0 commit comments

Comments
 (0)