lib/picky/query/combinations/redis.rb in picky-2.7.0 vs lib/picky/query/combinations/redis.rb in picky-3.0.0.pre1

- old
+ new

@@ -1,83 +1,87 @@ -module Query +module Picky - # Combinations are a number of Combination-s. - # - # They are the core of an allocation. - # An allocation consists of a number of combinations. - # - module Combinations # :nodoc:all + module Query - # Redis Combinations contain specific methods for - # calculating score and ids in memory. + # Combinations are a number of Combination-s. # - class Redis < Base + # They are the core of an allocation. + # An allocation consists of a number of combinations. + # + module Combinations # :nodoc:all - # Connect to the backend. + # Redis Combinations contain specific methods for + # calculating score and ids in memory. # - # TODO Use specific Picky Redis wrapper. - # - def self.redis - @redis ||= ::Redis.new :db => 15 - end + class Redis < Base - attr_reader :redis + # Connect to the backend. + # + # TODO Use specific Picky Redis wrapper. + # + def self.redis + @redis ||= ::Redis.new :db => 15 + end - # - # - def initialize combinations - super combinations + attr_reader :redis - @redis = self.class.redis - end + # + # + def initialize combinations + super combinations - # Returns the result ids for the allocation. - # - def ids amount, offset - return [] if @combinations.empty? - - identifiers = @combinations.inject([]) do |identifiers, combination| - identifiers << "#{combination.identifier}" + @redis = self.class.redis end - result_id = generate_intermediate_result_id - - # Intersect and store. + # Returns the result ids for the allocation. # - redis.zinterstore result_id, identifiers + def ids amount, offset + return [] if @combinations.empty? - # Get the stored result. - # - results = redis.zrange result_id, offset, (offset + amount) + identifiers = @combinations.inject([]) do |identifiers, combination| + identifiers << "#{combination.identifier}" + end - # Delete the stored result as it was only for temporary purposes. + result_id = generate_intermediate_result_id + + # Intersect and store. + # + redis.zinterstore result_id, identifiers + + # Get the stored result. + # + results = redis.zrange result_id, offset, (offset + amount) + + # Delete the stored result as it was only for temporary purposes. + # + # Note: I could also not delete it, but that would not be clean at all. + # + redis.del result_id + + results + end + + # Generate a multiple host/process safe result id. # - # Note: I could also not delete it, but that would not be clean at all. + # Note: Generated when this class loads. # - redis.del result_id + require 'socket' + def self.extract_host + @host ||= Socket.gethostname + end + def host + self.class.extract_host + end + extract_host + def pid + @pid ||= Process.pid + end + # Use the host and pid (generated lazily in child processes) for the result. + # + def generate_intermediate_result_id + :"#{host}:#{pid}:picky:result" + end - results - end - - # Generate a multiple host/process safe result id. - # - # Note: Generated when this class loads. - # - require 'socket' - def self.extract_host - @host ||= Socket.gethostname - end - def host - self.class.extract_host - end - extract_host - def pid - @pid ||= Process.pid - end - # Use the host and pid (generated lazily in child processes) for the result. - # - def generate_intermediate_result_id - :"#{host}:#{pid}:picky:result" end end end \ No newline at end of file