Class: ODBA::Index

Inherits:
IndexCommon show all
Defined in:
lib/odba/index.rb

Overview

Constant Summary

Constants included from Persistable

Persistable::Exact, Persistable::Find, Persistable::ODBA_EXCLUDE_VARS, Persistable::ODBA_INDEXABLE, Persistable::ODBA_PREDEFINE_EXCLUDE_VARS, Persistable::ODBA_PREDEFINE_SERIALIZABLE, Persistable::ODBA_PREFETCH, Persistable::ODBA_SERIALIZABLE

Instance Attribute Summary

Attributes inherited from IndexCommon

#class_filter, #index_name, #origin_klass, #resolve_origin, #resolve_search_term, #resolve_target, #target_klass

Attributes included from Persistable

#odba_id, #odba_name, #odba_persistent, #odba_prefetch

Instance Method Summary collapse

Methods inherited from IndexCommon

#current_origin_ids, #current_target_ids, #delete, #delete_origin, #delete_target, #do_update_index, #fill, #keys, #matches, #origin_class?, #proc_instance_origin, #proc_instance_target, #proc_resolve_search_term, #search_term, #set_relevance, #update, #update_target

Methods included from Persistable

#==, append_features, #dup, #eql?, #odba_add_observer, #odba_collection, #odba_cut_connection, #odba_delete, #odba_delete_observer, #odba_delete_observers, #odba_dup, #odba_exclude_vars, #odba_indexable?, #odba_isolated_dump, #odba_isolated_store, #odba_isolated_stub, #odba_isolated_twin, #odba_notify_observers, #odba_observers, #odba_potentials, #odba_prefetch?, #odba_replace!, #odba_replace_persistables, #odba_replace_stubs, #odba_restore, #odba_serializables, #odba_snapshot, #odba_store, #odba_store_unsaved, #odba_stubize, #odba_take_snapshot, #odba_target_ids, #odba_unsaved?, #odba_unsaved_neighbors, sanitize

Constructor Details

#initialize(index_definition, origin_module) ⇒ Index

:nodoc: all



238
239
240
241
# File 'lib/odba/index.rb', line 238

def initialize(index_definition, origin_module) # :nodoc:
  super
  ODBA.storage.create_index(index_definition.index_name)
end

Instance Method Details

#fetch_ids(search_term, meta = nil) ⇒ Object

:nodoc:



243
244
245
246
247
248
249
250
251
# File 'lib/odba/index.rb', line 243

def fetch_ids(search_term, meta = nil) # :nodoc:
  exact = meta.respond_to?(:exact) && meta.exact
  limit = meta.respond_to?(:limit) && meta.limit
  rows = ODBA.storage.retrieve_from_index(@index_name,
    search_term.to_s.downcase,
    exact, limit)
  set_relevance(meta, rows)
  rows.collect { |row| row.at(0) }
end

#search_terms(origin) ⇒ Object



293
294
295
# File 'lib/odba/index.rb', line 293

def search_terms(origin)
  super.collect { |term| term.to_s.downcase }.uniq
end

#update_origin(object) ⇒ Object

:nodoc:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/odba/index.rb', line 253

def update_origin(object) # :nodoc:
  # Possible changes:
  # - search_terms of origin have changed
  # - targets have changed, except if @resolve_target == :none
  # => we need a matrix of all current [term, target_id]
  #                     and of all new [term, target_id]
  origin_id = object.odba_id
  search_terms = search_terms(object)
  current = current_target_ids(origin_id)
  target_ids = if @resolve_target == :none
    current.dup
  else
    proc_instance_target.call(object).collect { |obj|
      obj.odba_id
    }
  end
  target_ids.compact!
  target_ids.uniq!
  current_ids = []
  current_terms = []
  current.each { |row|
    current_ids.push(row[0])
    current_terms.push(row[1])
  }
  current_ids.uniq!
  current_terms.uniq!
  current_combinations = current_ids.each_with_object([]) { |id, memo|
    current_terms.each { |term| memo.push [term, id] }
  }
  combinations = target_ids.each_with_object([]) { |id, memo|
    search_terms.each { |term| memo.push [term, id] }
  }
  (current_combinations - combinations).each { |pair|
    delete_target(origin_id, *pair)
  }
  (combinations - current_combinations).each { |pair|
    do_update_index(origin_id, *pair)
  }
end