6161import java .util .function .BiPredicate ;
6262import java .util .function .Function ;
6363import 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 ();
0 commit comments