61
61
import java .util .function .BiPredicate ;
62
62
import java .util .function .Function ;
63
63
import java .util .function .Predicate ;
64
- import java .util .function .Supplier ;
65
64
66
65
/**
67
66
* TODO.
@@ -546,10 +545,10 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
546
545
new PriorityBlockingQueue <>(config .getM (),
547
546
Comparator .comparing (NodeReferenceWithDistance ::getDistance ));
548
547
candidates .addAll (entryNeighbors );
549
- final Queue <NodeReferenceWithDistance > furthestNeighbors =
548
+ final Queue <NodeReferenceWithDistance > nearestNeighbors =
550
549
new PriorityBlockingQueue <>(config .getM (),
551
550
Comparator .comparing (NodeReferenceWithDistance ::getDistance ).reversed ());
552
- furthestNeighbors .addAll (entryNeighbors );
551
+ nearestNeighbors .addAll (entryNeighbors );
553
552
final Map <Tuple , Node <N >> nodeCache = Maps .newConcurrentMap ();
554
553
final Metric metric = getConfig ().getMetric ();
555
554
@@ -559,7 +558,7 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
559
558
}
560
559
561
560
final NodeReferenceWithDistance candidate = candidates .poll ();
562
- final NodeReferenceWithDistance furthestNeighbor = Objects .requireNonNull (furthestNeighbors .peek ());
561
+ final NodeReferenceWithDistance furthestNeighbor = Objects .requireNonNull (nearestNeighbors .peek ());
563
562
564
563
if (candidate .getDistance () > furthestNeighbor .getDistance ()) {
565
564
return AsyncUtil .READY_FALSE ;
@@ -575,23 +574,23 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> searchLayer
575
574
for (final NodeReferenceWithVector current : neighborReferences ) {
576
575
visited .add (current .getPrimaryKey ());
577
576
final double furthestDistance =
578
- Objects .requireNonNull (furthestNeighbors .peek ()).getDistance ();
577
+ Objects .requireNonNull (nearestNeighbors .peek ()).getDistance ();
579
578
580
579
final double currentDistance =
581
580
Vector .comparativeDistance (metric , current .getVector (), queryVector );
582
- if (currentDistance < furthestDistance || furthestNeighbors .size () < efSearch ) {
581
+ if (currentDistance < furthestDistance || nearestNeighbors .size () < efSearch ) {
583
582
final NodeReferenceWithDistance currentWithDistance =
584
583
new NodeReferenceWithDistance (current .getPrimaryKey (), currentDistance );
585
584
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 ();
589
588
}
590
589
}
591
590
}
592
591
return true ;
593
592
});
594
- }).thenCompose (ignored -> fetchResultsIfNecessary (nodeFactory , readTransaction , layer , furthestNeighbors ,
593
+ }).thenCompose (ignored -> fetchResultsIfNecessary (nodeFactory , readTransaction , layer , nearestNeighbors ,
595
594
nodeCache ));
596
595
}
597
596
@@ -606,7 +605,10 @@ private <N extends NodeReference> CompletableFuture<Node<N>> fetchNodeIfNotCache
606
605
@ Nonnull final Map <Tuple , Node <N >> nodeCache ) {
607
606
return fetchNodeIfNecessaryAndApply (nodeFactory , readTransaction , layer , nodeReference ,
608
607
nR -> nodeCache .get (nR .getPrimaryKey ()),
609
- (ignored , node ) -> node );
608
+ (nR , node ) -> {
609
+ nodeCache .put (nR .getPrimaryKey (), node );
610
+ return node ;
611
+ });
610
612
}
611
613
612
614
/**
@@ -649,8 +651,10 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceWithVector
649
651
}
650
652
return new NodeReferenceWithVector (neighborReference .getPrimaryKey (), neighborNode .asCompactNode ().getVector ());
651
653
},
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
+ });
654
658
}
655
659
656
660
/**
@@ -670,7 +674,10 @@ private <N extends NodeReference> CompletableFuture<SearchResult<N>> fetchResult
670
674
}
671
675
return new SearchResult .NodeReferenceWithNode <>(nodeReference , node );
672
676
},
673
- SearchResult .NodeReferenceWithNode ::new )
677
+ (nodeReferenceWithDistance , node ) -> {
678
+ nodeCache .put (nodeReferenceWithDistance .getPrimaryKey (), node );
679
+ return new SearchResult .NodeReferenceWithNode <N >(nodeReferenceWithDistance , node );
680
+ })
674
681
.thenApply (nodeReferencesWithNodes -> {
675
682
final ImmutableMap .Builder <NodeReferenceWithDistance , Node <N >> nodeMapBuilder =
676
683
ImmutableMap .builder ();
0 commit comments