Skip to content

Commit 2869154

Browse files
committed
JAVA-1128: Don't apply key checking for inserts into system.indexes. Ensure that key checking and _id creation occurs for all insert paths
1 parent 186b736 commit 2869154

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,18 @@ protected WriteResult insert(List<DBObject> list, boolean shouldApply , WriteCon
173173
}
174174
}
175175

176-
if ( shouldApply ){
177-
for (DBObject o : list) {
178-
apply(o);
179-
_checkObject(o, false, false);
180-
Object id = o.get("_id");
181-
if (id instanceof ObjectId) {
182-
((ObjectId) id).notNew();
183-
}
184-
}
185-
}
186-
187176
DBPort port = db.getConnector().getPrimaryPort();
188177
try {
189178
if (useWriteCommands(concern, port)) {
190179
try {
191-
return translateBulkWriteResult(insertWithCommandProtocol(list, concern, encoder, port), INSERT, concern,
180+
return translateBulkWriteResult(insertWithCommandProtocol(list, concern, encoder, port, shouldApply), INSERT, concern,
192181
port.getAddress());
193182
} catch (BulkWriteException e) {
194183
throw translateBulkWriteException(e, INSERT);
195184
}
196185
}
197186
else {
198-
return insertWithWriteProtocol(list, concern, encoder, port);
187+
return insertWithWriteProtocol(list, concern, encoder, port, shouldApply);
199188
}
200189
} finally {
201190
db.getConnector().releasePort(port);
@@ -365,7 +354,7 @@ public void createIndex(final DBObject keys, final DBObject options, DBEncoder e
365354
}
366355
} else {
367356
db.doGetCollection("system.indexes").insertWithWriteProtocol(asList(index), WriteConcern.SAFE,
368-
DefaultDBEncoder.FACTORY.create(), port);
357+
DefaultDBEncoder.FACTORY.create(), port, false);
369358
}
370359
} catch (IOException e) {
371360
throw new MongoException.Network("Operation on server " + port.getAddress() + " failed", e);
@@ -376,9 +365,9 @@ public void createIndex(final DBObject keys, final DBObject options, DBEncoder e
376365

377366
private BulkWriteResult insertWithCommandProtocol(final List<DBObject> list, final WriteConcern writeConcern,
378367
final DBEncoder encoder,
379-
final DBPort port) {
380-
for (DBObject o : list) {
381-
_checkObject(o, false, false);
368+
final DBPort port, final boolean shouldApply) {
369+
if ( shouldApply ){
370+
applyRulesForInsert(list);
382371
}
383372

384373
BaseWriteCommandMessage message = new InsertCommandMessage(getNamespace(), writeConcern, list,
@@ -387,6 +376,17 @@ private BulkWriteResult insertWithCommandProtocol(final List<DBObject> list, fin
387376
return writeWithCommandProtocol(port, INSERT, message, writeConcern);
388377
}
389378

379+
private void applyRulesForInsert(final List<DBObject> list) {
380+
for (DBObject o : list) {
381+
_checkObject(o, false, false);
382+
apply(o);
383+
Object id = o.get("_id");
384+
if (id instanceof ObjectId) {
385+
((ObjectId) id).notNew();
386+
}
387+
}
388+
}
389+
390390
private BulkWriteResult removeWithCommandProtocol(final List<RemoveRequest> removeList,
391391
final WriteConcern writeConcern,
392392
final DBEncoder encoder, final DBPort port) {
@@ -486,9 +486,9 @@ public CommandResult execute() throws IOException {
486486

487487

488488
private WriteResult insertWithWriteProtocol(final List<DBObject> list, final WriteConcern concern, final DBEncoder encoder,
489-
final DBPort port) {
490-
for (DBObject o : list) {
491-
_checkObject(o, false, false);
489+
final DBPort port, final boolean shouldApply) {
490+
if ( shouldApply ){
491+
applyRulesForInsert(list);
492492
}
493493

494494
WriteResult last = null;
@@ -792,7 +792,7 @@ BulkWriteResult executeWriteCommandProtocol() {
792792
for (InsertRequest cur : insertRequests) {
793793
documents.add(cur.getDocument());
794794
}
795-
return insertWithCommandProtocol(documents, writeConcern, encoder, port);
795+
return insertWithCommandProtocol(documents, writeConcern, encoder, port, true);
796796
}
797797

798798
@Override

src/test/com/mongodb/BulkWriteOperationSpecification.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ class BulkWriteOperationSpecification extends FunctionalSpecification {
7878
ordered << [true, false]
7979
}
8080

81+
def 'when a document with no _id is inserted, the _id should be generated by the driver'() {
82+
given:
83+
def operation = initializeBulkOperation(ordered)
84+
def document = new BasicDBObject()
85+
operation.insert(document)
86+
87+
when:
88+
def result = operation.execute()
89+
90+
then:
91+
result == new AcknowledgedBulkWriteResult(INSERT, 1, [])
92+
document._id instanceof ObjectId
93+
collection.findOne() == document
94+
95+
where:
96+
ordered << [true, false]
97+
}
98+
8199
def 'when documents match the query, a remove of one should remove one of them'() {
82100
given:
83101
collection.insert(new BasicDBObject('x', true))

0 commit comments

Comments
 (0)