class Cassandra::TimestampGenerator::TickingOnDuplicate

In JRuby, {::Time} has millisecond precision. We require client timestamps to have microsecond precision to minimize clashes in C*. This generator keeps track of the last generated timestamp, and if the current-time is within the same millisecond as the last, it fills the microsecond portion of the new timestamp with the value of an incrementing counter.

For example, if the generator triggers twice at time 12345678000 (microsecond granularity, but ms precisions as shown by 0's for the three least-significant digits), it'll return 12345678000 and 12345678001.

Public Class Methods

new() click to toggle source

@private

   # File lib/cassandra/timestamp_generator/ticking_on_duplicate.rb
33 def initialize
34   mon_initialize
35   @last = 0
36 end

Public Instance Methods

next() click to toggle source

Create a new timestamp, as a 64-bit integer.

@return [Integer] an integer representing a timestamp in microseconds.

   # File lib/cassandra/timestamp_generator/ticking_on_duplicate.rb
41 def next
42   now = ::Time.now
43   now_millis = now.tv_sec * 1000 + now.tv_usec / 1000
44   synchronize do
45     millis = @last / 1000
46     counter = @last % 1000
47     if millis >= now_millis
48       counter += 1
49     else
50       millis = now_millis
51       counter = 0
52     end
53     @last = millis * 1000 + counter
54   end
55 end