|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.nodes.arrow; |
| 42 | + |
| 43 | +import com.oracle.graal.python.builtins.objects.capsule.PyCapsule; |
| 44 | +import com.oracle.graal.python.util.PythonUtils; |
| 45 | +import sun.misc.Unsafe; |
| 46 | + |
| 47 | +import static com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess.POINTER_SIZE; |
| 48 | + |
| 49 | +/** |
| 50 | + * C Data Interface ArrowArray. |
| 51 | + * <p> |
| 52 | + * Represents a wrapper for the following C structure: |
| 53 | + * |
| 54 | + * <pre> |
| 55 | + * struct ArrowArray { |
| 56 | + * // Array data description |
| 57 | + * int64_t length; 0 |
| 58 | + * int64_t null_count; 8 |
| 59 | + * int64_t offset; 16 |
| 60 | + * int64_t n_buffers; 24 |
| 61 | + * int64_t n_children; 32 |
| 62 | + * const void** buffers; 40 |
| 63 | + * struct ArrowArray** children; 48 |
| 64 | + * struct ArrowArray* dictionary; 56 |
| 65 | + * |
| 66 | + * // Release callback |
| 67 | + * void (*release)(struct ArrowArray*); 64 |
| 68 | + * // Opaque producer-specific data |
| 69 | + * void* private_data; 72 |
| 70 | + * }; |
| 71 | + * </pre> |
| 72 | + */ |
| 73 | +public class ArrowArray { |
| 74 | + |
| 75 | + private static final Unsafe unsafe = PythonUtils.initUnsafe(); |
| 76 | + public static final byte[] CAPSULE_NAME = PyCapsule.capsuleName("arrow_array"); |
| 77 | + |
| 78 | + public static final byte NULL = 0; |
| 79 | + private static final byte SIZE_OF = 80; |
| 80 | + |
| 81 | + private static final long LENGTH_INDEX = 0; |
| 82 | + private static final long NULL_COUNT_INDEX = POINTER_SIZE; |
| 83 | + private static final long OFFSET_INDEX = 2 * POINTER_SIZE; |
| 84 | + private static final long N_BUFFERS_INDEX = 3 * POINTER_SIZE; |
| 85 | + private static final long N_CHILDREN_INDEX = 4 * POINTER_SIZE; |
| 86 | + private static final long BUFFERS_INDEX = 5 * POINTER_SIZE; |
| 87 | + private static final long CHILDREN_INDEX = 6 * POINTER_SIZE; |
| 88 | + private static final long DICTIONARY_INDEX = 7 * POINTER_SIZE; |
| 89 | + private static final long RELEASE_CALLBACK_INDEX = 8 * POINTER_SIZE; |
| 90 | + private static final long PRIVATE_DATA_INDEX = 9 * POINTER_SIZE; |
| 91 | + |
| 92 | + public final long memoryAddr; |
| 93 | + |
| 94 | + private ArrowArray(long memoryAddr) { |
| 95 | + this.memoryAddr = memoryAddr; |
| 96 | + } |
| 97 | + |
| 98 | + public static ArrowArray allocate() { |
| 99 | + var arrowArray = new ArrowArray(unsafe.allocateMemory(SIZE_OF)); |
| 100 | + arrowArray.markReleased(); |
| 101 | + return arrowArray; |
| 102 | + } |
| 103 | + |
| 104 | + public static ArrowArray allocateFromSnapshot(Snapshot snapshot) { |
| 105 | + var arrowArray = new ArrowArray(unsafe.allocateMemory(SIZE_OF)); |
| 106 | + arrowArray.load(snapshot); |
| 107 | + return arrowArray; |
| 108 | + } |
| 109 | + |
| 110 | + public static ArrowArray wrap(long arrowArrayPointer) { |
| 111 | + return new ArrowArray(arrowArrayPointer); |
| 112 | + } |
| 113 | + |
| 114 | + public void markReleased() { |
| 115 | + unsafe.putLong(memoryAddr + RELEASE_CALLBACK_INDEX, NULL); |
| 116 | + } |
| 117 | + |
| 118 | + public boolean isReleased() { |
| 119 | + return unsafe.getLong(memoryAddr + RELEASE_CALLBACK_INDEX) == NULL; |
| 120 | + } |
| 121 | + |
| 122 | + public long getBuffers() { |
| 123 | + return unsafe.getLong(memoryAddr + BUFFERS_INDEX); |
| 124 | + } |
| 125 | + |
| 126 | + public long getValueBuffer() { |
| 127 | + return unsafe.getLong(getBuffers() + POINTER_SIZE); |
| 128 | + } |
| 129 | + |
| 130 | + private void load(Snapshot snapshot) { |
| 131 | + unsafe.putLong(memoryAddr + LENGTH_INDEX, snapshot.length); |
| 132 | + unsafe.putLong(memoryAddr + NULL_COUNT_INDEX, snapshot.null_count); |
| 133 | + unsafe.putLong(memoryAddr + OFFSET_INDEX, snapshot.offset); |
| 134 | + unsafe.putLong(memoryAddr + N_BUFFERS_INDEX, snapshot.n_buffers); |
| 135 | + unsafe.putLong(memoryAddr + N_CHILDREN_INDEX, snapshot.n_children); |
| 136 | + unsafe.putLong(memoryAddr + BUFFERS_INDEX, snapshot.buffers); |
| 137 | + unsafe.putLong(memoryAddr + CHILDREN_INDEX, snapshot.children); |
| 138 | + unsafe.putLong(memoryAddr + DICTIONARY_INDEX, snapshot.dictionary); |
| 139 | + unsafe.putLong(memoryAddr + RELEASE_CALLBACK_INDEX, snapshot.release); |
| 140 | + unsafe.putLong(memoryAddr + PRIVATE_DATA_INDEX, snapshot.private_data); |
| 141 | + } |
| 142 | + |
| 143 | + public static class Snapshot { |
| 144 | + public long length = 0L; |
| 145 | + public long null_count = 0L; |
| 146 | + public long offset = 0L; |
| 147 | + public long n_buffers = 0L; |
| 148 | + public long n_children = 0L; |
| 149 | + public long buffers = 0L; |
| 150 | + public long children = 0L; |
| 151 | + public long dictionary = 0L; |
| 152 | + public long release = 0L; |
| 153 | + public long private_data = 0L; |
| 154 | + } |
| 155 | +} |
0 commit comments