类 Rinda::TupleBag

TupleBag 是一个无序的元组集合。它是元组空间的基础。

公共实例方法

delete(tuple) 点击切换源代码

TupleBag 中移除 tuple

# 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() 点击切换源代码

TupleBag 中删除已失效的元组,并返回已删除的元组。

# 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
find(template) 点击切换源代码

查找与 template 匹配的活动元组。

# 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) 点击切换源代码

查找所有与 template 匹配的活动元组。

# 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) 点击切换源代码

查找 TupleBag 中所有元组,这些元组在被视为模板时,与 tuple 匹配并且处于活动状态。

# File lib/rinda/tuplespace.rb, line 371
def find_all_template(tuple)
  @enum.find_all do |template|
    template.alive? && template.match(tuple)
  end
end
has_expires?() 点击切换源代码

如果 TupleBag 包含任何已过期的条目,则返回 true

# File lib/rinda/tuplespace.rb, line 323
def has_expires?
  @enum.find do |tuple|
    tuple.expires
  end
end
push(tuple) 点击切换源代码

tuple 添加到 TupleBag 中。

# File lib/rinda/tuplespace.rb, line 332
def push(tuple)
  key = bin_key(tuple)
  @hash[key] ||= TupleBin.new
  @hash[key].add(tuple)
end

私有实例方法

bin_for_find(template) 点击切换源代码
# 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) 点击切换源代码
# File lib/rinda/tuplespace.rb, line 403
def bin_key(tuple)
  head = tuple[0]
  if head.class == Symbol
    return head
  else
    false
  end
end
each_entry(&blk) 点击切换源代码
# File lib/rinda/tuplespace.rb, line 397
def each_entry(&blk)
  @hash.each do |k, v|
    v.each(&blk)
  end
end