Class: AMQ::IntAllocator
- Inherits:
-
Object
- Object
- AMQ::IntAllocator
- Defined in:
- lib/amq/int_allocator.rb
Overview
Simple bitset-based integer allocator, heavily inspired by com.rabbitmq.utility.IntAllocator class in the RabbitMQ Java client.
Unlike monotonically incrementing identifier, this allocator is suitable for very long running programs that aggressively allocate and release channels.
Instance Attribute Summary collapse
-
#hi ⇒ Integer
readonly
Upper boundary of the integer range available for allocation.
-
#lo ⇒ Integer
readonly
Lower boundary of the integer range available for allocation.
-
#number_of_bits ⇒ Integer
readonly
Number of integers in the allocation range.
Instance Method Summary collapse
-
#allocate ⇒ Integer
Attempts to allocate next available integer.
-
#allocated?(reservation) ⇒ Boolean
True if provided argument was previously allocated, false otherwise.
-
#free(reservation) ⇒ NilClass
(also: #release)
Releases previously allocated integer.
-
#initialize(lo, hi) ⇒ IntAllocator
constructor
A new instance of IntAllocator.
-
#reset ⇒ Object
Releases the whole allocation range.
Constructor Details
#initialize(lo, hi) ⇒ IntAllocator
Returns a new instance of IntAllocator.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/amq/int_allocator.rb', line 27 def initialize(lo, hi) raise ArgumentError.new "upper boundary must be greater than the lower one (given: hi = #{hi}, lo = #{lo})" unless hi > lo @hi = hi @lo = lo @number_of_bits = hi - lo @range = Range.new(1, @number_of_bits) @free_set = BitSet.new(@number_of_bits) end |
Instance Attribute Details
#hi ⇒ Integer (readonly)
Returns Upper boundary of the integer range available for allocation.
20 21 22 |
# File 'lib/amq/int_allocator.rb', line 20 def hi @hi end |
#lo ⇒ Integer (readonly)
Returns Lower boundary of the integer range available for allocation.
22 23 24 |
# File 'lib/amq/int_allocator.rb', line 22 def lo @lo end |
#number_of_bits ⇒ Integer (readonly)
Returns Number of integers in the allocation range.
18 19 20 |
# File 'lib/amq/int_allocator.rb', line 18 def number_of_bits @number_of_bits end |
Instance Method Details
#allocate ⇒ Integer
Attempts to allocate next available integer. If allocation succeeds, allocated value is returned. Otherwise, nil is returned.
Current implementation of this method is O(n), where n is number of bits in the range available for allocation.
45 46 47 48 49 50 51 52 53 |
# File 'lib/amq/int_allocator.rb', line 45 def allocate if n = find_unallocated_position @free_set.set(n) n else -1 end end |
#allocated?(reservation) ⇒ Boolean
Returns true if provided argument was previously allocated, false otherwise.
65 66 67 |
# File 'lib/amq/int_allocator.rb', line 65 def allocated?(reservation) @free_set.get(reservation) end |
#free(reservation) ⇒ NilClass Also known as: release
Releases previously allocated integer. If integer provided as argument was not previously allocated, this method has no effect.
59 60 61 |
# File 'lib/amq/int_allocator.rb', line 59 def free(reservation) @free_set.unset(reservation) end |
#reset ⇒ Object
Releases the whole allocation range
70 71 72 |
# File 'lib/amq/int_allocator.rb', line 70 def reset @free_set.clear end |