-
Notifications
You must be signed in to change notification settings - Fork 140
Parallelize graph writes #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
a67bfcf
16aaaf6
9b4dea1
7433443
f2df4da
b45766c
09bc7d9
9bdd64c
2cfa7c9
4646ba5
ec68bda
a0c89a7
67edfae
09eb70f
7daf69e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.disk; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
|
|
||
| /** | ||
| * An IndexWriter implementation backed by a ByteBuffer for in-memory record building. | ||
| * This allows existing Feature.writeInline() implementations to write to memory buffers | ||
| * that can later be bulk-written to disk. | ||
| * <p> | ||
| * Byte order is set to BIG_ENDIAN to match Java's DataOutput specification and ensure | ||
| * cross-platform compatibility. | ||
| * <p> | ||
| * Not thread-safe. Each thread should use its own instance. | ||
| */ | ||
| public class ByteBufferIndexWriter implements IndexWriter { | ||
| private final ByteBuffer buffer; | ||
| private final int initialPosition; | ||
|
|
||
| /** | ||
| * Creates a writer that writes to the given buffer starting at its current position. | ||
| * The buffer's position will be advanced as data is written. | ||
| * The buffer's byte order is set to BIG_ENDIAN to match DataOutput behavior. | ||
| */ | ||
| public ByteBufferIndexWriter(ByteBuffer buffer) { | ||
| this.buffer = buffer; | ||
| this.buffer.order(ByteOrder.BIG_ENDIAN); | ||
| this.initialPosition = buffer.position(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a writer with a new heap ByteBuffer of the given capacity. | ||
| * The buffer uses BIG_ENDIAN byte order. | ||
| */ | ||
| public static ByteBufferIndexWriter allocate(int capacity) { | ||
| ByteBuffer buffer = ByteBuffer.allocate(capacity); | ||
| buffer.order(ByteOrder.BIG_ENDIAN); | ||
| return new ByteBufferIndexWriter(buffer); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a writer with a new direct ByteBuffer of the given capacity. | ||
| * The buffer uses BIG_ENDIAN byte order. | ||
| */ | ||
| public static ByteBufferIndexWriter allocateDirect(int capacity) { | ||
| ByteBuffer buffer = ByteBuffer.allocateDirect(capacity); | ||
| buffer.order(ByteOrder.BIG_ENDIAN); | ||
| return new ByteBufferIndexWriter(buffer); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the underlying buffer. The buffer's position will be at the end of written data. | ||
| */ | ||
| public ByteBuffer getBuffer() { | ||
| return buffer; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a read-only view of the written data (from initial position to current position). | ||
| */ | ||
| public ByteBuffer getWrittenData() { | ||
| int currentPos = buffer.position(); | ||
| buffer.position(initialPosition); | ||
| ByteBuffer slice = buffer.slice(); | ||
| slice.limit(currentPos - initialPosition); | ||
| buffer.position(currentPos); | ||
| return slice.asReadOnlyBuffer(); | ||
| } | ||
|
|
||
| /** | ||
| * Resets the buffer position to the initial position, allowing reuse. | ||
| */ | ||
| public void reset() { | ||
| buffer.position(initialPosition); | ||
| } | ||
|
|
||
| @Override | ||
| public long position() { | ||
| return buffer.position() - initialPosition; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // No-op for ByteBuffer | ||
| } | ||
|
|
||
| // DataOutput methods | ||
|
|
||
| @Override | ||
| public void write(int b) { | ||
| buffer.put((byte) b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b) { | ||
| buffer.put(b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) { | ||
| buffer.put(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBoolean(boolean v) { | ||
| buffer.put((byte) (v ? 1 : 0)); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeByte(int v) { | ||
| buffer.put((byte) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeShort(int v) { | ||
| buffer.putShort((short) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeChar(int v) { | ||
| buffer.putChar((char) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeInt(int v) { | ||
| buffer.putInt(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeLong(long v) { | ||
| buffer.putLong(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeFloat(float v) { | ||
| buffer.putFloat(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeDouble(double v) { | ||
| buffer.putDouble(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBytes(String s) { | ||
| int len = s.length(); | ||
| for (int i = 0; i < len; i++) { | ||
| buffer.put((byte) s.charAt(i)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeChars(String s) { | ||
| int len = s.length(); | ||
| for (int i = 0; i < len; i++) { | ||
| buffer.putChar(s.charAt(i)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeUTF(String s) throws IOException { | ||
| // Use standard DataOutputStream UTF encoding | ||
| byte[] bytes = s.getBytes("UTF-8"); | ||
| int utflen = bytes.length; | ||
|
|
||
| if (utflen > 65535) { | ||
MarkWolters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IOException("encoded string too long: " + utflen + " bytes"); | ||
| } | ||
|
|
||
| buffer.putShort((short) utflen); | ||
| buffer.put(bytes); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.graph.disk; | ||
|
|
||
| /** | ||
| * Enum defining the available types of graph index writers. | ||
| * <p> | ||
| * Different writer types offer different tradeoffs between performance, | ||
| * compatibility, and features. | ||
| */ | ||
| public enum GraphIndexWriterTypes { | ||
MarkWolters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Sequential on-disk writer optimized for write-once scenarios. | ||
| * Writes all data sequentially without seeking back, making it suitable | ||
| * for cloud storage or systems that optimize for sequential I/O. | ||
| * Writes header as footer. Does not support incremental updates. | ||
| * Accepts any IndexWriter. | ||
| */ | ||
| ON_DISK_SEQUENTIAL, | ||
|
|
||
| /** | ||
| * Parallel on-disk writer that uses asynchronous I/O for improved throughput. | ||
| * Builds records in parallel across multiple threads and writes them | ||
| * asynchronously using AsynchronousFileChannel. | ||
| * Requires a Path to be provided for async file channel access. | ||
| */ | ||
| ON_DISK_PARALLEL | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the prefix "ON_DISK" in this enum? Additionally, would it be better to use RANDOM instead of PARALLEL, to better indicate the default behavior? Since parallelization is optional... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically of course we can call it whatever we want. The naming is based on the fact that we have two main classes extending |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.