|
| 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 | +import cython |
| 18 | +from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING, PyBytes_GET_SIZE |
| 19 | +from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free |
| 20 | +from libc.string cimport memcpy |
| 21 | +from libc.stdint cimport uint8_t, uint64_t, int64_t |
| 22 | + |
| 23 | +from pyiceberg.avro import STRUCT_DOUBLE, STRUCT_FLOAT |
| 24 | + |
| 25 | +cdef uint64_t _INITIAL_CAPACITY = 1024 |
| 26 | + |
| 27 | + |
| 28 | +@cython.final |
| 29 | +cdef class CythonBinaryEncoder: |
| 30 | + """In-memory BinaryEncoder that writes to a growable C buffer. |
| 31 | +
|
| 32 | + Drop-in replacement for BinaryEncoder for the block-encoding path: |
| 33 | + exposes the same write_* methods the Writer tree calls, plus |
| 34 | + getvalue() to materialise the encoded bytes once at the end. |
| 35 | + """ |
| 36 | + |
| 37 | + cdef unsigned char *_data |
| 38 | + cdef uint64_t _size |
| 39 | + cdef uint64_t _capacity |
| 40 | + |
| 41 | + def __cinit__(self): |
| 42 | + self._data = <unsigned char *> PyMem_Malloc(_INITIAL_CAPACITY) |
| 43 | + if not self._data: |
| 44 | + raise MemoryError() |
| 45 | + self._size = 0 |
| 46 | + self._capacity = _INITIAL_CAPACITY |
| 47 | + |
| 48 | + def __dealloc__(self): |
| 49 | + PyMem_Free(self._data) |
| 50 | + |
| 51 | + cdef inline int _ensure(self, uint64_t n) except -1: |
| 52 | + cdef uint64_t need = self._size + n |
| 53 | + if need <= self._capacity: |
| 54 | + return 0 |
| 55 | + cdef uint64_t cap = self._capacity |
| 56 | + while cap < need: |
| 57 | + cap <<= 1 |
| 58 | + cdef unsigned char *grown = <unsigned char *> PyMem_Realloc(self._data, cap) |
| 59 | + if not grown: |
| 60 | + raise MemoryError() |
| 61 | + self._data = grown |
| 62 | + self._capacity = cap |
| 63 | + return 0 |
| 64 | + |
| 65 | + cpdef bytes getvalue(self): |
| 66 | + return PyBytes_FromStringAndSize(<char *> self._data, self._size) |
| 67 | + |
| 68 | + cpdef void write(self, bytes b): |
| 69 | + cdef Py_ssize_t n = PyBytes_GET_SIZE(b) |
| 70 | + self._ensure(n) |
| 71 | + memcpy(self._data + self._size, PyBytes_AS_STRING(b), n) |
| 72 | + self._size += n |
| 73 | + |
| 74 | + cpdef void write_boolean(self, bint v): |
| 75 | + self._ensure(1) |
| 76 | + self._data[self._size] = 1 if v else 0 |
| 77 | + self._size += 1 |
| 78 | + |
| 79 | + cpdef void write_int(self, int64_t v): |
| 80 | + # zigzag then base-128 varint; a 64-bit value needs at most 10 bytes |
| 81 | + self._ensure(10) |
| 82 | + cdef uint64_t uv = <uint64_t> v |
| 83 | + cdef uint64_t datum = (uv << 1) ^ (0 - (uv >> 63)) |
| 84 | + cdef unsigned char *p = self._data + self._size |
| 85 | + while datum & <uint64_t> ~0x7F: |
| 86 | + p[0] = <uint8_t> ((datum & 0x7F) | 0x80) |
| 87 | + p += 1 |
| 88 | + datum >>= 7 |
| 89 | + p[0] = <uint8_t> datum |
| 90 | + p += 1 |
| 91 | + self._size = p - self._data |
| 92 | + |
| 93 | + def write_float(self, v: float) -> None: |
| 94 | + self.write(STRUCT_FLOAT.pack(v)) |
| 95 | + |
| 96 | + def write_double(self, v: float) -> None: |
| 97 | + self.write(STRUCT_DOUBLE.pack(v)) |
| 98 | + |
| 99 | + cpdef void write_bytes(self, b): |
| 100 | + cdef bytes bb = bytes(b) if type(b) is not bytes else b |
| 101 | + cdef Py_ssize_t n = PyBytes_GET_SIZE(bb) |
| 102 | + self.write_int(n) |
| 103 | + self._ensure(n) |
| 104 | + memcpy(self._data + self._size, PyBytes_AS_STRING(bb), n) |
| 105 | + self._size += n |
| 106 | + |
| 107 | + def write_utf8(self, s) -> None: |
| 108 | + self.write_bytes(s.encode("utf-8")) |
| 109 | + |
| 110 | + def write_uuid(self, uuid) -> None: |
| 111 | + cdef bytes b = uuid.bytes |
| 112 | + if PyBytes_GET_SIZE(b) != 16: |
| 113 | + raise ValueError(f"Expected UUID to have 16 bytes, got: len({b!r})") |
| 114 | + self._ensure(16) |
| 115 | + memcpy(self._data + self._size, PyBytes_AS_STRING(b), 16) |
| 116 | + self._size += 16 |
| 117 | + |
| 118 | + def write_unknown(self, _) -> None: |
| 119 | + pass |
0 commit comments