adamdebreceni commented on code in PR #1038: URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r844804316
########## 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: added `gsl_ExpectsAudit` to this and `getLargestChildOrGrandchild` as well ########## 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: added -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
