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.

Defined Under Namespace

Classes: TupleBin

Instance Method Summary collapse

Constructor Details

#initializeTupleBag

:nodoc:



316
317
318
319
# File 'lib/rinda/tuplespace.rb', line 316

def initialize # :nodoc:
  @hash = {}
  @enum = enum_for(:each_entry)
end

Instance Method Details

#delete(tuple) ⇒ Object

Removes tuple from the TupleBag.



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

def delete(tuple)
  key = bin_key(tuple)
  bin = @hash[key]
  return nil unless bin
  bin.delete(tuple)
  @hash.delete(key) if bin.empty?
  tuple
end

#delete_unless_aliveObject

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/rinda/tuplespace.rb', line 382

def delete_unless_alive
  deleted = []
  @hash.each do |key, bin|
    bin.delete_if do |tuple|
      if tuple.alive?
        false
      else
        deleted.push(tuple)
        true
      end
    end
  end
  deleted
end

#find(template) ⇒ Object

Finds a live tuple that matches template.



362
363
364
365
366
# File 'lib/rinda/tuplespace.rb', line 362

def find(template)
  bin_for_find(template).find do |tuple|
    tuple.alive? && template.match(tuple)
  end
end

#find_all(template) ⇒ Object

Finds all live tuples that match template.



353
354
355
356
357
# File 'lib/rinda/tuplespace.rb', line 353

def find_all(template)
  bin_for_find(template).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.



372
373
374
375
376
# File 'lib/rinda/tuplespace.rb', line 372

def find_all_template(tuple)
  @enum.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)


324
325
326
327
328
# File 'lib/rinda/tuplespace.rb', line 324

def has_expires?
  @enum.find do |tuple|
    tuple.expires
  end
end

#push(tuple) ⇒ Object

Add tuple to the TupleBag.



333
334
335
336
337
# File 'lib/rinda/tuplespace.rb', line 333

def push(tuple)
  key = bin_key(tuple)
  @hash[key] ||= TupleBin.new
  @hash[key].add(tuple)
end