|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.jbellis.jvector.disk; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.foreign.Arena; |
| 21 | +import java.lang.foreign.MemorySegment; |
| 22 | +import java.lang.foreign.ValueLayout; |
| 23 | +import java.lang.foreign.ValueLayout.OfFloat; |
| 24 | +import java.lang.foreign.ValueLayout.OfInt; |
| 25 | +import java.lang.foreign.ValueLayout.OfLong; |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.nio.ByteOrder; |
| 28 | +import java.nio.channels.FileChannel; |
| 29 | +import java.nio.channels.FileChannel.MapMode; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.nio.file.StandardOpenOption; |
| 32 | + |
| 33 | +/** |
| 34 | + * {@link MemorySegment} based implementation of RandomAccessReader. |
| 35 | + * MemorySegmentReader doesn't have 2GB file size limitation of {@link SimpleMappedReader}. |
| 36 | + */ |
| 37 | +public class MemorySegmentReader implements RandomAccessReader { |
| 38 | + |
| 39 | + private static final OfInt intLayout = ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN); |
| 40 | + private static final OfFloat floatLayout = ValueLayout.JAVA_FLOAT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN); |
| 41 | + private static final OfLong longLayout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN); |
| 42 | + |
| 43 | + private final Arena arena; |
| 44 | + private final MemorySegment memory; |
| 45 | + private long position = 0; |
| 46 | + |
| 47 | + public MemorySegmentReader(Path path) throws IOException { |
| 48 | + arena = Arena.ofShared(); |
| 49 | + try (var ch = FileChannel.open(path, StandardOpenOption.READ)) { |
| 50 | + memory = ch.map(MapMode.READ_ONLY, 0L, ch.size(), arena); |
| 51 | + } catch (Exception e) { |
| 52 | + arena.close(); |
| 53 | + throw e; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private MemorySegmentReader(Arena arena, MemorySegment memory) { |
| 58 | + this.arena = arena; |
| 59 | + this.memory = memory; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void seek(long offset) { |
| 64 | + this.position = offset; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public long getPosition() { |
| 69 | + return position; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void readFully(float[] buffer) { |
| 74 | + MemorySegment.copy(memory, floatLayout, position, buffer, 0, buffer.length); |
| 75 | + position += buffer.length * 4L; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void readFully(byte[] b) { |
| 80 | + MemorySegment.copy(memory, ValueLayout.JAVA_BYTE, position, b, 0, b.length); |
| 81 | + position += b.length; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void readFully(ByteBuffer buffer) { |
| 86 | + var remaining = buffer.remaining(); |
| 87 | + var slice = memory.asSlice(position, remaining).asByteBuffer(); |
| 88 | + buffer.put(slice); |
| 89 | + position += remaining; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void readFully(long[] vector) { |
| 94 | + MemorySegment.copy(memory, longLayout, position, vector, 0, vector.length); |
| 95 | + position += vector.length * 8L; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public int readInt() { |
| 100 | + var k = memory.get(intLayout, position); |
| 101 | + position += 4; |
| 102 | + return k; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public float readFloat() { |
| 107 | + var f = memory.get(floatLayout, position); |
| 108 | + position += 4; |
| 109 | + return f; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void read(int[] ints, int offset, int count) { |
| 114 | + MemorySegment.copy(memory, intLayout, position, ints, offset, count); |
| 115 | + position += count * 4L; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void read(float[] floats, int offset, int count) { |
| 120 | + MemorySegment.copy(memory, floatLayout, position, floats, offset, count); |
| 121 | + position += count * 4L; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Loads the contents of the mapped segment into physical memory. |
| 126 | + * This is a best-effort mechanism. |
| 127 | + */ |
| 128 | + public void loadMemory() { |
| 129 | + memory.load(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void close() { |
| 134 | + arena.close(); |
| 135 | + } |
| 136 | + |
| 137 | + public MemorySegmentReader duplicate() { |
| 138 | + return new MemorySegmentReader(arena, memory); |
| 139 | + } |
| 140 | +} |
0 commit comments