Sha256: a7f840e9a4935a76f6acc74cbbfef06ff9eef5fbb0905a53537b9d2758c566bc

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

class Cassandra
  # A temporally-ordered Long class for use in Cassandra column names
  class Long < Comparable
  
    def initialize(bytes = nil)      
      case bytes
      when String
        raise TypeError, "8 bytes required" if bytes.size != 8
        @bytes = bytes
      when Integer
        raise TypeError, "Integer must be between 0 and 2**64" if bytes < 0 or bytes > 2**64
        @bytes = [bytes >> 32, bytes % 2**32].pack("NN")
      when NilClass
        # Time.stamp is 52 bytes, so we have 12 bytes of entropy left over
        int = (Time.stamp << 12) + rand(2**12)
        @bytes = [int >> 32, int % 2**32].pack("NN")
      else
        raise TypeError, "Can't convert from #{bytes.class}"
      end
    end

    def to_i
      @to_i ||= begin
        ints = @bytes.unpack("NN")
        (ints[0] << 32) + 
        ints[1]
      end
    end    
    
    def inspect
      "<Cassandra::Long##{object_id} time: #{
        Time.at((to_i >> 12) / 1_000_000).inspect
      }, usecs: #{
        (to_i >> 12) % 1_000_000
      }, jitter: #{
        to_i % 2**12
      }>"
    end      
  end  
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
slillibri-cassandra-0.4 lib/cassandra/long.rb
cassandra-0.5.3 lib/cassandra/long.rb
cassandra-0.5.4 lib/cassandra/long.rb
cassandra-0.5 lib/cassandra/long.rb
cassandra-0.5.1 lib/cassandra/long.rb
cassandra-0.5.2 lib/cassandra/long.rb