Class: Cassandra::TimestampGenerator::TickingOnDuplicate

Inherits:
Object
  • Object
show all
Includes:
Cassandra::TimestampGenerator, MonitorMixin
Defined in:
lib/cassandra/timestamp_generator/ticking_on_duplicate.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#nextInteger

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

Returns:

  • (Integer)

    an integer representing a timestamp in microseconds.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cassandra/timestamp_generator/ticking_on_duplicate.rb', line 41

def next
  now = ::Time.now
  now_millis = now.tv_sec * 1000 + now.tv_usec / 1000
  synchronize do
    millis = @last / 1000
    counter = @last % 1000
    if millis >= now_millis
      counter += 1
    else
      millis = now_millis
      counter = 0
    end
    @last = millis * 1000 + counter
  end
end