Skip to content

Commit 1963a0a

Browse files
committed
adressing comments and simplifying code
1 parent 1a5fe4a commit 1963a0a

21 files changed

+168
-154
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ abstract class AbstractNode<N extends NodeReference> implements Node<N> {
4545

4646
/**
4747
* Constructs a new {@code AbstractNode} with a specified primary key and a list of neighbors.
48-
* <p>
49-
* This constructor creates a defensive, immutable copy of the provided {@code neighbors} list.
50-
* This ensures that the internal state of the node cannot be modified by external
51-
* changes to the original list after construction.
5248
*
5349
* @param primaryKey the unique identifier for this node; must not be {@code null}
5450
* @param neighbors the list of nodes connected to this node; must not be {@code null}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* An abstract base class for {@link StorageAdapter} implementations.
3838
* <p>
39-
* This class provides the common infrastructure for managing HNSW graph data within {@link Subspace}.
39+
* This class provides the common infrastructure for managing HNSW graph data within a {@link Subspace}.
4040
* It handles the configuration, node creation, and listener management, while delegating the actual
4141
* storage-specific read and write operations to concrete subclasses through the {@code fetchNodeInternal}
4242
* and {@code writeNodeInternal} abstract methods.

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
package com.apple.foundationdb.async.hnsw;
2222

2323
import com.apple.foundationdb.linear.RealVector;
24+
import com.google.common.base.Objects;
2425

2526
import javax.annotation.Nonnull;
2627

28+
/**
29+
* A record-like class wrapping a {@link RealVector} and a count. This data structure is used to keep a running sum
30+
* of many vectors in order to compute their centroid at a later time.
31+
*/
2732
class AggregatedVector {
2833
private final int partialCount;
2934
@Nonnull
@@ -44,20 +49,17 @@ public RealVector getPartialVector() {
4449
}
4550

4651
@Override
47-
public final boolean equals(final Object o) {
52+
public boolean equals(final Object o) {
4853
if (!(o instanceof AggregatedVector)) {
4954
return false;
5055
}
51-
5256
final AggregatedVector that = (AggregatedVector)o;
53-
return partialCount == that.partialCount && partialVector.equals(that.partialVector);
57+
return partialCount == that.partialCount && Objects.equal(partialVector, that.partialVector);
5458
}
5559

5660
@Override
5761
public int hashCode() {
58-
int result = partialCount;
59-
result = 31 * result + partialVector.hashCode();
60-
return result;
62+
return Objects.hashCode(partialCount, partialVector);
6163
}
6264

6365
@Override

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ class CompactStorageAdapter extends AbstractStorageAdapter<NodeReference> implem
5757

5858
/**
5959
* Constructs a new {@code CompactStorageAdapter}.
60-
* <p>
61-
* This constructor initializes the adapter by delegating to the superclass,
62-
* setting up the necessary components for managing an HNSW graph.
6360
*
6461
* @param config the HNSW graph configuration, must not be null. See {@link Config}.
6562
* @param nodeFactory the factory used to create new nodes of type {@link NodeReference}, must not be null.
@@ -223,7 +220,7 @@ private AbstractNode<NodeReference> compactNodeFromTuples(@Nonnull final AffineO
223220
@Nonnull final Tuple vectorTuple,
224221
@Nonnull final Tuple neighborsTuple) {
225222
final RealVector vector =
226-
storageTransform.applyInvert(StorageAdapter.vectorFromTuple(getConfig(), vectorTuple));
223+
storageTransform.invertedApply(StorageAdapter.vectorFromTuple(getConfig(), vectorTuple));
227224
final List<NodeReference> nodeReferences = Lists.newArrayListWithExpectedSize(neighborsTuple.size());
228225

229226
for (int i = 0; i < neighborsTuple.size(); i ++) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public EntryNodeReference withVector(@Nonnull RealVector newVector) {
7777
*/
7878
@Override
7979
public boolean equals(final Object o) {
80-
if (!(o instanceof EntryNodeReference)) {
81-
return false;
82-
}
8380
if (!super.equals(o)) {
8481
return false;
8582
}

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private Quantizer quantizer(@Nullable final AccessInfo accessInfo) {
276276
final EntryNodeReference entryNodeReference = accessInfo.getEntryNodeReference();
277277

278278
final AffineOperator storageTransform = storageTransform(accessInfo);
279-
final RealVector transformedQueryVector = storageTransform.applyInvert(queryVector);
279+
final RealVector transformedQueryVector = storageTransform.invertedApply(queryVector);
280280
final Quantizer quantizer = quantizer(accessInfo);
281281
final Estimator estimator = quantizer.estimator();
282282

@@ -788,7 +788,7 @@ public CompletableFuture<Void> insert(@Nonnull final Transaction transaction, @N
788788
.thenCompose(accessInfo -> {
789789
final AccessInfo currentAccessInfo;
790790
final AffineOperator storageTransform = storageTransform(accessInfo);
791-
final RealVector transformedNewVector = storageTransform.applyInvert(newVector);
791+
final RealVector transformedNewVector = storageTransform.invertedApply(newVector);
792792
final Quantizer quantizer = quantizer(accessInfo);
793793
final Estimator estimator = quantizer.estimator();
794794

@@ -888,10 +888,10 @@ private CompletableFuture<Void> addToStats(@Nonnull final Transaction transactio
888888

889889
final RealVector centroid =
890890
partialVector.multiply(1.0d / partialCount);
891-
final RealVector transformedCentroid = rotator.applyInvert(centroid);
891+
final RealVector transformedCentroid = rotator.invertedApply(centroid);
892892

893893
final var transformedEntryNodeVector =
894-
rotator.applyInvert(currentAccessInfo.getEntryNodeReference()
894+
rotator.invertedApply(currentAccessInfo.getEntryNodeReference()
895895
.getVector()).subtract(transformedCentroid);
896896

897897
final AccessInfo newAccessInfo =
@@ -1547,34 +1547,4 @@ private boolean shouldMaintainStats(final int batchSize) {
15471547
double pBatch = -Math.expm1(batchSize * Math.log1p(-getConfig().getMaintainStatsProbability()));
15481548
return random.nextDouble() < pBatch;
15491549
}
1550-
1551-
private static class NodeReferenceWithLayer extends NodeReferenceWithVector {
1552-
private final int layer;
1553-
1554-
public NodeReferenceWithLayer(@Nonnull final Tuple primaryKey, @Nonnull final RealVector vector,
1555-
final int layer) {
1556-
super(primaryKey, vector);
1557-
this.layer = layer;
1558-
}
1559-
1560-
public int getLayer() {
1561-
return layer;
1562-
}
1563-
1564-
@Override
1565-
public boolean equals(final Object o) {
1566-
if (!(o instanceof NodeReferenceWithLayer)) {
1567-
return false;
1568-
}
1569-
if (!super.equals(o)) {
1570-
return false;
1571-
}
1572-
return layer == ((NodeReferenceWithLayer)o).layer;
1573-
}
1574-
1575-
@Override
1576-
public int hashCode() {
1577-
return Objects.hash(super.hashCode(), layer);
1578-
}
1579-
}
15801550
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private NodeReferenceWithVector neighborFromTuples(@Nonnull final AffineOperator
212212
@Nonnull final Tuple keyTuple, @Nonnull final Tuple valueTuple) {
213213
final Tuple neighborPrimaryKey = keyTuple.getNestedTuple(2); // neighbor primary key
214214
final RealVector neighborVector =
215-
storageTransform.applyInvert(
215+
storageTransform.invertedApply(
216216
StorageAdapter.vectorFromTuple(getConfig(), valueTuple)); // the entire value is the vector
217217
return new NodeReferenceWithVector(neighborPrimaryKey, neighborVector);
218218
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ public NodeReferenceWithVector asNodeReferenceWithVector() {
7878
*/
7979
@Override
8080
public boolean equals(final Object o) {
81-
if (!(o instanceof NodeReference)) {
81+
if (o == null) {
82+
return false;
83+
}
84+
if (this == o) {
85+
return true;
86+
}
87+
if (o.getClass() != this.getClass()) {
8288
return false;
8389
}
8490
final NodeReference that = (NodeReference)o;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public double getDistance() {
7272
*/
7373
@Override
7474
public boolean equals(final Object o) {
75-
if (!(o instanceof NodeReferenceWithDistance)) {
76-
return false;
77-
}
7875
if (!super.equals(o)) {
7976
return false;
8077
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ public NodeReferenceWithVector asNodeReferenceWithVector() {
9494
*/
9595
@Override
9696
public boolean equals(final Object o) {
97-
if (!(o instanceof NodeReferenceWithVector)) {
98-
return false;
99-
}
10097
if (!super.equals(o)) {
10198
return false;
10299
}

0 commit comments

Comments
 (0)