davisusanibar commented on code in PR #12794:
URL: https://github.com/apache/arrow/pull/12794#discussion_r845199308


##########
docs/source/java/cdata.rst:
##########
@@ -0,0 +1,313 @@
+.. 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.
+
+================
+C Data Interface
+================
+
+.. contents::
+
+Arrow supports exchanging data within the same process through the 
:ref:`c-data-interface`.
+
+Java to Python
+--------------
+
+Use this guide to implement :doc:`Java to Python 
<../python/integration/python_java.rst>`
+communication using the C Data Interface.
+
+Java to C++
+-----------
+
+The C Data Interface is a protocol implemented in Arrow to exchange data 
within different
+environments without the cost of marshaling and copying data.
+
+Example: How to expose an int32 array using C Data Interface?
+
+**C++ Side**
+
+Compile arrow minimal library:
+
+.. code-block:: shell
+
+    git clone https://github.com/apache/arrow.git
+    cd arrow/cpp
+    cmake --preset -N ninja-debug-minimal
+    mkdir build   # from inside the `cpp` subdirectory
+    cd build
+    cmake .. --preset ninja-debug-minimal
+    cmake --build .
+    tree debug/
+    debug/
+    ├── libarrow.800.0.0.dylib
+    ├── libarrow.800.dylib -> libarrow.800.0.0.dylib
+    └── libarrow.dylib -> libarrow.800.dylib
+
+Define c++ code CDataInterfaceLibrary.h that export functions for third party 
like Java consumer:
+
+.. code-block:: cpp
+
+    #include <iostream>
+    #include <arrow/api.h>
+
+    #define ARROW_FLAG_DICTIONARY_ORDERED 1
+    #define ARROW_FLAG_NULLABLE 2
+    #define ARROW_FLAG_MAP_KEYS_SORTED 4
+
+    using arrow::Int64Builder;
+
+    struct ArrowSchema {
+        // Array type description
+        const char* format;
+        const char* name;
+        const char* metadata;
+        int64_t flags;
+        int64_t n_children;
+        struct ArrowSchema** children;
+        struct ArrowSchema* dictionary;
+
+        // Release callback
+        void (*release)(struct ArrowSchema*);
+        // Opaque producer-specific data
+        void* private_data;
+    };
+
+    struct ArrowArray {
+        // Array data description
+        int64_t length;
+        int64_t null_count;
+        int64_t offset;
+        int64_t n_buffers;
+        int64_t n_children;
+        const void** buffers;
+        struct ArrowArray** children;
+        struct ArrowArray* dictionary;
+
+        // Release callback
+        void (*release)(struct ArrowArray*);
+        // Opaque producer-specific data
+        void* private_data;
+    };
+
+    static void release_int32_type(struct ArrowSchema* schema) {
+        // Mark released
+        schema->release = NULL;
+    }
+
+    void export_int32_type(struct ArrowSchema* schema) {
+        *schema = (struct ArrowSchema) {
+                // Type description
+                .format = "l",
+                .name = "",
+                .metadata = NULL,
+                .flags = 0,
+                .n_children = 0,
+                .children = NULL,
+                .dictionary = NULL,
+                // Bookkeeping
+                .release = &release_int32_type
+        };
+        std::cout << "C Data - Schema Pointer = " << schema << std::endl;
+    }
+
+    static void release_int32_array(struct ArrowArray* array) {
+        assert(array->n_buffers == 2);
+        // Free the buffers and the buffers array
+        free((void *) array->buffers[1]);
+        free(array->buffers);
+        // Mark released
+        array->release = NULL;
+    }
+
+    void export_int32_array(struct ArrowArray* array) {
+        arrow::Int64Builder builder;
+        builder.Append(1);
+        builder.Append(2);
+        builder.Append(3);
+        builder.AppendNull();
+        builder.Append(5);
+        builder.Append(6);
+        builder.Append(7);
+        builder.Append(8);
+
+        auto maybe_array = builder.Finish();
+        std::shared_ptr<arrow::Array> array_arrow = *maybe_array;
+        auto int64_array = 
std::static_pointer_cast<arrow::Int64Array>(array_arrow);
+        const int64_t* data = int64_array->raw_values();
+        std::cout << "Data To Exchange Pointer = " << data << std::endl;
+        for (int j = 0; j < int64_array->length(); j++){
+            std::cout << "Data To Exchange Value[" << j << "] = " << data[j] 
<< std::endl;
+        }
+
+        *array = (struct ArrowArray) {
+                // Data description
+                .length = int64_array->length(),
+                .offset = 0,
+                .null_count = 0,
+                .n_buffers = 2,
+                .n_children = 0,
+                .children = NULL,
+                .dictionary = NULL,
+                // Bookkeeping
+                .release = &release_int32_array
+        };
+
+        // Allocate list of buffers
+        array->buffers = (const void**) malloc(sizeof(void*) * 
array->n_buffers);
+        assert(array->buffers != NULL);
+        array->buffers[0] = NULL;  // no nulls, null bitmap can be omitted
+        array->buffers[1] = data;
+
+        std::cout << "C Data - Array Pointer = " << array << std::endl;
+        std::cout << "C Data - Array Data Pointer Buffer array->buffers[1] = " 
<< array->buffers[1] << std::endl;
+    }
+
+**Java Side**
+
+Define Java code CDataInterfaceLibraryConfig.java that consume by JNI C++ 
functions exported through
+C Data Interface:

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]

Reply via email to