Sha256: 3d917bb0fb1a9221ad94cd12d6b8dca9e83a6f81ec0ea67342edff64ac146b4c

Contents?: true

Size: 1.35 KB

Versions: 111

Compression:

Stored size: 1.35 KB

Contents

require 'concurrent/hash'

require_relative 'sparql_reader'

module BELParser
  module Resource
    # EagerSPARQLReader
    class EagerSPARQLReader < SPARQLReader
      def initialize(sparql_endpoint_url, validate_url = true)
        @resources = Concurrent::Hash.new
        @locks     = Concurrent::Hash.new
        super
      end

      def retrieve_resource(resource_identifier)
        if @resources.key?(resource_identifier)
          concepts = @resources[resource_identifier]
          concepts[value]
        else
          lock = @locks[resource_identifier] ||= Mutex.new
          if lock.try_lock
            Thread.new do
              concepts = retrieve_values_from_resource(resource_identifier).to_a
              @resources[resource_identifier] = 
                Hash[
                  concepts.map do |c|
                    [c.name, c]
                  end
                ]
              lock.unlock
            end
          end

          super
        end
      end

      def retrieve_value_from_resource(resource_identifier, value)
        concepts = @resources[resource_identifier]
        return super unless concepts
        concepts[value]
      end

      def retrieve_values_from_resource(resource_identifier)
        concepts = @resources[resource_identifier]
        return super unless concepts
        concepts
      end
    end
  end
end

Version data entries

111 entries across 111 versions & 1 rubygems

Version Path
bel_parser-1.0.0.alpha.56 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.55-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.55 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.54-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.54 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.53-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.53 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.52-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.52 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.51-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.51 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.50-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.50 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.49-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.49 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.48-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.48 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.47-java lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.47 lib/bel_parser/resource/eager_sparql_reader.rb
bel_parser-1.0.0.alpha.46-java lib/bel_parser/resource/eager_sparql_reader.rb