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 2021/03/23 11:41:48 UTC

[GitHub] [nifi-minifi-cpp] adamdebreceni opened a new pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

adamdebreceni opened a new pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038


   Thank you for submitting a contribution to Apache NiFi - MiNiFi C++.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
   
   - [ ] Does your PR title start with MINIFICPP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [ ] 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?
   - [ ] If applicable, have you updated the NOTICE file?
   
   ### 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 results 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-minifi-cpp] martinzink commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

Posted by GitBox <gi...@apache.org>.
martinzink commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r767141667



##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(1);
+      return item;
+    } else {
+      std::swap(data_[2], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(2);
+      return item;
+    }
+  }
+
+ private:
+  friend struct ::MinMaxHeapTestAccessor;
+
+  static size_t getLevel(size_t index) {
+    // more performant solutions are possible
+    // investigate if this turns out to be a bottleneck
+    size_t level = 0;
+    ++index;
+    while (index >>= 1) {
+      ++level;
+    }
+    return level;
+  }
+
+  static bool isOnMinLevel(size_t index) {
+    return getLevel(index) % 2 == 0;
+  }
+
+  static size_t getParent(size_t index) {
+    return (index - 1) / 2;
+  }
+
+  /**
+   * WARNING! must only be called when index is on a min-level.

Review comment:
       I think we should throw if this is violated.

##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);

Review comment:
       I think if we were to extract the max() logic into a getMaxIndex() then this and max() could be simplified.




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



[GitHub] [nifi-minifi-cpp] martinzink commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

Posted by GitBox <gi...@apache.org>.
martinzink commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r767141667



##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(1);
+      return item;
+    } else {
+      std::swap(data_[2], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(2);
+      return item;
+    }
+  }
+
+ private:
+  friend struct ::MinMaxHeapTestAccessor;
+
+  static size_t getLevel(size_t index) {
+    // more performant solutions are possible
+    // investigate if this turns out to be a bottleneck
+    size_t level = 0;
+    ++index;
+    while (index >>= 1) {
+      ++level;
+    }
+    return level;
+  }
+
+  static bool isOnMinLevel(size_t index) {
+    return getLevel(index) % 2 == 0;
+  }
+
+  static size_t getParent(size_t index) {
+    return (index - 1) / 2;
+  }
+
+  /**
+   * WARNING! must only be called when index is on a min-level.

Review comment:
       I think we should throw if this is violated.

##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);

Review comment:
       I think if we were to extract the max() logic into a getMaxIndex() then this and max() could be simplified.




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



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r704567374



##########
File path: extensions/rocksdb-repos/FlowFileLoader.h
##########
@@ -0,0 +1,68 @@
+/**
+ * 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 <future>
+#include <list>
+
+#include "RocksDatabase.h"
+#include "FlowFile.h"
+#include "gsl.h"
+#include "core/ContentRepository.h"
+#include "SwapManager.h"
+#include "utils/ThreadPool.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+
+class FlowFileLoader {
+  using FlowFilePtr = std::shared_ptr<core::FlowFile>;
+  using FlowFilePtrVec = std::vector<FlowFilePtr>;
+
+  static constexpr size_t thread_count_ = 2;
+
+ public:
+  FlowFileLoader();
+
+  ~FlowFileLoader();
+
+  void initialize(gsl::not_null<minifi::internal::RocksDatabase*> db, std::shared_ptr<core::ContentRepository> content_repo);
+
+  void start();
+
+  void stop();
+
+  std::future<FlowFilePtrVec> load(std::vector<SwappedFlowFile> flow_files);
+
+ private:
+  utils::TaskRescheduleInfo loadImpl(const std::vector<SwappedFlowFile>& flow_files, std::shared_ptr<std::promise<FlowFilePtrVec>>& output);
+
+  utils::ThreadPool<utils::TaskRescheduleInfo> thread_pool_{thread_count_, false, nullptr, "FlowFileLoaderThreadPool"};
+
+  minifi::internal::RocksDatabase* db_{nullptr};
+
+  std::shared_ptr<core::ContentRepository> content_repo_;
+  std::shared_ptr<logging::Logger> logger_;

Review comment:
       Missing `#include "core/logging/Logger.h"`

##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(1);
+      return item;
+    } else {
+      std::swap(data_[2], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(2);
+      return item;
+    }
+  }
+
+ private:
+  friend struct ::MinMaxHeapTestAccessor;
+
+  static size_t getLevel(size_t index) {
+    // more performant solutions are possible
+    // investigate if this turns out to be a bottleneck
+    size_t level = 0;
+    ++index;
+    while (index >>= 1) {
+      ++level;
+    }
+    return level;
+  }
+
+  static bool isOnMinLevel(size_t index) {
+    return getLevel(index) % 2 == 0;
+  }
+
+  static size_t getParent(size_t index) {
+    return (index - 1) / 2;
+  }
+
+  /**
+   * WARNING! must only be called when index is on a min-level.

Review comment:
       This sounds like a precondition, so I think `gsl_Expects` or `gsl_ExpectsAudit` (if checking is expensive) fits the problem better.




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



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r704567374



##########
File path: extensions/rocksdb-repos/FlowFileLoader.h
##########
@@ -0,0 +1,68 @@
+/**
+ * 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 <future>
+#include <list>
+
+#include "RocksDatabase.h"
+#include "FlowFile.h"
+#include "gsl.h"
+#include "core/ContentRepository.h"
+#include "SwapManager.h"
+#include "utils/ThreadPool.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+
+class FlowFileLoader {
+  using FlowFilePtr = std::shared_ptr<core::FlowFile>;
+  using FlowFilePtrVec = std::vector<FlowFilePtr>;
+
+  static constexpr size_t thread_count_ = 2;
+
+ public:
+  FlowFileLoader();
+
+  ~FlowFileLoader();
+
+  void initialize(gsl::not_null<minifi::internal::RocksDatabase*> db, std::shared_ptr<core::ContentRepository> content_repo);
+
+  void start();
+
+  void stop();
+
+  std::future<FlowFilePtrVec> load(std::vector<SwappedFlowFile> flow_files);
+
+ private:
+  utils::TaskRescheduleInfo loadImpl(const std::vector<SwappedFlowFile>& flow_files, std::shared_ptr<std::promise<FlowFilePtrVec>>& output);
+
+  utils::ThreadPool<utils::TaskRescheduleInfo> thread_pool_{thread_count_, false, nullptr, "FlowFileLoaderThreadPool"};
+
+  minifi::internal::RocksDatabase* db_{nullptr};
+
+  std::shared_ptr<core::ContentRepository> content_repo_;
+  std::shared_ptr<logging::Logger> logger_;

Review comment:
       Missing `#include "core/logging/Logger.h"`




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



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r767163102



##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(1);
+      return item;
+    } else {
+      std::swap(data_[2], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(2);
+      return item;
+    }
+  }
+
+ private:
+  friend struct ::MinMaxHeapTestAccessor;
+
+  static size_t getLevel(size_t index) {
+    // more performant solutions are possible
+    // investigate if this turns out to be a bottleneck
+    size_t level = 0;
+    ++index;
+    while (index >>= 1) {
+      ++level;
+    }
+    return level;
+  }
+
+  static bool isOnMinLevel(size_t index) {
+    return getLevel(index) % 2 == 0;
+  }
+
+  static size_t getParent(size_t index) {
+    return (index - 1) / 2;
+  }
+
+  /**
+   * WARNING! must only be called when index is on a min-level.

Review comment:
       This sounds like a precondition, so I think `gsl_Expects` or `gsl_ExpectsAudit` (if checking is expensive) fits the problem better.




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