-
Notifications
You must be signed in to change notification settings - Fork 139
New MutableGraphIndex and ImmutableGraphIndex interfaces #534
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
47048e1
Add MutableGraphIndex and ImmutableGraphIndex to clarify intent and h…
marianotepper d710997
Fix docs
marianotepper 672ea37
Use "level" when referring to integers instead of "layer"
marianotepper ff61ae2
Now OnHeapGraphIndex will return a ConcurrentGraphIndexView while it …
marianotepper 7ef3c74
Make allMutationsCompleted volatile
marianotepper 441e613
Clarifying comment in OnHeapGraphIndex.getView
marianotepper 140812d
Make sure that the view gets instantiated when actually writing the g…
marianotepper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 54 additions & 43 deletions
97
jvector-base/src/main/java/io/github/jbellis/jvector/graph/GraphIndexBuilder.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
jvector-base/src/main/java/io/github/jbellis/jvector/graph/MutableGraphIndex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* All changes to the original code are Copyright DataStax, Inc. | ||
* | ||
* Please see the included license file for details. | ||
*/ | ||
|
||
/* | ||
* Original license: | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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; | ||
|
||
import io.github.jbellis.jvector.util.BitSet; | ||
import io.github.jbellis.jvector.util.ThreadSafeGrowableBitSet; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
|
||
/** | ||
* An {@link ImmutableGraphIndex} that offers concurrent access; for typical graphs you will get significant | ||
* speedups in construction and searching as you add threads. | ||
* | ||
* <p>The base layer (layer 0) contains all nodes, while higher layers are stored in sparse maps. | ||
* For searching, use a view obtained from {@link #getView()} which supports level–aware operations. | ||
*/ | ||
interface MutableGraphIndex extends ImmutableGraphIndex { | ||
/** | ||
* Add the given node ordinal with an empty set of neighbors. | ||
* | ||
* <p>Nodes can be inserted out of order, but it requires that the nodes preceded by the node | ||
* inserted out of order are eventually added. | ||
* | ||
* <p>Actually populating the neighbors, and establishing bidirectional links, is the | ||
* responsibility of the caller. | ||
* | ||
* <p>It is also the responsibility of the caller to ensure that each node is only added once. | ||
*/ | ||
void addNode(NodeAtLevel nodeLevel); | ||
|
||
/** | ||
* Add the given node ordinal with an empty set of neighbors. | ||
* | ||
* <p>Nodes can be inserted out of order, but it requires that the nodes preceded by the node | ||
* inserted out of order are eventually added. | ||
* | ||
* <p>Actually populating the neighbors, and establishing bidirectional links, is the | ||
* responsibility of the caller. | ||
* | ||
* <p>It is also the responsibility of the caller to ensure that each node is only added once. | ||
*/ | ||
void addNode(int level, int node); | ||
|
||
/** | ||
* Whether the given node is present in the graph. | ||
*/ | ||
boolean contains(NodeAtLevel nodeLevel); | ||
|
||
/** | ||
* Whether the given node is present in the given layer of the graph. | ||
*/ | ||
boolean contains(int level, int node); | ||
|
||
/** | ||
* Add the given node ordinal with an empty set of neighbors. | ||
* | ||
* <p>Nodes can be inserted out of order, but it requires that the nodes preceded by the node | ||
* inserted out of order are eventually added. | ||
* | ||
* <p>Actually populating the neighbors, and establishing bidirectional links, is the | ||
* responsibility of the caller. | ||
* | ||
* <p>It is also the responsibility of the caller to ensure that each node is only added once. | ||
*/ | ||
void connectNode(int level, int node, NodeArray nodes); | ||
|
||
/** | ||
* Use with extreme caution. Used by Builder to load a saved graph and for rescoring. | ||
*/ | ||
void connectNode(NodeAtLevel nodeLevel, NodeArray nodes); | ||
|
||
/** | ||
* Mark the given node deleted. Does NOT remove the node from the graph. | ||
*/ | ||
void markDeleted(int node); | ||
|
||
/** must be called after addNode once neighbors are linked in all levels. */ | ||
void markComplete(NodeAtLevel nodeLevel); | ||
|
||
void updateEntryNode(NodeAtLevel newEntry); | ||
|
||
/** | ||
* Returns an upper bound on the amount of memory used by a single node, in bytes. | ||
*/ | ||
long ramBytesUsedOneNode(int layer); | ||
|
||
ThreadSafeGrowableBitSet getDeletedNodes(); | ||
|
||
void setDegrees(List<Integer> layerDegrees); | ||
|
||
/** | ||
* Enforce the degree of the given node in all layers. | ||
*/ | ||
void enforceDegree(int node); | ||
|
||
/** | ||
* Returns an iterator over the neighbors for the given node at the specified level, which can be empty if the node does not belong to that layer. | ||
*/ | ||
NodesIterator getNeighborsIterator(NodeAtLevel nodeLevel); | ||
michaeljmarshall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Returns an iterator over the neighbors for the given node at the specified level, which can be empty if the node does not belong to that layer. | ||
*/ | ||
NodesIterator getNeighborsIterator(int level, int node); | ||
|
||
/** | ||
* Removes the given node from all layers. | ||
* | ||
* @param node the node id to remove | ||
* @return the number of layers from which it was removed | ||
*/ | ||
int removeNode(int node); | ||
|
||
/** | ||
* Returns an Integer stream with the nodes contained in the specified level. | ||
*/ | ||
IntStream nodeStream(int level); | ||
|
||
/** | ||
* Returns the maximum (coarser) level that contains a vector in the graph or -1 if the node is not in the graph. | ||
*/ | ||
int getMaxLevelForNode(int node); | ||
|
||
/** | ||
* @return the node of the graph to start searches at | ||
*/ | ||
NodeAtLevel entryNode(); | ||
|
||
/** | ||
* Add the given neighbors to the given node at the specified level, maintaining diversity | ||
* It also adds backlinks from the neighbors to the given node. | ||
* The edges will only be added if the out-degree of the node is less than overflowRatio times the max degree. | ||
*/ | ||
void addEdges(int level, int node, NodeArray candidates, float overflowRatio); | ||
|
||
/** | ||
* Remove edges to deleted nodes and add the new connections, maintaining diversity | ||
*/ | ||
void replaceDeletedNeighbors(int level, int node, BitSet toDelete, NodeArray candidates); | ||
|
||
/** | ||
* Signals that all mutations have been completed and the graph will not be mutated any further. | ||
* Should be called by the builder after all mutations are completed (during cleanup). | ||
*/ | ||
void allMutationsCompleted(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.