@@ -75,21 +75,22 @@ public ByNodeStorageAdapter(@Nonnull final HNSW.Config config, @Nonnull final Su
75
75
}
76
76
77
77
@ Override
78
- public CompletableFuture <NodeKeyWithLayer > fetchEntryNodeKey (@ Nonnull final ReadTransaction readTransaction ) {
78
+ public CompletableFuture <EntryPointAndLayer > 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
84
return null ; // not a single node in the index
85
85
}
86
+ final OnReadListener onReadListener = getOnReadListener ();
87
+ onReadListener .onKeyValueRead (key , valueBytes );
86
88
87
89
final Tuple entryTuple = Tuple .fromBytes (valueBytes );
88
90
final int lMax = (int )entryTuple .getLong (0 );
89
91
final Tuple primaryKey = entryTuple .getNestedTuple (1 );
90
- final OnReadListener onReadListener = getOnReadListener ();
91
- onReadListener .onKeyValueRead (key , valueBytes );
92
- return new NodeKeyWithLayer (lMax , primaryKey );
92
+ final Tuple vectorTuple = entryTuple .getNestedTuple (2 );
93
+ return new EntryPointAndLayer (lMax , primaryKey , vectorFromTuple (vectorTuple ));
93
94
});
94
95
}
95
96
@@ -156,77 +157,79 @@ private Tuple toTuple(@Nonnull final Node node) {
156
157
return Tuple .from (node .getKind ().getSerialized (), slotTuples );
157
158
}
158
159
159
- @ Nonnull
160
- @ Override
161
- public CompletableFuture <Node > fetchNodeInternal (@ Nonnull final ReadTransaction transaction ,
162
- @ Nonnull final byte [] nodeId ) {
163
- final byte [] key = packWithSubspace (nodeId );
164
- return transaction .get (key )
165
- .thenApply (valueBytes -> {
166
- if (valueBytes == null ) {
167
- return null ;
168
- }
169
- final Node node = nodeFromTuple (nodeId , Tuple .fromBytes (valueBytes ));
170
- final OnReadListener onReadListener = getOnReadListener ();
171
- onReadListener .onNodeRead (node );
172
- onReadListener .onKeyValueRead (node , key , valueBytes );
173
- return node ;
174
- });
175
- }
176
-
177
160
@ Nonnull
178
161
private <N extends Neighbor > Node <N > nodeFromTuple (@ Nonnull final Node .NodeCreator <N > creator ,
179
162
@ Nonnull final Tuple tuple ) {
180
163
final NodeKind nodeKind = NodeKind .fromSerializedNodeKind ((byte )tuple .getLong (0 ));
181
164
final Tuple primaryKey = tuple .getNestedTuple (1 );
182
- final Tuple vectorTuple = tuple .getNestedTuple (2 );
183
- final Tuple neighborsTuple = tuple .getNestedTuple (3 );
165
+ final Tuple vectorTuple ;
166
+ final Tuple neighborsTuple ;
167
+
168
+ switch (nodeKind ) {
169
+ case DATA :
170
+ vectorTuple = tuple .getNestedTuple (2 );
171
+ neighborsTuple = tuple .getNestedTuple (3 );
172
+ return dataNodeFromTuples (creator , primaryKey , vectorTuple , neighborsTuple );
173
+ case INTERMEDIATE :
174
+ neighborsTuple = tuple .getNestedTuple (3 );
175
+ return intermediateNodeFromTuples (creator , primaryKey , neighborsTuple );
176
+ default :
177
+ throw new IllegalStateException ("unknown node kind" );
178
+ }
179
+ }
184
180
185
- final Half [] vectorHalfs = new Half [vectorTuple .size ()];
186
- for (int i = 0 ; i < vectorTuple .size (); i ++) {
187
- vectorHalfs [i ] = Half .shortBitsToHalf (shortFromBytes (vectorTuple .getBytes (i )));
181
+ @ Nonnull
182
+ private <N extends Neighbor > Node <N > dataNodeFromTuples (@ Nonnull final Node .NodeCreator <N > creator ,
183
+ @ Nonnull final Tuple primaryKey ,
184
+ @ Nonnull final Tuple vectorTuple ,
185
+ @ Nonnull final Tuple neighborsTuple ) {
186
+ final Vector <Half > vector = vectorFromTuple (vectorTuple );
187
+
188
+ List <Neighbor > neighbors = Lists .newArrayListWithExpectedSize (neighborsTuple .size ());
189
+
190
+ for (final Object neighborObject : neighborsTuple ) {
191
+ final Tuple neighborTuple = (Tuple )neighborObject ;
192
+ neighbors .add (new Neighbor (neighborTuple ));
188
193
}
189
- final Vector .HalfVector vector = new Vector .HalfVector (vectorHalfs );
190
- List <NeighborWithVector > neighborsWithVectors = null ;
194
+
195
+ return creator .create (NodeKind .DATA , primaryKey , vector , neighbors );
196
+ }
197
+
198
+ @ Nonnull
199
+ private <N extends Neighbor > Node <N > intermediateNodeFromTuples (@ Nonnull final Node .NodeCreator <N > creator ,
200
+ @ Nonnull final Tuple primaryKey ,
201
+ @ Nonnull final Tuple neighborsTuple ) {
202
+ List <NeighborWithVector > neighborsWithVectors = Lists .newArrayListWithExpectedSize (neighborsTuple .size ());
191
203
Half [] neighborVectorHalfs = null ;
192
- List <Neighbor > neighbors = null ;
193
204
194
205
for (final Object neighborObject : neighborsTuple ) {
195
206
final Tuple neighborTuple = (Tuple )neighborObject ;
196
- switch (nodeKind ) {
197
- case DATA :
198
- if (neighbors == null ) {
199
- neighbors = Lists .newArrayListWithExpectedSize (neighborsTuple .size ());
200
- }
201
- neighbors .add (new Neighbor (neighborTuple ));
202
- break ;
203
-
204
- case INTERMEDIATE :
205
- final Tuple neighborPrimaryKey = neighborTuple .getNestedTuple (0 );
206
- final Tuple neighborVectorTuple = neighborTuple .getNestedTuple (1 );
207
- if (neighborsWithVectors == null ) {
208
- neighborsWithVectors = Lists .newArrayListWithExpectedSize (neighborsTuple .size ());
209
- neighborVectorHalfs = new Half [neighborVectorTuple .size ()];
210
- }
211
-
212
- for (int i = 0 ; i < neighborVectorTuple .size (); i ++) {
213
- neighborVectorHalfs [i ] = Half .shortBitsToHalf (shortFromBytes (neighborVectorTuple .getBytes (i )));
214
- }
215
- neighborsWithVectors .add (new NeighborWithVector (neighborPrimaryKey , new Vector .HalfVector (neighborVectorHalfs )));
216
- break ;
207
+ final Tuple neighborPrimaryKey = neighborTuple .getNestedTuple (0 );
208
+ final Tuple neighborVectorTuple = neighborTuple .getNestedTuple (1 );
209
+ if (neighborVectorHalfs == null ) {
210
+ neighborVectorHalfs = new Half [neighborVectorTuple .size ()];
211
+ }
217
212
218
- default :
219
- throw new IllegalStateException ( "unknown node kind" );
213
+ for ( int i = 0 ; i < neighborVectorTuple . size (); i ++) {
214
+ neighborVectorHalfs [ i ] = Half . shortBitsToHalf ( shortFromBytes ( neighborVectorTuple . getBytes ( i )) );
220
215
}
216
+ neighborsWithVectors .add (new NeighborWithVector (neighborPrimaryKey , new Vector .HalfVector (neighborVectorHalfs )));
221
217
}
222
218
223
- Verify . verify (( nodeKind == NodeKind .DATA && neighbors != null ) ||
224
- ( nodeKind == NodeKind . INTERMEDIATE && neighborsWithVectors != null ));
219
+ return creator . create ( NodeKind .INTERMEDIATE , primaryKey , null , neighborsWithVectors );
220
+ }
225
221
226
- return creator .create (nodeKind , primaryKey , vector ,
227
- nodeKind == NodeKind .DATA ? neighbors : neighborsWithVectors );
222
+ @ Nonnull
223
+ private Vector <Half > vectorFromTuple (final Tuple vectorTuple ) {
224
+ final Half [] vectorHalfs = new Half [vectorTuple .size ()];
225
+ for (int i = 0 ; i < vectorTuple .size (); i ++) {
226
+ vectorHalfs [i ] = Half .shortBitsToHalf (shortFromBytes (vectorTuple .getBytes (i )));
227
+ }
228
+ return new Vector .HalfVector (vectorHalfs );
228
229
}
229
230
231
+
232
+
230
233
@ Nonnull
231
234
@ Override
232
235
public <S extends NodeSlot , N extends AbstractNode <S , N >> AbstractChangeSet <S , N >
0 commit comments