|
| 1 | +package io.github.dfa1.vortex.encoding; |
| 2 | + |
| 3 | +import io.github.dfa1.vortex.core.ArrayStats; |
| 4 | +import io.github.dfa1.vortex.core.DType; |
| 5 | +import io.github.dfa1.vortex.core.PType; |
| 6 | +import io.github.dfa1.vortex.core.VortexException; |
| 7 | +import io.github.dfa1.vortex.core.array.Array; |
| 8 | +import io.github.dfa1.vortex.core.array.ByteArray; |
| 9 | +import io.github.dfa1.vortex.core.array.DoubleArray; |
| 10 | +import io.github.dfa1.vortex.core.array.FloatArray; |
| 11 | +import io.github.dfa1.vortex.core.array.IntArray; |
| 12 | +import io.github.dfa1.vortex.core.array.LongArray; |
| 13 | +import io.github.dfa1.vortex.core.array.ShortArray; |
| 14 | +import io.github.dfa1.vortex.core.array.StructArray; |
| 15 | + |
| 16 | +import java.lang.foreign.MemorySegment; |
| 17 | +import java.lang.foreign.SegmentAllocator; |
| 18 | +import java.lang.foreign.ValueLayout; |
| 19 | +import java.nio.ByteOrder; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/// Encoding for {@code vortex.chunked} — a segment-level chunked array. |
| 24 | +/// |
| 25 | +/// <p>Wire format (per Rust vtable): |
| 26 | +/// <ul> |
| 27 | +/// <li>Metadata: empty bytes |
| 28 | +/// <li>Buffers: 0 |
| 29 | +/// <li>Children: N+1 — {@code children[0]} is the chunk offsets (U64, non-nullable, |
| 30 | +/// length = nchunks+1, cumulative from 0); {@code children[1..N]} are the chunk arrays. |
| 31 | +/// </ul> |
| 32 | +public final class ChunkedEncoding implements Encoding { |
| 33 | + |
| 34 | + @Override |
| 35 | + public EncodingId encodingId() { |
| 36 | + return EncodingId.VORTEX_CHUNKED; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean accepts(DType dtype) { |
| 41 | + return dtype instanceof DType.Primitive || dtype instanceof DType.Struct; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public EncodeResult encode(DType dtype, Object data) { |
| 46 | + throw new UnsupportedOperationException("encode not yet implemented for " + encodingId()); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Array decode(DecodeContext ctx) { |
| 51 | + return Decoder.decode(ctx); |
| 52 | + } |
| 53 | + |
| 54 | + private static final class Decoder { |
| 55 | + |
| 56 | + private static final ValueLayout.OfLong LE_LONG = |
| 57 | + ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 58 | + |
| 59 | + static Array decode(DecodeContext ctx) { |
| 60 | + int nchildren = ctx.node().children().length; |
| 61 | + if (nchildren < 1) { |
| 62 | + throw new VortexException(EncodingId.VORTEX_CHUNKED, |
| 63 | + "needs at least one child (chunk offsets)"); |
| 64 | + } |
| 65 | + int nchunks = nchildren - 1; |
| 66 | + long[] offsets = readOffsets(ctx, nchunks); |
| 67 | + |
| 68 | + DType dtype = ctx.dtype(); |
| 69 | + List<Array> chunks = new ArrayList<>(nchunks); |
| 70 | + for (int i = 0; i < nchunks; i++) { |
| 71 | + long chunkLen = offsets[i + 1] - offsets[i]; |
| 72 | + ArrayNode chunkNode = ctx.node().children()[i + 1]; |
| 73 | + var chunkCtx = new DecodeContext( |
| 74 | + chunkNode, dtype, chunkLen, ctx.segmentBuffers(), ctx.registry(), ctx.arena()); |
| 75 | + chunks.add(ctx.registry().decode(chunkCtx)); |
| 76 | + } |
| 77 | + |
| 78 | + return concat(chunks, dtype, ctx.rowCount(), ctx.arena()); |
| 79 | + } |
| 80 | + |
| 81 | + private static long[] readOffsets(DecodeContext ctx, int nchunks) { |
| 82 | + ArrayNode offsetsNode = ctx.node().children()[0]; |
| 83 | + DType u64 = new DType.Primitive(PType.U64, false); |
| 84 | + var offsetsCtx = new DecodeContext( |
| 85 | + offsetsNode, u64, nchunks + 1L, ctx.segmentBuffers(), ctx.registry(), ctx.arena()); |
| 86 | + Array offsetsArray = ctx.registry().decode(offsetsCtx); |
| 87 | + MemorySegment offsetsBuf = offsetsArray.buffer(0); |
| 88 | + long[] offsets = new long[nchunks + 1]; |
| 89 | + for (int i = 0; i <= nchunks; i++) { |
| 90 | + offsets[i] = offsetsBuf.get(LE_LONG, (long) i * 8); |
| 91 | + } |
| 92 | + return offsets; |
| 93 | + } |
| 94 | + |
| 95 | + private static Array concat(List<Array> chunks, DType dtype, long totalRows, SegmentAllocator arena) { |
| 96 | + if (dtype instanceof DType.Primitive pt) { |
| 97 | + return concatPrimitive(chunks, pt, dtype, totalRows, arena); |
| 98 | + } |
| 99 | + if (dtype instanceof DType.Struct struct) { |
| 100 | + return concatStruct(chunks, struct, totalRows, arena); |
| 101 | + } |
| 102 | + throw new VortexException(EncodingId.VORTEX_CHUNKED, |
| 103 | + "concat not supported for dtype: " + dtype); |
| 104 | + } |
| 105 | + |
| 106 | + private static Array concatPrimitive( |
| 107 | + List<Array> chunks, DType.Primitive pt, DType dtype, long totalRows, SegmentAllocator arena |
| 108 | + ) { |
| 109 | + PType ptype = pt.ptype(); |
| 110 | + MemorySegment combined = arena.allocate(totalRows * ptype.byteSize()); |
| 111 | + long byteOffset = 0; |
| 112 | + for (Array chunk : chunks) { |
| 113 | + MemorySegment src = chunk.buffer(0); |
| 114 | + MemorySegment.copy(src, 0, combined, byteOffset, src.byteSize()); |
| 115 | + byteOffset += src.byteSize(); |
| 116 | + } |
| 117 | + MemorySegment ro = combined.asReadOnly(); |
| 118 | + return switch (ptype) { |
| 119 | + case I64, U64 -> new LongArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 120 | + case I32, U32 -> new IntArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 121 | + case F64 -> new DoubleArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 122 | + case F32 -> new FloatArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 123 | + case I16, U16 -> new ShortArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 124 | + case I8, U8 -> new ByteArray(dtype, totalRows, ro, ArrayStats.empty()); |
| 125 | + default -> throw new VortexException(EncodingId.VORTEX_CHUNKED, |
| 126 | + "unsupported ptype for concat: " + ptype); |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + private static StructArray concatStruct( |
| 131 | + List<Array> chunks, DType.Struct struct, long totalRows, SegmentAllocator arena |
| 132 | + ) { |
| 133 | + int nfields = struct.fieldTypes().size(); |
| 134 | + List<Array> concatFields = new ArrayList<>(nfields); |
| 135 | + for (int f = 0; f < nfields; f++) { |
| 136 | + DType fieldDtype = struct.fieldTypes().get(f); |
| 137 | + List<Array> fieldChunks = new ArrayList<>(chunks.size()); |
| 138 | + for (Array chunk : chunks) { |
| 139 | + fieldChunks.add(((StructArray) chunk).field(f)); |
| 140 | + } |
| 141 | + concatFields.add(concat(fieldChunks, fieldDtype, totalRows, arena)); |
| 142 | + } |
| 143 | + return new StructArray(struct, totalRows, concatFields); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments