hnswlib 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f313f103d9a4a6e6ae92f21cc92a4aaaecd3121e3a2c5be533f87954afb5ac7d
4
- data.tar.gz: 3b7ec61621c905759f466df1ac76c18a76186a5ef5c84517ada5c1493524ec5c
3
+ metadata.gz: dfe29cb17343b0e602684976fa3bd91a9756226e42fdaedded8398e8d97dc83d
4
+ data.tar.gz: 5b1fca956e5a4bb3e28b46dd891f969a4dbfe7b1f03295657322ffc77b089595
5
5
  SHA512:
6
- metadata.gz: 12717a35c16fb2ef3f8884fb541559349410e43c9ba35c98de4fbf4a5faa6cd58ffba2cd26ac049ffaea855cf2c4e33f77ffefea023b901cbd8605ada3b63d5c
7
- data.tar.gz: c134905c5c268313a8a6bfff69567a66ca62e3d2e1a9d133cd87592262994eaf3731ac3b59e1eaed212a9dfa514f2c91b9eceb11f420e6f988d15f9250dbe863
6
+ metadata.gz: 213e43f294b0c6e306aef4a700df4b733b1cbd40253a61d91b15b272b7f0e2b095a5eebc79efb5d5d8af8b01048752a87bad4f610b528d49c1809d09e8e9c51b
7
+ data.tar.gz: 2cffaef5381339dd85d8ad93aff9bf1e3ac271a7797b9e5a8c6122836d3401da17380f31db6594577d5412a0c1372609ed3e3b717c401b603370113d8d811e05
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.5.3] - 2022-03-05
2
+
3
+ - Add error handling for std::runtime_error throwed from hnswlib.
4
+ - Add memory release for existing search index when loading index.
5
+
1
6
  ## [0.5.2] - 2022-02-19
2
7
 
3
8
  - Update bundled hnswlib version to 0.6.2.
@@ -277,7 +277,12 @@ class RbHnswlibHierarchicalNSW {
277
277
  const size_t random_seed = (size_t)NUM2INT(kw_values[4]);
278
278
 
279
279
  hnswlib::HierarchicalNSW<float>* ptr = get_hnsw_hierarchicalnsw(self);
280
- new (ptr) hnswlib::HierarchicalNSW<float>(space, max_elements, m, ef_construction, random_seed);
280
+ try {
281
+ new (ptr) hnswlib::HierarchicalNSW<float>(space, max_elements, m, ef_construction, random_seed);
282
+ } catch(const std::runtime_error& e) {
283
+ rb_raise(rb_eRuntimeError, "%s", e.what());
284
+ return Qnil;
285
+ }
281
286
 
282
287
  return Qnil;
283
288
  };
@@ -334,8 +339,14 @@ class RbHnswlibHierarchicalNSW {
334
339
  vec[i] = (float)NUM2DBL(rb_ary_entry(arr, i));
335
340
  }
336
341
 
337
- std::priority_queue<std::pair<float, size_t>> result =
338
- get_hnsw_hierarchicalnsw(self)->searchKnn((void *)vec, (size_t)NUM2INT(k));
342
+ std::priority_queue<std::pair<float, size_t>> result;
343
+ try {
344
+ result = get_hnsw_hierarchicalnsw(self)->searchKnn((void *)vec, (size_t)NUM2INT(k));
345
+ } catch(const std::runtime_error& e) {
346
+ ruby_xfree(vec);
347
+ rb_raise(rb_eRuntimeError, "%s", e.what());
348
+ return Qnil;
349
+ }
339
350
 
340
351
  ruby_xfree(vec);
341
352
 
@@ -376,7 +387,21 @@ class RbHnswlibHierarchicalNSW {
376
387
  } else {
377
388
  space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(ivspace);
378
389
  }
379
- get_hnsw_hierarchicalnsw(self)->loadIndex(filename, space);
390
+ hnswlib::HierarchicalNSW<float>* index = get_hnsw_hierarchicalnsw(self);
391
+ if (index->data_level0_memory_) free(index->data_level0_memory_);
392
+ if (index->linkLists_) {
393
+ for (hnswlib::tableint i = 0; i < index->cur_element_count; i++) {
394
+ if (index->element_levels_[i] > 0 && index->linkLists_[i]) free(index->linkLists_[i]);
395
+ }
396
+ free(index->linkLists_);
397
+ }
398
+ if (index->visited_list_pool_) delete index->visited_list_pool_;
399
+ try {
400
+ index->loadIndex(filename, space);
401
+ } catch(const std::runtime_error& e) {
402
+ rb_raise(rb_eRuntimeError, "%s", e.what());
403
+ return Qnil;
404
+ }
380
405
  RB_GC_GUARD(_filename);
