Class: StrokeDB::LamportTimestamp
- Defined in:
- lib/strokedb/sync/lamport_timestamp.rb
Defined Under Namespace
Classes: CounterOverflow
Constant Summary collapse
- MAX_COUNTER =
2**64
- BASE =
16
- BASE_LENGTH =
16
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
-
.from_raw(raw_string) ⇒ Object
Raw format.
- .zero(__uuid = Util.random_uuid) ⇒ Object
- .zero_string ⇒ Object
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #dup ⇒ Object
-
#initialize(c = 0, __uuid = Util.random_uuid) ⇒ LamportTimestamp
constructor
A new instance of LamportTimestamp.
- #marshal_dump ⇒ Object
- #marshal_load(dumped) ⇒ Object
- #next ⇒ Object
- #next! ⇒ Object
- #to_json ⇒ Object
- #to_raw ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(c = 0, __uuid = Util.random_uuid) ⇒ LamportTimestamp
Returns a new instance of LamportTimestamp.
9 10 11 12 13 14 15 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 9 def initialize(c = 0, __uuid = Util.random_uuid) if c > MAX_COUNTER raise CounterOverflow.new, "Max counter value is 2**64" end @counter = c @uuid = __uuid end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
7 8 9 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 7 def counter @counter end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
7 8 9 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 7 def uuid @uuid end |
Class Method Details
.from_raw(raw_string) ⇒ Object
Raw format
40 41 42 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 40 def self.from_raw(raw_string) new.marshal_load(raw_string) end |
.zero(__uuid = Util.random_uuid) ⇒ Object
69 70 71 72 73 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 69 def self.zero(__uuid = Util.random_uuid) ts = new(0) ts.instance_variable_set(:@uuid, __uuid) ts end |
.zero_string ⇒ Object
74 75 76 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 74 def self.zero_string "0"*BASE_LENGTH + NIL_UUID end |
Instance Method Details
#<(other) ⇒ Object
57 58 59 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 57 def <(other) (self <=> other) < 0 end |
#<=(other) ⇒ Object
60 61 62 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 60 def <=(other) (self <=> other) <= 0 end |
#<=>(other) ⇒ Object
50 51 52 53 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 50 def <=>(other) primary = (@counter <=> other.counter) primary == 0 ? (@uuid <=> other.uuid) : primary end |
#==(other) ⇒ Object
54 55 56 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 54 def ==(other) @counter == other.counter && @uuid == other.uuid end |
#>(other) ⇒ Object
63 64 65 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 63 def >(other) (self <=> other) > 0 end |
#>=(other) ⇒ Object
66 67 68 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 66 def >=(other) (self <=> other) >= 0 end |
#dup ⇒ Object
23 24 25 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 23 def dup LamportTimestamp.new(@counter, @uuid) end |
#marshal_dump ⇒ Object
26 27 28 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 26 def marshal_dump @counter.to_s(BASE).rjust(BASE_LENGTH, '0') + @uuid end |
#marshal_load(dumped) ⇒ Object
29 30 31 32 33 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 29 def marshal_load(dumped) @counter = dumped[0, BASE_LENGTH].to_i(BASE) @uuid = dumped[BASE_LENGTH, 36] self end |
#next ⇒ Object
16 17 18 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 16 def next LamportTimestamp.new(@counter + 1, @uuid) end |
#next! ⇒ Object
19 20 21 22 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 19 def next! @counter += 1 self end |
#to_json ⇒ Object
35 36 37 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 35 def to_json marshal_dump.to_json end |
#to_raw ⇒ Object
43 44 45 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 43 def to_raw marshal_dump end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/strokedb/sync/lamport_timestamp.rb', line 47 def to_s marshal_dump end |