Skip to content

Commit d1388b8

Browse files
committed
save point
1 parent 66547ca commit d1388b8

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

fdb-extensions/src/main/java/com/apple/foundationdb/async/hnsw/HNSW.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import java.util.function.BiPredicate;
6262
import java.util.function.Function;
6363
import java.util.function.Predicate;
64-
import java.util.function.Supplier;
6564

6665
/**
6766
* TODO.
@@ -546,10 +545,10 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
546545
new PriorityBlockingQueue<>(config.getM(),
547546
Comparator.comparing(NodeReferenceWithDistance::getDistance));
548547
candidates.addAll(entryNeighbors);
549-
final Queue<NodeReferenceWithDistance> furthestNeighbors =
548+
final Queue<NodeReferenceWithDistance> nearestNeighbors =
550549
new PriorityBlockingQueue<>(config.getM(),
551550
Comparator.comparing(NodeReferenceWithDistance::getDistance).reversed());
552-
furthestNeighbors.addAll(entryNeighbors);
551+
nearestNeighbors.addAll(entryNeighbors);
553552
final Map<Tuple, Node<N>> nodeCache = Maps.newConcurrentMap();
554553
final Metric metric = getConfig().getMetric();
555554

@@ -559,7 +558,7 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
559558
}
560559

561560
final NodeReferenceWithDistance candidate = candidates.poll();
562-
final NodeReferenceWithDistance furthestNeighbor = Objects.requireNonNull(furthestNeighbors.peek());
561+
final NodeReferenceWithDistance furthestNeighbor = Objects.requireNonNull(nearestNeighbors.peek());
563562

564563
if (candidate.getDistance() > furthestNeighbor.getDistance()) {
565564
return AsyncUtil.READY_FALSE;
@@ -575,23 +574,23 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
575574
for (final NodeReferenceWithVector current : neighborReferences) {
576575
visited.add(current.getPrimaryKey());
577576
final double furthestDistance =
578-
Objects.requireNonNull(furthestNeighbors.peek()).getDistance();
577+
Objects.requireNonNull(nearestNeighbors.peek()).getDistance();
579578

580579
final double currentDistance =
581580
Vector.comparativeDistance(metric, current.getVector(), queryVector);
582-
if (currentDistance < furthestDistance || furthestNeighbors.size() < efSearch) {
581+
if (currentDistance < furthestDistance || nearestNeighbors.size() < efSearch) {
583582
final NodeReferenceWithDistance currentWithDistance =
584583
new NodeReferenceWithDistance(current.getPrimaryKey(), currentDistance);
585584
candidates.add(currentWithDistance);
586-
furthestNeighbors.add(currentWithDistance);
587-
if (furthestNeighbors.size() > efSearch) {
588-
furthestNeighbors.poll();
585+
nearestNeighbors.add(currentWithDistance);
586+
if (nearestNeighbors.size() > efSearch) {
587+
nearestNeighbors.poll();
589588
}
590589
}
591590
}
592591
return true;
593592
});
594-
}).thenCompose(ignored -> fetchResultsIfNecessary(nodeFactory, readTransaction, layer, furthestNeighbors,
593+
}).thenCompose(ignored -> fetchResultsIfNecessary(nodeFactory, readTransaction, layer, nearestNeighbors,
595594
nodeCache));
596595
}
597596

@@ -606,7 +605,10 @@ private <N extends NodeReference> CompletableFuture<Node<N>> fetchNodeIfNotCache
606605
@Nonnull final Map<Tuple, Node<N>> nodeCache) {
607606
return fetchNodeIfNecessaryAndApply(nodeFactory, readTransaction, layer, nodeReference,
608607
nR -> nodeCache.get(nR.getPrimaryKey()),
609-
(ignored, node) -> node);
608+
(nR, node) -> {
609+
nodeCache.put(nR.getPrimaryKey(), node);
610+
return node;
611+
});
610612
}
611613

612614
/**
@@ -649,8 +651,10 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceWithVector
649651
}
650652
return new NodeReferenceWithVector(neighborReference.getPrimaryKey(), neighborNode.asCompactNode().getVector());
651653
},
652-
(neighborReference, neighborNode) ->
653-
new NodeReferenceWithVector(neighborReference.getPrimaryKey(), neighborNode.asCompactNode().getVector()));
654+
(neighborReference, neighborNode) -> {
655+
nodeCache.put(neighborReference.getPrimaryKey(), neighborNode);
656+
return new NodeReferenceWithVector(neighborReference.getPrimaryKey(), neighborNode.asCompactNode().getVector());
657+
});
654658
}
655659

656660
/**
@@ -670,7 +674,10 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> fetchResult
670674
}
671675
return new SearchResult.NodeReferenceWithNode<>(nodeReference, node);
672676
},
673-
SearchResult.NodeReferenceWithNode::new)
677+
(nodeReferenceWithDistance, node) -> {
678+
nodeCache.put(nodeReferenceWithDistance.getPrimaryKey(), node);
679+
return new SearchResult.NodeReferenceWithNode<N>(nodeReferenceWithDistance, node);
680+
})
674681
.thenApply(nodeReferencesWithNodes -> {
675682
final ImmutableMap.Builder<NodeReferenceWithDistance, Node<N>> nodeMapBuilder =
676683
ImmutableMap.builder();

fdb-extensions/src/main/java/com/apple/foundationdb/async/hnsw/Vector.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
public abstract class Vector<R extends Number> {
3636
@Nonnull
3737
protected R[] data;
38+
@Nonnull
39+
protected Supplier<Integer> hashCodeSupplier;
3840

3941
public Vector(@Nonnull final R[] data) {
4042
this.data = data;
43+
this.hashCodeSupplier = Suppliers.memoize(this::computeHashCode);
4144
}
4245

4346
public int size() {
@@ -71,6 +74,10 @@ public boolean equals(final Object o) {
7174

7275
@Override
7376
public int hashCode() {
77+
return hashCodeSupplier.get();
78+
}
79+
80+
private int computeHashCode() {
7481
return Arrays.hashCode(data);
7582
}
7683

0 commit comments

Comments
 (0)