@@ -75,22 +75,44 @@ public ByNodeStorageAdapter(@Nonnull final HNSW.Config config, @Nonnull final Su
75
75
}
76
76
77
77
@ Override
78
- public CompletableFuture <NodeWithLayer <? extends Neighbor >> fetchEntryNode (@ Nonnull final ReadTransaction readTransaction ) {
78
+ public CompletableFuture <NodeKeyWithLayer > fetchEntryNodeKey (@ Nonnull final ReadTransaction readTransaction ) {
79
79
final byte [] key = getEntryNodeSubspace ().pack ();
80
80
81
81
return readTransaction .get (key )
82
82
.thenApply (valueBytes -> {
83
83
if (valueBytes == null ) {
84
- throw new IllegalStateException ( "cannot fetch entry point" );
84
+ return null ; // not a single node in the index
85
85
}
86
86
87
87
final Tuple entryTuple = Tuple .fromBytes (valueBytes );
88
88
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 );
90
112
final OnReadListener onReadListener = getOnReadListener ();
91
113
onReadListener .onNodeRead (node );
92
- onReadListener .onKeyValueRead (node , key , valueBytes );
93
- return node .withLayer (lMax );
114
+ onReadListener .onKeyValueRead (key , valueBytes );
115
+ return node .withLayer (layer );
94
116
});
95
117
}
96
118
@@ -153,7 +175,8 @@ public CompletableFuture<Node> fetchNodeInternal(@Nonnull final ReadTransaction
153
175
}
154
176
155
177
@ 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 ) {
157
180
final NodeKind nodeKind = NodeKind .fromSerializedNodeKind ((byte )tuple .getLong (0 ));
158
181
final Tuple primaryKey = tuple .getNestedTuple (1 );
159
182
final Tuple vectorTuple = tuple .getNestedTuple (2 );
@@ -200,9 +223,8 @@ private Node<? extends Neighbor> nodeFromTuple(@Nonnull final Tuple tuple) {
200
223
Verify .verify ((nodeKind == NodeKind .DATA && neighbors != null ) ||
201
224
(nodeKind == NodeKind .INTERMEDIATE && neighborsWithVectors != null ));
202
225
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 );
206
228
}
207
229
208
230
@ Nonnull
0 commit comments