Class: Cassandra::Uuid::Generator
- Inherits:
-
Object
- Object
- Cassandra::Uuid::Generator
- Defined in:
- lib/cassandra/uuid/generator.rb
Overview
Instances of this class are absolutely not threadsafe. You should never share instances between threads.
A UUID generator.
This class can be used to genereate Apache Cassandra timeuuid and uuid values.
Instance Method Summary collapse
-
#at(*args) ⇒ Object
Returns a new UUID with a time component based on the specified Time.
-
#initialize(node_id = (SecureRandom.random_number(2**47) | 0x010000000000), clock_id = SecureRandom.random_number(65536), clock = Time) ⇒ Generator
constructor
Create a new UUID generator.
-
#now ⇒ Cassandra::TimeUuid
Returns a new UUID with a time component that is the current time.
-
#uuid ⇒ Cassandra::Uuid
Returns a completely random version 4 UUID.
Constructor Details
#initialize(node_id = (SecureRandom.random_number(2**47) | 0x010000000000), clock_id = SecureRandom.random_number(65536), clock = Time) ⇒ Generator
Create a new UUID generator.
The clock ID and node ID components are set to random numbers when the generator is created. These are used for generation of time UUIDs only.
47 48 49 50 51 52 53 |
# File 'lib/cassandra/uuid/generator.rb', line 47 def initialize(node_id = (SecureRandom.random_number(2**47) | 0x010000000000), clock_id = SecureRandom.random_number(65536), clock = Time) raise ::ArgumentError, "invalid clock" unless clock.respond_to?(:now) @node_id = Integer(node_id) @clock_id = Integer(clock_id) @clock = clock end |
Instance Method Details
#at(time, jitter = SecureRandom.random_number(65536)) ⇒ Cassandra::TimeUuid #at(seconds_with_frac, jitter = SecureRandom.random_number(65536)) ⇒ Cassandra::TimeUuid #at(seconds, microseconds_with_frac, jitter = SecureRandom.random_number(65536)) ⇒ Cassandra::TimeUuid
the jitter
argument accepted by all variants of this method is
required to add randomness to generated TimeUuid and
might affect the order of generated timestamps. You should set
jitter
to 0 when the source time(stamp)s are unique.
Returns a new UUID with a time component based on the specified Time. A piece of jitter is added to ensure that multiple calls with the same time do not generate the same UUID (if you want determinism you can set the second parameter to zero).
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cassandra/uuid/generator.rb', line 152 def at(*args) raise ::ArgumentError, "not enough arguments" if args.empty? raise ::ArgumentError, "too many arguments" if args.size > 3 if args.first.is_a?(Time) time = args.shift jitter = args.empty? ? SecureRandom.random_number(65536) : Integer(args.shift) else jitter = args.size > 2 ? Integer(args.pop) : SecureRandom.random_number(65536) time = Time.at(*args) end from_usecs(time.to_i * 1_000_000 + time.usec + jitter) end |
#now ⇒ Cassandra::TimeUuid
Returns a new UUID with a time component that is the current time.
If two calls to #now happen within the time afforded by the system clock resolution a counter is incremented and added to the time component.
If the clock moves backwards the clock ID is reset to a new random number.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cassandra/uuid/generator.rb', line 80 def now now = @clock.now usecs = now.to_i * 1_000_000 + now.usec if @last_usecs && @last_usecs - @sequence <= usecs && usecs <= @last_usecs @sequence += 1 elsif @last_usecs && @last_usecs > usecs @sequence = 0 @clock_id = SecureRandom.random_number(65536) else @sequence = 0 end @last_usecs = usecs + @sequence from_usecs(@last_usecs) end |
#uuid ⇒ Cassandra::Uuid
Returns a completely random version 4 UUID.
181 182 183 |
# File 'lib/cassandra/uuid/generator.rb', line 181 def uuid Uuid.new(SecureRandom.uuid) end |