A garbage-collected immutable UTF-8 string over owned native memory, originating from a Java String.
| 36 | * A garbage-collected immutable UTF-8 string over owned native memory, originating from a Java String. |
| 37 | */ |
| 38 | public class GcUtf8String implements DirectUtf8Sequence { |
| 39 | private static final long BUFFER_ADDRESS_OFFSET; |
| 40 | @SuppressWarnings("FieldCanBeLocal") // We need to hold a reference to `buffer` or it will be GC'd. |
| 41 | private final ByteBuffer buffer; // We use a ByteBuffer here, instead of a ptr, to avoid Closeable requirement. |
| 42 | @NotNull |
| 43 | private final String original; |
| 44 | private final long ptr; |
| 45 | private final int size; |
| 46 | private final long zeroPaddedSixPrefix; |
| 47 | |
| 48 | public GcUtf8String(@NotNull String original) { |
| 49 | // ***** NOTE ***** |
| 50 | // This class causes garbage collection. |
| 51 | // It should be used with care. |
| 52 | // It is currently intended to be used for the `dirName` and `tableName` fields |
| 53 | // of `TableToken` and similar things. |
| 54 | this.original = original; |
| 55 | final byte[] bytes = original.getBytes(StandardCharsets.UTF_8); |
| 56 | this.buffer = ByteBuffer.allocateDirect(bytes.length); |
| 57 | this.buffer.put(bytes); |
| 58 | this.buffer.rewind(); |
| 59 | this.ptr = Unsafe.getLong(this.buffer, BUFFER_ADDRESS_OFFSET); |
| 60 | this.size = bytes.length; |
| 61 | this.zeroPaddedSixPrefix = Utf8s.zeroPaddedSixPrefix(this); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public @NotNull CharSequence asAsciiCharSequence() { |
| 66 | return original; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean equals(Object o) { |
| 71 | if (this == o) { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | if (o == null || getClass() != o.getClass()) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | final GcUtf8String that = (GcUtf8String) o; |
| 80 | return original.equals(that.original); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return original.hashCode(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean isAscii() { |
| 90 | return original.length() == size; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public long ptr() { |
| 95 | return ptr; |
nothing calls this directly
no test coverage detected
searching dependent graphs…