@@ -595,6 +595,10 @@ namespace hnswlib {
595
595
596
596
std::ifstream input (location, std::ios::binary);
597
597
598
+ if (!input.is_open ())
599
+ throw std::runtime_error (" Cannot open file" );
600
+
601
+
598
602
// get file size:
599
603
input.seekg (0 ,input.end );
600
604
std::streampos total_filesize=input.tellg ();
@@ -625,16 +629,15 @@ namespace hnswlib {
625
629
fstdistfunc_ = s->get_dist_func ();
626
630
dist_func_param_ = s->get_dist_func_param ();
627
631
628
- // / Legacy, check that everything is ok
629
-
630
- bool old_index=false ;
631
-
632
632
auto pos=input.tellg ();
633
+
634
+
635
+ // / Optional - check if index is ok:
636
+
633
637
input.seekg (cur_element_count * size_data_per_element_,input.cur );
634
638
for (size_t i = 0 ; i < cur_element_count; i++) {
635
639
if (input.tellg () < 0 || input.tellg ()>=total_filesize){
636
- old_index = true ;
637
- break ;
640
+ throw std::runtime_error (" Index seems to be corrupted or unsupported" );
638
641
}
639
642
640
643
unsigned int linkListSize;
@@ -644,23 +647,21 @@ namespace hnswlib {
644
647
}
645
648
}
646
649
647
- // check if file is ok, if not this is either corrupted or old index
650
+ // throw exception if it either corrupted or old index
648
651
if (input.tellg ()!=total_filesize)
649
- old_index = true ;
652
+ throw std::runtime_error ( " Index seems to be corrupted or unsupported " ) ;
650
653
651
- if (old_index) {
652
- std::cerr << " Warning: loading of old indexes will be deprecated before 2019.\n "
653
- << " Please resave the index in the new format.\n " ;
654
- }
655
654
input.clear ();
655
+
656
+ // / Optional check end
657
+
656
658
input.seekg (pos,input.beg );
657
659
658
660
659
661
data_level0_memory_ = (char *) malloc (max_elements * size_data_per_element_);
660
662
input.read (data_level0_memory_, cur_element_count * size_data_per_element_);
661
663
662
- if (old_index)
663
- input.seekg (((max_elements_-cur_element_count) * size_data_per_element_), input.cur );
664
+
664
665
665
666
666
667
size_links_per_element_ = maxM_ * sizeof (tableint) + sizeof (linklistsizeint);
@@ -691,6 +692,14 @@ namespace hnswlib {
691
692
input.read (linkLists_[i], linkListSize);
692
693
}
693
694
}
695
+
696
+ has_deletions_=false ;
697
+
698
+ for (size_t i = 0 ; i < cur_element_count; i++) {
699
+ if (isMarkedDeleted (i))
700
+ has_deletions_=true ;
701
+ }
702
+
694
703
input.close ();
695
704
696
705
return ;
0 commit comments