Class: Cassandra::Long
- Inherits:
-
Comparable
- Object
- Comparable
- Cassandra::Long
- Defined in:
- lib/cassandra/long.rb
Overview
A temporally-ordered Long class for use in Cassandra column names
Instance Method Summary collapse
-
#initialize(bytes = nil) ⇒ Long
constructor
FIXME Should unify with or subclass Cassandra::UUID.
- #inspect ⇒ Object
- #to_guid ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object (also: #bytes)
Methods inherited from Comparable
Constructor Details
#initialize(bytes = nil) ⇒ Long
FIXME Should unify with or subclass Cassandra::UUID
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cassandra/long.rb', line 7 def initialize(bytes = nil) case bytes when self.class # Long @bytes = bytes.to_s when String case bytes.size when 8 # Raw byte array @bytes = bytes when 18 # Human-readable UUID-like representation; inverse of #to_guid elements = bytes.split("-") raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (malformed UUID-like representation)" if elements.size != 3 @bytes = [elements.join].pack('H32') else raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (invalid bytecount)" end when Integer raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (integer out of range)" if bytes < 0 or bytes > 2**64 @bytes = [bytes >> 32, bytes % 2**32].pack("NN") when NilClass, Time # Time.stamp is 52 bytes, so we have 12 bytes of entropy left over int = ((bytes || Time).stamp << 12) + rand(2**12) @bytes = [int >> 32, int % 2**32].pack("NN") else raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (unknown source class)" end end |
Instance Method Details
#inspect ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cassandra/long.rb', line 51 def inspect "<Cassandra::Long##{object_id} time: #{ Time.at((to_i >> 12) / 1_000_000).utc.inspect }, usecs: #{ (to_i >> 12) % 1_000_000 }, jitter: #{ to_i % 2**12 }, guid: #{ to_guid }>" end |
#to_guid ⇒ Object
47 48 49 |
# File 'lib/cassandra/long.rb', line 47 def to_guid "%08x-%04x-%04x" % @bytes.unpack("Nnn") end |
#to_i ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/cassandra/long.rb', line 34 def to_i @to_i ||= begin ints = @bytes.unpack("NN") (ints[0] << 32) + ints[1] end end |
#to_s ⇒ Object Also known as: bytes
42 43 44 |
# File 'lib/cassandra/long.rb', line 42 def to_s @bytes end |