LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/generic - ogrlayerarrow.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 25 25 100.0 %
Date: 2025-01-18 12:42:00 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Parts of OGRLayer dealing with Arrow C interface
       5             :  * Author:   Even Rouault, <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2023, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef OGRLAYERARROW_H_DEFINED
      14             : #define OGRLAYERARROW_H_DEFINED
      15             : 
      16             : #include "cpl_port.h"
      17             : 
      18             : #include <map>
      19             : #include <string>
      20             : 
      21             : #include "ogr_recordbatch.h"
      22             : 
      23             : constexpr const char *ARROW_EXTENSION_NAME_KEY = "ARROW:extension:name";
      24             : constexpr const char *ARROW_EXTENSION_METADATA_KEY = "ARROW:extension:metadata";
      25             : constexpr const char *EXTENSION_NAME_OGC_WKB = "ogc.wkb";
      26             : constexpr const char *EXTENSION_NAME_GEOARROW_WKB = "geoarrow.wkb";
      27             : constexpr const char *EXTENSION_NAME_ARROW_JSON = "arrow.json";
      28             : 
      29             : // GetArrowStream(GAS) options
      30             : constexpr const char *GAS_OPT_DATETIME_AS_STRING = "DATETIME_AS_STRING";
      31             : 
      32             : std::map<std::string, std::string>
      33             :     CPL_DLL OGRParseArrowMetadata(const char *pabyMetadata);
      34             : 
      35             : bool CPL_DLL OGRCloneArrowArray(const struct ArrowSchema *schema,
      36             :                                 const struct ArrowArray *array,
      37             :                                 struct ArrowArray *out_array);
      38             : 
      39             : bool CPL_DLL OGRCloneArrowSchema(const struct ArrowSchema *schema,
      40             :                                  struct ArrowSchema *out_schema);
      41             : 
      42             : /** C++ wrapper on top of ArrowArrayStream */
      43             : class OGRArrowArrayStream
      44             : {
      45             :   public:
      46             :     /** Constructor: instantiate an empty ArrowArrayStream  */
      47        1818 :     inline OGRArrowArrayStream()
      48        1818 :     {
      49        1818 :         memset(&m_stream, 0, sizeof(m_stream));
      50        1818 :     }
      51             : 
      52             :     /** Destructor: call release() on the ArrowArrayStream if not already done */
      53        1818 :     inline ~OGRArrowArrayStream()
      54        1818 :     {
      55        1818 :         clear();
      56        1818 :     }
      57             : 
      58             :     /** Call release() on the ArrowArrayStream if not already done */
      59             :     // cppcheck-suppress functionStatic
      60        2727 :     inline void clear()
      61             :     {
      62        2727 :         if (m_stream.release)
      63             :         {
      64         154 :             m_stream.release(&m_stream);
      65         154 :             m_stream.release = nullptr;
      66             :         }
      67        2727 :     }
      68             : 
      69             :     /** Return the raw ArrowArrayStream* */
      70         154 :     inline ArrowArrayStream *get()
      71             :     {
      72         154 :         return &m_stream;
      73             :     }
      74             : 
      75             :     /** Get the schema */
      76             :     // cppcheck-suppress functionStatic
      77         308 :     inline int get_schema(struct ArrowSchema *schema)
      78             :     {
      79         308 :         return m_stream.get_schema(&m_stream, schema);
      80             :     }
      81             : 
      82             :     /** Get the next ArrowArray batch */
      83             :     // cppcheck-suppress functionStatic
      84         298 :     inline int get_next(struct ArrowArray *array)
      85             :     {
      86         298 :         return m_stream.get_next(&m_stream, array);
      87             :     }
      88             : 
      89             :     /** Move assignment operator */
      90         909 :     inline OGRArrowArrayStream &operator=(OGRArrowArrayStream &&other)
      91             :     {
      92         909 :         if (this != &other)
      93             :         {
      94         909 :             clear();
      95         909 :             memcpy(&m_stream, &(other.m_stream), sizeof(m_stream));
      96             :             // Reset other content, in particular its "release" member
      97             :             // as per https://arrow.apache.org/docs/format/CDataInterface.html#moving-an-array
      98         909 :             memset(&(other.m_stream), 0, sizeof(m_stream));
      99             :         }
     100         909 :         return *this;
     101             :     }
     102             : 
     103             :   private:
     104             :     struct ArrowArrayStream m_stream
     105             :     {
     106             :     };
     107             : 
     108             :     OGRArrowArrayStream(const OGRArrowArrayStream &) = delete;
     109             :     OGRArrowArrayStream(OGRArrowArrayStream &&) = delete;
     110             :     OGRArrowArrayStream &operator=(const OGRArrowArrayStream &) = delete;
     111             : };
     112             : 
     113             : #endif  // OGRLAYERARROW_H_DEFINED

Generated by: LCOV version 1.14