Module: Picky::Pool

Defined in:
lib/picky/pool.rb

Overview

Module that handles object pool behaviour for you.

Class Method Summary collapse

Class Method Details

.add(klass) ⇒ Object

Add a pooled class to the managed pools.



36
37
38
# File 'lib/picky/pool.rb', line 36

def add klass
  @pools << klass
end

.clearObject

Initialise/Reset the pool.



29
30
31
32
# File 'lib/picky/pool.rb', line 29

def clear
  require 'set'
  @pools = Set.new
end

.extended(klass) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/picky/pool.rb', line 52

def self.extended klass
  add klass
  
  class << klass
    
    # Initialise/Reset the class pool.
    #
    def clear
      @__free__ = []
      @__used__ = []
    end
       
    # Obtain creates a new reference if there is no free one
    # and uses an existing one if there is.
    #
    def new *args, &block
      unless reference = @__free__.shift
        reference = allocate
      end
      reference.send :initialize, *args, &block
      @__used__ << reference
      reference
    end
  
    # Releasing an instance adds it to the free pool.
    # (And removes it from the used pool)
    #
    def release instance
      @__free__ << instance
       
      # Note: This is relatively fast as there are often only
      # few instances in the used pool.
      #
      @__used__.delete instance 
    end
  
    # After you have called release all, you can't externally
    # use any reference that has formerly been obtained
    # anymore.
    #
    def release_all
      @__used__.each { |used| @__free__ << used } # +0 Array per release_all
      # @__used__ += @__free__ # +1 Array per release_all
      @__used__.clear
    end
  
    # How many obtainable objects are there?
    #
    def free_size
      @__free__.size
    end
  
    # How many used objects are there?
    #
    def used_size
      @__used__.size
    end
  end
  
  # Initialize the pool.
  #
  klass.clear

  # Pooled objects can be released using
  #   object.release
  #
  # Note: Do not use the object in any form after the release.
  #
  klass.send :define_method, :release do
    klass.release self
  end
  
end

.installObject

Installs itself on a few internal classes.

Note: If you need to run two consecutive queries, this can’t be used.

Note: You need to call Picky::Pool.release_all after each query (or after a few queries).



18
19
20
21
22
23
24
25
# File 'lib/picky/pool.rb', line 18

def install
  Query::Token.extend self
  Query::Tokens.extend self
  Query::Combination.extend self
  Query::Combinations.extend self
  Query::Allocation.extend self
  Query::Allocations.extend self
end

.release_allObject

Releases all obtained objects.



42
43
44
# File 'lib/picky/pool.rb', line 42

def release_all
  @pools.each { |pool| pool.release_all }
end