123456789_123456789_123456789_123456789_123456789_

Class: Rinda::TupleEntry

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, DRbUndumped
Inherits: Object
Defined in: lib/rinda/tuplespace.rb

Overview

A TupleEntry is a Tuple (i.e. a possible entry in some Tuplespace) together with expiry and cancellation data.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #[](key)

    Retrieves key from the tuple.

  • #cancel

    Marks this TupleEntry as canceled.

  • #fetch(key)

    Fetches key from the tuple.

  • #make_expires(sec = nil)
    Returns an expiry Time based on sec which can be one of: Numeric
    sec seconds into the future true
    the expiry time is the start of 1970 (i.e. expired) nil

    it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when.

  • #make_tuple(ary)

    Creates a Tuple for ary.

  • #renew(sec_or_renewer)

    Reset the expiry time according to sec_or_renewer.

  • #size

    The size of the tuple.

  • #value

    Return the object which makes up the tuple itself: the Array or Hash.

  • #get_renewer(it) private

    Returns a valid argument to make_expires and the renewer or nil.

Constructor Details

.new(ary, sec = nil) ⇒ TupleEntry

Creates a TupleEntry based on ary with an optional renewer or expiry time sec.

A renewer must implement the #renew method which returns a Numeric, nil, or true to indicate when the tuple has expired.

[ GitHub ]

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

def initialize(ary, sec=nil)
  @cancel = false
  @expires = nil
  @tuple = make_tuple(ary)
  @renewer = nil
  renew(sec)
end

Instance Attribute Details

#alive?Boolean (readonly)

A TupleEntry is dead when it is canceled or expired.

[ GitHub ]

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

def alive?
  !canceled? && !expired?
end

#canceled?Boolean (readonly)

Returns the canceled status.

[ GitHub ]

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

def canceled?; @cancel; end

#expired?Boolean (readonly)

Has this tuple expired? (true/false).

A tuple has expired when its expiry timer based on the sec argument to #initialize runs out.

[ GitHub ]

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

def expired?
  return true unless @expires
  return false if @expires > Time.now
  return true if @renewer.nil?
  renew(@renewer)
  return true unless @expires
  return @expires < Time.now
end

#expires (rw)

[ GitHub ]

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

attr_accessor :expires

Instance Method Details

#[](key)

Retrieves key from the tuple.

[ GitHub ]

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

def [](key)
  @tuple[key]
end

#cancel

Marks this TupleEntry as canceled.

[ GitHub ]

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

def cancel
  @cancel = true
end

#fetch(key)

Fetches key from the tuple.

[ GitHub ]

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

def fetch(key)
  @tuple.fetch(key)
end

#get_renewer(it) (private)

Returns a valid argument to make_expires and the renewer or nil.

Given true, nil, or Numeric, returns that value and nil (no actual renewer). Otherwise it returns an expiry value from calling it.renew and the renewer.

[ GitHub ]

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

def get_renewer(it)
  case it
  when Numeric, true, nil
    return it, nil
  else
    begin
      return it.renew, it
    rescue Exception
      return it, nil
    end
  end
end

#make_expires(sec = nil)

Returns an expiry Time based on sec which can be one of:

Numeric

sec seconds into the future

true

the expiry time is the start of 1970 (i.e. expired)

nil

it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when UNIX clocks will die)

[ GitHub ]

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

def make_expires(sec=nil)
  case sec
  when Numeric
    Time.now + sec
  when true
    Time.at(1)
  when nil
    Time.at(2**31-1)
  end
end

#make_tuple(ary)

Creates a Tuple for ary.

[ GitHub ]

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

def make_tuple(ary)
  Rinda::Tuple.new(ary)
end

#renew(sec_or_renewer)

Reset the expiry time according to sec_or_renewer.

nil

it is set to expire in the far future.

true

it has expired.

Numeric

it will expire in that many seconds.

Otherwise the argument refers to some kind of renewer object which will reset its expiry time.

[ GitHub ]

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

def renew(sec_or_renewer)
  sec, @renewer = get_renewer(sec_or_renewer)
  @expires = make_expires(sec)
end

#size

The size of the tuple.

[ GitHub ]

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

def size
  @tuple.size
end

#value

Return the object which makes up the tuple itself: the Array or Hash.

[ GitHub ]

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

def value; @tuple.value; end