szaszm commented on a change in pull request #1138:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1138#discussion_r699111472



##########
File path: libminifi/include/utils/file/FilePattern.h
##########
@@ -0,0 +1,122 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include <set>
+#include <utility>
+#include <memory>
+#include <filesystem>
+
+#include "utils/OptionalUtils.h"
+#include "core/logging/Logger.h"
+
+struct FilePatternTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace file {
+
+class FilePatternError : public std::invalid_argument {
+ public:
+  explicit FilePatternError(const std::string& msg) : invalid_argument(msg) {}
+};
+
+class FilePattern {
+  friend struct ::FilePatternTestAccessor;
+
+  friend std::set<std::filesystem::path> match(const FilePattern& pattern);
+
+  class FilePatternSegmentError : public std::invalid_argument {
+   public:
+    explicit FilePatternSegmentError(const std::string& msg) : 
invalid_argument(msg) {}
+  };
+
+  class FilePatternSegment {
+    static void defaultSegmentErrorHandler(std::string_view error_message) {
+      throw FilePatternError(std::string{error_message});
+    }
+
+   public:
+    explicit FilePatternSegment(std::string pattern);
+
+    enum class MatchResult {
+      INCLUDE,  // dir/file should be processed according to the pattern
+      EXCLUDE,  // dir/file is explicitly rejected by the pattern
+      NOT_MATCHING  // dir/file does not match pattern, do what you may
+    };
+
+    bool isExcluding() const {
+      return excluding_;
+    }
+
+    MatchResult match(const std::string& directory) const;
+
+    MatchResult match(const std::string& directory, const std::string& 
filename) const;
+
+    MatchResult match(const std::filesystem::path& path) const;
+    /**
+     * @return The lowermost parent directory without wildcards.
+     */
+    std::filesystem::path getBaseDirectory() const;
+
+   private:
+    enum class DirMatchResult {
+      NONE,  // pattern does not match the directory (e.g. p = 
"/home/inner/*test", v = "/home/banana")
+      PARENT,  // directory is a parent of the pattern (e.g. p = 
"/home/inner/*test", v = "/home/inner")
+      EXACT,  // pattern exactly matches the directory (e.g. p = 
"/home/inner/*test", v = "/home/inner/cool_test")
+      TREE  // pattern matches the whole subtree of the directory (e.g. p = 
"/home/**", v = "/home/banana")
+    };
+
+    using DirIt = std::filesystem::path::const_iterator;
+    static DirMatchResult matchDirectory(DirIt pattern_begin, DirIt 
pattern_end, DirIt value_begin, DirIt value_end);
+
+    std::filesystem::path directory_pattern_;
+    std::string file_pattern_;
+    bool excluding_;
+  };
+
+  using ErrorHandler = std::function<void(std::string_view /*subpattern*/, 
std::string_view /*error_message*/)>;
+
+  static void defaultErrorHandler(std::string_view subpattern, 
std::string_view error_message) {
+    std::string message = "Error in subpattern '";
+    message += subpattern;
+    message += "': ";
+    message += error_message;
+    throw FilePatternError(message);
+  }
+
+ public:
+  explicit FilePattern(const std::string& pattern, ErrorHandler error_handler 
= defaultErrorHandler);
+
+ private:
+  std::vector<FilePatternSegment> segments_;
+};
+
+std::set<std::filesystem::path> match(const FilePattern& pattern);

Review comment:
       I just prefer vector as the default unless we have a compelling reason 
not to use it. If you still feel that set is a better fit, then feel free to 
revert this change.
   I also did some measurements and they didn't show a significant difference 
in performance:
   ```
   --------------------------------------------------------------
   Benchmark                    Time             CPU   Iterations
   --------------------------------------------------------------
   BM_MatchVector/4        150744 ns       148394 ns         4553
   BM_MatchVector/8        534604 ns       526563 ns         1330
   BM_MatchVector/64     31438122 ns     30888763 ns           23
   BM_MatchVector/512  2112878189 ns   2075348002 ns            1
   BM_MatchVector/2048 3325256795 ns   3258781806 ns            1
   BM_MatchSet/4           151805 ns       149650 ns         4707
   BM_MatchSet/8           534444 ns       527351 ns         1328
   BM_MatchSet/64        31374911 ns     30869050 ns           23
   BM_MatchSet/512     2110077546 ns   2075058340 ns            1
   BM_MatchSet/2048    3297083600 ns   3242293291 ns            1
   ```




-- 
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-unsubscr...@nifi.apache.org

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


Reply via email to