Overview

Namespaces

  • cassandra
  • None
  • PHP
  • phpcassa
    • Batch
    • Connection
    • Index
    • Iterator
    • Schema
      • DataType
    • Util
    • UUID

Classes

  • AsciiType
  • BooleanType
  • BytesType
  • CassandraType
  • CompositeType
  • DateType
  • DoubleType
  • FloatType
  • Int32Type
  • IntegerType
  • LexicalUUIDType
  • LongType
  • TimeUUIDType
  • UTF8Type
  • UUIDType

Interfaces

  • Serialized
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace phpcassa\Schema\DataType;
  3: 
  4: /**
  5:  * Stores data as an 8-byte signed integer.
  6:  *
  7:  * @package phpcassa\Schema\DataType
  8:  */
  9: class LongType extends CassandraType {
 10: 
 11:     public function pack($value, $is_name=null, $slice_end=null, $is_data=null)
 12:     {
 13:         // If we are on a 32bit architecture we have to explicitly deal with
 14:         // 64-bit twos-complement arithmetic since PHP wants to treat all ints
 15:         // as signed and any int over 2^31 - 1 as a float
 16:         if (PHP_INT_SIZE == 4) {
 17:             $neg = $value < 0;
 18: 
 19:             if ($neg) {
 20:               $value *= -1;
 21:             }
 22: 
 23:             $hi = (int)($value / 4294967296);
 24:             $lo = (int)$value;
 25: 
 26:             if ($neg) {
 27:                 $hi = ~$hi;
 28:                 $lo = ~$lo;
 29:                 if (($lo & (int)0xffffffff) == (int)0xffffffff) {
 30:                     $lo = 0;
 31:                     $hi++;
 32:                 } else {
 33:                     $lo++;
 34:                 }
 35:             }
 36:             $data = pack('N2', $hi, $lo);
 37:         } else {
 38:             $hi = $value >> 32;
 39:             $lo = $value & 0xFFFFFFFF;
 40:             $data = pack('N2', $hi, $lo);
 41:         }
 42:         return $data;
 43:     }
 44: 
 45:     public function unpack($data, $is_name=null)
 46:     {
 47:         $arr = unpack('N2', $data);
 48: 
 49:         // If we are on a 32bit architecture we have to explicitly deal with
 50:         // 64-bit twos-complement arithmetic since PHP wants to treat all ints
 51:         // as signed and any int over 2^31 - 1 as a float
 52:         if (PHP_INT_SIZE == 4) {
 53: 
 54:             $hi = $arr[1];
 55:             $lo = $arr[2];
 56:             $isNeg = $hi  < 0;
 57: 
 58:             // Check for a negative
 59:             if ($isNeg) {
 60:                 $hi = ~$hi & (int)0xffffffff;
 61:                 $lo = ~$lo & (int)0xffffffff;
 62: 
 63:                 if ($lo == (int)0xffffffff) {
 64:                     $hi++;
 65:                     $lo = 0;
 66:                 } else {
 67:                     $lo++;
 68:                 }
 69:             }
 70: 
 71:             // Force 32bit words in excess of 2G to pe positive - we deal wigh sign
 72:             // explicitly below
 73: 
 74:             if ($hi & (int)0x80000000) {
 75:                 $hi &= (int)0x7fffffff;
 76:                 $hi += 0x80000000;
 77:             }
 78: 
 79:             if ($lo & (int)0x80000000) {
 80:                 $lo &= (int)0x7fffffff;
 81:                 $lo += 0x80000000;
 82:             }
 83: 
 84:             $value = $hi * 4294967296 + $lo;
 85: 
 86:             if ($isNeg)
 87:                 $value = 0 - $value;
 88: 
 89:         } else {
 90:             // Upcast negatives in LSB bit
 91:             if ($arr[2] & 0x80000000)
 92:                 $arr[2] = $arr[2] & 0xffffffff;
 93: 
 94:             // Check for a negative
 95:             if ($arr[1] & 0x80000000) {
 96:                 $arr[1] = $arr[1] & 0xffffffff;
 97:                 $arr[1] = $arr[1] ^ 0xffffffff;
 98:                 $arr[2] = $arr[2] ^ 0xffffffff;
 99:                 $value = 0 - $arr[1]*4294967296 - $arr[2] - 1;
100:             } else {
101:                 $value = $arr[1]*4294967296 + $arr[2];
102:             }
103:         }
104:         return $value;
105:     }
106: }
107: 
108: 
phpcassa API documentation generated by ApiGen 2.8.0