Class: Ngt::Index
- Inherits:
-
Object
- Object
- Ngt::Index
- Defined in:
- lib/ngt/index.rb
Class Method Summary collapse
- .create(path, dimension, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: "Float", distance_type: "L2") ⇒ Object
-
.ffi(method, *args) ⇒ Object
private.
- .finalize(error) ⇒ Object
Instance Method Summary collapse
-
#batch_insert(objects, num_threads: 8) ⇒ Object
TODO make more performant for Numo.
- #build_index(num_threads: 8) ⇒ Object
- #close ⇒ Object
-
#initialize(path) ⇒ Index
constructor
A new instance of Index.
- #insert(object) ⇒ Object
- #object(id) ⇒ Object
- #remove(id) ⇒ Object
- #save(path: nil) ⇒ Object
- #search(query, size: 20, epsilon: 0.1, radius: nil) ⇒ Object
Constructor Details
#initialize(path) ⇒ Index
Returns a new instance of Index.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ngt/index.rb', line 3 def initialize(path) @path = path @error = FFI.ngt_create_error_object @index = ffi(:ngt_open_index, path) property = ffi(:ngt_create_property) ffi(:ngt_get_property, @index, property) @dimension = ffi(:ngt_get_property_dimension, property) object_type = ffi(:ngt_get_property_object_type, property) @float = FFI.ngt_is_property_object_type_float(object_type) @object_space = ffi(:ngt_get_object_space, @index) ObjectSpace.define_finalizer(self, self.class.finalize(@error)) end |
Class Method Details
.create(path, dimension, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: "Float", distance_type: "L2") ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ngt/index.rb', line 83 def self.create(path, dimension, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: "Float", distance_type: "L2") error = FFI.ngt_create_error_object property = ffi(:ngt_create_property, error) ffi(:ngt_set_property_dimension, property, dimension, error) ffi(:ngt_set_property_edge_size_for_creation, property, edge_size_for_creation, error) ffi(:ngt_set_property_edge_size_for_search, property, edge_size_for_search, error) case object_type.to_s when "Float", "float" ffi(:ngt_set_property_object_type_float, property, error) when "Integer", "integer" ffi(:ngt_set_property_object_type_integer, property, error) else raise ArgumentError, "Unknown object type: #{object_type}" end case distance_type.to_s when "L1" ffi(:ngt_set_property_distance_type_l1, property, error) when "L2" ffi(:ngt_set_property_distance_type_l2, property, error) when "Angle" ffi(:ngt_set_property_distance_type_angle, property, error) when "Hamming" ffi(:ngt_set_property_distance_type_hamming, property, error) when "Jaccard" ffi(:ngt_set_property_distance_type_jaccard, property, error) when "Cosine" ffi(:ngt_set_property_distance_type_cosine, property, error) else raise ArgumentError, "Unknown distance type: #{distance_type}" end index = ffi(:ngt_create_graph_and_tree, path, property, error) FFI.ngt_close_index(index) index = nil Index.new(path) ensure FFI.ngt_destroy_error_object(error) if error FFI.ngt_destroy_property(property) if property FFI.ngt_close_index(index) if index end |
.ffi(method, *args) ⇒ Object
private
130 131 132 133 134 135 |
# File 'lib/ngt/index.rb', line 130 def self.ffi(method, *args) res = FFI.send(method, *args) = FFI.ngt_get_error_string(args.last) raise Error, unless .empty? res end |
.finalize(error) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/ngt/index.rb', line 137 def self.finalize(error) # must use proc instead of stabby lambda proc do # TODO clean-up more objects FFI.ngt_destroy_error_object(error) end end |
Instance Method Details
#batch_insert(objects, num_threads: 8) ⇒ Object
TODO make more performant for Numo
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ngt/index.rb', line 26 def batch_insert(objects, num_threads: 8) objects = objects.to_a flat_objects = objects.flatten obj = ::FFI::MemoryPointer.new(:float, flat_objects.size) obj.write_array_of_float(flat_objects) ids = ::FFI::MemoryPointer.new(:uint32, objects.size) ffi(:ngt_batch_insert_index, @index, obj, objects.size, ids) build_index(num_threads: num_threads) ids.read_array_of_uint32(objects.size) end |
#build_index(num_threads: 8) ⇒ Object
40 41 42 |
# File 'lib/ngt/index.rb', line 40 def build_index(num_threads: 8) ffi(:ngt_create_index, @index, num_threads) end |
#close ⇒ Object
79 80 81 |
# File 'lib/ngt/index.rb', line 79 def close FFI.ngt_close_index(@index) end |
#insert(object) ⇒ Object
21 22 23 |
# File 'lib/ngt/index.rb', line 21 def insert(object) ffi(:ngt_insert_index, @index, c_object(object.to_a), @dimension) end |
#object(id) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/ngt/index.rb', line 44 def object(id) if float? res = ffi(:ngt_get_object_as_float, @object_space, id) res.read_array_of_float(@dimension) else res = ffi(:ngt_get_object_as_integer, @object_space, id) res.read_array_of_uint8(@dimension) end end |
#remove(id) ⇒ Object
54 55 56 |
# File 'lib/ngt/index.rb', line 54 def remove(id) ffi(:ngt_remove_index, @index, id) end |
#save(path: nil) ⇒ Object
74 75 76 77 |
# File 'lib/ngt/index.rb', line 74 def save(path: nil) path ||= @path ffi(:ngt_save_index, @index, path) end |
#search(query, size: 20, epsilon: 0.1, radius: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ngt/index.rb', line 58 def search(query, size: 20, epsilon: 0.1, radius: nil) radius ||= -1.0 results = ffi(:ngt_create_empty_results) ffi(:ngt_search_index, @index, c_object(query.to_a), @dimension, size, epsilon, radius, results) result_size = ffi(:ngt_get_result_size, results) ret = [] result_size.times do |i| res = ffi(:ngt_get_result, results, i) ret << { id: res[:id], distance: res[:distance] } end ret end |