Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added mutliple locking plans to the database

  • Loading branch information...
commit e85b24d3b39b577b9be63d478784d3e8a92c600f 1 parent 72c6699
Ryan R. Smith authored
Showing with 47 additions and 27 deletions.
  1. +1 −0  benchmark.rb
  2. +46 −27 lib/queue_classic/database.rb
View
1  benchmark.rb
@@ -14,6 +14,7 @@ def self.length(string)
array = QC::Queue.instance.data_store
database = array.database
+database.plan = :random_offset
database.init_db
Benchmark.bm(10) do |x|
View
73 lib/queue_classic/database.rb
@@ -1,5 +1,6 @@
module QC
class Database
+ attr_accessor :plan
def initialize(url,opts={})
@db_params = URI.parse(url)
@@ -53,37 +54,55 @@ def create_table
end
def load_functions
- execute(<<-EOD
- CREATE OR REPLACE FUNCTION lock_head() RETURNS SETOF jobs AS $$
- DECLARE
- unlocked integer;
- relative_top integer;
- job_count integer;
- job jobs%rowtype;
+ @plan ||= :fifo
- BEGIN
- SELECT TRUNC(random() * #{@top_boundry} + 1) INTO relative_top;
- SELECT count(*) from jobs INTO job_count;
+ case @plan
+ when :random_offset
+ execute(<<-EOD
+ CREATE OR REPLACE FUNCTION lock_head() RETURNS SETOF jobs AS $$
+ DECLARE
+ unlocked integer;
+ relative_top integer;
+ job_count integer;
+ job jobs%rowtype;
- IF job_count < 10 THEN
- relative_top = 0;
- END IF;
+ BEGIN
+ SELECT TRUNC(random() * #{@top_boundry} + 1) INTO relative_top;
+ SELECT count(*) from jobs INTO job_count;
- SELECT id INTO unlocked
- FROM jobs
- WHERE locked_at IS NULL
- ORDER BY id ASC
- LIMIT 1
- OFFSET relative_top
- FOR UPDATE NOWAIT;
- RETURN QUERY UPDATE jobs
- SET locked_at = (CURRENT_TIMESTAMP)
- WHERE id = unlocked AND locked_at IS NULL
+ IF job_count < 10 THEN
+ relative_top = 0;
+ END IF;
+
+ SELECT id INTO unlocked
+ FROM jobs
+ WHERE locked_at IS NULL
+ ORDER BY id ASC
+ LIMIT 1
+ OFFSET relative_top
+ FOR UPDATE NOWAIT;
+ RETURN QUERY UPDATE jobs
+ SET locked_at = (CURRENT_TIMESTAMP)
+ WHERE id = unlocked AND locked_at IS NULL
+ RETURNING *;
+ END;
+ $$ LANGUAGE plpgsql;
+ EOD
+ )
+ when :fifo
+ execute(<<-EOD
+ CREATE OR REPLACE FUNCTION lock_head() RETURNS jobs AS $$
+ UPDATE jobs SET locked_at = (CURRENT_TIMESTAMP)
+ WHERE id = (
+ SELECT id FROM jobs
+ WHERE locked_at IS NULL
+ ORDER BY id ASC LIMIT 1
+ )
RETURNING *;
- END;
- $$ LANGUAGE plpgsql;
- EOD
- )
+ $$ LANGUAGE SQL;
+ EOD
+ )
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.