Skip to content

Commit 1a5fe4a

Browse files
committed
adressing comments and simplifying code
1 parent 3330d6c commit 1a5fe4a

23 files changed

+810
-757
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This source file is part of the FoundationDB open source project
55
*
6-
* Copyright 2015-2023 Apple Inc. and the FoundationDB project authors
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -81,4 +81,25 @@ public Tuple getPrimaryKey() {
8181
public List<N> getNeighbors() {
8282
return neighbors;
8383
}
84+
85+
86+
/**
87+
* Converts this node into its {@link CompactNode} representation.
88+
* <p>
89+
* A {@code CompactNode} is a space-efficient implementation {@code Node}. This method provides the
90+
* conversion logic to transform the current object into that compact form.
91+
*
92+
* @return a non-null {@link CompactNode} representing the current node.
93+
*/
94+
@Nonnull
95+
public abstract CompactNode asCompactNode();
96+
97+
/**
98+
* Converts this node into its {@link InliningNode} representation.
99+
* @return this object cast to an {@link InliningNode}; never {@code null}.
100+
* @throws ClassCastException if this object is not actually an instance of
101+
* {@link InliningNode}.
102+
*/
103+
@Nonnull
104+
public abstract InliningNode asInliningNode();
84105
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This source file is part of the FoundationDB open source project
55
*
6-
* Copyright 2015-2023 Apple Inc. and the FoundationDB project authors
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ abstract class AbstractStorageAdapter<N extends NodeReference> implements Storag
4848
private static final Logger logger = LoggerFactory.getLogger(AbstractStorageAdapter.class);
4949

5050
@Nonnull
51-
private final HNSW.Config config;
51+
private final Config config;
5252
@Nonnull
5353
private final NodeFactory<N> nodeFactory;
5454
@Nonnull
@@ -73,7 +73,7 @@ abstract class AbstractStorageAdapter<N extends NodeReference> implements Storag
7373
* @param onWriteListener the listener to be called on write operations
7474
* @param onReadListener the listener to be called on read operations
7575
*/
76-
protected AbstractStorageAdapter(@Nonnull final HNSW.Config config, @Nonnull final NodeFactory<N> nodeFactory,
76+
protected AbstractStorageAdapter(@Nonnull final Config config, @Nonnull final NodeFactory<N> nodeFactory,
7777
@Nonnull final Subspace subspace,
7878
@Nonnull final OnWriteListener onWriteListener,
7979
@Nonnull final OnReadListener onReadListener) {
@@ -88,11 +88,11 @@ protected AbstractStorageAdapter(@Nonnull final HNSW.Config config, @Nonnull fin
8888
/**
8989
* Returns the configuration used to build and search this HNSW graph.
9090
*
91-
* @return the current {@link HNSW.Config} object, never {@code null}.
91+
* @return the current {@link Config} object, never {@code null}.
9292
*/
9393
@Override
9494
@Nonnull
95-
public HNSW.Config getConfig() {
95+
public Config getConfig() {
9696
return config;
9797
}
9898

@@ -195,14 +195,14 @@ public OnReadListener getOnReadListener() {
195195
* @param layer the layer of the tree from which to fetch the node
196196
* @param primaryKey the non-null primary key that identifies the node to fetch
197197
*
198-
* @return a {@link CompletableFuture} that will complete with the fetched {@link Node}
198+
* @return a {@link CompletableFuture} that will complete with the fetched {@link AbstractNode}
199199
* once it has been read from storage and validated
200200
*/
201201
@Nonnull
202202
@Override
203-
public CompletableFuture<Node<N>> fetchNode(@Nonnull final ReadTransaction readTransaction,
204-
@Nonnull final AffineOperator storageTransform,
205-
int layer, @Nonnull Tuple primaryKey) {
203+
public CompletableFuture<AbstractNode<N>> fetchNode(@Nonnull final ReadTransaction readTransaction,
204+
@Nonnull final AffineOperator storageTransform,
205+
int layer, @Nonnull Tuple primaryKey) {
206206
return fetchNodeInternal(readTransaction, storageTransform, layer, primaryKey).thenApply(this::checkNode);
207207

208208
}
@@ -220,13 +220,13 @@ public CompletableFuture<Node<N>> fetchNode(@Nonnull final ReadTransaction readT
220220
* @param layer the layer index from which to fetch the node
221221
* @param primaryKey the primary key that uniquely identifies the node to be fetched; must not be {@code null}
222222
*
223-
* @return a {@link CompletableFuture} that will be completed with the fetched {@link Node}.
223+
* @return a {@link CompletableFuture} that will be completed with the fetched {@link AbstractNode}.
224224
* The future will complete with {@code null} if no node is found for the given key and layer.
225225
*/
226226
@Nonnull
227-
protected abstract CompletableFuture<Node<N>> fetchNodeInternal(@Nonnull ReadTransaction readTransaction,
228-
@Nonnull AffineOperator storageTransform,
229-
int layer, @Nonnull Tuple primaryKey);
227+
protected abstract CompletableFuture<AbstractNode<N>> fetchNodeInternal(@Nonnull ReadTransaction readTransaction,
228+
@Nonnull AffineOperator storageTransform,
229+
int layer, @Nonnull Tuple primaryKey);
230230

231231
/**
232232
* Method to perform basic invariant check(s) on a newly-fetched node.
@@ -237,7 +237,7 @@ protected abstract CompletableFuture<Node<N>> fetchNodeInternal(@Nonnull ReadTra
237237
* @return the node that was passed in
238238
*/
239239
@Nullable
240-
private Node<N> checkNode(@Nullable final Node<N> node) {
240+
private <T extends Node<N>> T checkNode(@Nullable final T node) {
241241
return node;
242242
}
243243

@@ -259,7 +259,7 @@ private Node<N> checkNode(@Nullable final Node<N> node) {
259259
*/
260260
@Override
261261
public void writeNode(@Nonnull final Transaction transaction, @Nonnull final Quantizer quantizer,
262-
@Nonnull final Node<N> node, final int layer,
262+
@Nonnull final AbstractNode<N> node, final int layer,
263263
@Nonnull final NeighborsChangeSet<N> changeSet) {
264264
writeNodeInternal(transaction, quantizer, node, layer, changeSet);
265265
if (logger.isTraceEnabled()) {
@@ -283,7 +283,7 @@ public void writeNode(@Nonnull final Transaction transaction, @Nonnull final Qua
283283
* removals of neighbor links
284284
*/
285285
protected abstract void writeNodeInternal(@Nonnull Transaction transaction, @Nonnull Quantizer quantizer,
286-
@Nonnull Node<N> node, int layer,
286+
@Nonnull AbstractNode<N> node, int layer,
287287
@Nonnull NeighborsChangeSet<N> changeSet);
288288

289289
}

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,34 @@
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
import javax.annotation.Nullable;
27-
import java.util.Objects;
2828

29+
/**
30+
* Class to capture the current state of this HNSW that cannot be expressed as metadata but that also is not the actual
31+
* data that is inserted, organized and retrieved. For instance, an HNSW needs to keep track of the entry point that
32+
* resides in the highest layer(currently). Another example is any information that pertains to coordinate system
33+
* transformations that have to be carried out prior/posterior to inserting/retrieving an item into/from the HNSW.
34+
*/
2935
class AccessInfo {
36+
/**
37+
* The current entry point. All searches start here.
38+
*/
3039
@Nonnull
3140
private final EntryNodeReference entryNodeReference;
3241

42+
/**
43+
* A seed that can be used to reconstruct a random rotator {@link com.apple.foundationdb.linear.FhtKacRotator} used
44+
* in ({@link StorageTransform}.
45+
*/
3346
private final long rotatorSeed;
3447

48+
/**
49+
* A centroid that is usually derived as an average over some vectors seen so far. It is used to create the
50+
* {@link StorageTransform}.
51+
*/
3552
@Nullable
3653
private final RealVector centroid;
3754

@@ -66,23 +83,19 @@ public AccessInfo withNewEntryNodeReference(@Nonnull final EntryNodeReference en
6683
}
6784

6885
@Override
69-
public final boolean equals(final Object o) {
86+
public boolean equals(final Object o) {
7087
if (!(o instanceof AccessInfo)) {
7188
return false;
7289
}
73-
7490
final AccessInfo that = (AccessInfo)o;
7591
return rotatorSeed == that.rotatorSeed &&
76-
entryNodeReference.equals(that.entryNodeReference) &&
77-
Objects.equals(centroid, that.centroid);
92+
Objects.equal(entryNodeReference, that.entryNodeReference) &&
93+
Objects.equal(centroid, that.centroid);
7894
}
7995

8096
@Override
8197
public int hashCode() {
82-
int result = entryNodeReference.hashCode();
83-
result = 31 * result + Long.hashCode(rotatorSeed);
84-
result = 31 * result + Objects.hashCode(centroid);
85-
return result;
98+
return Objects.hashCode(entryNodeReference, rotatorSeed, centroid);
8699
}
87100

88101
@Nonnull

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This source file is part of the FoundationDB open source project
55
*
6-
* Copyright 2015-2023 Apple Inc. and the FoundationDB project authors
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -89,7 +89,7 @@ public List<N> merge() {
8989
*/
9090
@Override
9191
public void writeDelta(@Nonnull final InliningStorageAdapter storageAdapter, @Nonnull final Transaction transaction,
92-
@Nonnull final Quantizer quantizer, final int layer, @Nonnull final Node<N> node,
92+
@Nonnull final Quantizer quantizer, final int layer, @Nonnull final AbstractNode<N> node,
9393
@Nonnull final Predicate<Tuple> primaryKeyPredicate) {
9494
// nothing to be written
9595
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This source file is part of the FoundationDB open source project
55
*
6-
* Copyright 2015-2023 Apple Inc. and the FoundationDB project authors
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -47,8 +47,8 @@ class CompactNode extends AbstractNode<NodeReference> {
4747
@Nonnull
4848
@Override
4949
@SpotBugsSuppressWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
50-
public Node<NodeReference> create(@Nonnull final Tuple primaryKey, @Nullable final RealVector vector,
51-
@Nonnull final List<? extends NodeReference> neighbors) {
50+
public AbstractNode<NodeReference> create(@Nonnull final Tuple primaryKey, @Nullable final RealVector vector,
51+
@Nonnull final List<? extends NodeReference> neighbors) {
5252
return new CompactNode(primaryKey, Objects.requireNonNull(vector), (List<NodeReference>)neighbors);
5353
}
5454

0 commit comments

Comments
 (0)