381
406
  return Qnil;
382
407
  };
@@ -389,8 +414,9 @@ class RbHnswlibHierarchicalNSW {
389
414
  for (size_t i = 0; i < vec.size(); i++) {
390
415
  rb_ary_store(ret, i, DBL2NUM((double)vec[i]));
391
416
  }
392
- } catch(std::runtime_error const& e) {
417
+ } catch(const std::runtime_error& e) {
393
418
  rb_raise(rb_eRuntimeError, "%s", e.what());
419
+ return Qnil;
394
420
  }
395
421
  return ret;
396
422
  };
@@ -404,7 +430,12 @@ class RbHnswlibHierarchicalNSW {
404
430
  };
405
431
 
406
432
  static VALUE _hnsw_hierarchicalnsw_mark_deleted(VALUE self, VALUE idx) {
407
- get_hnsw_hierarchicalnsw(self)->markDelete((size_t)NUM2INT(idx));
433
+ try {
434
+ get_hnsw_hierarchicalnsw(self)->markDelete((size_t)NUM2INT(idx));
435
+ } catch(const std::runtime_error& e) {
436
+ rb_raise(rb_eRuntimeError, "%s", e.what());
437
+ return Qnil;
438
+ }
408
439
  return Qnil;
409
440
  };
410
441
 
@@ -413,7 +444,12 @@ class RbHnswlibHierarchicalNSW {
413
444
  rb_raise(rb_eArgError, "Cannot resize, max element is less than the current number of elements.");
414
445
  return Qnil;
415
446
  }
416
- get_hnsw_hierarchicalnsw(self)->resizeIndex((size_t)NUM2INT(new_max_elements));
447
+ try {
448
+ get_hnsw_hierarchicalnsw(self)->resizeIndex((size_t)NUM2INT(new_max_elements));
449
+ } catch(const std::runtime_error& e) {
450
+ rb_raise(rb_eRuntimeError, "%s", e.what());
451
+ return Qnil;
452
+ }
417
453
  return Qnil;
418
454
  };
419
455
 
@@ -510,7 +546,12 @@ class RbHnswlibBruteforceSearch {
510
546
  const size_t max_elements = (size_t)NUM2INT(kw_values[1]);
511
547
 
512
548
  hnswlib::BruteforceSearch<float>* ptr = get_hnsw_bruteforcesearch(self);
513
- new (ptr) hnswlib::BruteforceSearch<float>(space, max_elements);
549
+ try {
550
+ new (ptr) hnswlib::BruteforceSearch<float>(space, max_elements);
551
+ } catch(const std::runtime_error& e) {
552
+ rb_raise(rb_eRuntimeError, "%s", e.what());
553
+ return Qnil;
554
+ }
514
555
 
515
556
  return Qnil;
516
557
  };
@@ -538,7 +579,13 @@ class RbHnswlibBruteforceSearch {
538
579
  vec[i] = (float)NUM2DBL(rb_ary_entry(arr, i));
539
580
  }
540
581
 
541
- get_hnsw_bruteforcesearch(self)->addPoint((void *)vec, (size_t)NUM2INT(idx));
582
+ try {
583
+ get_hnsw_bruteforcesearch(self)->addPoint((void *)vec, (size_t)NUM2INT(idx));
584
+ } catch(const std::runtime_error& e) {
585
+ ruby_xfree(vec);
586
+ rb_raise(rb_eRuntimeError, "%s", e.what());
587
+ return Qfalse;
588
+ }
542
589
 
543
590
  ruby_xfree(vec);
544
591
  return Qtrue;
@@ -609,7 +656,14 @@ class RbHnswlibBruteforceSearch {
609
656
  } else {
610
657
  space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(ivspace);
611
658
  }
612
- get_hnsw_bruteforcesearch(self)->loadIndex(filename, space);
659
+ hnswlib::BruteforceSearch<float>* index = get_hnsw_bruteforcesearch(self);
660
+ if (index->data_) free(index->data_);
661
+ try {
662
+ index->loadIndex(filename, space);
663
+ } catch(const std::runtime_error& e) {
664
+ rb_raise(rb_eRuntimeError, "%s", e.what());
665
+ return Qnil;
666
+ }
613
667
  RB_GC_GUARD(_filename);
614
668
  return Qnil;
615
669
  };
@@ -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.5.2'
6
+ VERSION = '0.5.3'
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.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-19 00:00:00.000000000 Z
11
+ date: 2022-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Hnswlib.rb provides Ruby bindings for the Hnswlib.
14
14
  email: