hnswlib 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6aed34b1cfcf85d2df88ccb2f1a3b417780e0f614fe1a1e3870a060479207ff
4
- data.tar.gz: ef5272e25876da853ae49cb26d39ecb7e7d708a25e3442d51e4d998f227334a9
3
+ metadata.gz: 293c9038e57db28357f77c753988b593ef921a2d8caf7234f4a547547580f2cb
4
+ data.tar.gz: 668eba08220e29d970f886b91382a335834ac746d9a208d9add986f2ff21fbfc
5
5
  SHA512:
6
- metadata.gz: 0f9c77270306ea4363d94d45025f7e5e14da66ea5bd9c2ec2b0f62ec578afd2ee75c73a5cce2dac668a70adb9fdc653bfe9eb72e1608928fc648bb73571efd48
7
- data.tar.gz: 3d5ab21ea7b9ae8d79f182fb406d98f147d7c534e4be992be238c2c4f450989cb353aa159d62e342ce3e58a7ed8756c59528400554f6229c5298ebbb23414ec3
6
+ metadata.gz: 1a748a2d3f8291453221b60b55f184b152f56aa35ec5a0830d7b6c6f82adb90e09d757737f0b0527e3404f30c61d7da7f8567c1a7d087045fada991e6152a333
7
+ data.tar.gz: 4f5fbf6a8b14e4179862f3e9030bf8e08fa971e874e518141261e2f83942fdb96b68217adcb0dbd2a26b12b5684ef7b645fd7b7fa8025b03fc6e08694a14d9c9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.6.1] - 2022-04-30
2
+
3
+ - Change the `search_knn` method of `BruteforceSearch` to output warning message instead of rasing RuntimeError
4
+ when the number of search results is less than the requested number of neighbors.
5
+ - Fix to raise RuntimeError when failing to open file on the `load_index` method of `BruteforceSearch`.
6
+ - Add guard to not free unallocated memory space.
7
+
1
8
  ## [0.6.0] - 2022-04-16
2
9
 
3
10
  **Breaking change:**
@@ -366,14 +366,24 @@ private:
366
366
  space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(ivspace);
367
367
  }
368
368
  hnswlib::HierarchicalNSW<float>* index = get_hnsw_hierarchicalnsw(self);
369
- if (index->data_level0_memory_) free(index->data_level0_memory_);
369
+ if (index->data_level0_memory_) {
370
+ free(index->data_level0_memory_);
371
+ index->data_level0_memory_ = nullptr;
372
+ }
370
373
  if (index->linkLists_) {
371
374
  for (hnswlib::tableint i = 0; i < index->cur_element_count; i++) {
372
- if (index->element_levels_[i] > 0 && index->linkLists_[i]) free(index->linkLists_[i]);
375
+ if (index->element_levels_[i] > 0 && index->linkLists_[i]) {
376
+ free(index->linkLists_[i]);
377
+ index->linkLists_[i] = nullptr;
378
+ }
373
379
  }
374
380
  free(index->linkLists_);
381
+ index->linkLists_ = nullptr;
382
+ }
383
+ if (index->visited_list_pool_) {
384
+ delete index->visited_list_pool_;
385
+ index->visited_list_pool_ = nullptr;
375
386
  }
376
- if (index->visited_list_pool_) delete index->visited_list_pool_;
377
387
  try {
378
388
  index->loadIndex(filename, space);
379
389
  } catch (const std::runtime_error& e) {
@@ -590,17 +600,16 @@ private:
590
600
  ruby_xfree(vec);
591
601
 
592
602
  if (result.size() != (size_t)NUM2INT(k)) {
593
- rb_raise(rb_eRuntimeError, "Cannot return the results in a contigious 2D array. Probably ef or M is too small.");
594
- return Qnil;
603
+ rb_warning("Cannot return as many search results as the requested number of neighbors.");
595
604
  }
596
605
 
597
606
  VALUE distances_arr = rb_ary_new2(result.size());
598
607
  VALUE neighbors_arr = rb_ary_new2(result.size());
599
608
 
600
- for (int i = NUM2INT(k) - 1; i >= 0; i--) {
609
+ while (!result.empty()) {
601
610
  const std::pair<float, size_t>& result_tuple = result.top();
602
- rb_ary_store(distances_arr, i, DBL2NUM((double)result_tuple.first));
603
- rb_ary_store(neighbors_arr, i, INT2NUM((int)result_tuple.second));
611
+ rb_ary_unshift(distances_arr, DBL2NUM((double)result_tuple.first));
612
+ rb_ary_unshift(neighbors_arr, INT2NUM((int)result_tuple.second));
604
613
  result.pop();
605
614
  }
606
615
 
@@ -627,7 +636,10 @@ private:
627
636
  space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(ivspace);
628
637
  }
629
638
  hnswlib::BruteforceSearch<float>* index = get_hnsw_bruteforcesearch(self);
630
- if (index->data_) free(index->data_);
639
+ if (index->data_) {
640
+ free(index->data_);
641
+ index->data_ = nullptr;
642
+ }
631
643
  try {
632
644
  index->loadIndex(filename, space);
633
645
  } catch (const std::runtime_error& e) {
@@ -29,7 +29,7 @@ namespace hnswlib {
29
29
  }
30
30
 
31
31
  ~BruteforceSearch() {
32
- free(data_);
32
+ if (data_) free(data_);
33
33
  }
34
34
 
35
35
  char *data_;
@@ -129,6 +129,10 @@ namespace hnswlib {
129
129
 
130
130
 
131
131
  std::ifstream input(location, std::ios::binary);
132
+
133
+ if (!input.is_open())
134
+ throw std::runtime_error("Cannot open file");
135
+
132
136
  std::streampos position;
133
137
 
134
138
  readBinaryPOD(input, maxelements_);
@@ -76,14 +76,15 @@ namespace hnswlib {
76
76
  };
77
77
 
78
78
  ~HierarchicalNSW() {
79
-
80
- free(data_level0_memory_);
81
- for (tableint i = 0; i < cur_element_count; i++) {
82
- if (element_levels_[i] > 0)
83
- free(linkLists_[i]);
79
+ if (data_level0_memory_) free(data_level0_memory_);
80
+ if (linkLists_) {
81
+ for (tableint i = 0; i < cur_element_count; i++) {
82
+ if (element_levels_[i] > 0)
83
+ if (linkLists_[i]) free(linkLists_[i]);
84
+ }
85
+ free(linkLists_);
84
86
  }
85
- free(linkLists_);
86
- delete visited_list_pool_;
87
+ if (visited_list_pool_) delete visited_list_pool_;
87
88
  }
88
89
 
89
90
  size_t max_elements_;
@@ -3,7 +3,7 @@
3
3
  # Hnswlib.rb provides Ruby bindings for the Hnswlib.
4
4
  module Hnswlib
5
5
  # The version of Hnswlib.rb you install.
6
- VERSION = '0.6.0'
6
+ VERSION = '0.6.1'
7
7
 
8
8
  # The version of Hnswlib included with gem.
9
9
  HSWLIB_VERSION = '0.6.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hnswlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-16 00:00:00.000000000 Z
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Hnswlib.rb provides Ruby bindings for the Hnswlib.
14
14
  email: