Class: UnifiedQueues::Multi::Driver::UnifiedQueuesDriver::Single

Inherits:
UnifiedQueues::Multi::Driver show all
Defined in:
lib/unified-queues/multi/driver/unified-queues.rb

Overview

Multi queue driver for unified queues single queue interface.

Instance Attribute Summary

Attributes inherited from UnifiedQueues::Multi::Driver

#native

Instance Method Summary collapse

Methods inherited from UnifiedQueues::Multi::Driver

#close

Constructor Details

#initialize(cls, *args, &block) ⇒ Single

Constructor.



70
71
72
73
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 70

def initialize(cls, *args, &block)
    @queues = { }
    @class = [cls, args, block]
end

Instance Method Details

#create(name, cls = @class) ⇒ Queue

Creates new queue under given name.

Parameters:

  • name (Object)

    identification

Returns:

  • (Queue)

    new queue



105
106
107
108
109
110
111
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 105

def create(name, cls = @class)
    if not name.in? @queues
        self[name] = cls.first::new(*cls.second, &cls.third)
    else
        self[name]
    end        
end

#get(name) ⇒ Object Also known as: []

Returns named queue from instance.

Parameters:

  • name (Object)

    queue name



131
132
133
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 131

def get(name)
    @queues[name]
end

#insert(name, queue) ⇒ Object Also known as: []=

Inserts queue instance to queues.

Parameters:

  • name (Object)

    identification

  • queue (Containers::Heap)

    heap instance



120
121
122
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 120

def insert(name, queue) 
    @queues[name] = queue
end

#listArray

Lists names of all available queues.

Returns:

  • (Array)


201
202
203
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 201

def list
    @queues.keys
end

#list_subscribedArray

Lists all subscribed queues.

Returns:

  • (Array)


219
220
221
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 219

def list_subscribed
    [@subscribed.first]
end

#list_usedArray

Lists all used queues.

Returns:

  • (Array)


210
211
212
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 210

def list_used
    [@used.first]
end

#pop(blocking = false, &block) ⇒ Object|nil

Pops value from the queue. In contrast to default Queue library, blocks or returns nil if empty.

Parameters:

  • blocking (Boolean|Integer) (defaults to: false)

    true or timeout if it should block, false otherwise

Returns:

  • (Object|nil)


94
95
96
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 94

def pop(blocking = false, &block)
    self.subscribed.pop(blocking, &block)
end

#push(value, key = value, &block) ⇒ Object

Pushes value to the currently used queue.

Parameters:

  • value (Object)
  • key (Object) (defaults to: value)

    key for priority queues



82
83
84
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 82

def push(value, key = value, &block)
    self.used.push(value, key, &block)
end

#subscribe(name, &block) ⇒ Object

Subscribes to the queue. So marks it as target for #pop. Note, than only single queue can be subscribed at one time.

Parameters:

  • name (Object)

    name of the required queue



157
158
159
160
161
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 157

def subscribe(name, &block)
    self.create(name)
    @subscribed = [name, self[name]]
    yield if not block.nil?        
end

#subscribed {|@subscribed.second| ... } ⇒ Queue

Currently subscribed queue.

Yields:

Returns:

  • (Queue)


191
192
193
194
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 191

def subscribed(&block)
    yield @subscribed.second if not block.nil?
    return @subscribed.second
end

#unsubscribe(name, &block) ⇒ Object

Unsubscribes from the queue.

Parameters:

  • name (Object)

    name of the required queue\



168
169
170
171
172
173
174
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 168

def unsubscribe(name, &block)
    if not @subscribed.nil? and (@subscribed.first == name)
        @subscribed = nil
    end
    
    yield if not block.nil?
end

#use(name, &block) ⇒ Object

Sets queue with given name as used. So marks it as target for #push.

Parameters:

  • name (Object)

    name of the required queue



144
145
146
147
148
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 144

def use(name, &block)
    self.create(name)
    @used = [name, self[name]]
    yield if not block.nil?
end

#used {|@used.second| ... } ⇒ Queue

Currently used queue.

Yields:

Returns:

  • (Queue)


181
182
183
184
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 181

def used(&block)
    yield @used.second if not block.nil?
    return @used.second
end