|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datasketches.common; |
| 21 | + |
| 22 | +import static java.lang.foreign.ValueLayout.JAVA_BYTE; |
| 23 | +import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED; |
| 24 | +import static org.apache.datasketches.common.ByteArrayUtil.copyBytes; |
| 25 | +import static org.apache.datasketches.common.ByteArrayUtil.putIntLE; |
| 26 | + |
| 27 | +import java.lang.foreign.MemorySegment; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * Methods of serializing and deserializing arrays of String. |
| 33 | + * This class serializes strings in UTF-8 format, which is more compact compared to |
| 34 | + * {@link ArrayOfUtf16StringsSerDe}. In an extreme case when all strings are in ASCII, |
| 35 | + * this method is 2 times more compact, but it takes more time to encode and decode |
| 36 | + * by a factor of 1.5 to 2. |
| 37 | + * |
| 38 | + * <p>The serialization |
| 39 | + * |
| 40 | + * @author Alexander Saydakov |
| 41 | + */ |
| 42 | +public class ArrayOfStringsSerDe2 extends ArrayOfItemsSerDe2<String> { |
| 43 | + |
| 44 | + @Override |
| 45 | + public byte[] serializeToByteArray(final String item) { |
| 46 | + Objects.requireNonNull(item, "Item must not be null"); |
| 47 | + if (item.isEmpty()) { return new byte[] { 0, 0, 0, 0 }; } |
| 48 | + final byte[] utf8ByteArr = item.getBytes(StandardCharsets.UTF_8); |
| 49 | + final int numBytes = utf8ByteArr.length; |
| 50 | + final byte[] out = new byte[numBytes + Integer.BYTES]; |
| 51 | + copyBytes(utf8ByteArr, 0, out, 4, numBytes); |
| 52 | + putIntLE(out, 0, numBytes); |
| 53 | + return out; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public byte[] serializeToByteArray(final String[] items) { |
| 58 | + Objects.requireNonNull(items, "Items must not be null"); |
| 59 | + if (items.length == 0) { return new byte[0]; } |
| 60 | + int totalBytes = 0; |
| 61 | + final int numItems = items.length; |
| 62 | + final byte[][] serialized2DArray = new byte[numItems][]; |
| 63 | + for (int i = 0; i < numItems; i++) { |
| 64 | + serialized2DArray[i] = items[i].getBytes(StandardCharsets.UTF_8); |
| 65 | + totalBytes += serialized2DArray[i].length + Integer.BYTES; |
| 66 | + } |
| 67 | + final byte[] bytesOut = new byte[totalBytes]; |
| 68 | + int offset = 0; |
| 69 | + for (int i = 0; i < numItems; i++) { |
| 70 | + final int utf8len = serialized2DArray[i].length; |
| 71 | + putIntLE(bytesOut, offset, utf8len); |
| 72 | + offset += Integer.BYTES; |
| 73 | + copyBytes(serialized2DArray[i], 0, bytesOut, offset, utf8len); |
| 74 | + offset += utf8len; |
| 75 | + } |
| 76 | + return bytesOut; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String[] deserializeFromMemorySegment(final MemorySegment seg, final int numItems) { |
| 81 | + return deserializeFromMemorySegment(seg, 0, numItems); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String[] deserializeFromMemorySegment(final MemorySegment seg, final long offsetBytes, final int numItems) { |
| 86 | + Objects.requireNonNull(seg, "MemorySegment must not be null"); |
| 87 | + if (numItems <= 0) { return new String[0]; } |
| 88 | + final String[] array = new String[numItems]; |
| 89 | + long offset = offsetBytes; |
| 90 | + for (int i = 0; i < numItems; i++) { |
| 91 | + Util.checkBounds(offset, Integer.BYTES, seg.byteSize()); |
| 92 | + final int strLength = seg.get(JAVA_INT_UNALIGNED, offset); |
| 93 | + offset += Integer.BYTES; |
| 94 | + final byte[] utf8Bytes = new byte[strLength]; |
| 95 | + Util.checkBounds(offset, strLength, seg.byteSize()); |
| 96 | + MemorySegment.copy(seg, JAVA_BYTE, offset, utf8Bytes, 0, strLength); |
| 97 | + offset += strLength; |
| 98 | + array[i] = new String(utf8Bytes, StandardCharsets.UTF_8); |
| 99 | + } |
| 100 | + return array; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int sizeOf(final String item) { |
| 105 | + Objects.requireNonNull(item, "Item must not be null"); |
| 106 | + if (item.isEmpty()) { return Integer.BYTES; } |
| 107 | + return item.getBytes(StandardCharsets.UTF_8).length + Integer.BYTES; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int sizeOf(final MemorySegment seg, final long offsetBytes, final int numItems) { |
| 112 | + Objects.requireNonNull(seg, "MemorySegment must not be null"); |
| 113 | + if (numItems <= 0) { return 0; } |
| 114 | + long offset = offsetBytes; |
| 115 | + final long segCap = seg.byteSize(); |
| 116 | + for (int i = 0; i < numItems; i++) { |
| 117 | + Util.checkBounds(offset, Integer.BYTES, segCap); |
| 118 | + final int itemLenBytes = seg.get(JAVA_INT_UNALIGNED, offset); |
| 119 | + offset += Integer.BYTES; |
| 120 | + Util.checkBounds(offset, itemLenBytes, segCap); |
| 121 | + offset += itemLenBytes; |
| 122 | + } |
| 123 | + return (int)(offset - offsetBytes); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String toString(final String item) { |
| 128 | + if (item == null) { return "null"; } |
| 129 | + return item; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Class<String> getClassOfT() { return String.class; } |
| 134 | +} |
0 commit comments