Skip to content

Commit a804023

Browse files
committed
refactored Vector class to be more aligned with math libraries
1 parent 3552ca6 commit a804023

File tree

17 files changed

+385
-209
lines changed

17 files changed

+385
-209
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Represents a compact node within a graph structure, extending {@link AbstractNode}.
3434
* <p>
3535
* This node type is considered "compact" because it directly stores its associated
36-
* data vector of type {@code Vector<Half>}. It is used to represent a vector in a
36+
* data vector of type {@link Vector}. It is used to represent a vector in a
3737
* vector space and maintains references to its neighbors via {@link NodeReference} objects.
3838
*
3939
* @see AbstractNode
@@ -46,7 +46,7 @@ public class CompactNode extends AbstractNode<NodeReference> {
4646
@Nonnull
4747
@Override
4848
@SpotBugsSuppressWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
49-
public Node<NodeReference> create(@Nonnull final Tuple primaryKey, @Nullable final Vector<Half> vector,
49+
public Node<NodeReference> create(@Nonnull final Tuple primaryKey, @Nullable final Vector vector,
5050
@Nonnull final List<? extends NodeReference> neighbors) {
5151
return new CompactNode(primaryKey, Objects.requireNonNull(vector), (List<NodeReference>)neighbors);
5252
}
@@ -59,7 +59,7 @@ public NodeKind getNodeKind() {
5959
};
6060

6161
@Nonnull
62-
private final Vector<Half> vector;
62+
private final Vector vector;
6363

6464
/**
6565
* Constructs a new {@code CompactNode} instance.
@@ -69,11 +69,11 @@ public NodeKind getNodeKind() {
6969
* {@code primaryKey} and {@code neighbors} to the superclass constructor.
7070
*
7171
* @param primaryKey the primary key that uniquely identifies this node; must not be {@code null}.
72-
* @param vector the data vector of type {@code Vector<Half>} associated with this node; must not be {@code null}.
72+
* @param vector the data vector of type {@code Vector} associated with this node; must not be {@code null}.
7373
* @param neighbors a list of {@link NodeReference} objects representing the neighbors of this node; must not be
7474
* {@code null}.
7575
*/
76-
public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final Vector<Half> vector,
76+
public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final Vector vector,
7777
@Nonnull final List<NodeReference> neighbors) {
7878
super(primaryKey, neighbors);
7979
this.vector = vector;
@@ -92,7 +92,7 @@ public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final Vector<Half>
9292
*/
9393
@Nonnull
9494
@Override
95-
public NodeReference getSelfReference(@Nullable final Vector<Half> vector) {
95+
public NodeReference getSelfReference(@Nullable final Vector vector) {
9696
return new NodeReference(getPrimaryKey());
9797
}
9898

@@ -112,7 +112,7 @@ public NodeKind getKind() {
112112
* @return the non-null vector of {@link Half} objects.
113113
*/
114114
@Nonnull
115-
public Vector<Half> getVector() {
115+
public Vector getVector() {
116116
return vector;
117117
}
118118

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.apple.foundationdb.subspace.Subspace;
3131
import com.apple.foundationdb.tuple.ByteArrayUtil;
3232
import com.apple.foundationdb.tuple.Tuple;
33-
import com.christianheina.langx.half4j.Half;
3433
import com.google.common.base.Verify;
3534
import com.google.common.collect.Lists;
3635
import org.slf4j.Logger;
@@ -208,7 +207,7 @@ private Node<NodeReference> nodeFromKeyValuesTuples(@Nonnull final Tuple primary
208207
private Node<NodeReference> compactNodeFromTuples(@Nonnull final Tuple primaryKey,
209208
@Nonnull final Tuple vectorTuple,
210209
@Nonnull final Tuple neighborsTuple) {
211-
final Vector<Half> vector = StorageAdapter.vectorFromTuple(vectorTuple);
210+
final Vector vector = StorageAdapter.vectorFromTuple(vectorTuple);
212211
final List<NodeReference> nodeReferences = Lists.newArrayListWithExpectedSize(neighborsTuple.size());
213212

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package com.apple.foundationdb.async.hnsw;
2222

2323
import com.apple.foundationdb.tuple.Tuple;
24-
import com.christianheina.langx.half4j.Half;
2524

2625
import javax.annotation.Nonnull;
2726
import java.util.Objects;
@@ -48,7 +47,7 @@ class EntryNodeReference extends NodeReferenceWithVector {
4847
* @param vector the vector data associated with the node. Must not be {@code null}.
4948
* @param layer the layer number where this entry node is located.
5049
*/
51-
public EntryNodeReference(@Nonnull final Tuple primaryKey, @Nonnull final Vector<Half> vector, final int layer) {
50+
public EntryNodeReference(@Nonnull final Tuple primaryKey, @Nonnull final Vector vector, final int layer) {
5251
super(primaryKey, vector);
5352
this.layer = layer;
5453
}

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.apple.foundationdb.async.MoreAsyncUtil;
2929
import com.apple.foundationdb.subspace.Subspace;
3030
import com.apple.foundationdb.tuple.Tuple;
31-
import com.christianheina.langx.half4j.Half;
3231
import com.google.common.base.Verify;
3332
import com.google.common.collect.ImmutableList;
3433
import com.google.common.collect.Iterables;
@@ -239,7 +238,7 @@ public static class ConfigBuilder {
239238
public ConfigBuilder() {
240239
}
241240

242-
public ConfigBuilder(@Nonnull Random random, @Nonnull final Metric metric, final boolean useInlining,
241+
public ConfigBuilder(@Nonnull final Random random, @Nonnull final Metric metric, final boolean useInlining,
243242
final int m, final int mMax, final int mMax0, final int efSearch, final int efConstruction,
244243
final boolean extendCandidates, final boolean keepPrunedConnections) {
245244
this.random = random;
@@ -481,7 +480,7 @@ public OnReadListener getOnReadListener() {
481480
public CompletableFuture<? extends List<? extends NodeReferenceAndNode<? extends NodeReference>>> kNearestNeighborsSearch(@Nonnull final ReadTransaction readTransaction,
482481
final int k,
483482
final int efSearch,
484-
@Nonnull final Vector<Half> queryVector) {
483+
@Nonnull final Vector queryVector) {
485484
return StorageAdapter.fetchEntryNodeReference(readTransaction, getSubspace(), getOnReadListener())
486485
.thenCompose(entryPointAndLayer -> {
487486
if (entryPointAndLayer == null) {
@@ -572,7 +571,7 @@ private <N extends NodeReference> CompletableFuture<NodeReferenceWithDistance> g
572571
@Nonnull final ReadTransaction readTransaction,
573572
@Nonnull final NodeReferenceWithDistance entryNeighbor,
574573
final int layer,
575-
@Nonnull final Vector<Half> queryVector) {
574+
@Nonnull final Vector queryVector) {
576575
if (storageAdapter.getNodeKind() == NodeKind.INLINING) {
577576
return greedySearchInliningLayer(storageAdapter.asInliningStorageAdapter(), readTransaction, entryNeighbor, layer, queryVector);
578577
} else {
@@ -612,7 +611,7 @@ private CompletableFuture<NodeReferenceWithDistance> greedySearchInliningLayer(@
612611
@Nonnull final ReadTransaction readTransaction,
613612
@Nonnull final NodeReferenceWithDistance entryNeighbor,
614613
final int layer,
615-
@Nonnull final Vector<Half> queryVector) {
614+
@Nonnull final Vector queryVector) {
616615
Verify.verify(layer > 0);
617616
final Metric metric = getConfig().getMetric();
618617
final AtomicReference<NodeReferenceWithDistance> currentNodeReferenceAtomic =
@@ -685,7 +684,7 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceAndNode<N>
685684
final int layer,
686685
final int efSearch,
687686
@Nonnull final Map<Tuple, Node<N>> nodeCache,
688-
@Nonnull final Vector<Half> queryVector) {
687+
@Nonnull final Vector queryVector) {
689688
final Set<Tuple> visited = Sets.newConcurrentHashSet(NodeReference.primaryKeys(entryNeighbors));
690689
final Queue<NodeReferenceWithDistance> candidates =
691690
new PriorityBlockingQueue<>(config.getM(),
@@ -995,7 +994,7 @@ public CompletableFuture<Void> insert(@Nonnull final Transaction transaction, @N
995994
*/
996995
@Nonnull
997996
public CompletableFuture<Void> insert(@Nonnull final Transaction transaction, @Nonnull final Tuple newPrimaryKey,
998-
@Nonnull final Vector<Half> newVector) {
997+
@Nonnull final Vector newVector) {
999998
final Metric metric = getConfig().getMetric();
1000999

10011000
final int insertionLayer = insertionLayer(getConfig().getRandom());
@@ -1104,7 +1103,7 @@ public CompletableFuture<Void> insertBatch(@Nonnull final Transaction transactio
11041103
return CompletableFuture.completedFuture(null);
11051104
}
11061105

1107-
final Vector<Half> itemVector = item.getVector();
1106+
final Vector itemVector = item.getVector();
11081107
final int itemL = item.getLayer();
11091108

11101109
final NodeReferenceWithDistance initialNodeReference =
@@ -1128,7 +1127,7 @@ public CompletableFuture<Void> insertBatch(@Nonnull final Transaction transactio
11281127
(index, currentEntryNodeReference) -> {
11291128
final NodeReferenceWithLayer item = batchWithLayers.get(index);
11301129
final Tuple itemPrimaryKey = item.getPrimaryKey();
1131-
final Vector<Half> itemVector = item.getVector();
1130+
final Vector itemVector = item.getVector();
11321131
final int itemL = item.getLayer();
11331132

11341133
final EntryNodeReference newEntryNodeReference;
@@ -1202,7 +1201,7 @@ public CompletableFuture<Void> insertBatch(@Nonnull final Transaction transactio
12021201
@Nonnull
12031202
private CompletableFuture<Void> insertIntoLayers(@Nonnull final Transaction transaction,
12041203
@Nonnull final Tuple newPrimaryKey,
1205-
@Nonnull final Vector<Half> newVector,
1204+
@Nonnull final Vector newVector,
12061205
@Nonnull final NodeReferenceWithDistance nodeReference,
12071206
final int lMax,
12081207
final int insertionLayer) {
@@ -1258,7 +1257,7 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceWithDistan
12581257
@Nonnull final List<NodeReferenceWithDistance> nearestNeighbors,
12591258
int layer,
12601259
@Nonnull final Tuple newPrimaryKey,
1261-
@Nonnull final Vector<Half> newVector) {
1260+
@Nonnull final Vector newVector) {
12621261
if (logger.isDebugEnabled()) {
12631262
logger.debug("begin insert key={} at layer={}", newPrimaryKey, layer);
12641263
}
@@ -1490,7 +1489,7 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceAndNode<N>
14901489
final int m,
14911490
final boolean isExtendCandidates,
14921491
@Nonnull final Map<Tuple, Node<N>> nodeCache,
1493-
@Nonnull final Vector<Half> vector) {
1492+
@Nonnull final Vector vector) {
14941493
return extendCandidatesIfNecessary(storageAdapter, readTransaction, nearestNeighbors, layer, isExtendCandidates, nodeCache, vector)
14951494
.thenApply(extendedCandidates -> {
14961495
final List<NodeReferenceWithDistance> selected = Lists.newArrayListWithExpectedSize(m);
@@ -1575,7 +1574,7 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceAndNode<N>
15751574
int layer,
15761575
boolean isExtendCandidates,
15771576
@Nonnull final Map<Tuple, Node<N>> nodeCache,
1578-
@Nonnull final Vector<Half> vector) {
1577+
@Nonnull final Vector vector) {
15791578
if (isExtendCandidates) {
15801579
final Metric metric = getConfig().getMetric();
15811580

@@ -1639,7 +1638,7 @@ private <N extends NodeReference> CompletableFuture<List<NodeReferenceAndNode<N>
16391638
*/
16401639
private void writeLonelyNodes(@Nonnull final Transaction transaction,
16411640
@Nonnull final Tuple primaryKey,
1642-
@Nonnull final Vector<Half> vector,
1641+
@Nonnull final Vector vector,
16431642
final int highestLayerInclusive,
16441643
final int lowestLayerExclusive) {
16451644
for (int layer = highestLayerInclusive; layer > lowestLayerExclusive; layer --) {
@@ -1667,7 +1666,7 @@ private <N extends NodeReference> void writeLonelyNodeOnLayer(@Nonnull final Sto
16671666
@Nonnull final Transaction transaction,
16681667
final int layer,
16691668
@Nonnull final Tuple primaryKey,
1670-
@Nonnull final Vector<Half> vector) {
1669+
@Nonnull final Vector vector) {
16711670
storageAdapter.writeNode(transaction,
16721671
storageAdapter.getNodeFactory()
16731672
.create(primaryKey, vector, ImmutableList.of()), layer,
@@ -1777,7 +1776,7 @@ private void info(@Nonnull final Consumer<Logger> loggerConsumer) {
17771776
private static class NodeReferenceWithLayer extends NodeReferenceWithVector {
17781777
private final int layer;
17791778

1780-
public NodeReferenceWithLayer(@Nonnull final Tuple primaryKey, @Nonnull final Vector<Half> vector,
1779+
public NodeReferenceWithLayer(@Nonnull final Tuple primaryKey, @Nonnull final Vector vector,
17811780
final int layer) {
17821781
super(primaryKey, vector);
17831782
this.layer = layer;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
2424
import com.apple.foundationdb.tuple.Tuple;
25-
import com.christianheina.langx.half4j.Half;
2625

2726
import javax.annotation.Nonnull;
2827
import javax.annotation.Nullable;
@@ -44,7 +43,7 @@ public class InliningNode extends AbstractNode<NodeReferenceWithVector> {
4443
@Nonnull
4544
@Override
4645
public Node<NodeReferenceWithVector> create(@Nonnull final Tuple primaryKey,
47-
@Nullable final Vector<Half> vector,
46+
@Nullable final Vector vector,
4847
@Nonnull final List<? extends NodeReference> neighbors) {
4948
return new InliningNode(primaryKey, (List<NodeReferenceWithVector>)neighbors);
5049
}
@@ -85,7 +84,7 @@ public InliningNode(@Nonnull final Tuple primaryKey,
8584
@Nonnull
8685
@Override
8786
@SpotBugsSuppressWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
88-
public NodeReferenceWithVector getSelfReference(@Nullable final Vector<Half> vector) {
87+
public NodeReferenceWithVector getSelfReference(@Nullable final Vector vector) {
8988
return new NodeReferenceWithVector(getPrimaryKey(), Objects.requireNonNull(vector));
9089
}
9190

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.apple.foundationdb.subspace.Subspace;
3131
import com.apple.foundationdb.tuple.ByteArrayUtil;
3232
import com.apple.foundationdb.tuple.Tuple;
33-
import com.christianheina.langx.half4j.Half;
3433
import com.google.common.collect.ImmutableList;
3534

3635
import javax.annotation.Nonnull;
@@ -182,7 +181,7 @@ private NodeReferenceWithVector neighborFromRaw(final int layer, final @Nonnull
182181
final Tuple neighborValueTuple = Tuple.fromBytes(value);
183182

184183
final Tuple neighborPrimaryKey = neighborKeyTuple.getNestedTuple(2); // neighbor primary key
185-
final Vector<Half> neighborVector = StorageAdapter.vectorFromTuple(neighborValueTuple); // the entire value is the vector
184+
final Vector neighborVector = StorageAdapter.vectorFromTuple(neighborValueTuple); // the entire value is the vector
186185
return new NodeReferenceWithVector(neighborPrimaryKey, neighborVector);
187186
}
188187

0 commit comments

Comments
 (0)