From e42bf0b626c57ea194be93678025c6fa11674ecc Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Wed, 21 Apr 2010 19:22:08 -0400 Subject: [PATCH] Setting up redis based prompt_queue to speed up catchup algorithm --- app/models/choice.rb | 3 +-- app/models/question.rb | 21 +++++++++++++++++++++ config/initializers/redis.rb | 2 ++ spec/models/question_spec.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 config/initializers/redis.rb diff --git a/app/models/choice.rb b/app/models/choice.rb index ce96cf2..361884d 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -108,7 +108,7 @@ class Choice < ActiveRecord::Base def generate_prompts #once a choice is added, we need to generate the new prompts (possible combinations of choices) - #do this in a new process (via delayed jobs) + #do this in a new process (via delayed jobs)? Maybe just for uploaded ideas previous_choices = (self.question.choices - [self]) return if previous_choices.empty? inserts = [] @@ -127,7 +127,6 @@ class Choice < ActiveRecord::Base Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size) - logger.info("The sql is:::: #{sql}") ActiveRecord::Base.connection.execute(sql) diff --git a/app/models/question.rb b/app/models/question.rb index 20b8174..f45ce02 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -298,6 +298,27 @@ class Question < ActiveRecord::Base end end + def pq_key + @pq_key ||= "#{self.id}_prompt_queue" + end + + def clear_prompt_queue + $redis.del(self.pq_key) + end + + def add_prompt_to_queue + prompt = self.catchup_choose_prompt + $redis.rpush(self.pq_key, prompt.id) + prompt + end + + def pop_prompt_queue + prompt_id = $redis.lpop(self.pq_key) + prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i) + end + + + end diff --git a/config/initializers/redis.rb b/config/initializers/redis.rb new file mode 100644 index 0000000..bf03bc0 --- /dev/null +++ b/config/initializers/redis.rb @@ -0,0 +1,2 @@ +require 'redis' +$redis = Redis.new diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 537e7d9..2168db5 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -84,6 +84,49 @@ describe Question do (sum - 1.0).abs.should < 0.000001 end + it "should allow the prompt queue to be cleared" do + @catchup_q.add_prompt_to_queue + @catchup_q.clear_prompt_queue + + @catchup_q.pop_prompt_queue.should == nil + end + it "should allow a prompt to be added to the prompt queue" do + @catchup_q.clear_prompt_queue + @catchup_q.pop_prompt_queue.should == nil + + @catchup_q.add_prompt_to_queue + + prompt = @catchup_q.pop_prompt_queue + + prompt.should_not == nil + prompt.active?.should == true + end + it "should return prompts from the queue in FIFO order" do + @catchup_q.clear_prompt_queue + @catchup_q.pop_prompt_queue.should == nil + + prompt1 = @catchup_q.add_prompt_to_queue + prompt2 = @catchup_q.add_prompt_to_queue + prompt3 = @catchup_q.add_prompt_to_queue + + prompt_1 = @catchup_q.pop_prompt_queue + prompt_2 = @catchup_q.pop_prompt_queue + prompt_3 = @catchup_q.pop_prompt_queue + + + prompt_1.should == prompt1 + prompt_2.should == prompt2 + prompt_3.should == prompt3 + + # there is a small probability that the catchup algorithm + # choose two prompts that are indeed equal + prompt_1.should_not == prompt_2 + prompt_1.should_not == prompt_3 + prompt_2.should_not == prompt_3 + + + @catchup_q.pop_prompt_queue.should == nil + end end -- libgit2 0.21.2