feat(mongodb): support bulkWrite
This commit is contained in:
1 parent
3f3c21aae9
commit
f78dc1ba16
25 files changed
+1089
-18
No files matched your search
@@ -28,10 +28,21 @@ import com.mongodb.client.model.CollationAlternate;
|
||||
import com.mongodb.client.model.CollationCaseFirst;
|
||||
import com.mongodb.client.model.CollationMaxVariable;
|
||||
import com.mongodb.client.model.CollationStrength;
|
||||
import com.mongodb.MongoBulkWriteException;
|
||||
import com.mongodb.bulk.BulkWriteError;
|
||||
import com.mongodb.bulk.BulkWriteResult;
|
||||
import com.mongodb.client.model.BulkWriteOptions;
|
||||
import com.mongodb.client.model.CountOptions;
|
||||
import com.mongodb.client.model.DeleteManyModel;
|
||||
import com.mongodb.client.model.DeleteOneModel;
|
||||
import com.mongodb.client.model.InsertOneModel;
|
||||
import com.mongodb.client.model.InsertManyOptions;
|
||||
import com.mongodb.client.model.ReplaceOneModel;
|
||||
import com.mongodb.client.model.ReplaceOptions;
|
||||
import com.mongodb.client.model.UpdateManyModel;
|
||||
import com.mongodb.client.model.UpdateOneModel;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import com.mongodb.client.model.WriteModel;
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
@@ -1884,6 +1895,168 @@ public final class MongoAgent {
|
||||
return Collections.singletonMap("modified_count", result.getModifiedCount());
|
||||
}
|
||||
|
||||
private static Object bulkWrite(JsonObject params) {
|
||||
MongoClient c = requireClient();
|
||||
String database = params.get("database").getAsString();
|
||||
String collection = params.get("collection").getAsString();
|
||||
String operationsJson = params.get("operations_json").getAsString();
|
||||
String optionsJson = params.has("options_json") && !params.get("options_json").isJsonNull()
|
||||
? params.get("options_json").getAsString()
|
||||
: null;
|
||||
|
||||
var col = c.getDatabase(database).getCollection(collection);
|
||||
List<WriteModel<Document>> models = bulkWriteModelsForWrite(operationsJson);
|
||||
BulkWriteOptions options = bulkWriteOptionsForWrite(optionsJson);
|
||||
try {
|
||||
return bulkWriteCounts(col.bulkWrite(models, options));
|
||||
} catch (MongoBulkWriteException error) {
|
||||
// Report what did complete alongside the failures, as the native driver does.
|
||||
StringBuilder failures = new StringBuilder();
|
||||
for (BulkWriteError writeError : error.getWriteErrors()) {
|
||||
if (failures.length() > 0) {
|
||||
failures.append("; ");
|
||||
}
|
||||
failures.append("operation ").append(writeError.getIndex() + 1).append(": ").append(writeError.getMessage());
|
||||
}
|
||||
Map<String, Object> completed = bulkWriteCounts(error.getWriteResult());
|
||||
throw new IllegalArgumentException(
|
||||
"bulkWrite " + (options.isOrdered() ? "stopped at " : "finished with failed ")
|
||||
+ failures + ". Completed: inserted " + completed.get("inserted_count")
|
||||
+ ", matched " + completed.get("matched_count") + ", modified " + completed.get("modified_count")
|
||||
+ ", deleted " + completed.get("deleted_count") + ", upserted " + completed.get("upserted_count"));
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> bulkWriteCounts(BulkWriteResult result) {
|
||||
Map<String, Object> counts = new LinkedHashMap<>();
|
||||
counts.put("inserted_count", result.getInsertedCount());
|
||||
counts.put("matched_count", result.getMatchedCount());
|
||||
counts.put("modified_count", result.getModifiedCount());
|
||||
counts.put("deleted_count", result.getDeletedCount());
|
||||
counts.put("upserted_count", result.getUpserts().size());
|
||||
return counts;
|
||||
}
|
||||
|
||||
/** The shell's `bulkWrite([...])` entries, each `{ <op>: { ... } }`, as driver write models. */
|
||||
static List<WriteModel<Document>> bulkWriteModelsForWrite(String operationsJson) {
|
||||
JsonElement parsed = JsonParser.parseString(operationsJson);
|
||||
if (!parsed.isJsonArray() || parsed.getAsJsonArray().isEmpty()) {
|
||||
throw new IllegalArgumentException("bulkWrite requires a non-empty array of operations");
|
||||
}
|
||||
List<WriteModel<Document>> models = new ArrayList<>();
|
||||
int position = 0;
|
||||
for (JsonElement entry : parsed.getAsJsonArray()) {
|
||||
position++;
|
||||
if (!entry.isJsonObject() || entry.getAsJsonObject().size() != 1) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " must have exactly one operation key");
|
||||
}
|
||||
String kind = entry.getAsJsonObject().keySet().iterator().next();
|
||||
JsonElement rawSpec = entry.getAsJsonObject().get(kind);
|
||||
if (!rawSpec.isJsonObject()) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") must be a document");
|
||||
}
|
||||
Document spec = documentForWrite(rawSpec.toString());
|
||||
models.add(bulkWriteModel(position, kind, spec));
|
||||
}
|
||||
return models;
|
||||
}
|
||||
|
||||
private static WriteModel<Document> bulkWriteModel(int position, String kind, Document spec) {
|
||||
switch (kind) {
|
||||
case "insertOne":
|
||||
return new InsertOneModel<>(requireBulkField(position, kind, spec, "document"));
|
||||
case "updateOne":
|
||||
case "updateMany": {
|
||||
Document filter = requireBulkField(position, kind, spec, "filter");
|
||||
Object rawUpdate = spec.get("update");
|
||||
UpdateOptions options = new UpdateOptions();
|
||||
if (spec.containsKey("upsert")) {
|
||||
options.upsert(requireBulkBoolean(position, kind, spec, "upsert"));
|
||||
}
|
||||
if (spec.get("arrayFilters") instanceof List<?> filters) {
|
||||
List<Document> arrayFilters = new ArrayList<>();
|
||||
for (Object filterEntry : filters) {
|
||||
if (!(filterEntry instanceof Document)) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " arrayFilters entries must be documents");
|
||||
}
|
||||
arrayFilters.add((Document) filterEntry);
|
||||
}
|
||||
options.arrayFilters(arrayFilters);
|
||||
}
|
||||
if (rawUpdate instanceof List<?> stages) {
|
||||
List<Document> pipeline = new ArrayList<>();
|
||||
for (Object stage : stages) {
|
||||
if (!(stage instanceof Document)) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") pipeline stages must be documents");
|
||||
}
|
||||
pipeline.add((Document) stage);
|
||||
}
|
||||
if (pipeline.isEmpty()) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") update pipeline must not be empty");
|
||||
}
|
||||
return kind.equals("updateOne") ? new UpdateOneModel<>(filter, pipeline, options) : new UpdateManyModel<>(filter, pipeline, options);
|
||||
}
|
||||
if (!(rawUpdate instanceof Document update)) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") requires an update document or pipeline");
|
||||
}
|
||||
requireBulkUpdateOperatorDocument(update);
|
||||
return kind.equals("updateOne") ? new UpdateOneModel<>(filter, update, options) : new UpdateManyModel<>(filter, update, options);
|
||||
}
|
||||
case "replaceOne": {
|
||||
Document replacement = requireBulkField(position, kind, spec, "replacement");
|
||||
requireReplacementDocument(replacement);
|
||||
ReplaceOptions options = new ReplaceOptions();
|
||||
if (spec.containsKey("upsert")) {
|
||||
options.upsert(requireBulkBoolean(position, kind, spec, "upsert"));
|
||||
}
|
||||
return new ReplaceOneModel<>(requireBulkField(position, kind, spec, "filter"), replacement, options);
|
||||
}
|
||||
case "deleteOne":
|
||||
return new DeleteOneModel<>(requireBulkField(position, kind, spec, "filter"));
|
||||
case "deleteMany":
|
||||
return new DeleteManyModel<>(requireBulkField(position, kind, spec, "filter"));
|
||||
default:
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " uses unsupported operation " + kind);
|
||||
}
|
||||
}
|
||||
|
||||
private static Document requireBulkField(int position, String kind, Document spec, String field) {
|
||||
Object value = spec.get(field);
|
||||
if (!(value instanceof Document)) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") requires a " + field + " document");
|
||||
}
|
||||
return (Document) value;
|
||||
}
|
||||
|
||||
private static boolean requireBulkBoolean(int position, String kind, Document spec, String field) {
|
||||
Object value = spec.get(field);
|
||||
if (!(value instanceof Boolean)) {
|
||||
throw new IllegalArgumentException("bulkWrite operation " + position + " (" + kind + ") " + field + " must be a boolean");
|
||||
}
|
||||
return (Boolean) value;
|
||||
}
|
||||
|
||||
static BulkWriteOptions bulkWriteOptionsForWrite(String optionsJson) {
|
||||
BulkWriteOptions result = new BulkWriteOptions();
|
||||
if (optionsJson == null || optionsJson.trim().isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
Document options = Document.parse(optionsJson);
|
||||
for (String key : options.keySet()) {
|
||||
if (!"ordered".equals(key)) {
|
||||
throw new IllegalArgumentException("Unsupported bulkWrite option: " + key);
|
||||
}
|
||||
}
|
||||
Object ordered = options.get("ordered");
|
||||
if (ordered != null) {
|
||||
if (!(ordered instanceof Boolean)) {
|
||||
throw new IllegalArgumentException("ordered must be a boolean");
|
||||
}
|
||||
result.ordered((Boolean) ordered);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** replaceOne swaps the whole document; update operators here mean updateOne was intended. */
|
||||
static void requireReplacementDocument(Document doc) {
|
||||
for (String key : doc.keySet()) {
|
||||
@@ -2106,6 +2279,7 @@ public final class MongoAgent {
|
||||
case AgentProtocol.MONGO_METHOD_UPDATE_DOCUMENT -> updateDocument(params);
|
||||
case AgentProtocol.MONGO_METHOD_UPDATE_DOCUMENTS -> updateDocuments(params);
|
||||
case AgentProtocol.MONGO_METHOD_REPLACE_DOCUMENT -> replaceDocument(params);
|
||||
case AgentProtocol.MONGO_METHOD_BULK_WRITE -> bulkWrite(params);
|
||||
case AgentProtocol.MONGO_METHOD_DELETE_DOCUMENT -> deleteDocument(params);
|
||||
case AgentProtocol.MONGO_METHOD_DELETE_DOCUMENTS -> deleteDocuments(params);
|
||||
case AgentProtocol.MONGO_METHOD_RUN_COMMAND -> runCommand(params);
|
||||
|
||||
@@ -32,6 +32,13 @@ import com.mongodb.client.model.CollationStrength;
|
||||
import com.mongodb.client.model.CountOptions;
|
||||
import com.mongodb.client.model.InsertManyOptions;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import com.mongodb.client.model.DeleteManyModel;
|
||||
import com.mongodb.client.model.DeleteOneModel;
|
||||
import com.mongodb.client.model.InsertOneModel;
|
||||
import com.mongodb.client.model.ReplaceOneModel;
|
||||
import com.mongodb.client.model.UpdateManyModel;
|
||||
import com.mongodb.client.model.UpdateOneModel;
|
||||
import com.mongodb.client.model.WriteModel;
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
import java.io.FileInputStream;
|
||||
import java.lang.reflect.Proxy;
|
||||
@@ -1329,6 +1336,68 @@ class MongoAgentTest {
|
||||
assertTrue(AgentProtocol.MONGO_LEGACY_CAPABILITIES.contains(AgentProtocol.CAPABILITY_MONGO_REPLACE_DOCUMENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bulkWriteMethodIsRecognizedOverJsonRpc() {
|
||||
String response = MongoAgent.handleRequest(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":12,\"method\":\"bulk_write\","
|
||||
+ "\"params\":{\"database\":\"app\",\"collection\":\"orders\","
|
||||
+ "\"operations_json\":\"[{\\\"insertOne\\\":{\\\"document\\\":{\\\"a\\\":1}}}]\"}}");
|
||||
|
||||
JsonObject json = JsonParser.parseString(response).getAsJsonObject();
|
||||
assertEquals(12, json.get("id").getAsInt());
|
||||
assertEquals("Not connected", json.getAsJsonObject("error").get("message").getAsString());
|
||||
assertTrue(AgentProtocol.MONGO_LEGACY_METHODS.contains(AgentProtocol.MONGO_METHOD_BULK_WRITE));
|
||||
assertTrue(AgentProtocol.MONGO_LEGACY_CAPABILITIES.contains(AgentProtocol.CAPABILITY_MONGO_BULK_WRITE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildsBulkWriteModelsForEveryOperationKind() {
|
||||
List<WriteModel<Document>> models = MongoAgent.bulkWriteModelsForWrite(
|
||||
"[{\"insertOne\":{\"document\":{\"sku\":\"A1\"}}},"
|
||||
+ "{\"updateOne\":{\"filter\":{\"sku\":\"A1\"},\"update\":{\"$inc\":{\"stock\":1}},\"upsert\":true}},"
|
||||
+ "{\"updateMany\":{\"filter\":{},\"update\":[{\"$set\":{\"stock\":0}}]}},"
|
||||
+ "{\"replaceOne\":{\"filter\":{\"sku\":\"B2\"},\"replacement\":{\"sku\":\"B2\"}}},"
|
||||
+ "{\"deleteOne\":{\"filter\":{\"sku\":\"C3\"}}},"
|
||||
+ "{\"deleteMany\":{\"filter\":{\"stock\":{\"$lt\":0}}}}]");
|
||||
|
||||
assertEquals(6, models.size());
|
||||
assertTrue(models.get(0) instanceof InsertOneModel);
|
||||
assertTrue(models.get(1) instanceof UpdateOneModel);
|
||||
assertTrue(((UpdateOneModel<Document>) models.get(1)).getOptions().isUpsert());
|
||||
assertTrue(models.get(2) instanceof UpdateManyModel);
|
||||
assertNotNull(((UpdateManyModel<Document>) models.get(2)).getUpdatePipeline());
|
||||
assertTrue(models.get(3) instanceof ReplaceOneModel);
|
||||
assertTrue(models.get(4) instanceof DeleteOneModel);
|
||||
assertTrue(models.get(5) instanceof DeleteManyModel);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedBulkWriteOperations() {
|
||||
for (String[] item : new String[][] {
|
||||
{"[]", "non-empty array"},
|
||||
{"[{\"insertOne\":{},\"deleteOne\":{}}]", "exactly one operation key"},
|
||||
{"[{\"upsertOne\":{\"document\":{}}}]", "unsupported operation upsertOne"},
|
||||
{"[{\"insertOne\":{}}]", "requires a document document"},
|
||||
{"[{\"updateOne\":{\"filter\":{},\"update\":{\"a\":1}}}]", "update operators such as $set"},
|
||||
{"[{\"updateOne\":{\"filter\":{},\"update\":{\"$set\":{\"a\":1}},\"upsert\":1}}]", "upsert must be a boolean"},
|
||||
{"[{\"replaceOne\":{\"filter\":{},\"replacement\":{\"$set\":{\"a\":1}}}}]", "must not contain update operators"},
|
||||
}) {
|
||||
IllegalArgumentException error = assertThrows(IllegalArgumentException.class, () -> MongoAgent.bulkWriteModelsForWrite(item[0]), item[0]);
|
||||
assertTrue(error.getMessage().contains(item[1]), item[0] + " -> " + error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesBulkWriteOptions() {
|
||||
assertTrue(MongoAgent.bulkWriteOptionsForWrite(null).isOrdered());
|
||||
assertTrue(MongoAgent.bulkWriteOptionsForWrite("{\"ordered\":true}").isOrdered());
|
||||
assertFalse(MongoAgent.bulkWriteOptionsForWrite("{\"ordered\":false}").isOrdered());
|
||||
IllegalArgumentException unsupported = assertThrows(IllegalArgumentException.class, () -> MongoAgent.bulkWriteOptionsForWrite("{\"writeConcern\":{}}"));
|
||||
assertEquals("Unsupported bulkWrite option: writeConcern", unsupported.getMessage());
|
||||
IllegalArgumentException notBoolean = assertThrows(IllegalArgumentException.class, () -> MongoAgent.bulkWriteOptionsForWrite("{\"ordered\":\"yes\"}"));
|
||||
assertEquals("ordered must be a boolean", notBoolean.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesReplaceOptions() {
|
||||
assertTrue(MongoAgent.replaceOptionsForWrite("{\"upsert\":true}").isUpsert());
|
||||
|
||||
Reference in new issue
Block a user