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/09/21 14:03:21 UTC

[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #839: MINIFICPP-1279 - Optimize for size + refactor

arpadboda commented on a change in pull request #839:
URL: https://github.com/apache/nifi-minifi-cpp/pull/839#discussion_r492043884



##########
File path: libminifi/include/utils/FlatMap.h
##########
@@ -0,0 +1,211 @@
+/**
+ *
+ * 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.
+ */
+
+#ifndef LIBMINIFI_INCLUDE_UTILS_FLATMAP_H_
+#define LIBMINIFI_INCLUDE_UTILS_FLATMAP_H_
+
+#include <tuple>
+#include <functional>
+#include <vector>
+#include <utility>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename K, typename V>
+class FlatMap{
+ public:
+  using value_type = std::pair<K, V>;
+
+ private:
+  using Container = std::vector<value_type>;
+
+ public:
+  class iterator{
+    friend class const_iterator;
+    friend class FlatMap;
+    explicit iterator(typename Container::iterator it): it_(it) {}
+
+   public:
+    using difference_type = void;
+    using value_type = FlatMap::value_type;
+    using pointer = void;
+    using reference = void;
+    using iterator_category = void;
+
+    value_type* operator->() const {return &(*it_);}
+    value_type& operator*() const {return *it_;}
+
+    bool operator==(const iterator& other) const {
+      return it_ == other.it_;
+    }
+
+    bool operator!=(const iterator& other) const {
+      return !(*this == other);
+    }
+
+    iterator& operator++() {
+      ++it_;
+      return *this;
+    }
+
+   private:
+    typename Container::iterator it_;
+  };
+
+  class const_iterator{
+    friend class FlatMap;
+    explicit const_iterator(typename Container::const_iterator it): it_(it) {}
+
+   public:
+    const_iterator(iterator it): it_(it.it_) {}  // NOLINT

Review comment:
       = default?

##########
File path: extensions/http-curl/tests/HTTPHandlers.h
##########
@@ -242,10 +242,6 @@ class FlowFileResponder : public ServerAwareHandler {
       if(!isServerRunning())return false;
       assert(read == length);
 
-      assert(flow->attributes["path"] == ".");
-      assert(!flow->attributes["uuid"].empty());

Review comment:
       Did you verify that SiteToSite communication to NiFi works without sending these attributes?
   

##########
File path: libminifi/src/core/ProcessSession.cpp
##########
@@ -144,41 +120,34 @@ std::shared_ptr<core::FlowFile> ProcessSession::clone(const std::shared_ptr<core
 }
 
 std::shared_ptr<core::FlowFile> ProcessSession::cloneDuringTransfer(std::shared_ptr<core::FlowFile> &parent) {
-  std::map<std::string, std::string> empty;
-  std::shared_ptr<core::FlowFile> record = std::make_shared<FlowFileRecord>(process_context_->getFlowFileRepository(), process_context_->getContentRepository(), empty);
+  auto record = std::make_shared<FlowFileRecord>();
 
-  if (record) {
-    auto flow_version = process_context_->getProcessorNode()->getFlowIdentifier();
-    if (flow_version != nullptr) {
-      auto flow_id = flow_version->getFlowId();
-      std::string attr = FlowAttributeKey(FLOW_ID);
-      record->setAttribute(attr, flow_version->getFlowId());
-    }
-    this->_clonedFlowFiles[record->getUUIDStr()] = record;
-    logger_->log_debug("Clone FlowFile with UUID %s during transfer", record->getUUIDStr());
-    // Copy attributes
-    std::map<std::string, std::string> parentAttributes = parent->getAttributes();
-    std::map<std::string, std::string>::iterator it;
-    for (it = parentAttributes.begin(); it != parentAttributes.end(); it++) {
-      if (it->first == FlowAttributeKey(ALTERNATE_IDENTIFIER) || it->first == FlowAttributeKey(DISCARD_REASON) || it->first == FlowAttributeKey(UUID))
-        // Do not copy special attributes from parent
-        continue;
-      record->setAttribute(it->first, it->second);
-    }
-    record->setLineageStartDate(parent->getlineageStartDate());
-
-    record->setLineageIdentifiers(parent->getlineageIdentifiers());
-    record->getlineageIdentifiers().insert(parent->getUUIDStr());
-
-    // Copy Resource Claim
-    std::shared_ptr<ResourceClaim> parent_claim = parent->getResourceClaim();
-    record->setResourceClaim(parent_claim);
-    if (parent_claim) {
-      record->setOffset(parent->getOffset());
-      record->setSize(parent->getSize());
-    }
-    provenance_report_->clone(parent, record);
+  auto flow_version = process_context_->getProcessorNode()->getFlowIdentifier();
+  if (flow_version != nullptr) {
+    record->setAttribute(SpecialFlowAttribute::FLOW_ID, flow_version->getFlowId());
+  }
+  this->_clonedFlowFiles[record->getUUIDStr()] = record;
+  logger_->log_debug("Clone FlowFile with UUID %s during transfer", record->getUUIDStr());
+  // Copy attributes
+  for (const auto& attribute : parent->getAttributes()) {
+    if (attribute.first == SpecialFlowAttribute::ALTERNATE_IDENTIFIER || attribute.first == SpecialFlowAttribute::DISCARD_REASON || attribute.first == SpecialFlowAttribute::UUID) {

Review comment:
       How I miss python here... :) 
   
   Given the line lenght, I think splitting this into 3 lines would increase the readability. 

##########
File path: libminifi/test/Utils.h
##########
@@ -0,0 +1,38 @@
+/**
+ * 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.
+ */
+#ifndef LIBMINIFI_TEST_UTILS_H_
+#define LIBMINIFI_TEST_UTILS_H_
+
+#define FIELD_ACCESSOR(ClassName, field) \

Review comment:
       Poor man's reflection? :)

##########
File path: libminifi/include/utils/FlatMap.h
##########
@@ -0,0 +1,211 @@
+/**
+ *
+ * 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.
+ */
+
+#ifndef LIBMINIFI_INCLUDE_UTILS_FLATMAP_H_
+#define LIBMINIFI_INCLUDE_UTILS_FLATMAP_H_
+
+#include <tuple>
+#include <functional>
+#include <vector>
+#include <utility>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename K, typename V>
+class FlatMap{
+ public:
+  using value_type = std::pair<K, V>;
+
+ private:
+  using Container = std::vector<value_type>;
+
+ public:
+  class iterator{
+    friend class const_iterator;
+    friend class FlatMap;
+    explicit iterator(typename Container::iterator it): it_(it) {}
+
+   public:
+    using difference_type = void;
+    using value_type = FlatMap::value_type;
+    using pointer = void;
+    using reference = void;
+    using iterator_category = void;
+
+    value_type* operator->() const {return &(*it_);}
+    value_type& operator*() const {return *it_;}
+
+    bool operator==(const iterator& other) const {
+      return it_ == other.it_;
+    }
+
+    bool operator!=(const iterator& other) const {
+      return !(*this == other);
+    }
+
+    iterator& operator++() {
+      ++it_;
+      return *this;
+    }
+
+   private:
+    typename Container::iterator it_;
+  };
+
+  class const_iterator{
+    friend class FlatMap;
+    explicit const_iterator(typename Container::const_iterator it): it_(it) {}
+
+   public:
+    const_iterator(iterator it): it_(it.it_) {}  // NOLINT
+    using difference_type = void;
+    using value_type = const FlatMap::value_type;
+    using pointer = void;
+    using reference = void;
+    using iterator_category = void;
+
+    value_type* operator->() const {return &(*it_);}
+    value_type& operator*() const {return *it_;}
+
+    bool operator==(const const_iterator& other) const {
+      return it_ == other.it_;
+    }
+
+    bool operator!=(const const_iterator& other) const {
+      return !(*this == other);
+    }
+
+    const_iterator& operator++() {
+      ++it_;
+      return *this;
+    }
+
+   private:
+    typename Container::const_iterator it_;
+  };
+
+  FlatMap() = default;
+  FlatMap(const FlatMap&) = default;
+  FlatMap(FlatMap&&) noexcept = default;
+  FlatMap(std::initializer_list<value_type> items) : data_(items) {}
+  template<class InputIterator>
+  FlatMap(InputIterator begin, InputIterator end) : data_(begin, end) {}
+
+  FlatMap& operator=(const FlatMap& source) = default;
+  FlatMap& operator=(FlatMap&& source) = default;
+  FlatMap& operator=(std::initializer_list<value_type> items) {
+    data_ = items;
+    return *this;
+  }
+
+  std::size_t size() {
+    return data_.size();
+  }
+
+  V& operator[](const K& key) {
+    auto it = find(key);
+    if (it != end()) {
+      return it->second;
+    }
+    data_.emplace_back(key, V{});
+    return data_.rbegin()->second;
+  }
+
+  iterator erase(const_iterator pos) {
+    auto offset = pos.it_ - data_.begin();
+    std::swap(*data_.rbegin(), *(data_.begin() + offset));
+    data_.pop_back();
+    return iterator{data_.begin() + offset};
+  }
+
+  std::size_t erase(const K& key) {
+    for (auto it = data_.begin(); it != data_.end(); ++it) {
+      if (it->first == key) {
+        std::swap(*data_.rbegin(), *it);
+        data_.pop_back();
+        return 1;
+      }
+    }
+    return 0;
+  }
+
+  std::pair<iterator, bool> insert(const value_type& value) {
+    auto it = find(value.first);
+    if (it != end()) {
+      return {it, false};
+    }
+    data_.push_back(value);
+    return {iterator{data_.begin() + data_.size() - 1}, true};
+  }
+
+  template<typename M>
+  std::pair<iterator, bool> insert_or_assign(const K& key, M&& value) {
+    auto it = find(key);
+    if (it != end()) {
+      it->second = std::forward<M>(value);
+      return {it, false};
+    }
+    data_.emplace_back(key, std::forward<M>(value));
+    return {iterator{data_.begin() + data_.size() - 1}, true};
+  }
+
+  iterator find(const K& key) {
+    for (auto it = data_.begin(); it != data_.end(); ++it) {
+      if (it->first == key) return iterator{it};
+    }
+    return end();
+  }
+
+  const_iterator find(const K& key) const {
+    for (auto it = data_.begin(); it != data_.end(); ++it) {
+      if (it->first == key) return const_iterator{it};
+    }
+    return end();
+  }
+
+  iterator begin() {
+    return iterator{data_.begin()};
+  }
+
+  iterator end() {
+    return iterator{data_.end()};
+  }
+
+  const_iterator begin() const {

Review comment:
       According to the naming conviction followed in stl, I would name these cbegin and cend. 




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