Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Arrow generic code 4 : * Purpose: Arrow generic code 5 : * Author: Even Rouault, <even.rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2022, Planet Labs 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #ifndef OGR_ARROW_WRITABLE_FILE_H 14 : #define OGR_ARROW_WRITABLE_FILE_H 15 : 16 : #include "cpl_vsi_virtual.h" 17 : 18 : #include "arrow/buffer.h" 19 : #include "arrow/io/file.h" 20 : #include "arrow/io/interfaces.h" 21 : 22 : #if defined(__clang__) 23 : #pragma clang diagnostic push 24 : #pragma clang diagnostic ignored "-Wweak-vtables" 25 : #endif 26 : 27 : /************************************************************************/ 28 : /* OGRArrowWritableFile */ 29 : /************************************************************************/ 30 : 31 : class OGRArrowWritableFile final : public arrow::io::OutputStream 32 : { 33 : VSIVirtualHandleUniquePtr m_fp; 34 : 35 : OGRArrowWritableFile(const OGRArrowWritableFile &) = delete; 36 : OGRArrowWritableFile &operator=(const OGRArrowWritableFile &) = delete; 37 : 38 : public: 39 433 : explicit OGRArrowWritableFile(VSIVirtualHandleUniquePtr fp) 40 433 : : m_fp(std::move(fp)) 41 : { 42 433 : } 43 : 44 0 : arrow::Status Close() override 45 : { 46 0 : int ret = m_fp->Close(); 47 0 : m_fp.reset(); 48 : return ret == 0 ? arrow::Status::OK() 49 0 : : arrow::Status::IOError("Error while closing"); 50 : } 51 : 52 17926 : arrow::Result<int64_t> Tell() const override 53 : { 54 17926 : return static_cast<int64_t>(m_fp->Tell()); 55 : } 56 : 57 0 : bool closed() const override 58 : { 59 0 : return m_fp == nullptr; 60 : } 61 : 62 23306 : arrow::Status Write(const void *data, int64_t nbytes) override 63 : { 64 : CPLAssert(static_cast<int64_t>(static_cast<size_t>(nbytes)) == nbytes); 65 23306 : if (m_fp->Write(data, 1, static_cast<size_t>(nbytes)) == 66 23306 : static_cast<size_t>(nbytes)) 67 23306 : return arrow::Status::OK(); 68 0 : return arrow::Status::IOError("Error while writing"); 69 : } 70 : 71 4928 : arrow::Status Write(const std::shared_ptr<arrow::Buffer> &data) override 72 : { 73 4928 : return Write(data->data(), data->size()); 74 : } 75 : }; 76 : 77 : #if defined(__clang__) 78 : #pragma clang diagnostic pop 79 : #endif 80 : 81 : #endif // OGR_ARROW_WRITABLE_FILE_H