class Cassandra::Long

A temporally-ordered Long class for use in Cassandra column names

Public Class Methods

new(bytes = nil) click to toggle source

FIXME Should unify with or subclass Cassandra::UUID

   # File lib/cassandra/long.rb
 7 def initialize(bytes = nil)
 8   case bytes
 9   when self.class # Long
10     @bytes = bytes.to_s
11   when String
12     case bytes.size
13     when 8 # Raw byte array
14       @bytes = bytes
15     when 18 # Human-readable UUID-like representation; inverse of #to_guid
16       elements = bytes.split("-")
17       raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (malformed UUID-like representation)" if elements.size != 3
18       @bytes = [elements.join].pack('H32')
19     else
20       raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (invalid bytecount)"
21     end
22   when Integer
23     raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (integer out of range)" if bytes < 0 or bytes > 2**64
24     @bytes = [bytes >> 32, bytes % 2**32].pack("NN")
25   when NilClass, Time
26     # Time.stamp is 52 bytes, so we have 12 bytes of entropy left over
27     int = ((bytes || Time).stamp << 12) + rand(2**12)
28     @bytes = [int >> 32, int % 2**32].pack("NN")
29   else
30     raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (unknown source class)"
31   end
32 end

Public Instance Methods

inspect() click to toggle source
   # File lib/cassandra/long.rb
46 def inspect
47   "<Cassandra::Long##{object_id} time: #{
48     Time.at((to_i >> 12) / 1_000_000).utc.inspect
49   }, usecs: #{
50     (to_i >> 12) % 1_000_000
51   }, jitter: #{
52     to_i % 2**12
53   }, guid: #{
54     to_guid
55   }>"
56 end
to_guid() click to toggle source
   # File lib/cassandra/long.rb
42 def to_guid
43   "%08x-%04x-%04x" % @bytes.unpack("Nnn")
44 end
to_i() click to toggle source
   # File lib/cassandra/long.rb
34 def to_i
35   @to_i ||= begin
36     ints = @bytes.unpack("NN")
37     (ints[0] << 32) +
38     ints[1]
39   end
40 end