Sha256: adb510521571fc0d5cc900fe3ba11862dcaee606ea56c085de0b7fe8e021e3b1

Contents?: true

Size: 1.8 KB

Versions: 38

Compression:

Stored size: 1.8 KB

Contents

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

    # FIXME Should unify with or subclass Cassandra::UUID
    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

    def to_i
      @to_i ||= begin
        ints = @bytes.unpack("NN")
        (ints[0] << 32) +
        ints[1]
      end
    end

    def to_guid
      "%08x-%04x-%04x" % @bytes.unpack("Nnn")
    end    

    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
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
sessionm-cassandra-1.1.2 lib/cassandra/long.rb
sessionm-cassandra-1.1.1 lib/cassandra/long.rb
sessionm-cassandra-1.1.0 lib/cassandra/long.rb
cassandra-0.23.0 lib/cassandra/long.rb
cassandra-0.22.0 lib/cassandra/long.rb
cassandra-mavericks-0.21.1 lib/cassandra/long.rb
cassandra-0.21.0 lib/cassandra/long.rb
cassandra-0.20.0 lib/cassandra/long.rb
cassandra-0.19.0 lib/cassandra/long.rb
sessionm-cassandra-1.0.2 lib/cassandra/long.rb
cassandra-0.18.0 lib/cassandra/long.rb
sessionm-cassandra-1.0.1 lib/cassandra/long.rb
sessionm-cassandra-1.0.0 lib/cassandra/long.rb
cassandra-0.17.0 lib/cassandra/long.rb
cassandra-0.16.0 lib/cassandra/long.rb
cassandra-0.15.0 lib/cassandra/long.rb
cassandra-0.14.0 lib/cassandra/long.rb
cassandra-0.13.0 lib/cassandra/long.rb
hallelujah-cassandra-0.12.3 lib/cassandra/long.rb
mcmire-cassandra-0.12.3 lib/cassandra/long.rb