You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/05/09 16:18:55 UTC

[GitHub] [nifi] ottobackwards opened a new pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

ottobackwards opened a new pull request #4262:
URL: https://github.com/apache/nifi/pull/4262


   Thank you for submitting a contribution to Apache NiFi.
   
   Please provide a short description of the PR here:
   
   This pr adds utility classes to walk the FieldValue hierarchy from any given node to root, and build a logical path as a result.
   
   #### Description of PR
   
   _Enables X functionality; fixes bug NIFI-YYYY._
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced 
        in the commit message?
   
   - [x] Does your PR title start with **NIFI-XXXX** where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [x] Has your PR been rebased against the latest commit within the target branch (typically `master`)?
   
   - [x] Is your initial contribution a single, squashed commit? _Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not `squash` or use `--force` when pushing to allow for clean monitoring of changes._
   
   ### For code changes:
   - [x] Have you ensured that the full suite of tests is executed via `mvn -Pcontrib-check clean install` at the root `nifi` folder?
   - [x] Have you written or updated unit tests to verify your changes?
   - [-] Have you verified that the full build is successful on both JDK 8 and JDK 11?
   - [-] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
   - [-] If applicable, have you updated the `LICENSE` file, including the main `LICENSE` file under `nifi-assembly`?
   - [-] If applicable, have you updated the `NOTICE` file, including the main `NOTICE` file found under `nifi-assembly`?
   - [-] If adding new Properties, have you added `.displayName` in addition to .name (programmatic access) for each of the new properties?
   
   ### For documentation related changes:
   - [-] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.
   


----------------------------------------------------------------
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.

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



[GitHub] [nifi] ottobackwards commented on pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
ottobackwards commented on pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#issuecomment-754007637


   Happy New Year!


----------------------------------------------------------------
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.

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



[GitHub] [nifi] ottobackwards commented on pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
ottobackwards commented on pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#issuecomment-674441397


   any chance to get a review on this?


----------------------------------------------------------------
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.

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



[GitHub] [nifi] ottobackwards closed pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
ottobackwards closed pull request #4262:
URL: https://github.com/apache/nifi/pull/4262


   


----------------------------------------------------------------
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.

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



[GitHub] [nifi] markap14 commented on a change in pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#discussion_r551367649



