You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "exceptionfactory (via GitHub)" <gi...@apache.org> on 2024/07/09 14:08:17 UTC

Re: [PR] NIFI-12224: Add support for updateMany operation with operators enabled option in `PutMongo` processor [nifi]

exceptionfactory commented on code in PR #8610:
URL: https://github.com/apache/nifi/pull/8610#discussion_r1670587873


##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/AbstractMongoProcessor.java:
##########
@@ -159,11 +161,42 @@ public abstract class AbstractMongoProcessor extends AbstractProcessor {
     static final List<PropertyDescriptor> descriptors;
 
     static {
-        List<PropertyDescriptor> _temp = new ArrayList<>();
-        _temp.add(CLIENT_SERVICE);
-        _temp.add(DATABASE_NAME);
-        _temp.add(COLLECTION_NAME);
-        descriptors = Collections.unmodifiableList(_temp);
+      descriptors = List.of(CLIENT_SERVICE, DATABASE_NAME, COLLECTION_NAME);
+    }
+
+    public enum MongoUpdateOption implements DescribedValue {

Review Comment:
   As this is an `enum` under the Processor, I believe the name can be shortened. I recommend using `UpdateMethod` to match the new property name, even though the property is still named `Update Mode` in the `PutMongoRecord` Processor.
   ```suggestion
       public enum UpdateMethod implements DescribedValue {
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongoRecord.java:
##########
@@ -159,10 +152,7 @@ public class PutMongoRecord extends AbstractMongoProcessor {
         _propertyDescriptors.add(UPDATE_MODE);
         propertyDescriptors = Collections.unmodifiableList(_propertyDescriptors);
 
-        final Set<Relationship> _relationships = new HashSet<>();
-        _relationships.add(REL_SUCCESS);
-        _relationships.add(REL_FAILURE);
-        relationships = Collections.unmodifiableSet(_relationships);
+      relationships = Set.of(REL_SUCCESS, REL_FAILURE);

Review Comment:
   This looks like it needs two extra spaces for indentation.
   ```suggestion
           relationships = Set.of(REL_SUCCESS, REL_FAILURE);
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java:
##########
@@ -82,39 +95,50 @@ public class PutMongo extends AbstractMongoProcessor {
         .description("When true, inserts a document if no document matches the update query criteria; this property is valid only when using update mode, "
                 + "otherwise it is ignored")
         .required(true)
+        .dependsOn(MODE, MODE_UPDATE)
         .allowableValues("true", "false")
         .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
         .defaultValue("false")
         .build();
     static final PropertyDescriptor UPDATE_QUERY_KEY = new PropertyDescriptor.Builder()
         .name("Update Query Key")
-        .description("Key name used to build the update query criteria; this property is valid only when using update mode, "
-                + "otherwise it is ignored. Example: _id")
+        .description("Comma separated key names used to build the update query criteria. Their values are taken from incoming flowfile. Example: _id")

Review Comment:
   The reference to the FlowFile is optional, so the description should be updated.
   ```suggestion
           .description("One or more comma-separated document key names used to build the update query criteria, such as _id")
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java:
##########
@@ -183,16 +204,16 @@ protected Collection<ValidationResult> customValidate(final ValidationContext va
 
     @Override
     public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
-        final FlowFile flowFile = session.get();
+        FlowFile flowFile = session.get();
         if (flowFile == null) {
             return;
         }
 
         final ComponentLog logger = getLogger();
 
         final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
-        final String mode = context.getProperty(MODE).getValue();
-        final String updateMode = context.getProperty(UPDATE_MODE).getValue();
+        final String processorMode = context.getProperty(MODE).getValue();
+        final String flowfileType = context.getProperty(UPDATE_OPERATION_MODE).getValue();

Review Comment:
   The `flowfileType` is not descriptive of the property value, so recommend `updateOperationMode`.
   ```suggestion
           final String updateOperationMode = context.getProperty(UPDATE_OPERATION_MODE).getValue();
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java:
##########
@@ -82,39 +95,50 @@ public class PutMongo extends AbstractMongoProcessor {
         .description("When true, inserts a document if no document matches the update query criteria; this property is valid only when using update mode, "
                 + "otherwise it is ignored")
         .required(true)
+        .dependsOn(MODE, MODE_UPDATE)
         .allowableValues("true", "false")
         .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
         .defaultValue("false")
         .build();
     static final PropertyDescriptor UPDATE_QUERY_KEY = new PropertyDescriptor.Builder()
         .name("Update Query Key")
-        .description("Key name used to build the update query criteria; this property is valid only when using update mode, "
-                + "otherwise it is ignored. Example: _id")
+        .description("Comma separated key names used to build the update query criteria. Their values are taken from incoming flowfile. Example: _id")
         .required(false)
+        .dependsOn(MODE, MODE_UPDATE)
         .addValidator(StandardValidators.NON_BLANK_VALIDATOR)
         .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
         .build();
     static final PropertyDescriptor UPDATE_QUERY = new PropertyDescriptor.Builder()
         .name("putmongo-update-query")
         .displayName("Update Query")
-        .description("Specify a full MongoDB query to be used for the lookup query to do an update/upsert.")
+        .description("Specify a full MongoDB query to be used for the lookup query to do an update/upsert. NOTE: this field is ignored if the '%s' value is not empty."
+            .formatted(UPDATE_QUERY_KEY.getDisplayName()))
         .required(false)
+        .dependsOn(MODE, MODE_UPDATE)
         .addValidator(JsonValidator.INSTANCE)
         .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
         .build();
 
-    static final PropertyDescriptor UPDATE_MODE = new PropertyDescriptor.Builder()
+    static final PropertyDescriptor UPDATE_OPERATION_MODE = new PropertyDescriptor.Builder()
         .displayName("Update Mode")
         .name("put-mongo-update-mode")
         .required(true)
+        .dependsOn(MODE, MODE_UPDATE)
         .allowableValues(UPDATE_WITH_DOC, UPDATE_WITH_OPERATORS)
-        .defaultValue(UPDATE_WITH_DOC.getValue())
+        .defaultValue(UPDATE_WITH_DOC)
         .description("Choose an update mode. You can either supply a JSON document to use as a direct replacement " +
                 "or specify a document that contains update operators like $set, $unset, and $inc. " +
                 "When Operators mode is enabled, the flowfile content is expected to be the operator part " +
                 "for example: {$set:{\"key\": \"value\"},$inc:{\"count\":1234}} and the update query will come " +
                 "from the configured Update Query property.")
          .build();
+    static final PropertyDescriptor UPDATE_METHOD = new PropertyDescriptor.Builder()
+        .name("Update Method")
+        .dependsOn(UPDATE_OPERATION_MODE, UPDATE_WITH_OPERATORS)
+        .description("Choose between 'updateOne' or 'updateMany' Mongo documents per incoming flow file.")

Review Comment:
   The flow file reference should be removed.
   ```suggestion
           .description("MongoDB method for running collection update operations, such as updateOne or updateMany")
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/AbstractMongoProcessor.java:
##########
@@ -235,4 +268,20 @@ protected synchronized void configureMapper(String setting, String dateFormat) {
             objectMapper.setDateFormat(df);
         }
     }
+
+    /**
+     * Checks if given update mode option matches for the incoming flow file
+     * @param updateModeToMatch the value against which processor's mode is compared
+     * @param processorMode the value coming from running processor
+     * @param flowFile incoming flow file to extract processor mode
+     * @return true if the incoming files update mode matches with updateModeToMatch
+     */
+    public boolean updateModeMatches(
+        MongoUpdateOption updateModeToMatch, PropertyValue processorMode, FlowFile flowFile) {

Review Comment:
   ```suggestion
           MongoUpdateOption updateMethodToMatch, PropertyValue configuredUpdateMethod, FlowFile flowFile) {
   ```



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/AbstractMongoProcessor.java:
##########
@@ -235,4 +268,20 @@ protected synchronized void configureMapper(String setting, String dateFormat) {
             objectMapper.setDateFormat(df);
         }
     }
+
+    /**
+     * Checks if given update mode option matches for the incoming flow file
+     * @param updateModeToMatch the value against which processor's mode is compared
+     * @param processorMode the value coming from running processor
+     * @param flowFile incoming flow file to extract processor mode
+     * @return true if the incoming files update mode matches with updateModeToMatch
+     */
+    public boolean updateModeMatches(

Review Comment:
   This should be `protected`:
   ```suggestion
       protected boolean updateModeMatches(
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org