Permalink
Browse files

Bug fixes, cleaned up dependencies, added SuperColumn load/saves

  • Loading branch information...
1 parent e43beb6 commit ddd8a5fdfb4da8f26a1f5f4af8d558ef9b3f8ed9 @mjpearson committed Jan 24, 2010
View
@@ -31,6 +31,8 @@
define('PANDRA_STANDARD', 0);
define('PANDRA_SUPER', 1);
+define('PANDRA_DEFAULT_CREATE_MODE', TRUE);
+
function _pandraAutoLoad($className) {
if (!preg_match("/^pandra/i", $className)) return;
View
@@ -8,7 +8,7 @@
* @author Michael Pearson <pandra-support@phpgrease.net>
*/
class PandraColumn extends cassandra_Column {
-
+
/* @var string column name */
public $name = NULL;
@@ -23,7 +23,7 @@ class PandraColumn extends cassandra_Column {
public $typeDef = array();
/* @var string last processing error */
- public $lastError = array();
+ public $errors = array();
/* @var string callback function for this column pre-save */
public $callback = NULL;
@@ -80,12 +80,12 @@ public function bindTime($timeOverride = NULL) {
* Sets the value of the column
* @param mixed $value new value
* @param bool $validate validate the value, if typeDef is set
- * @return bool column set ok (check lastError for details)
+ * @return bool column set ok (check errors for details)
*/
public function setValue($value, $validate = TRUE) {
if ($validate && !empty($this->typeDef)) {
- if (!PandraValidator::check($value, $this->name, $this->typeDef, $this->lastError)) {
- $this->_parentCF->errors[] = $this->lastError[0];
+ if (!PandraValidator::check($value, $this->name, $this->typeDef, $this->errors)) {
+ $this->_parentCF->errors[] = $this->errors[0];
return FALSE;
}
}
@@ -101,44 +101,65 @@ public function callbackvalue() {
return $value;
}
- public function cast($object, $parentCF = NULL) {
- if (get_class($object) == 'cassandra_Column') {
- $newObj = new PandraColumn($object->name, ($parentCF === NULL) ? $this->_parentCF : $parentCF);
- $newObj->value = $object->value;
- $newObj->timestamp = $object->timestamp;
- return $newObj;
- }
- return NULL;
+ /**
+ * Casts from a cassandra_Column type, to PandraColumn
+ * @param cassandra_Column $object source objct
+ * @param PandraColumnFamily $parentCF parent container
+ * @return PandraColumn new column object
+ */
+ static public function cast(cassandra_Column $object, $parentCF = NULL) {
+ $newObj = new PandraColumn($object->name, ($parentCF === NULL) ? $this->_parentCF : $parentCF);
+ $newObj->value = $object->value;
+ $newObj->timestamp = $object->timestamp;
+ return $newObj;
}
- public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
+ public function save($keyID, $keySpace, $columnFamily, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
if (!$this->isModified()) return TRUE;
- //if (!$this->_modified) return FALSE;
- $this->_parentCF->checkCFState();
+ // Build the column path for modifying this individual column
+ $columnPath = new cassandra_ColumnPath();
+ $columnPath->column_family = $columnFamily;
+ $columnPath->column = $this->name;
- $client = Pandra::getClient(TRUE);
+ $ok = FALSE;
- // build the column path
- if ($columnPath == NULL) {
- $columnPath = new cassandra_ColumnPath();
+ if ($this->_delete) {
+ $ok = Pandra::deleteColumnPath($keySpace, $keyID, $columnPath, $this->bindTime(), $consistencyLevel);
+ } else {
+ $ok = Pandra::saveColumnPath($keySpace, $keyID, $columnPath, ($this->callback === NULL) ? $this->value : $this->callbackvalue(), $this->bindTime(), $consistencyLevel);
}
- $columnPath->column_family = $this->_parentCF->name;
- $columnPath->column = $this->name;
- try {
- if ($this->_delete) {
- $client->remove($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->bindTime(), $consistencyLevel);
+ if (!$ok) {
+ if (empty(Pandra::$errors)) {
+ $errorStr = 'Unkown Error';
} else {
- $client->insert($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, ($this->callback === NULL) ? $this->value : $this->callbackvalue(), $this->bindTime(), $consistencyLevel);
+ $errorStr = Pandra::$errors;
}
- } catch (TException $te) {
- array_push($this->lastError, $te->getMessage());
- $this->_parentCF->errors[] = $this->lastError[0];
- return FALSE;
+
+ $this->registerError($errorStr);
}
- return TRUE;
+
+ if ($ok) $this->reset();
+ return $ok;
+ }
+
+ public function registerError($errorStr) {
+ if (empty($errorStr)) return;
+ array_push($this->errors, $errorStr);
+ if ($this->_parentCF !== NULL) $this->_parentCF->registerError($errorStr);
+ }
+
+ public function getErrors() {
+ return $this->errors;
+ }
+
+ public function getLastError() {
+ if (count($this->errors)) {
+ return $this->errors[0];
+ }
+ return NULL;
}
/**
@@ -154,14 +175,14 @@ public function reset() {
*/
public function delete() {
$this->_delete = TRUE;
- $this->_modified = TRUE;
+ $this->_modified = TRUE;
}
/**
* @return bool Column is marked for deletion
*/
public function isDeleted() {
- return $this->_delete;
+ return ($this->_delete && $this->_modified);
}
/**
@@ -170,4 +191,5 @@ public function isDeleted() {
public function isModified() {
return $this->_modified;
}
-}
+}
+?>
@@ -14,10 +14,11 @@
abstract class PandraColumnContainer implements ArrayAccess {
/* @var this column families name (table name) */
- public $name = NULL;
+ protected $name = NULL;
/* @var mixed keyID key for the working row */
- public $keyID = NULL;
+ // @todo GET RID OF THIS
+ private $keyID = NULL;
/* @var string magic set/get prefixes for Columns */
const _columnNamePrefix = 'column_'; // magic __get/__set column prefix in column famliy
@@ -32,22 +33,48 @@
public $errors = array();
/* var bool columnfamily marked for deletion */
- private $_delete = FALSE;
+ private $_delete = FALSE;
- /**
- * Constructor, builds column structures
- */
- public function __construct($keyID = NULL) {
+ private $_modified = FALSE;
+
+ protected $_loaded = FALSE;
+
+ public function __construct() {
$this->constructColumns();
- if ($keyID !== NULL) $this->load($keyID);
+ }
+
+ public function getName() {
+ return $this->name;
+ }
+
+ public function setName($name) {
+ $this->name = $name;
+
}
/**
* constructFields builds column objects via addColumn/addSuper methods
* when defining hard schemas. It is called automatically by the constructor.
* @return void
*/
- public function constructColumns() {
+ public function constructColumns() { }
+
+ protected function setModified($modified) {
+ $this->_modified = $modified;
+ }
+
+ protected function setDelete($delete) {
+ $this->setModified(TRUE);
+ $this->_delete = $delete;
+ }
+
+ protected function getDelete() {
+ return $this->_delete;
+ }
+
+ public function registerError($errorStr) {
+ if (empty($errorStr)) return;
+ $this->errors[] = $errorStr;
}
/**
@@ -136,7 +163,7 @@ public function getColumn($colName) {
* @param bool $validate opt validate from typeDef (default TRUE)
* @return bool column set ok
*/
- public function setColumn($colName, $value, $validate = TRUE) {
+ public function setColumn($colName, $value, $validate = TRUE) {
return (array_key_exists($colName, $this->_columns) && $this->_columns[$colName]->setValue($value, $validate));
}
@@ -154,32 +181,6 @@ public function listColumns() {
*/
/**
- * Gets complete slice of Thrift cassandra_Column objects for keyID
- * @param mixed $keyID key id for row
- * @param int $consistencyLevel cassandra consistency level
- * @return cassandra_Cassandra_get_slice_result
- */
- public function getRawSlice($keyID, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
- $this->keyID = $keyID;
-
- $this->_parentCF->checkCFState();
-
- $client = Pandra::getClient();
-
- // build the column path
- $columnParent = new cassandra_ColumnParent();
- $columnParent->column_family = $this->name;
- $columnParent->super_column = null;
-
- $predicate = new cassandra_SlicePredicate();
- $predicate->slice_range = new cassandra_SliceRange();
- $predicate->slice_range->start = '';
- $predicate->slice_range->finish = '';
-
- return $client->get_slice($this->keySpace, $keyID, $columnParent, $predicate, $consistencyLevel);
- }
-
- /**
* Resets deletion states for the column family
*/
public function reset() {
@@ -192,18 +193,33 @@ public function reset() {
/**
* Populates container object (ColumnFamily, ColumnFamilySuper or SuperColumn)
- * @param mixed $data associative array or json string of key => values.
+ * @param mixed $data associative string array, array of cassandra_Column's or JSON string of key => values.
* @return bool column values set without error
*/
- public function populate($data) {
+ public function populate($data, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE) {
if (is_string($data)) {
$data = json_decode($data, TRUE);
}
- if (is_array($data)) {
- foreach ($data as $key => $value) {
- if (array_key_exists($key, $this->_columns)) {
- $this->_columns[$key]->setValue($value);
+ if (is_array($data) && count($data)) {
+ foreach ($data as $idx => $colValue) {
+
+ if ($value instanceof cassandra_Column) {
+ if ($colAutoCreate || array_key_exists($colValue->column->name, $this->_columns)) {
+ $this->_columns[$colValue->column->name] = PandraColumn::cast($colValue->column, $this);
+ }
+
+ } else {
+ $colExists = array_key_exists($idx, $this->_columns);
+ // Create a new named column object
+ if ($colAutoCreate && !array_key_exists($idx, $this->_columns)) {
+ $this->addColumn($idx);
+ }
+
+ // Set Value
+ if (array_key_exists($idx, $this->_columns)) {
+ $this->_columns[$idx]->setValue($colValue);
+ }
}
}
} else {
@@ -219,7 +235,7 @@ public function populate($data) {
* @return bool field exists
*/
protected function _gsMutable(&$colName) {
- $colName = preg_replace("/^".self::_columnNamePrefix."/", "", $colName);
+ $colName = preg_replace("/^".constant(get_class($this).'::_columnNamePrefix')."/", "", $colName);
return array_key_exists($colName, $this->_columns);
}
@@ -241,6 +257,7 @@ protected function __get($colName) {
return $this->_columns[$colName];
}
}
+ //echo "$colName mutable <br>";
return NULL;
}
@@ -253,7 +270,7 @@ protected function __get($colName) {
*/
protected function __set($colName, $value) {
if (!$this->_gsMutable($colName)) {
- $this->addColumn($offset);
+ $this->addColumn($colName);
}
$this->setColumn($colName, $value);
}
@@ -265,4 +282,28 @@ protected function __set($colName, $value) {
public function getType() {
return constant(get_class($this)."::TYPE");
}
-}
+
+ public function isModified() {
+ foreach ($this->_columns as $column) {
+ if ($column->isModified()) return TRUE;
+ }
+ return FALSE;
+ }
+
+ public function bindTimeModifiedColumns() {
+ foreach ($this->_columns as &$cObj) {
+ if ($cObj->isModified()) {
+ $cObj->bindTime();
+ }
+ }
+ }
+
+ public function getModifiedColumns() {
+ $modColumns = array();
+ foreach ($this->_columns as &$cObj) {
+ if ($cObj->isModified()) $modColumns[] = &$cObj;
+ }
+ return $modColumns;
+ }
+}
+?>
Oops, something went wrong.

0 comments on commit ddd8a5f

Please sign in to comment.