class Rinda::TupleBag

Public Class Methods

new() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 384
def initialize
  @hash = {}
  @mutex = Mutex.new
  @enum = enum_for(:each_entry)
  @special_bin = {}
end

Public Instance Methods

[](ident) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 391
def [](ident)
  @hash[ident]
end
all_tuples() click to toggle source

Returns all tuples in the bag.

# File lib/pione/patch/rinda-patch.rb, line 432
def all_tuples
  @mutex.synchronize{@hash.values}.map{|bin| bin.elements}.flatten
end
bin_class(key) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 421
def bin_class(key)
  return TupleBin unless @special_bin
  return @special_bin.has_key?(key) ? @special_bin[key] : TupleBin
end
data_size() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 495
def data_size
  @mutex.synchronize{@hash[:data]}.size rescue 0
end
delete(tuple) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 406
def delete(tuple)
  key = bin_key(tuple)
  bin = @mutex.synchronize {@hash[key]}
  return nil unless bin
  @mutex.synchronize {bin.delete(tuple)}
  @mutex.synchronize {@hash.delete(key) if bin.empty?}
  return tuple
end
delete_unless_alive() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 466
def delete_unless_alive
  deleted = []
  @mutex.synchronize do
    @hash.each do |key, bin|
      bin.delete_if do |tuple|
        if tuple.alive?
          false
        else
          deleted.push(tuple)
          true
        end
      end
    end
  end
  deleted
end
find(template) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 436
def find(template)
  key = bin_key(template)
  if @special_bin[key]
    prepare_table(key)
    @mutex.synchronize{@hash[key]}.find(template) do |tuple|
      tuple.alive? && template.match(tuple)
    end
  else
    orig_find(template)
  end
end
Also aliased as: orig_find
find_all(template) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 448
def find_all(template)
  key = bin_key(template)
  if @special_bin[key]
    prepare_table(key)
    @mutex.synchronize{@hash[key]}.find_all(template) do |tuple|
      tuple.alive? && template.match(tuple)
    end
  else
    orig_find_all(template)
  end
end
Also aliased as: orig_find_all
find_template(tuple) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 460
def find_template(tuple)
  @enum.find do |template|
    template.alive? && template.match(tuple)
  end
end
finished_size() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 491
def finished_size
  @mutex.synchronize{@hash[:finished]}.size rescue 0
end
orig_find(template)
Alias for: find
orig_find_all(template)
Alias for: find_all
prepare_table(key) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 415
def prepare_table(key)
  unless @mutex.synchronize {@hash[key]}
    @mutex.synchronize {@hash[key] = bin_class(key).new}
  end
end
push(tuple) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 400
def push(tuple)
  key = bin_key(tuple)
  prepare_table(key)
  @mutex.synchronize {@hash[key].add(tuple)}
end
set_special_bin(special_bin) click to toggle source

Sets special bin class table by identifier.

# File lib/pione/patch/rinda-patch.rb, line 396
def set_special_bin(special_bin)
  @special_bin = special_bin
end
task_size() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 483
def task_size
  @mutex.synchronize{@hash[:task]}.size rescue 0
end
working_size() click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 487
def working_size
  @mutex.synchronize{@hash[:working]}.size rescue 0
end

Private Instance Methods

each_entry(&blk) click to toggle source
# File lib/pione/patch/rinda-patch.rb, line 501
def each_entry(&blk)
  @mutex.synchronize do
    @hash.each do |k, v|
      v.each(&blk)
    end
  end
end