From 723d5add7536302631e01603479b68d573faf9fa Mon Sep 17 00:00:00 2001 From: Riekelt Kapitein Date: Tue, 4 Feb 2014 15:25:50 +0100 Subject: [PATCH 1/3] Add List & Set type --- lib/phpcassa/Schema/DataType.php | 17 ++++++++-- lib/phpcassa/Schema/DataType/ListType.php | 38 +++++++++++++++++++++++ lib/phpcassa/Schema/DataType/SetType.php | 12 +++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 lib/phpcassa/Schema/DataType/ListType.php create mode 100644 lib/phpcassa/Schema/DataType/SetType.php diff --git a/lib/phpcassa/Schema/DataType.php b/lib/phpcassa/Schema/DataType.php index 44cd6a86..62b2ecaf 100644 --- a/lib/phpcassa/Schema/DataType.php +++ b/lib/phpcassa/Schema/DataType.php @@ -22,6 +22,8 @@ class DataType const LEXICAL_UUID_TYPE = "LexicalUUIDType"; const UUID_TYPE = "UUIDType"; const DATE_TYPE = "DateType"; + const SET_TYPE = "SetType"; + const LIST_TYPE = "ListType"; public static $class_map; @@ -39,7 +41,9 @@ public static function init() { 'UUIDType' => 'phpcassa\Schema\DataType\UUIDType', 'BooleanType' => 'phpcassa\Schema\DataType\BooleanType', 'DateType' => 'phpcassa\Schema\DataType\DateType', - 'Int32Type' => 'phpcassa\Schema\DataType\Int32Type', + 'Int32Type' => 'phpcassa\Schema\DataType\Int32Type', + 'SetType' => 'phpcassa\Schema\DataType\SetType', + 'ListType' => 'phpcassa\Schema\DataType\ListType', ); } @@ -81,6 +85,15 @@ public static function get_type_for($typestr) { return new CompositeType(self::get_inner_types($typestr)); } else if (strpos($typestr, 'ReversedType') !== false) { return self::get_type_for(self::get_inner_type($typestr)); + } else if (strpos($typestr,"(") !== false){ + preg_match('/(?.+[^\(])(\(((?.+[^\)]))\))/is',$typestr,$columnType); + + $type_name = self::extract_type_name($columnType['type']); + $type_class = self::$class_map[$type_name]; + $subtype_name = self::extract_type_name($columnType['subtype']); + $subtype_class = self::$class_map[$subtype_name]; + + return new $type_class(new $subtype_class); } else { $type_name = self::extract_type_name($typestr); $type_class = self::$class_map[$type_name]; @@ -89,4 +102,4 @@ public static function get_type_for($typestr) { } } -DataType::init(); +DataType::init(); \ No newline at end of file diff --git a/lib/phpcassa/Schema/DataType/ListType.php b/lib/phpcassa/Schema/DataType/ListType.php new file mode 100644 index 00000000..d4af2867 --- /dev/null +++ b/lib/phpcassa/Schema/DataType/ListType.php @@ -0,0 +1,38 @@ +innerType = $innerType; + } + + private function readUInt16BE($value,$offset) + { + list(, $int) = unpack('s*', strrev(substr($value,$offset,2))); + return $int; + } + + public function unpack($data, $handle_serialize=true) { + $offset = 0; + $total = self::readUInt16BE($data,$offset); + $offset += 2; + + $items = []; + for ($i = 0;$i < $total;$i++){ + $length = self::readUInt16BE($data,$offset); + $offset += 2; + $items[] = $this->innerType->unpack(substr($data,$offset,$length)); + $offset += $length; + } + + return $items; + } +} diff --git a/lib/phpcassa/Schema/DataType/SetType.php b/lib/phpcassa/Schema/DataType/SetType.php new file mode 100644 index 00000000..f5e3a4d7 --- /dev/null +++ b/lib/phpcassa/Schema/DataType/SetType.php @@ -0,0 +1,12 @@ + Date: Tue, 4 Feb 2014 16:01:36 +0100 Subject: [PATCH 2/3] Add Map type --- lib/phpcassa/Schema/DataType.php | 21 +++++++---- lib/phpcassa/Schema/DataType/MapType.php | 46 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 lib/phpcassa/Schema/DataType/MapType.php diff --git a/lib/phpcassa/Schema/DataType.php b/lib/phpcassa/Schema/DataType.php index 62b2ecaf..abd40fc3 100644 --- a/lib/phpcassa/Schema/DataType.php +++ b/lib/phpcassa/Schema/DataType.php @@ -24,6 +24,7 @@ class DataType const DATE_TYPE = "DateType"; const SET_TYPE = "SetType"; const LIST_TYPE = "ListType"; + const MAP_TYPE = "MapType"; public static $class_map; @@ -44,6 +45,7 @@ public static function init() { 'Int32Type' => 'phpcassa\Schema\DataType\Int32Type', 'SetType' => 'phpcassa\Schema\DataType\SetType', 'ListType' => 'phpcassa\Schema\DataType\ListType', + 'MapType' => 'phpcassa\Schema\DataType\MapType' ); } @@ -85,15 +87,22 @@ public static function get_type_for($typestr) { return new CompositeType(self::get_inner_types($typestr)); } else if (strpos($typestr, 'ReversedType') !== false) { return self::get_type_for(self::get_inner_type($typestr)); - } else if (strpos($typestr,"(") !== false){ - preg_match('/(?.+[^\(])(\(((?.+[^\)]))\))/is',$typestr,$columnType); + } else if (preg_match('/(?.+[^\(])(\(((?.+[^\)]),(?.+[^\)]))\))/is',$typestr,$columnType)){ + $type_name = self::extract_type_name($columnType['type']); + $type_class = self::$class_map[$type_name]; + $keytype_name = self::extract_type_name($columnType['keytype']); + $keytype_class = self::$class_map[$keytype_name]; + $valuetype_name = self::extract_type_name($columnType['valuetype']); + $valuetype_class = self::$class_map[$valuetype_name]; + return new $type_class(new $keytype_class,new $valuetype_class); + } else if (preg_match('/(?.+[^\(])(\(((?.+[^\)]))\))/is',$typestr,$columnType)){ $type_name = self::extract_type_name($columnType['type']); $type_class = self::$class_map[$type_name]; - $subtype_name = self::extract_type_name($columnType['subtype']); - $subtype_class = self::$class_map[$subtype_name]; + $valuetype_name = self::extract_type_name($columnType['valuetype']); + $valuetype_class = self::$class_map[$valuetype_name]; - return new $type_class(new $subtype_class); + return new $type_class(new $valuetype_class); } else { $type_name = self::extract_type_name($typestr); $type_class = self::$class_map[$type_name]; @@ -102,4 +111,4 @@ public static function get_type_for($typestr) { } } -DataType::init(); \ No newline at end of file +DataType::init(); diff --git a/lib/phpcassa/Schema/DataType/MapType.php b/lib/phpcassa/Schema/DataType/MapType.php new file mode 100644 index 00000000..15a13c76 --- /dev/null +++ b/lib/phpcassa/Schema/DataType/MapType.php @@ -0,0 +1,46 @@ +keyType = $keyType; + $this->valueType = $valueType; + } + + private function readUInt16BE($value,$offset) + { + list(, $int) = unpack('s*', strrev(substr($value,$offset,2))); + return $int; + } + + public function unpack($data) { + $offset = 0; + $total = self::readUInt16BE($data,$offset); + $offset += 2; + + $items = []; + for ($i = 0;$i < $total;$i++){ + $keyLength = self::readUInt16BE($data,$offset); + $offset += 2; + $key = $this->keyType->unpack(substr($data,$offset,$keyLength)); + $offset += $keyLength; + $valueLength = self::readUInt16BE($data,$offset); + $offset += 2; + $value = $this->keyType->unpack(substr($data,$offset,$valueLength)); + $offset += $valueLength; + + $items[$key] = $value; + } + + return $items; + } +} From d9617786bcdae058740a79d3c5eeb07377908054 Mon Sep 17 00:00:00 2001 From: Riekelt Kapitein Date: Tue, 4 Feb 2014 16:13:43 +0100 Subject: [PATCH 3/3] Add pack option --- lib/phpcassa/Schema/DataType/ListType.php | 31 ++++++++++++++++++----- lib/phpcassa/Schema/DataType/MapType.php | 20 +++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/phpcassa/Schema/DataType/ListType.php b/lib/phpcassa/Schema/DataType/ListType.php index d4af2867..df1da3ec 100644 --- a/lib/phpcassa/Schema/DataType/ListType.php +++ b/lib/phpcassa/Schema/DataType/ListType.php @@ -8,10 +8,10 @@ */ class ListType extends CassandraType { - protected $innerType; + protected $valueType; - public function __construct(CassandraType $innerType){ - $this->innerType = $innerType; + public function __construct(CassandraType $valueType){ + $this->valueType = $valueType; } private function readUInt16BE($value,$offset) @@ -20,16 +20,33 @@ private function readUInt16BE($value,$offset) return $int; } - public function unpack($data, $handle_serialize=true) { + private function writeUInt16BE($value) + { + return strrev(pack('s*', $value)); + } + + public function pack(Array $data) { + $return = $this->writeUInt16BE(count($data)); + + foreach ($data as $value){ + $packed = $this->valueType->pack($value); + $return.= $this->writeUInt16BE(strlen($packed)); + $return.= $packed; + } + + return $return; + } + + public function unpack($data) { $offset = 0; - $total = self::readUInt16BE($data,$offset); + $total = $this->readUInt16BE($data,$offset); $offset += 2; $items = []; for ($i = 0;$i < $total;$i++){ - $length = self::readUInt16BE($data,$offset); + $length = $this->readUInt16BE($data,$offset); $offset += 2; - $items[] = $this->innerType->unpack(substr($data,$offset,$length)); + $items[] = $this->valueType->unpack(substr($data,$offset,$length)); $offset += $length; } diff --git a/lib/phpcassa/Schema/DataType/MapType.php b/lib/phpcassa/Schema/DataType/MapType.php index 15a13c76..29932d14 100644 --- a/lib/phpcassa/Schema/DataType/MapType.php +++ b/lib/phpcassa/Schema/DataType/MapType.php @@ -22,6 +22,26 @@ private function readUInt16BE($value,$offset) return $int; } + private function writeUInt16BE($value) + { + return strrev(pack('s*', $value)); + } + + public function pack(Array $data) { + $return = $this->writeUInt16BE(count($data)); + + foreach ($data as $key=>$value){ + $packed = $this->keyType->pack($key); + $return.= $this->writeUInt16BE(strlen($packed)); + $return.= $packed; + $packed = $this->valueType->pack($value); + $return.= $this->writeUInt16BE(strlen($packed)); + $return.= $packed; + } + + return $return; + } + public function unpack($data) { $offset = 0; $total = self::readUInt16BE($data,$offset);