|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import Foundation |
| 19 | +import ArrowC |
| 20 | +import Atomics |
| 21 | + |
| 22 | +extension String { |
| 23 | + var cstring: UnsafePointer<CChar> { |
| 24 | + (self as NSString).cString(using: String.Encoding.utf8.rawValue)! |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// The memory used by UnsafeAtomic is not automatically |
| 29 | +// reclaimed. Since this value is initialized once |
| 30 | +// and used until the program/app is closed it's |
| 31 | +// memory will be released on program/app exit |
| 32 | +let exportDataCounter: UnsafeAtomic<Int> = .create(0) |
| 33 | + |
| 34 | +public class ArrowCExporter { |
| 35 | + private class ExportData { |
| 36 | + let id: Int |
| 37 | + init() { |
| 38 | + id = exportDataCounter.loadThenWrappingIncrement(ordering: .relaxed) |
| 39 | + ArrowCExporter.exportedData[id] = self |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private class ExportSchema: ExportData { |
| 44 | + public let arrowTypeName: UnsafePointer<CChar> |
| 45 | + public let name: UnsafePointer<CChar> |
| 46 | + private let arrowType: ArrowType |
| 47 | + init(_ arrowType: ArrowType, name: String = "") throws { |
| 48 | + self.arrowType = arrowType |
| 49 | + self.arrowTypeName = try arrowType.cDataFormatId.cstring |
| 50 | + self.name = name.cstring |
| 51 | + super.init() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private class ExportArray: ExportData { |
| 56 | + private let arrowData: ArrowData |
| 57 | + private(set) var data = [UnsafeRawPointer?]() |
| 58 | + private(set) var buffers: UnsafeMutablePointer<UnsafeRawPointer?> |
| 59 | + init(_ arrowData: ArrowData) { |
| 60 | + // keep a reference to the ArrowData |
| 61 | + // obj so the memory doesn't get |
| 62 | + // deallocated |
| 63 | + self.arrowData = arrowData |
| 64 | + for arrowBuffer in arrowData.buffers { |
| 65 | + data.append(arrowBuffer.rawPointer) |
| 66 | + } |
| 67 | + |
| 68 | + self.buffers = UnsafeMutablePointer(mutating: data) |
| 69 | + super.init() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static var exportedData = [Int: ExportData]() |
| 74 | + public init() {} |
| 75 | + |
| 76 | + public func exportType(_ cSchema: inout ArrowC.ArrowSchema, arrowType: ArrowType, name: String = "") -> |
| 77 | + Result<Bool, ArrowError> { |
| 78 | + do { |
| 79 | + let exportSchema = try ExportSchema(arrowType, name: name) |
| 80 | + cSchema.format = exportSchema.arrowTypeName |
| 81 | + cSchema.name = exportSchema.name |
| 82 | + cSchema.private_data = |
| 83 | + UnsafeMutableRawPointer(mutating: UnsafeRawPointer(bitPattern: exportSchema.id)) |
| 84 | + cSchema.release = {(data: UnsafeMutablePointer<ArrowC.ArrowSchema>?) in |
| 85 | + let arraySchema = data!.pointee |
| 86 | + let exportId = Int(bitPattern: arraySchema.private_data) |
| 87 | + guard ArrowCExporter.exportedData[exportId] != nil else { |
| 88 | + fatalError("Export schema not found with id \(exportId)") |
| 89 | + } |
| 90 | + |
| 91 | + // the data associated with this exportSchema object |
| 92 | + // which includes the C strings for the format and name |
| 93 | + // be deallocated upon removal |
| 94 | + ArrowCExporter.exportedData.removeValue(forKey: exportId) |
| 95 | + ArrowC.ArrowSwiftClearReleaseSchema(data) |
| 96 | + } |
| 97 | + } catch { |
| 98 | + return .failure(.unknownError("\(error)")) |
| 99 | + } |
| 100 | + return .success(true) |
| 101 | + } |
| 102 | + |
| 103 | + public func exportField(_ schema: inout ArrowC.ArrowSchema, field: ArrowField) -> |
| 104 | + Result<Bool, ArrowError> { |
| 105 | + return exportType(&schema, arrowType: field.type, name: field.name) |
| 106 | + } |
| 107 | + |
| 108 | + public func exportArray(_ cArray: inout ArrowC.ArrowArray, arrowData: ArrowData) { |
| 109 | + let exportArray = ExportArray(arrowData) |
| 110 | + cArray.buffers = exportArray.buffers |
| 111 | + cArray.length = Int64(arrowData.length) |
| 112 | + cArray.null_count = Int64(arrowData.nullCount) |
| 113 | + cArray.n_buffers = Int64(arrowData.buffers.count) |
| 114 | + cArray.n_children = 0 |
| 115 | + cArray.children = nil |
| 116 | + cArray.dictionary = nil |
| 117 | + cArray.private_data = |
| 118 | + UnsafeMutableRawPointer(mutating: UnsafeRawPointer(bitPattern: exportArray.id)) |
| 119 | + cArray.release = {(data: UnsafeMutablePointer<ArrowC.ArrowArray>?) in |
| 120 | + let arrayData = data!.pointee |
| 121 | + let exportId = Int(bitPattern: arrayData.private_data) |
| 122 | + guard ArrowCExporter.exportedData[exportId] != nil else { |
| 123 | + fatalError("Export data not found with id \(exportId)") |
| 124 | + } |
| 125 | + |
| 126 | + // the data associated with this exportArray object |
| 127 | + // which includes the entire arrowData object |
| 128 | + // and the buffers UnsafeMutablePointer[] will |
| 129 | + // be deallocated upon removal |
| 130 | + ArrowCExporter.exportedData.removeValue(forKey: exportId) |
| 131 | + ArrowC.ArrowSwiftClearReleaseArray(data) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments