Permalink
Browse files

clause/query update (incomplete/untested), began using batch_mutate. …

…***unstable

// eg complex query:  Loads all addresses or just 1/2 columns with value '1'.
$result = $c[$q->Regex('/address$/i')->In(array(1,2))][1]->load();
  • Loading branch information...
1 parent 9f89a00 commit 0a54465cf9502040ec1fba4de7195efd6a69eb5c @mjpearson committed Jul 15, 2010
@@ -334,25 +334,25 @@ public function offsetUnset($columnName) {
$this->unsetColumn($columnName);
}
- public function unsetColumn($columnName) {
- if (array_key_exists($columnName, $this->_columns)) {
- $this->_columns[$columnName]->disown(FALSE);
- unset($this->_columns[$columnName]);
- }
- }
-
/**
* Get column value by column name
* @param string $offset column name
* @return mixed column value
*/
public function offsetGet($columnName) {
- if ($columnName instanceof PandraClause) {
+ if ($columnName instanceof PandraClause || $columnName instanceof PandraQuery) {
return $this->getColumn($columnName);
}
return $this->__get($columnName);
}
+ public function unsetColumn($columnName) {
+ if (array_key_exists($columnName, $this->_columns)) {
+ $this->_columns[$columnName]->disown(FALSE);
+ unset($this->_columns[$columnName]);
+ }
+ }
+
/**
* Count children
* @return int number of immediate children in container
@@ -438,7 +438,7 @@ public function getReversed() {
private function _setStartFinish($value, $attrib = '_start') {
if ($this->_containerType == self::TYPE_UUID ||
- $this->_containerType == self::TYPE_LONG) {
+ $this->_containerType == self::TYPE_LONG) {
$this->$attrib = $this->typeConvert($value, self::CONTEXT_BIN);
} else {
$this->$attrib = $value;
@@ -486,8 +486,8 @@ protected function typeConvert($columnName, $toFmt) {
// Save accidental double-conversions on binaries
if (($bin && $toFmt == self::CONTEXT_BIN) ||
- (!$bin && $toFmt == self::CONTEXT_STRING)) {
- return $columnName;
+ (!$bin && $toFmt == self::CONTEXT_STRING)) {
+ return $columnName;
}
if (($this->_containerType == self::TYPE_UUID) ) {
@@ -507,7 +507,7 @@ protected function typeConvert($columnName, $toFmt) {
if ($bin && $toFmt == self::CONTEXT_STRING) {
$columnName = array_pop(unpack('N', $columnName));
- // pack the long
+ // pack the long
} elseif (!$bin && $toFmt == self::CONTEXT_BIN) {
$columnName = pack('NN', $columnName, 0);
}
@@ -570,9 +570,14 @@ public function addColumnObj(PandraColumn $columnObj) {
*/
public function getColumn($columnMatch) {
- // Extract matching named columns based on clause
- if ($columnMatch instanceof PandraClause) {
+ // @todo document that only empty containers can act as dynamic views.
+ if ($columnMatch instanceof PandraQuery && empty($this->_columns)) {
+ $columnMatch->setInvoker($this);
+ $columnMatch->graphContext(get_class($this));
+ return $columnMatch;
+ // Extract matching named columns based on clause
+ } elseif ($columnMatch instanceof PandraClause) {
$matches = array();
foreach ($this->_columns as $columnName => &$column) {
View
@@ -120,17 +120,28 @@ public function save($consistencyLevel = NULL) {
} else {
- // @todo have this use thrift batch_insert method in core
$modifiedColumns = $this->getModifiedColumns();
- $err = array();
+ // build mutation
+ $map = array($this->getKeyID() => array($this->getName() => array()));
+
+ // @todo - test delete mutate
foreach ($modifiedColumns as &$cObj) {
- if (!$cObj->checkValue($cObj->value, $err) || !$cObj->save(PandraCore::getConsistency($consistencyLevel))) {
- $this->registerError($cObj->getLastError());
- return FALSE;
+ $cObj->bindTime();
+
+ if ($cObj->isDeleted()) {
+ $p = new PandraSlicePredicate(PandraSlicePredicate::TYPE_COLUMNS, array($cObj->getName()));
+ $sd = new cassandra_Deletion(array('timestamp' => PandraCore::getTime(), 'predicate' => $p));
+ $m = new cassandra_Mutation(array('deletion' => $sc));
+ } else {
+ $sc = new cassandra_ColumnOrSuperColumn(array('column' => $cObj));
+ $m = new cassandra_Mutation(array('column_or_supercolumn' => $sc));
}
+
+ $map[$this->getKeyID()][$this->getName()][] = $m;
}
- $ok = TRUE;
+
+ $ok = PandraCore::saveMutation($this->getKeySpace(), $map, $consistencyLevel);
}
if ($ok) $this->reset();
}
View
@@ -355,14 +355,15 @@ static public function connectSeededKeyspace($hosts, $keySpace = self::DEFAULT_P
new TBinaryProtocolAccelerated($transport) :
new TBinaryProtocol($transport)));
- // @todo this has been deprecated by 'describe_ring' 0.6.3
- $tokenMap = $client->get_string_property('token map');
+ $tokenMap = $client->describe_ring($keySpace);
$transport->close();
unset($transport); unset($client);
- $tokens = json_decode($tokenMap);
- foreach ($tokens as $token => $host) {
- if (!self::connect($token, $host, $keySpace)) {
- return FALSE;
+
+ foreach ($tokenMap as $tokenRange) {
+ foreach ($tokenRange->endpoints as $host) {
+ if (!self::connect(md5($host), $host, $keySpace)) {
+ return FALSE;
+ }
}
}
return TRUE;
@@ -695,6 +696,7 @@ static public function saveSuperColumn($keySpace,
}
// batch_insert inserts a supercolumn across multiple CF's for key
+ // @todo batch mutate
$client->batch_insert($keySpace, $keyID, $mutations, self::getConsistency($consistencyLevel));
} catch (TException $te) {
@@ -704,6 +706,17 @@ static public function saveSuperColumn($keySpace,
return TRUE;
}
+ public function saveMutation($keySpace, $mutation, $consistencyLevel = NULL) {
+ try {
+ $client = self::getClient(TRUE, $keySpace);
+ $client->batch_mutate($keySpace, $mutation, self::getConsistency($consistencyLevel));
+ } catch (TException $te) {
+ self::registerError( 'TException: '.$te->getMessage().' '.(isset($te->why) ? $te->why : ''));
+ return FALSE;
+ }
+ return TRUE;
+ }
+
/**
* Gets complete slice of Thrift cassandra_Column objects for keyID
* @param string $keySpace keyspace of key
@@ -819,8 +832,6 @@ static public function getColumnPath($keySpace,
$consistencyLevel = NULL) {
$client = self::getClient(FALSE, $keySpace);
-
-
try {
return $client->get($keySpace, $keyID, $columnPath, self::getConsistency($consistencyLevel));
} catch (TException $te) {
@@ -831,15 +842,15 @@ static public function getColumnPath($keySpace,
/**
* @param string $keySpace keyspace of key
- * @param array $keyRange associative array of keys indexed by 'start' and 'finish'
+ * @param cassandra_KeyRange $keyRange
* @param cassandra_ColumnParent $columnParent
* @param cassandra_SlicePredicate $predicate column names or range predicate
* @param int number of rows to return
* @param int $consistencyLevel response consistency level
* @return <type>
*/
- static public function getRangeKeys($keySpace,
- array $keyRange,
+ static public function getRangeSlices($keySpace,
+ cassandra_KeyRange $keyRange,
cassandra_ColumnParent $columnParent,
cassandra_SlicePredicate $predicate,
$numRows = DEFAULT_ROW_LIMIT,
@@ -848,11 +859,10 @@ static public function getRangeKeys($keySpace,
$client = self::getClient(FALSE, $keySpace);
try {
- return $client->get_range_slice($keySpace,
+ return $client->get_range_slices($keySpace,
$columnParent,
$predicate,
- (isset($keyRange['start']) ? $keyRange['start'] : ''),
- (isset($keyRange['finish']) ? $keyRange['finish'] : ''),
+ $keyRange,
$numRows,
self::getConsistency($consistencyLevel));
} catch (TException $te) {
View
@@ -11,20 +11,11 @@
abstract class PandraClause {
- const TYPE_LITERAL = 0;
-
- const TYPE_SLICE = 1;
-
- const TYPE_RANGE = 2;
-
- CONST TYPE_FILTER = 3;
-
- protected $_type = NULL;
-
public function match($value) {}
public function getType() {
return $this->_type;
}
+
}
?>
@@ -11,8 +11,6 @@
class PandraClauseIn extends PandraClause {
- protected $_type = self::TYPE_LITERAL;
-
private $_valueIn = array();
public function __construct() {
@@ -0,0 +1,43 @@
+<?php
+/**
+ * (c) 2010 phpgrease.net
+ *
+ * For licensing terms, plese see license.txt which should distribute with this source
+ *
+ * Explicit usage:
+ * $clause = new PandraClauseLiteral('abcdefg');
+ * $clause->match();
+ *
+ * Query Callback
+ * $q = new PandraQuery();
+ * $q->Literal('abcdefg'));
+ *
+ * @package Pandra
+ * @link http://www.phpgrease.net/projects/pandra
+ * @author Michael Pearson <pandra-support@phpgrease.net>
+ */
+
+class PandraClauseLiteral extends PandraClause {
+
+ private $_arg = NULL;
+
+ private $_strict = FALSE;
+
+ /**
+ * @param mixed $arg value to match against
+ * @param bool $strict match identical type and value (default false)
+ */
+ public function __construct() {
+ $this->_arg = func_get_arg(0);
+ if (func_num_args() > 1) {
+ $this->_strict = func_get_arg(1);
+ }
+ }
+
+ public function match($value) {
+ return $this->_strict ?
+ $value === $this->_arg :
+ $value == $this->_arg;
+ }
+}
+?>
@@ -21,8 +21,6 @@ class PandraClauseNumericRange extends PandraClause {
private $_args = array();
- protected $_type = self::TYPE_RANGE;
-
public function __construct() {
$this->_args = func_get_arg(0);
}
@@ -18,8 +18,6 @@ class PandraClauseRegex extends PandraClause {
public $matches = array();
- protected $_type = self::TYPE_FILTER;
-
public function __construct() {
$args = func_get_args();
Oops, something went wrong.

0 comments on commit 0a54465

Please sign in to comment.