Class: Rinda::TupleBag

Inherits:
Object
  • Object
show all
Defined in:
lib/rinda/tuplespace.rb

Overview

TupleBag is an unordered collection of tuples. It is the basis of Tuplespace.

Instance Method Summary collapse

Constructor Details

#initializeTupleBag

:nodoc:



290
291
292
# File 'lib/rinda/tuplespace.rb', line 290

def initialize # :nodoc:
  @hash = {}
end

Instance Method Details

#delete(ary) ⇒ Object

Removes ary from the TupleBag.



318
319
320
321
# File 'lib/rinda/tuplespace.rb', line 318

def delete(ary)
  size = ary.size
  @hash.fetch(size, []).delete(ary)
end

#delete_unless_aliveObject

Delete tuples which dead tuples from the TupleBag, returning the deleted tuples.



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rinda/tuplespace.rb', line 355

def delete_unless_alive
  deleted = []
  @hash.keys.each do |size|
    ary = []
    @hash[size].each do |tuple|
      if tuple.alive?
        ary.push(tuple)
      else
        deleted.push(tuple)
      end
    end
    @hash[size] = ary
  end
  deleted
end

#find(template) ⇒ Object

Finds a live tuple that matches template.



335
336
337
338
339
# File 'lib/rinda/tuplespace.rb', line 335

def find(template)
  @hash.fetch(template.size, []).find do |tuple|
    tuple.alive? && template.match(tuple)
  end
end

#find_all(template) ⇒ Object

Finds all live tuples that match template.



326
327
328
329
330
# File 'lib/rinda/tuplespace.rb', line 326

def find_all(template)
  @hash.fetch(template.size, []).find_all do |tuple|
    tuple.alive? && template.match(tuple)
  end
end

#find_all_template(tuple) ⇒ Object

Finds all tuples in the TupleBag which when treated as templates, match tuple and are alive.



345
346
347
348
349
# File 'lib/rinda/tuplespace.rb', line 345

def find_all_template(tuple)
  @hash.fetch(tuple.size, []).find_all do |template|
    template.alive? && template.match(tuple)
  end
end

#has_expires?Boolean

true if the TupleBag to see if it has any expired entries.

Returns:

  • (Boolean)


297
298
299
300
301
302
303
304
# File 'lib/rinda/tuplespace.rb', line 297

def has_expires?
  @hash.each do |k, v|
    v.each do |tuple|
      return true if tuple.expires
    end
  end
  false
end

#push(ary) ⇒ Object

Add ary to the TupleBag.



309
310
311
312
313
# File 'lib/rinda/tuplespace.rb', line 309

def push(ary)
  size = ary.size
  @hash[size] ||= []
  @hash[size].push(ary)
end