Skip to content

Commit a03c5fc

Browse files
committed
Transformed
1 parent 3904879 commit a03c5fc

23 files changed

+531
-269
lines changed

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

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ abstract class AbstractStorageAdapter<N extends NodeReference> implements Storag
5858
@Nonnull
5959
private final OnReadListener onReadListener;
6060

61+
@Nonnull
6162
private final Subspace dataSubspace;
6263

6364
/**
@@ -85,47 +86,26 @@ protected AbstractStorageAdapter(@Nonnull final Config config, @Nonnull final No
8586
this.dataSubspace = subspace.subspace(Tuple.from(SUBSPACE_PREFIX_DATA));
8687
}
8788

88-
/**
89-
* Returns the configuration used to build and search this HNSW graph.
90-
*
91-
* @return the current {@link Config} object, never {@code null}.
92-
*/
9389
@Override
9490
@Nonnull
9591
public Config getConfig() {
9692
return config;
9793
}
9894

99-
/**
100-
* Gets the factory responsible for creating new nodes.
101-
* <p>
102-
* This factory is used to instantiate nodes of the generic type {@code N}
103-
* for the current context. The {@code @Nonnull} annotation guarantees that
104-
* this method will never return {@code null}.
105-
*
106-
* @return the non-null {@link NodeFactory} instance.
107-
*/
10895
@Nonnull
10996
@Override
11097
public NodeFactory<N> getNodeFactory() {
11198
return nodeFactory;
11299
}
113100

114-
/**
115-
* Gets the subspace in which this key or value is stored.
116-
* <p>
117-
* This subspace provides a logical separation for keys within the underlying key-value store.
118-
*
119-
* @return the non-null {@link Subspace} for this context
120-
*/
121101
@Override
122102
@Nonnull
123103
public Subspace getSubspace() {
124104
return subspace;
125105
}
126106

127107
/**
128-
* Gets the subspace for the data associated with this component.
108+
* Gets the cached subspace for the data associated with this component.
129109
* <p>
130110
* The data subspace defines the portion of the directory space where the data
131111
* for this component is stored.
@@ -138,29 +118,12 @@ public Subspace getDataSubspace() {
138118
return dataSubspace;
139119
}
140120

141-
/**
142-
* Returns the listener that is notified upon write events.
143-
* <p>
144-
* This method is an override and guarantees a non-null return value,
145-
* as indicated by the {@code @Nonnull} annotation.
146-
*
147-
* @return the configured {@link OnWriteListener} instance; will never be {@code null}.
148-
*/
149121
@Override
150122
@Nonnull
151123
public OnWriteListener getOnWriteListener() {
152124
return onWriteListener;
153125
}
154126

