Skip to content

Commit

Permalink
Use deepEncodeKeyWhenUseObjectId only in query or filter
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchong committed Oct 12, 2023
1 parent aca97d7 commit 931355c
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public Future<Void> close() {
requireNonNull(options, OPTIONS_CANNOT_BE_NULL);

MongoCollection<JsonObject> coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
Bson bupdate = wrap(encodeKeyWhenUseObjectId(generateIdIfNeeded(query, update, options)));

com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions().upsert(options.isUpsert());
Expand Down Expand Up @@ -248,7 +248,7 @@ public Future<Void> close() {
requireNonNull(options, OPTIONS_CANNOT_BE_NULL);

MongoCollection<JsonObject> coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
List<Bson> bpipeline = new ArrayList<>(pipeline.size());
for (int i=0 ; i<pipeline.size() ; i++) {
bpipeline.add(wrap(pipeline.getJsonObject(i)));
Expand Down Expand Up @@ -307,7 +307,7 @@ private JsonObject generateIdIfNeeded(JsonObject query, JsonObject update, Updat
requireNonNull(options, OPTIONS_CANNOT_BE_NULL);

MongoCollection<JsonObject> coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
com.mongodb.client.model.ReplaceOptions replaceOptions = new com.mongodb.client.model.ReplaceOptions().upsert(options.isUpsert());
if (options.getHint() != null) {
replaceOptions.hint(wrap(options.getHint()));
Expand All @@ -334,7 +334,7 @@ public Future<List<JsonObject>> findWithOptions(String collection, JsonObject qu
requireNonNull(query, QUERY_CANNOT_BE_NULL);

Promise<List<JsonObject>> promise = vertx.promise();
doFind(collection, encodeKeyWhenUseObjectId(query), options)
doFind(collection, deepEncodeKeyWhenUseObjectId(query), options)
.subscribe(new MappingAndBufferingSubscriber<>(this::decodeKeyWhenUseObjectId, promise));
return promise.future();
}
Expand All @@ -357,7 +357,7 @@ public ReadStream<JsonObject> findBatchWithOptions(String collection, JsonObject
requireNonNull(collection, COLLECTION_CANNOT_BE_NULL);
requireNonNull(query, QUERY_CANNOT_BE_NULL);

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
JsonObject encodedQuery = deepEncodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);
Bson bfields = wrap(fields);
Expand All @@ -379,7 +379,7 @@ public ReadStream<JsonObject> findBatchWithOptions(String collection, JsonObject
requireNonNull(findOptions, FIND_OPTIONS_CANNOT_BE_NULL);
requireNonNull(updateOptions, "update options cannot be null");

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
JsonObject encodedQuery = deepEncodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);
Bson bupdate = wrap(update);
Expand Down Expand Up @@ -430,7 +430,7 @@ public ReadStream<JsonObject> findBatchWithOptions(String collection, JsonObject
requireNonNull(findOptions, FIND_OPTIONS_CANNOT_BE_NULL);
requireNonNull(updateOptions, "update options cannot be null");

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
JsonObject encodedQuery = deepEncodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);
FindOneAndReplaceOptions foarOptions = new FindOneAndReplaceOptions();
Expand Down Expand Up @@ -472,7 +472,7 @@ public ReadStream<JsonObject> findBatchWithOptions(String collection, JsonObject
requireNonNull(query, QUERY_CANNOT_BE_NULL);
requireNonNull(findOptions, FIND_OPTIONS_CANNOT_BE_NULL);

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
JsonObject encodedQuery = deepEncodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);
FindOneAndDeleteOptions foadOptions = new FindOneAndDeleteOptions();
Expand Down Expand Up @@ -506,7 +506,7 @@ public Future<Long> countWithOptions(String collection, JsonObject query, CountO
requireNonNull(collection, COLLECTION_CANNOT_BE_NULL);
requireNonNull(query, QUERY_CANNOT_BE_NULL);

Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
MongoCollection<JsonObject> coll = getCollection(collection);
Promise<Long> promise = vertx.promise();
Publisher<Long> countPublisher = countOptions != null
Expand All @@ -527,7 +527,7 @@ public Future<Long> countWithOptions(String collection, JsonObject query, CountO
requireNonNull(query, QUERY_CANNOT_BE_NULL);

MongoCollection<JsonObject> coll = getCollection(collection, writeOption);
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
Promise<DeleteResult> promise = vertx.promise();
coll.deleteMany(bquery).subscribe(new SingleResultSubscriber<>(promise));
return promise.future().map(Utils::toMongoClientDeleteResult);
Expand All @@ -544,7 +544,7 @@ public Future<Long> countWithOptions(String collection, JsonObject query, CountO
requireNonNull(query, QUERY_CANNOT_BE_NULL);

MongoCollection<JsonObject> coll = getCollection(collection, writeOption);
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
Promise<DeleteResult> promise = vertx.promise();
coll.deleteOne(bquery).subscribe(new SingleResultSubscriber<>(promise));
return promise.future().map(Utils::toMongoClientDeleteResult);
Expand Down Expand Up @@ -573,7 +573,7 @@ private List<WriteModel<JsonObject>> convertBulkOperations(List<BulkOperation> o
for (BulkOperation bulkOperation : operations) {
switch (bulkOperation.getType()) {
case DELETE:
Bson bsonFilter = toBson(encodeKeyWhenUseObjectId(bulkOperation.getFilter()));
Bson bsonFilter = toBson(deepEncodeKeyWhenUseObjectId(bulkOperation.getFilter()));
DeleteOptions deleteOptions = new DeleteOptions();
if (bulkOperation.getHint() != null) {
deleteOptions.hint(toBson(bulkOperation.getHint()));
Expand All @@ -591,7 +591,7 @@ private List<WriteModel<JsonObject>> convertBulkOperations(List<BulkOperation> o
}
break;
case INSERT:
result.add(new InsertOneModel<>(encodeKeyWhenUseObjectId(bulkOperation.getDocument())));
result.add(new InsertOneModel<>(deepEncodeKeyWhenUseObjectId(bulkOperation.getDocument())));
break;
case REPLACE:
ReplaceOptions replaceOptions = new ReplaceOptions();
Expand All @@ -604,11 +604,11 @@ private List<WriteModel<JsonObject>> convertBulkOperations(List<BulkOperation> o
if (bulkOperation.getHintString() != null && !bulkOperation.getHintString().isEmpty()) {
replaceOptions.hintString(bulkOperation.getHintString());
}
result.add(new ReplaceOneModel<>(toBson(encodeKeyWhenUseObjectId(bulkOperation.getFilter())), bulkOperation.getDocument(),
result.add(new ReplaceOneModel<>(toBson(deepEncodeKeyWhenUseObjectId(bulkOperation.getFilter())), bulkOperation.getDocument(),
replaceOptions.upsert(bulkOperation.isUpsert())));
break;
case UPDATE:
Bson filter = toBson(encodeKeyWhenUseObjectId(bulkOperation.getFilter()));
Bson filter = toBson(deepEncodeKeyWhenUseObjectId(bulkOperation.getFilter()));
Bson document = toBson(encodeKeyWhenUseObjectId(bulkOperation.getDocument()));
com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions()
.upsert(bulkOperation.isUpsert());
Expand Down Expand Up @@ -874,7 +874,7 @@ private DistinctPublisher<?> findDistinctValuesWithQuery(String collection, Stri
requireNonNull(fieldName, FIELD_NAME_CANNOT_BE_NULL);
requireNonNull(query, QUERY_CANNOT_BE_NULL);

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
JsonObject encodedQuery = deepEncodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);

Expand Down Expand Up @@ -907,16 +907,18 @@ private AggregatePublisher<JsonObject> doAggregate(final String collection, fina
return aggregate;
}

JsonArray encodeKeyWhenUseObjectId(JsonArray arr) {


JsonArray deepEncodeKeyWhenUseObjectId(JsonArray arr) {
if(!useObjectId) return arr;

JsonArray newArr = new JsonArray(new ArrayList<>(arr.size()));

for (Object item : arr) {
if (item instanceof JsonArray) {
newArr.add(encodeKeyWhenUseObjectId((JsonArray) item));
newArr.add(deepEncodeKeyWhenUseObjectId((JsonArray) item));
} else if (item instanceof JsonObject) {
newArr.add(encodeKeyWhenUseObjectId((JsonObject) item));
newArr.add(deepEncodeKeyWhenUseObjectId((JsonObject) item));
} else {
newArr.add(item);
}
Expand All @@ -926,7 +928,7 @@ JsonArray encodeKeyWhenUseObjectId(JsonArray arr) {
return newArr;
}

JsonObject encodeKeyWhenUseObjectId(JsonObject json) {
JsonObject deepEncodeKeyWhenUseObjectId(JsonObject json) {
if(!useObjectId) return json;

JsonObject newJson = new JsonObject(new LinkedHashMap<>(json.size()));
Expand All @@ -939,9 +941,9 @@ JsonObject encodeKeyWhenUseObjectId(JsonObject json) {
&& ObjectId.isValid((String) value)) {
newJson.put(key, new JsonObject().put(JsonObjectCodec.OID_FIELD, value));
} else if (value instanceof JsonObject) {
newJson.put(key, encodeKeyWhenUseObjectId((JsonObject) value));
newJson.put(key, deepEncodeKeyWhenUseObjectId((JsonObject) value));
} else if (value instanceof JsonArray) {
newJson.put(key, encodeKeyWhenUseObjectId((JsonArray) value));
newJson.put(key, deepEncodeKeyWhenUseObjectId((JsonArray) value));
} else {
newJson.put(key, value);
}
Expand All @@ -951,6 +953,18 @@ JsonObject encodeKeyWhenUseObjectId(JsonObject json) {
return newJson;
}

JsonObject encodeKeyWhenUseObjectId(JsonObject json) {
if (!useObjectId)
return json;

Object idString = json.getValue(ID_FIELD, null);
if (idString instanceof String && ObjectId.isValid((String) idString)) {
json.put(ID_FIELD, new JsonObject().put(JsonObjectCodec.OID_FIELD, idString));
}

return json;
}

private JsonObject decodeKeyWhenUseObjectId(JsonObject json) {
if (!useObjectId) return json;

Expand All @@ -967,7 +981,7 @@ private JsonObject decodeKeyWhenUseObjectId(JsonObject json) {

private FindPublisher<JsonObject> doFind(String collection, JsonObject query, FindOptions options) {
MongoCollection<JsonObject> coll = getCollection(collection);
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
Bson bquery = wrap(deepEncodeKeyWhenUseObjectId(query));
FindPublisher<JsonObject> find = coll.find(bquery, JsonObject.class);
if (options.getLimit() != -1) {
find.limit(options.getLimit());
Expand Down

0 comments on commit 931355c

Please sign in to comment.