|
| 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 | +#ifndef R_NANOARROW_VCTR_BUILDER_BLOB_H_INCLUDED |
| 19 | +#define R_NANOARROW_VCTR_BUILDER_BLOB_H_INCLUDED |
| 20 | + |
| 21 | +#define R_NO_REMAP |
| 22 | +#include <R.h> |
| 23 | +#include <Rinternals.h> |
| 24 | + |
| 25 | +#include "vctr_builder_base.h" |
| 26 | + |
| 27 | +class BlobBuilder : public VctrBuilder { |
| 28 | + public: |
| 29 | + explicit BlobBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_BLOB, ptype_sexp) {} |
| 30 | + |
| 31 | + ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override { |
| 32 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error)); |
| 33 | + SEXP value = PROTECT(Rf_allocVector(VECSXP, n)); |
| 34 | + SetValue(value); |
| 35 | + UNPROTECT(1); |
| 36 | + return NANOARROW_OK; |
| 37 | + } |
| 38 | + |
| 39 | + ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override { |
| 40 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error)); |
| 41 | + R_xlen_t length = array_view_.length; |
| 42 | + |
| 43 | + switch (array_view_.storage_type) { |
| 44 | + case NANOARROW_TYPE_NA: |
| 45 | + // Works because lists are filled with R_NilValue by default |
| 46 | + // when allocated. |
| 47 | + return NANOARROW_OK; |
| 48 | + case NANOARROW_TYPE_STRING: |
| 49 | + case NANOARROW_TYPE_LARGE_STRING: |
| 50 | + case NANOARROW_TYPE_BINARY: |
| 51 | + case NANOARROW_TYPE_LARGE_BINARY: |
| 52 | + break; |
| 53 | + default: |
| 54 | + return ENOTSUP; |
| 55 | + } |
| 56 | + |
| 57 | + struct ArrowBufferView item; |
| 58 | + SEXP item_sexp; |
| 59 | + for (R_xlen_t i = 0; i < length; i++) { |
| 60 | + if (!ArrowArrayViewIsNull(&array_view_, i)) { |
| 61 | + item = ArrowArrayViewGetBytesUnsafe(&array_view_, i); |
| 62 | + item_sexp = PROTECT(Rf_allocVector(RAWSXP, item.size_bytes)); |
| 63 | + memcpy(RAW(item_sexp), item.data.data, item.size_bytes); |
| 64 | + SET_VECTOR_ELT(value_, value_size_ + i, item_sexp); |
| 65 | + UNPROTECT(1); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return NANOARROW_OK; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +#endif |
0 commit comments