westonpace commented on a change in pull request #11426: URL: https://github.com/apache/arrow/pull/11426#discussion_r740478913
########## File path: cpp/src/arrow/compute/memory_resources.cc ########## @@ -0,0 +1,307 @@ +// 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. + +#include "arrow/compute/memory_resources.h" +#include "arrow/compute/exec.h" +#include "arrow/record_batch.h" +#include "arrow/table.h" +#include "arrow/util/make_unique.h" + +#include <memory> +#include <mutex> +#include <random> +#include <unordered_map> + +#include <arrow/filesystem/filesystem.h> +#include <arrow/ipc/feather.h> +#include <arrow/ipc/reader.h> +#include <arrow/ipc/writer.h> +#include "arrow/io/file.h" + +#ifdef __APPLE__ +#include <sys/sysctl.h> +#include <sys/types.h> +#endif + +#ifdef __linux__ +#include <sys/statvfs.h> +#include <sys/sysinfo.h> +#endif + +// Windows APIs +#include "arrow/util/windows_compatibility.h" + +namespace arrow { + +namespace compute { + +std::string MemoryLevelName(MemoryLevel memory_level) { + static const char* MemoryLevelNames[] = {ARROW_STRINGIFY(MemoryLevel::kDiskLevel), + ARROW_STRINGIFY(MemoryLevel::kCPULevel), + ARROW_STRINGIFY(MemoryLevel::kGPULevel)}; + + return MemoryLevelNames[static_cast<int>(memory_level)]; +} + +std::string MemoryResource::ToString() const { return MemoryLevelName(memory_level_); } + +class CPUDataHolder : public DataHolder { + public: + explicit CPUDataHolder(const std::shared_ptr<RecordBatch>& record_batch) + : DataHolder(MemoryLevel::kCPULevel), record_batch_(std::move(record_batch)) {} + + Result<ExecBatch> Get() override { return ExecBatch(*record_batch_); } + + private: + std::shared_ptr<RecordBatch> record_batch_; +}; + +namespace { + +std::string RandomString(std::size_t length) { + const std::string characters = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + std::random_device random_device; + std::mt19937 generator(random_device()); + std::uniform_int_distribution<> distribution(0, characters.size() - 1); + std::string random_string; + for (std::size_t i = 0; i < length; ++i) { + random_string += characters[distribution(generator)]; + } + return random_string; +} + +} // namespace + +Status StoreRecordBatch(const std::shared_ptr<RecordBatch>& record_batch, + const std::shared_ptr<fs::FileSystem>& filesystem, + const std::string& file_path) { + auto output = filesystem->OpenOutputStream(file_path).ValueOrDie(); + auto writer = + arrow::ipc::MakeFileWriter(output.get(), record_batch->schema()).ValueOrDie(); + ARROW_RETURN_NOT_OK(writer->WriteRecordBatch(*record_batch)); + return writer->Close(); +} +Result<std::shared_ptr<RecordBatch>> RecoverRecordBatch( + const std::shared_ptr<fs::FileSystem>& filesystem, const std::string& file_path) { + ARROW_ASSIGN_OR_RAISE(auto input, filesystem->OpenInputFile(file_path)); + ARROW_ASSIGN_OR_RAISE(auto reader, arrow::ipc::feather::Reader::Open(input)); + std::shared_ptr<Table> table; + ARROW_RETURN_NOT_OK(reader->Read(&table)); + TableBatchReader batch_iter(*table); + ARROW_ASSIGN_OR_RAISE(auto batch, batch_iter.Next()); + return batch; +} + +class DiskDataHolder : public DataHolder { Review comment: Datasets also seems like an odd home. I asked on Zulip: https://ursalabs.zulipchat.com/#narrow/stream/180245-dev/topic/Compute.20module.20.2F.20IPC.20module.20dependency -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org