Skip to content

Commit

Permalink
throw exception when use NAMES_FOR_VALUES flag in batch request
Browse files Browse the repository at this point in the history
issue #40
  • Loading branch information
shen2 committed Jul 11, 2015
1 parent 57f383a commit e8f94d2
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 306 deletions.
191 changes: 111 additions & 80 deletions src/Request/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,120 @@
use Cassandra\Protocol\Frame;
use Cassandra\Protocol;
use Cassandra\Connection;
use Cassandra\Type;

class Batch extends Request{
const TYPE_LOGGED = 0;
const TYPE_UNLOGGED = 1;
const TYPE_COUNTER = 2;
protected $opcode = Frame::OPCODE_BATCH;
/**
* @var array
*/
protected $_queryArray = [];
const TYPE_LOGGED = 0;
const TYPE_UNLOGGED = 1;
const TYPE_COUNTER = 2;
protected $opcode = Frame::OPCODE_BATCH;
/**
* @var array
*/
protected $_queryArray = [];

/**
* @var int
*/
protected $_batchType;
/**
*
* @var int
*/
protected $_consistency;
/**
*
* @var array
*/
protected $_options;
/**
*
* @param string $type
* @param string $consistency
* @param array $options
*/
public function __construct($type = null, $consistency = null, $options = []) {
$this->_batchType = $type === null ? Batch::TYPE_LOGGED : $type;
$this->_consistency = $consistency === null ? Request::CONSISTENCY_ONE : $consistency;
$this->_options = $options;
}
/**
* @var int
*/
protected $_batchType;
/**
*
* @var int
*/
protected $_consistency;
/**
*
* @var array
*/
protected $_options;
/**
*
* @param string $type
* @param string $consistency
* @param array $options
*/
public function __construct($type = null, $consistency = null, $options = []) {
$this->_batchType = $type === null ? Batch::TYPE_LOGGED : $type;
$this->_consistency = $consistency === null ? Request::CONSISTENCY_ONE : $consistency;
$this->_options = $options;
}

/**
* Exec transaction
*/
public function getBody() {
$body = pack('C', $this->_batchType);
$body .= pack('n', count($this->_queryArray)) . implode('', $this->_queryArray);

$body .= Request::queryParameters($this->_consistency, [], $this->_options);
return $body;
}
/**
* Exec transaction
*/
public function getBody() {
return pack('C', $this->_batchType)
. pack('n', count($this->_queryArray)) . implode('', $this->_queryArray)
. self::queryParameters($this->_consistency, $this->_options);
}

/**
* @param string $cql
* @param array $values
* @return self
*/
public function appendQuery($cql, array $values = []) {
$binary = pack('C', 0);

$binary .= pack('N', strlen($cql)) . $cql;
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));

$this->_queryArray[] = $binary;

return $this;
}

/**
*
* @param string $queryId
* @param array $values
* @return self
*/
public function appendQueryId($queryId, array $values = []) {
$binary = pack('C', 1);

$binary .= pack('n', strlen($queryId)) . $queryId;
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));

$this->_queryArray[] = $binary;

return $this;
}
/**
* @param string $cql
* @param array $values
* @return self
*/
public function appendQuery($cql, array $values = []) {
$binary = pack('C', 0);

$binary .= pack('N', strlen($cql)) . $cql;
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));

$this->_queryArray[] = $binary;

return $this;
}

/**
*
* @param string $queryId
* @param array $values
* @return self
*/
public function appendQueryId($queryId, array $values = []) {
$binary = pack('C', 1);

$binary .= pack('n', strlen($queryId)) . $queryId;
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));

$this->_queryArray[] = $binary;

return $this;
}

/**
*
* @param int $consistency
* @param array $options
* @return string
*/
public static function queryParameters($consistency, array $options = []){
$flags = 0;
$optional = '';

if (isset($options['serial_consistency'])) {
$flags |= Query::FLAG_WITH_SERIAL_CONSISTENCY;
$optional .= pack('n', $options['serial_consistency']);
}

if (isset($options['default_timestamp'])) {
$flags |= Query::FLAG_WITH_DEFAULT_TIMESTAMP;
$optional .= Type\Bigint::binary($options['default_timestamp']);
}

if (!empty($options['names_for_values'])){
/**
* @link https://github.com/duoshuo/php-cassandra/issues/40
*/
throw new \Cassandra\Exception('NAMES_FOR_VALUES in batch request seems never work in Cassandra 2.1.x. Keep NAMES_FOR_VALUES flag false to avoid this bug.');

$flags |= Query::FLAG_WITH_NAMES_FOR_VALUES;
}

return pack('n', $consistency) . pack('C', $flags) . $optional;
}
}
66 changes: 33 additions & 33 deletions src/Request/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
use Cassandra\Type;

class Register extends Request{
protected $opcode = Frame::OPCODE_REGISTER;
/**
*
* @var array
*/
protected $_events;
/**
* REGISTER
*
* Register this connection to receive some type of events. The body of the
* message is a [string list] representing the event types to register to. See
* section 4.2.6 for the list of valid event types.
*
* The response to a REGISTER message will be a READY message.
*
* Please note that if a client driver maintains multiple connections to a
* Cassandra node and/or connections to multiple nodes, it is advised to
* dedicate a handful of connections to receive events, but to *not* register
* for events on all connections, as this would only result in receiving
* multiple times the same event messages, wasting bandwidth.
*
* @param array $events
*/
public function __construct(array $events) {
$this->_events = $events;
}
public function getBody(){
return (new Type\CollectionList($this->_events, Type\Base::TEXT))->getBinary();
}
protected $opcode = Frame::OPCODE_REGISTER;
/**
*
* @var array
*/
protected $_events;
/**
* REGISTER
*
* Register this connection to receive some type of events. The body of the
* message is a [string list] representing the event types to register to. See
* section 4.2.6 for the list of valid event types.
*
* The response to a REGISTER message will be a READY message.
*
* Please note that if a client driver maintains multiple connections to a
* Cassandra node and/or connections to multiple nodes, it is advised to
* dedicate a handful of connections to receive events, but to *not* register
* for events on all connections, as this would only result in receiving
* multiple times the same event messages, wasting bandwidth.
*
* @param array $events
*/
public function __construct(array $events) {
$this->_events = $events;
}
public function getBody(){
return Type\CollectionList::binary($this->_events, [Type\Base::TEXT]);
}
}
Loading

0 comments on commit e8f94d2

Please sign in to comment.