Class: Rubernate::DBI::PKGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubernate/impl/dbi_genpk.rb

Overview

Represents primary key generator

Constant Summary collapse

@@chunk_pool_lock =
Mutex.new
@@chunk_pool =
[]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.chunk_factoryObject

Returns the value of attribute chunk_factory.



30
31
32
# File 'lib/rubernate/impl/dbi_genpk.rb', line 30

def chunk_factory
  @chunk_factory
end

Class Method Details

.acquire_chunk(dbh) ⇒ Object

Acquires chunk



40
41
42
43
44
45
# File 'lib/rubernate/impl/dbi_genpk.rb', line 40

def acquire_chunk dbh     
  @@chunk_pool_lock.synchronize {
    return @@chunk_pool.shift unless @@chunk_pool.empty?
  }
  self.chunk_factory.create dbh
end

.instanceObject

Gets PKGenerator instance



33
34
35
# File 'lib/rubernate/impl/dbi_genpk.rb', line 33

def instance
  PKGenerator.new
end

.release_chunk(chunk) ⇒ Object

Release chunk



47
48
49
50
51
# File 'lib/rubernate/impl/dbi_genpk.rb', line 47

def release_chunk chunk
  @@chunk_pool_lock.synchronize {
    @@chunk_pool << chunk
  }    
end

Instance Method Details

#generate(dbh) ⇒ Object

Generates pk.



55
56
57
58
# File 'lib/rubernate/impl/dbi_genpk.rb', line 55

def generate dbh
  @chunk = self.class.acquire_chunk dbh if @chunk.nil? or @chunk.empty?
  @chunk.get_pk
end

#releaseObject

Should be invoked at the end of the session.



61
62
63
64
# File 'lib/rubernate/impl/dbi_genpk.rb', line 61

def release
  self.class.release_chunk @chunk if @chunk and !@chunk.empty?
  @chunk = nil
end