@@ -546,8 +546,6 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
546
546
return write_neighbors (&ctx, 0 , target);
547
547
}
548
548
549
- longlong max_layer= graph->field [0 ]->val_int ();
550
-
551
549
List<FVectorNode> candidates;
552
550
List<FVectorNode> start_nodes;
553
551
String ref_str, *ref_ptr;
@@ -572,36 +570,37 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
572
570
double new_num= my_rnd (&thd->rand );
573
571
double log= -std::log (new_num) * NORMALIZATION_FACTOR;
574
572
longlong new_node_layer= static_cast <longlong>(std::floor (log));
573
+ longlong max_layer= graph->field [0 ]->val_int ();
575
574
576
- // XXX what is that?
577
- for (longlong cur_layer= new_node_layer; cur_layer >= max_layer + 1 ;
578
- cur_layer--)
575
+ if (new_node_layer > max_layer)
579
576
{
580
- if (int err= write_neighbors (&ctx, cur_layer , target))
577
+ if (int err= write_neighbors (&ctx, max_layer + 1 , target))
581
578
return err;
579
+ new_node_layer= max_layer;
582
580
}
583
-
584
- for (longlong cur_layer= max_layer; cur_layer > new_node_layer; cur_layer--)
581
+ else
585
582
{
586
- if (int err= search_layer (&ctx, start_nodes,
587
- thd->variables .hnsw_ef_constructor , cur_layer,
588
- &candidates))
589
- return err;
590
- start_nodes.empty ();
591
- start_nodes.push_back (candidates.head (), &ctx.root ); // XXX ef=1
592
- candidates.empty ();
583
+ for (longlong cur_layer= max_layer; cur_layer > new_node_layer; cur_layer--)
584
+ {
585
+ if (int err= search_layer (&ctx, start_nodes,
586
+ thd->variables .hnsw_ef_constructor , cur_layer,
587
+ &candidates))
588
+ return err;
589
+ start_nodes.empty ();
590
+ start_nodes.push_back (candidates.head (), &ctx.root ); // XXX ef=1
591
+ candidates.empty ();
592
+ }
593
593
}
594
594
595
- for (longlong cur_layer= std::min (max_layer, new_node_layer);
596
- cur_layer >= 0 ; cur_layer--)
595
+ for (longlong cur_layer= new_node_layer; cur_layer >= 0 ; cur_layer--)
597
596
{
598
597
if (int err= search_layer (&ctx, start_nodes,
599
598
thd->variables .hnsw_ef_constructor , cur_layer,
600
599
&candidates))
601
600
return err;
602
601
603
- uint max_neighbors= (cur_layer == 0 ) ? // heuristics from the paper
604
- thd->variables .hnsw_max_connection_per_layer * 2
602
+ uint max_neighbors= (cur_layer == 0 ) // heuristics from the paper
603
+ ? thd->variables .hnsw_max_connection_per_layer * 2
605
604
: thd->variables .hnsw_max_connection_per_layer ;
606
605
607
606
if (int err= select_neighbors (&ctx, cur_layer, target, candidates,
@@ -611,7 +610,6 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
611
610
return err;
612
611
start_nodes= candidates;
613
612
}
614
- start_nodes.empty ();
615
613
616
614
dbug_tmp_restore_column_map (&table->read_set , old_map);
617
615
0 commit comments