Module: BELParser::Resource::EagerReader

Defined in:
lib/bel_parser/resource/eager_reader.rb

Constant Summary collapse

EMPTY_ARRAY =
[].freeze
CACHE_KEYS =
[:name, :identifier, :title, :synonyms].freeze

Instance Method Summary collapse

Instance Method Details

#load_threadsObject



18
19
20
# File 'lib/bel_parser/resource/eager_reader.rb', line 18

def load_threads
  @load_threads ||= Concurrent::Hash.new
end

#load_values(identifier) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bel_parser/resource/eager_reader.rb', line 22

def load_values(identifier)
  lock = locks[identifier] ||= Mutex.new
  if identifier.include?('taxonomy')
    puts "load_all_values, try lock..."
      end
  if lock.try_lock
    load_threads[identifier] = Thread.new do
      value_hash = {
        :name       => {},
        :identifier => {},
        :title      => {},
        :synonyms   => {}
      }
      retrieve_values_from_resource(identifier).each do |concept|
        value_hash[:name][concept.name]             = concept
        value_hash[:identifier][concept.identifier] = concept
        value_hash[:title][concept.title]           = concept
        concept.synonyms.each do |synonym|
          value_hash[:synonyms][synonym] = concept
        end
      end
      resources[identifier] = value_hash
      lock.unlock
    end
  end
end

#locksObject



14
15
16
# File 'lib/bel_parser/resource/eager_reader.rb', line 14

def locks
  @locks ||= Concurrent::Hash.new
end

#resourcesObject



10
11
12
# File 'lib/bel_parser/resource/eager_reader.rb', line 10

def resources
  @resources ||= Concurrent::Hash.new
end

#retrieve_resource(resource_identifier) ⇒ Object



49
50
51
52
53
# File 'lib/bel_parser/resource/eager_reader.rb', line 49

def retrieve_resource(resource_identifier)
  if !resources.key?(resource_identifier)
    load_all_values(resource_identifier)
  end
end

#retrieve_value_from_resource(resource_identifier, value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bel_parser/resource/eager_reader.rb', line 55

def retrieve_value_from_resource(resource_identifier, value)
  if !resources.key?(resource_identifier)
    load_all_values(resource_identifier)
    if load_threads.key?(resource_identifier)
      load_thread = load_threads[resource_identifier]
      load_thread.join unless load_thread == Thread.current
    end
  end

  concepts = resources[resource_identifier]
  return nil unless concepts
  CACHE_KEYS.each do |key|
    cached_concept = concepts[key].fetch(value, nil)
    return cached_concept if cached_concept
  end
  nil
end

#retrieve_values_from_resource(resource_identifier) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bel_parser/resource/eager_reader.rb', line 73

def retrieve_values_from_resource(resource_identifier)
  if !resources.key?(resource_identifier)
    load_all_values(resource_identifier)
    if load_threads.key?(resource_identifier)
      load_thread = load_threads[resource_identifier]
      load_thread.join unless load_thread == Thread.current
    end
  end

  concepts = resources[resource_identifier]
  return nil unless concepts
  concepts[:name].values
end