##########
File path: nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/util/FieldValueLogicalPathBuilder.java
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.record.path.util;
+
+import java.util.Objects;
+
+import org.apache.nifi.record.path.ArrayIndexFieldValue;
+import org.apache.nifi.record.path.FieldValue;
+import org.apache.nifi.record.path.MapEntryFieldValue;
+
+public class FieldValueLogicalPathBuilder {
+
+    private static final CharSequence DEFAULT_DELIMITER = "/";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_LEFT = "[";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_RIGHT = "]";
+    private final CharSequence pathDelimiter;
+    private final CharSequence keyLeft;
+    private final CharSequence keyRight;
+    private final CharSequence indexLeft;
+    private final CharSequence indexRight;
+
+    public static class Builder {
+
+        private CharSequence pathDelimiter = DEFAULT_DELIMITER;
+        private CharSequence keyLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence keyRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+        private CharSequence indexLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence indexRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+
+        public Builder() {
+        }
+
+        public Builder withPathDelimiter(CharSequence delimiter) {
+            Objects.requireNonNull(delimiter, "delimiter cannot be null");
+            this.pathDelimiter = delimiter;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.keyLeft = left;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.keyRight = right;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.indexLeft = left;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.indexRight = right;
+            return this;
+        }
+
+        public FieldValueLogicalPathBuilder build() {
+            return new FieldValueLogicalPathBuilder(pathDelimiter, keyLeft, keyRight, indexLeft,
+                indexRight);
+        }
+    }
+
+    private FieldValueLogicalPathBuilder(CharSequence pathDelimiter,
+        CharSequence leftMapKeyWrapper, CharSequence rightMapKeyMapper, CharSequence leftArrayIndexWrapper,
+        CharSequence rightArrayIndexWrapper) {
+        this.keyLeft = leftMapKeyWrapper;
+        this.keyRight = rightMapKeyMapper;
+        this.indexLeft = leftArrayIndexWrapper;
+        this.indexRight = rightArrayIndexWrapper;
+        this.pathDelimiter = pathDelimiter;
+    }
+
+    /**
+     * Builds a logical path String using the configured wrappers for array or map values for a given
+     * {@code FieldValue}
+     *
+     * @param fieldValue the Field Value
+     * @return a String with a path
+     */
+    public String buildLogicalPath(FieldValue fieldValue) {
+        Objects.requireNonNull(fieldValue, "fieldValue cannot be null");
+        final StringBuilder stringBuilder = new StringBuilder();
+        FieldValueWalker.walk(fieldValue, (thisFieldValue) -> {

Review comment:
       This algorithm is quite expensive. The insert() operation is copying all of the characters in the underlying array. And this is done iteratively, which can be extremely expensive.
   One way to improve this would be to use something like `stringBuilder.insert(0, keyLeft + index + keyRight);` so that for each iteration there is only a single `insert` call instead of 3.
   Even that, though, is potentially very expensive. Would instead recommend moving to something along the lines of:
   ```
   List<String> paths = new ArrayList<>();
   FieldValueWalker.walk(fieldValue, thisFieldValue -> {
   ...
       paths.add(keyLeft + index + keyRight);
   ...
   });
   
   paths.reverse();
   paths.join("");
   ```
   This way, rather than constantly doing large array copies, we just keep a list of things to add, then reverse it at the end. Or you could do the same thing with the StringBuilder, building the string backwards. Then at the end call `stringBuilder.reverse()`




----------------------------------------------------------------
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.

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



[GitHub] [nifi] ottobackwards commented on a change in pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
ottobackwards commented on a change in pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#discussion_r551493870



##########
File path: nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/util/FieldValueLogicalPathBuilder.java
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.record.path.util;
+
+import java.util.Objects;
+
+import org.apache.nifi.record.path.ArrayIndexFieldValue;
+import org.apache.nifi.record.path.FieldValue;
+import org.apache.nifi.record.path.MapEntryFieldValue;
+
+public class FieldValueLogicalPathBuilder {
+
+    private static final CharSequence DEFAULT_DELIMITER = "/";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_LEFT = "[";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_RIGHT = "]";
+    private final CharSequence pathDelimiter;
+    private final CharSequence keyLeft;
+    private final CharSequence keyRight;
+    private final CharSequence indexLeft;
+    private final CharSequence indexRight;
+
+    public static class Builder {
+
+        private CharSequence pathDelimiter = DEFAULT_DELIMITER;
+        private CharSequence keyLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence keyRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+        private CharSequence indexLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence indexRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+
+        public Builder() {
+        }
+
+        public Builder withPathDelimiter(CharSequence delimiter) {
+            Objects.requireNonNull(delimiter, "delimiter cannot be null");
+            this.pathDelimiter = delimiter;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.keyLeft = left;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.keyRight = right;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.indexLeft = left;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.indexRight = right;
+            return this;
+        }
+
+        public FieldValueLogicalPathBuilder build() {
+            return new FieldValueLogicalPathBuilder(pathDelimiter, keyLeft, keyRight, indexLeft,
+                indexRight);
+        }
+    }
+
+    private FieldValueLogicalPathBuilder(CharSequence pathDelimiter,
+        CharSequence leftMapKeyWrapper, CharSequence rightMapKeyMapper, CharSequence leftArrayIndexWrapper,
+        CharSequence rightArrayIndexWrapper) {
+        this.keyLeft = leftMapKeyWrapper;
+        this.keyRight = rightMapKeyMapper;
+        this.indexLeft = leftArrayIndexWrapper;
+        this.indexRight = rightArrayIndexWrapper;
+        this.pathDelimiter = pathDelimiter;
+    }
+
+    /**
+     * Builds a logical path String using the configured wrappers for array or map values for a given
+     * {@code FieldValue}
+     *
+     * @param fieldValue the Field Value
+     * @return a String with a path
+     */
+    public String buildLogicalPath(FieldValue fieldValue) {
+        Objects.requireNonNull(fieldValue, "fieldValue cannot be null");
+        final StringBuilder stringBuilder = new StringBuilder();
+        FieldValueWalker.walk(fieldValue, (thisFieldValue) -> {

Review comment:
       thanks for the review, let me look at it and try to remember what the hell i was doing.  It has been a bit lol.




----------------------------------------------------------------
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.

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



[GitHub] [nifi] markap14 commented on pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
markap14 commented on pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#issuecomment-754021366


   Happy New Year @ottobackwards ! Should be able to review this this week.


----------------------------------------------------------------
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.

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



[GitHub] [nifi] markap14 closed pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
markap14 closed pull request #4262:
URL: https://github.com/apache/nifi/pull/4262


   


----------------------------------------------------------------
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.

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



[GitHub] [nifi] markap14 commented on pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
markap14 commented on pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#issuecomment-755388884


   Thanks @ottobackwards! Looks good to me. Merged to main!


----------------------------------------------------------------
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.

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



[GitHub] [nifi] ottobackwards commented on pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

Posted by GitBox <gi...@apache.org>.
ottobackwards commented on pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#issuecomment-700973644


   touch


----------------------------------------------------------------
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.

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