Skip to content

Commit

Permalink
More vestigial generic graph cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jkni committed Feb 8, 2024
1 parent 0900c09 commit e0dcfc8
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public OnDiskADCGraphIndex(ReaderSupplier readerSupplier, long offset)
* while preserving the original relative ordering in `graph`. That is, for all node ids i and j,
* if i < j in `graph` then map[i] < map[j] in the returned map.
*/
public static <T> Map<Integer, Integer> getSequentialRenumbering(GraphIndex graph) {
public static Map<Integer, Integer> getSequentialRenumbering(GraphIndex graph) {
try (var view = graph.getView()) {
Int2IntHashMap oldToNewMap = new Int2IntHashMap(-1);
int nextOrdinal = 0;
Expand Down Expand Up @@ -234,7 +234,7 @@ public void close() throws IOException {
*
* If any nodes have been deleted, you must use the overload specifying `oldToNewOrdinals` instead.
*/
public static <T> void write(GraphIndex graph, RandomAccessVectorValues vectors, PQVectors pqVectors, DataOutput out)
public static void write(GraphIndex graph, RandomAccessVectorValues vectors, PQVectors pqVectors, DataOutput out)
throws IOException
{
try (var view = graph.getView()) {
Expand All @@ -257,7 +257,7 @@ public static <T> void write(GraphIndex graph, RandomAccessVectorValues vectors,
* compressed representations are embedded in the serialized graph to support accelerated ADC.
* @param out the output to write to
*/
public static <T> void write(GraphIndex graph,
public static void write(GraphIndex graph,
RandomAccessVectorValues vectors,
Map<Integer, Integer> oldToNewOrdinals,
PQVectors pqVectors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public OnDiskGraphIndex(ReaderSupplier readerSupplier, long offset)
* while preserving the original relative ordering in `graph`. That is, for all node ids i and j,
* if i &lt; j in `graph` then map[i] &lt; map[j] in the returned map.
*/
public static <T> Map<Integer, Integer> getSequentialRenumbering(GraphIndex graph) {
public static Map<Integer, Integer> getSequentialRenumbering(GraphIndex graph) {
try (var view = graph.getView()) {
Int2IntHashMap oldToNewMap = new Int2IntHashMap(-1);
int nextOrdinal = 0;
Expand Down Expand Up @@ -179,7 +179,7 @@ public void close() throws IOException {
*
* If any nodes have been deleted, you must use the overload specifying `oldToNewOrdinals` instead.
*/
public static <T> void write(GraphIndex graph, RandomAccessVectorValues vectors, DataOutput out)
public static void write(GraphIndex graph, RandomAccessVectorValues vectors, DataOutput out)
throws IOException
{
try (var view = graph.getView()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ default int getIdUpperBound() {
}
}

static <T> String prettyPrint(GraphIndex graph) {
static String prettyPrint(GraphIndex graph) {
StringBuilder sb = new StringBuilder();
sb.append(graph);
sb.append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class GraphIndexBuilder {
private final VectorSimilarityFunction similarityFunction;
private final float neighborOverflow;
private final float alpha;
private final PoolingSupport<GraphSearcher<?>> graphSearcher;
private final PoolingSupport<GraphSearcher> graphSearcher;

@VisibleForTesting
final OnHeapGraphIndex graph;
Expand Down Expand Up @@ -153,7 +153,7 @@ public GraphIndexBuilder(
new OnHeapGraphIndex(
M, (node, m) -> new ConcurrentNeighborSet(node, m, similarity, alpha));
// this view will never get closed, but it's okay because we know it's an OHGI view, which has a no-op close
this.graphSearcher = PoolingSupport.newThreadBased(() -> new GraphSearcher.Builder<>(graph.getView()).withConcurrentUpdates().build());
this.graphSearcher = PoolingSupport.newThreadBased(() -> new GraphSearcher.Builder(graph.getView()).withConcurrentUpdates().build());

// in scratch we store candidates in reverse order: worse candidates are first
this.naturalScratch = PoolingSupport.newThreadBased(() -> new NodeArray(Math.max(beamWidth, M + 1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* Searches a graph to find nearest neighbors to a query vector. For more background on the
* search algorithm, see {@link GraphIndex}.
*/
public class GraphSearcher<T> {
public class GraphSearcher {

private final GraphIndex.View view;

Expand Down Expand Up @@ -76,9 +76,9 @@ public class GraphSearcher<T> {
* Convenience function for simple one-off searches. It is caller's responsibility to make sure that it
* is the unique owner of the vectors instance passed in here.
*/
public static <T> SearchResult search(VectorFloat<?> targetVector, int topK, RandomAccessVectorValues vectors, VectorSimilarityFunction similarityFunction, GraphIndex graph, Bits acceptOrds) {
public static SearchResult search(VectorFloat<?> targetVector, int topK, RandomAccessVectorValues vectors, VectorSimilarityFunction similarityFunction, GraphIndex graph, Bits acceptOrds) {
try (var view = graph.getView()) {
var searcher = new GraphSearcher.Builder<>(view).withConcurrentUpdates().build();
var searcher = new GraphSearcher.Builder(view).withConcurrentUpdates().build();
NodeSimilarity.ExactScoreFunction scoreFunction = i -> similarityFunction.compare(targetVector, vectors.vectorValue(i));
return searcher.search(scoreFunction, null, topK, acceptOrds);
} catch (Exception e) {
Expand All @@ -87,23 +87,23 @@ public static <T> SearchResult search(VectorFloat<?> targetVector, int topK, Ran
}

/** Builder */
public static class Builder<T> {
public static class Builder {
private final GraphIndex.View view;
private boolean concurrent;

public Builder(GraphIndex.View view) {
this.view = view;
}

public Builder<T> withConcurrentUpdates() {
public Builder withConcurrentUpdates() {
this.concurrent = true;
return this;
}

public GraphSearcher<T> build() {
public GraphSearcher build() {
int size = view.getIdUpperBound();
BitSet bits = concurrent ? new GrowableBitSet(size) : new SparseFixedBitSet(size);
return new GraphSearcher<>(view, bits);
return new GraphSearcher(view, bits);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static ResultSummary performQueries(DataSet ds, RandomAccessVectorValues
sf = cv.approximateScoreFunctionFor(queryVector, ds.similarityFunction);
}
NodeSimilarity.Reranker rr = (j) -> ds.similarityFunction.compare(queryVector, view.getVector(j));
sr = new GraphSearcher.Builder<>(view)
sr = new GraphSearcher.Builder(view)
.build()
.search(sf, rr, efSearch, Bits.ALL);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void testRecallInternal(GraphIndex graph, RandomAccessVectorValue
var queryVector = queryVectors.get(i);
SearchResult.NodeScore[] nn;
var view = graph.getView();
var searcher = new GraphSearcher.Builder<>(view).build();
var searcher = new GraphSearcher.Builder(view).build();
if (compressedVectors == null) {
NodeSimilarity.ExactScoreFunction sf = (j) -> VectorSimilarityFunction.EUCLIDEAN.compare(queryVector, ravv.vectorValue(j));
nn = searcher.search(sf, null, 100, Bits.ALL).getNodes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testThreshold() throws IOException {
var onHeapGraph = builder.build();

// test raw vectors
var searcher = new GraphSearcher.Builder<>(onHeapGraph.getView()).build();
var searcher = new GraphSearcher.Builder(onHeapGraph.getView()).build();
for (int i = 0; i < 10; i++) {
TestParams tp = createTestParams(vectors);

Expand All @@ -75,7 +75,7 @@ public void testThreshold() throws IOException {
{
for (int i = 0; i < 10; i++) {
TestParams tp = createTestParams(vectors);
searcher = new GraphSearcher.Builder<>(onDiskGraph.getView()).build();
searcher = new GraphSearcher.Builder(onDiskGraph.getView()).build();
NodeSimilarity.Reranker reranker = (j) -> VectorSimilarityFunction.EUCLIDEAN.compare(tp.q, ravv.vectorValue(j));
var asf = cv.approximateScoreFunctionFor(tp.q, VectorSimilarityFunction.EUCLIDEAN);
var result = searcher.search(asf, reranker, vectors.length, tp.th, Bits.ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void testResume() {
int initialTopK = 10;
int resumeTopK = 15;
var query = randomVector(dim);
var searcher = new GraphSearcher.Builder<>(graph.getView()).build();
var searcher = new GraphSearcher.Builder(graph.getView()).build();

var initial = searcher.search((NodeSimilarity.ExactScoreFunction) i -> similarityFunction.compare(query, vectors.vectorValue(i)), null, initialTopK, acceptOrds);
assertEquals(initialTopK, initial.getNodes().length);
Expand Down

0 comments on commit e0dcfc8

Please sign in to comment.