Class: Ngt::Index
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .create(path, dimensions, **options) ⇒ Object
-
.ffi(*args) ⇒ Object
private.
- .finalize(error, index, property) ⇒ Object
- .load(path) ⇒ Object
- .new(dimensions, path: nil, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: :float, distance_type: :l2) ⇒ Object
Instance Method Summary collapse
- #batch_insert(objects, num_threads: 8) ⇒ Object
- #build_index(num_threads: 8) ⇒ Object
- #close ⇒ Object
- #dimensions ⇒ Object
- #distance_type ⇒ Object
- #edge_size_for_creation ⇒ Object
- #edge_size_for_search ⇒ Object
-
#initialize(index, path) ⇒ Index
constructor
A new instance of Index.
- #insert(object) ⇒ Object
- #object(id) ⇒ Object
- #object_type ⇒ Object
- #remove(id) ⇒ Object
- #save(path = nil) ⇒ Object
- #search(query, size: 20, epsilon: 0.1, radius: nil) ⇒ Object
Constructor Details
#initialize(index, path) ⇒ Index
Returns a new instance of Index.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ngt/index.rb', line 7 def initialize(index, path) @index = index @path = path @error = FFI.ngt_create_error_object @property = ffi(:ngt_create_property) ffi(:ngt_get_property, @index, @property) ObjectSpace.define_finalizer(self, self.class.finalize(@error, @index, @property)) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/ngt/index.rb', line 5 def path @path end |
Class Method Details
.create(path, dimensions, **options) ⇒ Object
173 174 175 176 |
# File 'lib/ngt/index.rb', line 173 def self.create(path, dimensions, **) warn "[ngt] create is deprecated - use new instead" new(dimensions, path: path, **) end |
.ffi(*args) ⇒ Object
private
179 180 181 |
# File 'lib/ngt/index.rb', line 179 def self.ffi(*args) Utils.ffi(*args) end |
.finalize(error, index, property) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/ngt/index.rb', line 183 def self.finalize(error, index, property) # must use proc instead of stabby lambda proc do FFI.ngt_destroy_error_object(error) FFI.ngt_close_index(index) FFI.ngt_destroy_property(property) end end |
.load(path) ⇒ Object
169 170 171 |
# File 'lib/ngt/index.rb', line 169 def self.load(path) new(nil, path: path) end |
.new(dimensions, path: nil, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: :float, distance_type: :l2) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ngt/index.rb', line 112 def self.new(dimensions, path: nil, edge_size_for_creation: 10, edge_size_for_search: 40, object_type: :float, distance_type: :l2) error = FFI.ngt_create_error_object if path && dimensions.nil? index = ffi(:ngt_open_index, path, error) else property = ffi(:ngt_create_property, error) ffi(:ngt_set_property_dimension, property, dimensions, 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.downcase when "float" ffi(:ngt_set_property_object_type_float, property, error) when "integer" ffi(:ngt_set_property_object_type_integer, property, error) else raise ArgumentError, "Unknown object type: #{object_type}" end case distance_type.to_s.downcase 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) when "normalized_angle" ffi(:ngt_set_property_distance_type_normalized_angle, property, error) when "normalized_cosine" ffi(:ngt_set_property_distance_type_normalized_cosine, property, error) else raise ArgumentError, "Unknown distance type: #{distance_type}" end index = if path ffi(:ngt_create_graph_and_tree, path, property, error) else ffi(:ngt_create_graph_and_tree_in_memory, property, error) end end super(index, path) ensure FFI.ngt_destroy_error_object(error) if error FFI.ngt_destroy_property(property) if property end |
Instance Method Details
#batch_insert(objects, num_threads: 8) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ngt/index.rb', line 45 def batch_insert(objects, num_threads: 8) if narray?(objects) objects = objects.cast_to(Numo::SFloat) unless objects.is_a?(Numo::SFloat) count = objects.shape[0] obj = ::FFI::MemoryPointer.new(:char, objects.byte_size) obj.write_bytes(objects.to_binary) else objects = objects.to_a count = objects.size flat_objects = objects.flatten obj = ::FFI::MemoryPointer.new(:float, flat_objects.size) obj.write_array_of_float(flat_objects) end ids = ::FFI::MemoryPointer.new(:uint32, count) ffi(:ngt_batch_insert_index, @index, obj, count, ids) build_index(num_threads: num_threads) ids.read_array_of_uint32(count) end |
#build_index(num_threads: 8) ⇒ Object
67 68 69 |
# File 'lib/ngt/index.rb', line 67 def build_index(num_threads: 8) ffi(:ngt_create_index, @index, num_threads) end |
#close ⇒ Object
108 109 110 |
# File 'lib/ngt/index.rb', line 108 def close FFI.ngt_close_index(@index) end |
#dimensions ⇒ Object
18 19 20 |
# File 'lib/ngt/index.rb', line 18 def dimensions @dimensions ||= ffi(:ngt_get_property_dimension, @property) end |
#distance_type ⇒ Object
22 23 24 |
# File 'lib/ngt/index.rb', line 22 def distance_type @distance_type ||= ffi(:ngt_get_property_distance_type, @property) end |
#edge_size_for_creation ⇒ Object
26 27 28 |
# File 'lib/ngt/index.rb', line 26 def edge_size_for_creation @edge_size_for_creation ||= ffi(:ngt_get_property_edge_size_for_creation, @property) end |
#edge_size_for_search ⇒ Object
30 31 32 |
# File 'lib/ngt/index.rb', line 30 def edge_size_for_search @edge_size_for_search ||= ffi(:ngt_get_property_edge_size_for_search, @property) end |
#insert(object) ⇒ Object
41 42 43 |
# File 'lib/ngt/index.rb', line 41 def insert(object) ffi(:ngt_insert_index, @index, c_object(object.to_a), dimensions) end |
#object(id) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/ngt/index.rb', line 71 def object(id) if object_type == :float res = ffi(:ngt_get_object_as_float, @object_space, id) res.read_array_of_float(dimensions) else res = ffi(:ngt_get_object_as_integer, @object_space, id) res.read_array_of_uint8(dimensions) end end |
#object_type ⇒ Object
34 35 36 37 38 39 |
# File 'lib/ngt/index.rb', line 34 def object_type @object_type ||= begin object_type = ffi(:ngt_get_property_object_type, @property) FFI.ngt_is_property_object_type_float(object_type) ? :float : :integer end end |
#remove(id) ⇒ Object
81 82 83 |
# File 'lib/ngt/index.rb', line 81 def remove(id) ffi(:ngt_remove_index, @index, id) end |
#save(path = nil) ⇒ Object
103 104 105 106 |
# File 'lib/ngt/index.rb', line 103 def save(path = nil) @path = path || @path || Dir.mktmpdir ffi(:ngt_save_index, @index, @path) end |
#search(query, size: 20, epsilon: 0.1, radius: nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ngt/index.rb', line 85 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), dimensions, 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 ensure FFI.ngt_destroy_results(results) if results end |