123456789_123456789_123456789_123456789_123456789_

Class: Rinda::TupleBag

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rinda/tuplespace.rb

Overview

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newTupleBag

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 315

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

Instance Attribute Details

#has_expires?Boolean (readonly)

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

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 323

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

Instance Method Details

#bin_for_find(template) (private)

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 412

def bin_for_find(template)
  key = bin_key(template)
  key ? @hash.fetch(key, []) : @enum
end

#bin_key(tuple) (private)

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 403

def bin_key(tuple)
  head = tuple[0]
  if head.class == Symbol
    return head
  else
    false
  end
end

#delete(tuple)

Removes tuple from the TupleBag.

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 341

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_alive

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

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 381

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

#each_entry(&blk) (private)

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 397

def each_entry(&blk)
  @hash.each do |k, v|
    v.each(&blk)
  end
end

#find(template)

Finds a live tuple that matches template.

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 361

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

#find_all(template)

Finds all live tuples that match template.

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 352

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

#find_all_template(tuple)

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

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 371

def find_all_template(tuple)
  @enum.find_all do |template|
    template.alive? && template.match(tuple)
  end
end

#push(tuple)

Add tuple to the TupleBag.

[ GitHub ]

  
# File 'lib/rinda/tuplespace.rb', line 332

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