Skip to content

Commit

Permalink
Upgrade phpcassa
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Giroux committed Jun 9, 2012
1 parent a6ac35f commit 0b12afa
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 69 deletions.
32 changes: 17 additions & 15 deletions include/phpcassa/ColumnFamily.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,16 @@ class ColumnFamily {
/** @internal */
public $is_super;

/** @internal */
protected $cf_data_type;

/** @internal */
protected $col_name_type;

/** @internal */
protected $supercol_name_type;

/** @internal */
protected $col_type_dict;


Expand Down Expand Up @@ -533,11 +540,7 @@ protected function _get_range($start, $finish, $count, $cp, $slice, $cl, $buffsz
throw $ire;
}

$packed_key_start = $this->pack_key($start);
$packed_key_finish = $this->pack_key($finish);

return new RangeColumnFamilyIterator($this, $buffsz,
$packed_key_start, $packed_key_finish,
return new RangeColumnFamilyIterator($this, $buffsz, $start, $finish,
$count, $cp, $slice, $this->rcl($cl));
}

Expand Down Expand Up @@ -575,7 +578,7 @@ public function get_indexed_slices($index_clause,
$new_expr->op = $expr->op;
$new_clause->expressions[] = $new_expr;
}
$new_clause->start_key = $this->pack_key($index_clause->start_key);
$new_clause->start_key = $index_clause->start_key;
$new_clause->count = $index_clause->count;

$column_parent = $this->create_column_parent();
Expand Down Expand Up @@ -862,6 +865,7 @@ protected function create_column_parent($super_column=null) {
/** @internal */
const SLICE_FINISH = 2;

/** @internal */
public function pack_name($value,
$is_supercol_name=false,
$slice_end=self::NON_SLICE,
Expand All @@ -887,12 +891,14 @@ protected function unpack_name($b, $is_supercol_name=false, $handle_serialize=tr
return $this->col_name_type->unpack($b, $handle_serialize);
}

/** @internal */
public function pack_key($key, $handle_serialize=false) {
if (!$this->autopack_keys || $key === "")
return $key;
return $this->key_type->pack($key, true, null, $handle_serialize);
}

/** @internal */
public function unpack_key($b, $handle_serialize=true) {
if (!$this->autopack_keys)
return $b;
Expand All @@ -910,9 +916,6 @@ protected function pack_value($value, $col_name) {
if (!$this->autopack_values)
return $value;

if (!is_scalar($col_name) || is_float($col_name))
$col_name = serialize($col_name);

if (isset($this->col_type_dict[$col_name])) {
$dtype = $this->col_type_dict[$col_name];
return $dtype->pack($value, false);
Expand All @@ -925,9 +928,6 @@ protected function unpack_value($value, $col_name) {
if (!$this->autopack_values)
return $value;

if (!is_scalar($col_name) || is_float($col_name))
$col_name = serialize($col_name);

if (isset($this->col_type_dict[$col_name])) {
$dtype = $this->col_type_dict[$col_name];
return $dtype->unpack($value, false);
Expand All @@ -936,6 +936,7 @@ protected function unpack_value($value, $col_name) {
}
}

/** @internal */
public function keyslices_to_array($keyslices) {
$ret = array();
if ($this->return_format == self::DICTIONARY_FORMAT) {
Expand Down Expand Up @@ -1012,9 +1013,9 @@ protected function unpack_coscs_attrs($array_of_coscs) {
if($first->column) { // normal columns
foreach($array_of_coscs as $cosc) {
$col = $cosc->column;
$col->value = $this->unpack_value($col->value, $col->name);
$col->name = $this->unpack_name(
$col->name, false, $handle_serialize=false);
$col->value = $this->unpack_value($col->value, $col->name);
$ret[] = $col;
}
} else { // counter columns
Expand All @@ -1028,6 +1029,7 @@ protected function unpack_coscs_attrs($array_of_coscs) {
return $ret;
}

/** @internal */
public function make_mutation($array, $timestamp=null, $ttl=null) {
$coscs = $this->pack_data($array, $timestamp, $ttl);
$ret = array();
Expand Down Expand Up @@ -1068,7 +1070,7 @@ protected function dict_to_coscs($data, $timestamp, $ttl) {
}
$sub->name = $this->pack_name(
$name, false, self::NON_SLICE, true);
$sub->value = $this->pack_value($value, $name);
$sub->value = $this->pack_value($value, $sub->name);
$ret[] = $c_or_sc;
}
return $ret;
Expand All @@ -1091,7 +1093,7 @@ protected function array_to_coscs($data, $timestamp, $ttl) {
}
$sub->name = $this->pack_name(
$name, false, self::NON_SLICE, false);
$sub->value = $this->pack_value($value, $name);
$sub->value = $this->pack_value($value, $sub->name);
$ret[] = $c_or_sc;
}
return $ret;
Expand Down
6 changes: 4 additions & 2 deletions include/phpcassa/Iterator/RangeColumnFamilyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
*/
class RangeColumnFamilyIterator extends ColumnFamilyIterator {

private $key_start, $key_finish;
private $key_start;
private $key_finish;

public function __construct($column_family, $buffer_size,
$key_start, $key_finish, $row_count,
$column_parent, $predicate,
$read_consistency_level) {

// The key_start will be packed during the first get_buffer call
$this->key_start = $key_start;
$this->key_finish = $key_finish;
$this->key_finish = $column_family->pack_key($key_finish);

parent::__construct($column_family, $buffer_size, $row_count,
$key_start, $column_parent, $predicate,
Expand Down
6 changes: 5 additions & 1 deletion include/phpcassa/Schema/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class DataType
const BYTES_TYPE = "BytesType";
const LONG_TYPE = "LongType";
const INTEGER_TYPE = "IntegerType";
const INT32_TYPE = "Int32Type";
const FLOAT_TYPE = "FloatType";
const DOUBLE_TYPE = "DoubleType";
const ASCII_TYPE = "AsciiType";
const UTF8_TYPE = "UTF8Type";
const TIME_UUID_TYPE = "TimeUUIDType";
const LEXICAL_UUID_TYPE = "LexicalUUIDType";
const UUID_TYPE = "UUIDType";
const DATE_TYPE = "DateType";

public static $class_map;

Expand All @@ -35,7 +37,9 @@ public static function init() {
'TimeUUIDType' => 'phpcassa\Schema\DataType\TimeUUIDType',
'LexicalUUIDType' => 'phpcassa\Schema\DataType\LexicalUUIDType',
'UUIDType' => 'phpcassa\Schema\DataType\UUIDType',
'BooleanType' => 'phpcassa\Schema\DataType\BooleanType'
'BooleanType' => 'phpcassa\Schema\DataType\BooleanType',
'DateType' => 'phpcassa\Schema\DataType\DateType',
'Int32Type' => 'phpcassa\Schema\DataType\Int32Type',
);
}

Expand Down
3 changes: 1 addition & 2 deletions include/phpcassa/Schema/DataType/CompositeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function pack($value, $is_name=true, $slice_end=null, $is_data=false) {
$num_items = count($value);
for ($i = 0; $i < $num_items; $i++) {
$item = $value[$i];
$eoc = 0x00;
if (is_array($item)) {
list($item, $inclusive) = $item;
if ($inclusive) {
Expand All @@ -45,8 +46,6 @@ public function pack($value, $is_name=true, $slice_end=null, $is_data=false) {
$eoc = 0xFF;
else if ($slice_end == ColumnFamily::SLICE_FINISH)
$eoc = 0x01;
} else {
$eoc = 0x00;
}

$type = $this->inner_types[$i];
Expand Down
36 changes: 36 additions & 0 deletions include/phpcassa/Schema/DataType/DateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace phpcassa\Schema\DataType;

use phpcassa\Schema\DataType\LongType;
use phpcassa\Schema\DataType\Serialized;

/**
* Stores a date as a number of milliseconds since the unix epoch.
*
* @package phpcassa\Schema\DataType
*/
class DateType extends LongType implements Serialized {

public function pack($value, $is_name=true, $slice_end=null, $is_data=false)
{
if (false !== strpos($value, ' ')) {
list($usec, $sec) = explode(' ', $value);
$value = $sec + $usec;
} else if ($is_name && $is_data) {
$value = unserialize($value);
}

$value = floor($value * 1e3);
return parent::pack($value);
}

public function unpack($data, $handle_serialize=true)
{
$value = parent::unpack($data, false) / 1e3;
if ($handle_serialize) {
return serialize($value);
} else {
return $value;
}
}
}
22 changes: 22 additions & 0 deletions include/phpcassa/Schema/DataType/Int32Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace phpcassa\Schema\DataType;

/**
* Stores data as an 4-byte signed integer.
*
* @package phpcassa\Schema\DataType
*/
class Int32Type extends CassandraType {

public function pack($value, $is_name=null, $slice_end=null, $is_data=null)
{
// signed/unsigned doesn't matter when packing
return pack('N', $value);
}

public function unpack($data, $is_name=null)
{
return current(unpack('l', strrev($data)));
}
}

2 changes: 1 addition & 1 deletion include/phpcassa/Schema/DataType/LongType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace phpcassa\Schema\DataType;

/**
* Stores data as an 8-byte integer.
* Stores data as an 8-byte signed integer.
*
* @package phpcassa\Schema\DataType
*/
Expand Down
12 changes: 6 additions & 6 deletions include/phpcassa/SuperColumnFamily.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ protected function dict_to_columns($array, $timestamp, $ttl) {
}
$column->name = $this->pack_name(
$name, false, self::NON_SLICE, true);
$column->value = $this->pack_value($value, $name);
$column->value = $this->pack_value($value, $column->name);
$ret[] = $column;
}
return $ret;
Expand All @@ -333,7 +333,7 @@ protected function array_to_columns($array, $timestamp, $ttl) {
}
$column->name = $this->pack_name(
$name, false, self::NON_SLICE, false);
$column->value = $this->pack_value($value, $name);
$column->value = $this->pack_value($value, $column->name);
$ret[] = $column;
}
return $ret;
Expand Down Expand Up @@ -405,8 +405,8 @@ protected function unpack_coscs_attrs($array_of_coscs) {
if($first->column) { // normal columns
foreach($array_of_coscs as $cosc) {
$col = $cosc->column;
$col->name = $this->unpack_name($col->name, false, false);
$col->value = $this->unpack_value($col->value, $col->name);
$col->name = $this->unpack_name($col->name, false, false);
$ret[] = $col;
}
} else if($first->super_column) { // super columns
Expand Down Expand Up @@ -441,8 +441,8 @@ protected function unpack_subcolumn_attrs($columns, $have_counters) {
$ret = array();
if (!$have_counters) {
foreach($columns as $c) {
$c->name = $this->unpack_name($c->name, false, false);
$c->value = $this->unpack_value($c->value, $c->name);
$c->name = $this->unpack_name($c->name, false, false);
$ret[] = $c;
}
} else {
Expand All @@ -458,8 +458,8 @@ protected function columns_to_dict($columns, $have_counters) {
$ret = array();
if (!$have_counters) {
foreach($columns as $c) {
$name = $this->unpack_name($c->name, false);
$value = $this->unpack_value($c->value, $c->name);
$name = $this->unpack_name($c->name, false);
$ret[$name] = $value;
}
} else {
Expand All @@ -475,8 +475,8 @@ protected function columns_to_array($columns, $have_counters) {
$ret = array();
if (!$have_counters) {
foreach($columns as $c) {
$name = $this->unpack_name($c->name, false, false);
$value = $this->unpack_value($c->value, $c->name);
$name = $this->unpack_name($c->name, false, false);
$ret[] = array($name, $value);
}
} else {
Expand Down
Loading

0 comments on commit 0b12afa

Please sign in to comment.