Class: AMQ::IntAllocator

### Home Classes Methods

### In Files

- lib/amq/int_allocator.rb

### Parent

Object

### Methods

- ::new
- #allocate
- #allocated?
- #free
- #release
- #reset

### Class/Module Index

Quicksearch

- AMQ
- AMQ::BitSet
- AMQ::Endianness
- AMQ::Hacks
- AMQ::IntAllocator
- AMQ::Pack
- AMQ::Protocol
- AMQ::Protocol::AccessRefused
- AMQ::Protocol::BadResponseError
- AMQ::Protocol::Basic
- AMQ::Protocol::Basic::Ack
- AMQ::Protocol::Basic::Cancel
- AMQ::Protocol::Basic::CancelOk
- AMQ::Protocol::Basic::Consume
- AMQ::Protocol::Basic::ConsumeOk
- AMQ::Protocol::Basic::Deliver
- AMQ::Protocol::Basic::Get
- AMQ::Protocol::Basic::GetEmpty
- AMQ::Protocol::Basic::GetOk
- AMQ::Protocol::Basic::Nack
- AMQ::Protocol::Basic::Publish
- AMQ::Protocol::Basic::Qos
- AMQ::Protocol::Basic::QosOk
- AMQ::Protocol::Basic::Recover
- AMQ::Protocol::Basic::RecoverAsync
- AMQ::Protocol::Basic::RecoverOk
- AMQ::Protocol::Basic::Reject
- AMQ::Protocol::Basic::Return
- AMQ::Protocol::BodyFrame
- AMQ::Protocol::Channel
- AMQ::Protocol::Channel::Close
- AMQ::Protocol::Channel::CloseOk
- AMQ::Protocol::Channel::Flow
- AMQ::Protocol::Channel::FlowOk
- AMQ::Protocol::Channel::Open
- AMQ::Protocol::Channel::OpenOk
- AMQ::Protocol::ChannelError
- AMQ::Protocol::Class
- AMQ::Protocol::CommandInvalid
- AMQ::Protocol::Confirm
- AMQ::Protocol::Confirm::Select
- AMQ::Protocol::Confirm::SelectOk
- AMQ::Protocol::Connection
- AMQ::Protocol::Connection::Blocked
- AMQ::Protocol::Connection::Close
- AMQ::Protocol::Connection::CloseOk
- AMQ::Protocol::Connection::Open
- AMQ::Protocol::Connection::OpenOk
- AMQ::Protocol::Connection::Secure
- AMQ::Protocol::Connection::SecureOk
- AMQ::Protocol::Connection::Start
- AMQ::Protocol::Connection::StartOk
- AMQ::Protocol::Connection::Tune
- AMQ::Protocol::Connection::TuneOk
- AMQ::Protocol::Connection::Unblocked
- AMQ::Protocol::ConnectionForced
- AMQ::Protocol::ContentTooLarge
- AMQ::Protocol::EmptyResponseError
- AMQ::Protocol::Error
- AMQ::Protocol::Exchange
- AMQ::Protocol::Exchange::Bind
- AMQ::Protocol::Exchange::BindOk
- AMQ::Protocol::Exchange::Declare
- AMQ::Protocol::Exchange::DeclareOk
- AMQ::Protocol::Exchange::Delete
- AMQ::Protocol::Exchange::DeleteOk
- AMQ::Protocol::Exchange::Unbind
- AMQ::Protocol::Exchange::UnbindOk
- AMQ::Protocol::Float32Bit
- AMQ::Protocol::Frame
- AMQ::Protocol::FrameError
- AMQ::Protocol::FrameSubclass
- AMQ::Protocol::FrameTypeError
- AMQ::Protocol::HardError
- AMQ::Protocol::HeaderFrame
- AMQ::Protocol::HeartbeatFrame
- AMQ::Protocol::InternalError
- AMQ::Protocol::InvalidPath
- AMQ::Protocol::Method
- AMQ::Protocol::MethodFrame
- AMQ::Protocol::NoConsumers
- AMQ::Protocol::NoRoute
- AMQ::Protocol::NotAllowed
- AMQ::Protocol::NotFound
- AMQ::Protocol::NotImplemented
- AMQ::Protocol::PreconditionFailed
- AMQ::Protocol::Queue
- AMQ::Protocol::Queue::Bind
- AMQ::Protocol::Queue::BindOk
- AMQ::Protocol::Queue::Declare
- AMQ::Protocol::Queue::DeclareOk
- AMQ::Protocol::Queue::Delete
- AMQ::Protocol::Queue::DeleteOk
- AMQ::Protocol::Queue::Purge
- AMQ::Protocol::Queue::PurgeOk
- AMQ::Protocol::Queue::Unbind
- AMQ::Protocol::Queue::UnbindOk
- AMQ::Protocol::ResourceError
- AMQ::Protocol::ResourceLocked
- AMQ::Protocol::SoftError
- AMQ::Protocol::SyntaxError
- AMQ::Protocol::Table
- AMQ::Protocol::Table::InvalidTableError
- AMQ::Protocol::TableValueDecoder
- AMQ::Protocol::TableValueEncoder
- AMQ::Protocol::Tx
- AMQ::Protocol::Tx::Commit
- AMQ::Protocol::Tx::CommitOk
- AMQ::Protocol::Tx::Rollback
- AMQ::Protocol::Tx::RollbackOk
- AMQ::Protocol::Tx::Select
- AMQ::Protocol::Tx::SelectOk
- AMQ::Protocol::TypeConstants
- AMQ::Protocol::UnexpectedFrame
- AMQ::URI

No matching classes.

# AMQ::IntAllocator

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.

### Attributes

hi[R]

@return [Integer] Upper boundary of the integer range available for allocation

lo[R]

@return [Integer] Lower boundary of the integer range available for allocation

number_of_bits[R]

@return [Integer] Number of integers in the allocation range

### Public Class Methods

new(lo, hi) click to toggle source

@param [Integer] lo Lower boundary of the integer range available for allocation @param [Integer] hi Upper boundary of the integer range available for allocation @raise [ArgumentError] if upper boundary is not greater than the lower one

    # File lib/amq/int_allocator.rb, line 26
    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

### Public Instance Methods

allocate() click to toggle source

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.

@return [Integer] Allocated integer if allocation succeeded. nil otherwise.

    # File lib/amq/int_allocator.rb, line 44
    def allocate

      if n = @free_set.next_clear_bit

        if n < @hi - 1 then
          @free_set.set(n)
          n + 1
        else
          -1
        end

      else
        -1
      end
    end

allocated?(reservation) click to toggle source

@return [Boolean] true if provided argument was previously allocated, false otherwise

    # File lib/amq/int_allocator.rb, line 70
    def allocated?(reservation)
      @free_set.get(reservation-1)
    end

free(reservation) click to toggle source

Releases previously allocated integer. If integer provided as argument was not previously allocated, this method has no effect.

@return [NilClass] nil

    # File lib/amq/int_allocator.rb, line 64
    def free(reservation)
      @free_set.unset(reservation-1)
    end

Also aliased as: release
release(reservation) click to toggle source
Alias for: free
reset() click to toggle source

Releases the whole allocation range

    # File lib/amq/int_allocator.rb, line 75
    def reset
      @free_set.clear
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.