Skip to content

Commit c53d687

Browse files
committed
save point -- in the middle of just mess
1 parent 2723ece commit c53d687

File tree

11 files changed

+317
-193
lines changed

11 files changed

+317
-193
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,16 @@ public void deleteFromNodeIndexIfNecessary(@Nonnull final Transaction transactio
173173

174174
@Nonnull
175175
@Override
176-
public CompletableFuture<Node> fetchNode(@Nonnull final ReadTransaction transaction, @Nonnull final byte[] nodeId) {
177-
return getOnWriteListener().onAsyncReadForWrite(fetchNodeInternal(transaction, nodeId).thenApply(this::checkNode));
176+
public <N extends Neighbor> CompletableFuture<NodeWithLayer<N>> fetchNode(@Nonnull final Node.NodeCreator<N> creator,
177+
@Nonnull final ReadTransaction readTransaction,
178+
int layer, @Nonnull Tuple primaryKey) {
179+
return fetchNodeInternal(creator, readTransaction, layer, primaryKey).thenApply(this::checkNode);
178180
}
179181

180182
@Nonnull
181-
protected abstract CompletableFuture<Node> fetchNodeInternal(@Nonnull ReadTransaction transaction, @Nonnull byte[] nodeId);
183+
protected abstract <N extends Neighbor> CompletableFuture<NodeWithLayer<N>> fetchNodeInternal(@Nonnull Node.NodeCreator<N> creator,
184+
@Nonnull ReadTransaction readTransaction,
185+
int layer, @Nonnull Tuple primaryKey);
182186

183187
/**
184188
* Method to perform basic invariant check(s) on a newly-fetched node.
@@ -190,12 +194,7 @@ public CompletableFuture<Node> fetchNode(@Nonnull final ReadTransaction transact
190194
* @return the node that was passed in
191195
*/
192196
@Nullable
193-
private <N extends Node> N checkNode(@Nullable final N node) {
194-
if (node != null && (node.size() < getConfig().getMinM() || node.size() > getConfig().getMaxM())) {
195-
if (!node.isRoot()) {
196-
throw new IllegalStateException("packing of non-root is out of valid range");
197-
}
198-
}
197+
private <N extends Neighbor> NodeWithLayer<N> checkNode(@Nullable final NodeWithLayer<N> node) {
199198
return node;
200199
}
201200

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,44 @@ public ByNodeStorageAdapter(@Nonnull final HNSW.Config config, @Nonnull final Su
7575
}
7676

7777
@Override
78-
public CompletableFuture<NodeWithLayer<? extends Neighbor>> fetchEntryNode(@Nonnull final ReadTransaction readTransaction) {
78+
public CompletableFuture<NodeKeyWithLayer> fetchEntryNodeKey(@Nonnull final ReadTransaction readTransaction) {
7979
final byte[] key = getEntryNodeSubspace().pack();
8080

8181
return readTransaction.get(key)
8282
.thenApply(valueBytes -> {
8383
if (valueBytes == null) {
84-
throw new IllegalStateException("cannot fetch entry point");
84+
return null; // not a single node in the index
8585
}
8686

8787
final Tuple entryTuple = Tuple.fromBytes(valueBytes);
8888
final int lMax = (int)entryTuple.getLong(0);
89-
final Node<? extends Neighbor> node = nodeFromTuple(entryTuple.getNestedTuple(1));
89+
final Tuple primaryKey = entryTuple.getNestedTuple(1);
90+
final OnReadListener onReadListener = getOnReadListener();
91+
onReadListener.onKeyValueRead(key, valueBytes);
92+
return new NodeKeyWithLayer(lMax, primaryKey);
93+
});
94+
}
95+
96+
@Nonnull
97+
@Override
98+
protected <N extends Neighbor> CompletableFuture<NodeWithLayer<N>> fetchNodeInternal(@Nonnull final Node.NodeCreator<N> creator,
99+
@Nonnull final ReadTransaction readTransaction,
100+
final int layer,
101+
@Nonnull final Tuple primaryKey) {
102+
final byte[] key = getDataSubspace().pack(Tuple.from(layer, primaryKey));
103+
104+
return readTransaction.get(key)
105+
.thenApply(valueBytes -> {
106+
if (valueBytes == null) {
107+
throw new IllegalStateException("cannot fetch node");
108+
}
109+
110+
final Tuple nodeTuple = Tuple.fromBytes(valueBytes);
111+
final Node<N> node = nodeFromTuple(creator, nodeTuple);
90112
final OnReadListener onReadListener = getOnReadListener();
91113
onReadListener.onNodeRead(node);
92-
onReadListener.onKeyValueRead(node, key, valueBytes);
93-
return node.withLayer(lMax);
114+
onReadListener.onKeyValueRead(key, valueBytes);
115+
return node.withLayer(layer);
94116
});
95117
}
96118

@@ -153,7 +175,8 @@ public CompletableFuture<Node> fetchNodeInternal(@Nonnull final ReadTransaction
153175
}
154176

155177
@Nonnull
156-
private Node<? extends Neighbor> nodeFromTuple(@Nonnull final Tuple tuple) {
178+
private <N extends Neighbor> Node<N> nodeFromTuple(@Nonnull final Node.NodeCreator<N> creator,
179+
@Nonnull final Tuple tuple) {
157180
final NodeKind nodeKind = NodeKind.fromSerializedNodeKind((byte)tuple.getLong(0));
158181
final Tuple primaryKey = tuple.getNestedTuple(1);
159182
final Tuple vectorTuple = tuple.getNestedTuple(2);
@@ -200,9 +223,8 @@ private Node<? extends Neighbor> nodeFromTuple(@Nonnull final Tuple tuple) {
200223
Verify.verify((nodeKind == NodeKind.DATA && neighbors != null) ||
201224
(nodeKind == NodeKind.INTERMEDIATE && neighborsWithVectors != null));
202225

203-
return nodeKind == NodeKind.DATA
204-
? new DataNode(primaryKey, vector, neighbors)
205-
: new IntermediateNode(primaryKey, vector, neighborsWithVectors);
226+
return creator.create(nodeKind, primaryKey, vector,
227+
nodeKind == NodeKind.DATA ? neighbors : neighborsWithVectors);
206228
}
207229

208230
@Nonnull

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

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

2323
import com.apple.foundationdb.tuple.Tuple;
2424
import com.christianheina.langx.half4j.Half;
25+
import com.google.common.base.Verify;
2526
import com.google.common.collect.Lists;
2627

2728
import javax.annotation.Nonnull;
@@ -37,6 +38,12 @@ public DataNode(@Nonnull final Tuple primaryKey, @Nonnull final Vector<Half> vec
3738
super(primaryKey, vector, neighbors);
3839
}
3940

41+
@Nonnull
42+
@Override
43+
public NodeKind getKind() {
44+
return NodeKind.DATA;
45+
}
46+
4047
@Nonnull
4148
@Override
4249
public DataNode asDataNode() {
@@ -55,9 +62,18 @@ public NodeWithLayer<Neighbor> withLayer(final int layer) {
5562
return new NodeWithLayer<>(layer, this);
5663
}
5764

58-
@Nonnull
5965
@Override
60-
public NodeKind getKind() {
61-
return NodeKind.DATA;
66+
public NodeCreator<Neighbor> sameCreator() {
67+
return DataNode::creator;
68+
}
69+
70+
@Nonnull
71+
@SuppressWarnings("unchecked")
72+
public static Node<Neighbor> creator(@Nonnull final NodeKind nodeKind,
73+
@Nonnull final Tuple primaryKey,
74+
@Nonnull final Vector<Half> vector,
75+
@Nonnull final List<? extends Neighbor> neighbors) {
76+
Verify.verify(nodeKind == NodeKind.INTERMEDIATE);
77+
return new DataNode(primaryKey, vector, (List<Neighbor>)neighbors);
6278
}
6379
}

0 commit comments

Comments
 (0)