155-
/**
156-
* Gets the listener that is notified upon completion of a read operation.
157-
* <p>
158-
* This method is an override and provides the currently configured listener instance.
159-
* The returned listener is guaranteed to be non-null as indicated by the
160-
* {@code @Nonnull} annotation.
161-
*
162-
* @return the non-null {@link OnReadListener} instance.
163-
*/
164127
@Override
165128
@Nonnull
166129
public OnReadListener getOnReadListener() {
@@ -190,7 +153,6 @@ public CompletableFuture<AbstractNode<N>> fetchNode(@Nonnull final ReadTransacti
190153
@Nonnull final AffineOperator storageTransform,
191154
int layer, @Nonnull Tuple primaryKey) {
192155
return fetchNodeInternal(readTransaction, storageTransform, layer, primaryKey).thenApply(this::checkNode);
193-
194156
}
195157

196158
/**
@@ -271,5 +233,4 @@ public void writeNode(@Nonnull final Transaction transaction, @Nonnull final Qua
271233
protected abstract void writeNodeInternal(@Nonnull Transaction transaction, @Nonnull Quantizer quantizer,
272234
@Nonnull AbstractNode<N> node, int layer,
273235
@Nonnull NeighborsChangeSet<N> changeSet);
274-
275236
}

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

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

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

2627
import javax.annotation.Nonnull;
@@ -32,9 +33,9 @@
3233
class AggregatedVector {
3334
private final int partialCount;
3435
@Nonnull
35-
private final RealVector partialVector;
36+
private final Transformed<RealVector> partialVector;
3637

37-
public AggregatedVector(final int partialCount, @Nonnull final RealVector partialVector) {
38+
public AggregatedVector(final int partialCount, @Nonnull final Transformed<RealVector> partialVector) {
3839
this.partialCount = partialCount;
3940
this.partialVector = partialVector;
4041
}
@@ -44,7 +45,7 @@ public int getPartialCount() {
4445
}
4546

4647
@Nonnull
47-
public RealVector getPartialVector() {
48+
public Transformed<RealVector> getPartialVector() {
4849
return partialVector;
4950
}
5051

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
2424
import com.apple.foundationdb.half.Half;
2525
import com.apple.foundationdb.linear.RealVector;
26+
import com.apple.foundationdb.linear.Transformed;
2627
import com.apple.foundationdb.tuple.Tuple;
2728

2829
import javax.annotation.Nonnull;
@@ -47,7 +48,8 @@ class CompactNode extends AbstractNode<NodeReference> {
4748
@Nonnull
4849
@Override
4950
@SpotBugsSuppressWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
50-
public AbstractNode<NodeReference> create(@Nonnull final Tuple primaryKey, @Nullable final RealVector vector,
51+
public AbstractNode<NodeReference> create(@Nonnull final Tuple primaryKey,
52+
@Nullable final Transformed<RealVector> vector,
5153
@Nonnull final List<? extends NodeReference> neighbors) {
5254
return new CompactNode(primaryKey, Objects.requireNonNull(vector), (List<NodeReference>)neighbors);
5355
}
@@ -60,7 +62,7 @@ public NodeKind getNodeKind() {
6062
};
6163

6264
@Nonnull
63-
private final RealVector vector;
65+
private final Transformed<RealVector> vector;
6466

6567
/**
6668
* Constructs a new {@code CompactNode} instance.
@@ -74,7 +76,7 @@ public NodeKind getNodeKind() {
7476
* @param neighbors a list of {@link NodeReference} objects representing the neighbors of this node; must not be
7577
* {@code null}.
7678
*/
77-
public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final RealVector vector,
79+
public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final Transformed<RealVector> vector,
7880
@Nonnull final List<NodeReference> neighbors) {
7981
super(primaryKey, neighbors);
8082
this.vector = vector;
@@ -93,7 +95,7 @@ public CompactNode(@Nonnull final Tuple primaryKey, @Nonnull final RealVector ve
9395
*/
9496
@Nonnull
9597
@Override
96-
public NodeReference getSelfReference(@Nullable final RealVector vector) {
98+
public NodeReference getSelfReference(@Nullable final Transformed<RealVector> vector) {
9799
return new NodeReference(getPrimaryKey());
98100
}
99101

@@ -113,7 +115,7 @@ public NodeKind getKind() {
113115
* @return the non-null vector of {@link Half} objects.
114116
*/
115117
@Nonnull
116-
public RealVector getVector() {
118+
public Transformed<RealVector> getVector() {
117119
return vector;
118120
}
119121

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

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.apple.foundationdb.linear.AffineOperator;
3131
import com.apple.foundationdb.linear.Quantizer;
3232
import com.apple.foundationdb.linear.RealVector;
33+
import com.apple.foundationdb.linear.Transformed;
3334
import com.apple.foundationdb.subspace.Subspace;
3435
import com.apple.foundationdb.tuple.ByteArrayUtil;
3536
import com.apple.foundationdb.tuple.Tuple;
@@ -72,34 +73,6 @@ public CompactStorageAdapter(@Nonnull final Config config,
7273
super(config, nodeFactory, subspace, onWriteListener, onReadListener);
7374
}
7475

75-
/**
76-
* Returns this storage adapter instance, as it is already a compact storage adapter.
77-
* @return the current instance, which serves as its own compact representation.
78-
* This will never be {@code null}.
79-
*/
80-
@Nonnull
81-
@Override
82-
public StorageAdapter<NodeReference> asCompactStorageAdapter() {
83-
return this;
84-
}
85-
86-
/**
87-
* Returns this adapter as a {@code StorageAdapter} that supports inlining.
88-
* <p>
89-
* This operation is not supported by a compact storage adapter. Calling this method on this implementation will
90-
* always result in an {@code IllegalStateException}.
91-
*
92-
* @return an instance of {@code StorageAdapter} that supports inlining
93-
*
94-
* @throws IllegalStateException unconditionally, as this operation is not supported
95-
* on a compact storage adapter.
96-
*/
97-
@Nonnull
98-
@Override
99-
public StorageAdapter<NodeReferenceWithVector> asInliningStorageAdapter() {
100-
throw new IllegalStateException("cannot call this method on a compact storage adapter");
101-
}
102-
10376
/**
10477
* Asynchronously fetches a node from the database for a given layer and primary key.
10578
* <p>
@@ -219,8 +192,8 @@ private AbstractNode<NodeReference> compactNodeFromTuples(@Nonnull final AffineO
219192
@Nonnull final Tuple primaryKey,
220193
@Nonnull final Tuple vectorTuple,
221194
@Nonnull final Tuple neighborsTuple) {
222-
final RealVector vector =
223-
storageTransform.apply(StorageAdapter.vectorFromTuple(getConfig(), vectorTuple));
195+
final Transformed<RealVector> vector =
196+
storageTransform.transform(StorageAdapter.vectorFromTuple(getConfig(), vectorTuple));
224197
final List<NodeReference> nodeReferences = Lists.newArrayListWithExpectedSize(neighborsTuple.size());
225198

226199
for (int i = 0; i < neighborsTuple.size(); i ++) {
@@ -256,7 +229,8 @@ public void writeNodeInternal(@Nonnull final Transaction transaction, @Nonnull f
256229
final List<Object> nodeItems = Lists.newArrayListWithExpectedSize(3);
257230
nodeItems.add(NodeKind.COMPACT.getSerialized());
258231
final CompactNode compactNode = node.asCompactNode();
259-
nodeItems.add(StorageAdapter.tupleFromVector(quantizer.encode(compactNode.getVector())));
232+
// getting underlying vector is okay as it is only written to the database
233+
nodeItems.add(StorageAdapter.tupleFromVector(quantizer.encode(compactNode.getVector()).getUnderlyingVector()));
260234

261235
final Iterable<NodeReference> neighbors = neighborsChangeSet.merge();
262236

@@ -297,8 +271,8 @@ public void writeNodeInternal(@Nonnull final Transaction transaction, @Nonnull f
297271
*/
298272
@Nonnull
299273
@Override
300-
public Iterable<AbstractNode<NodeReference>> scanLayer(@Nonnull final ReadTransaction readTransaction, int layer,
301-
@Nullable final Tuple lastPrimaryKey, int maxNumRead) {
274+
public AsyncIterable<AbstractNode<NodeReference>> scanLayer(@Nonnull final ReadTransaction readTransaction, int layer,
275+
@Nullable final Tuple lastPrimaryKey, int maxNumRead) {
302276
final byte[] layerPrefix = getDataSubspace().pack(Tuple.from(layer));
303277
final Range range =
304278
lastPrimaryKey == null

0 commit comments

Comments
 (0)