Permalink
Browse files

Refactor ColumnFamilySuper => SuperColumnFamily (now inline with Cass…

…andra terminology), added Super CF populate + json populate/export
  • Loading branch information...
1 parent 0c76f94 commit 49af9c771c08bf45481a4908a1abd5bd64252b7c @mjpearson committed Jan 30, 2010
View
2 README
@@ -1,4 +1,4 @@
-version: alpha
+version: 0.1
== Welcome to the Pandra Toolkit ==
@@ -0,0 +1,94 @@
+<?php
+/**
+ * (c) 2010 phpgrease.net
+ *
+ * @author Michael Pearson <pandra-support@phpgrease.net>
+ */
+
+/**
+ * Example dynamic Address Book construct using Column Family (Super) and Super Columns
+ */
+
+session_start();
+
+error_reporting(E_ALL);
+require_once(dirname(__FILE__).'/../config.php');
+
+if (!Pandra::connect('default', 'localhost')) {
+ die(Pandra::$lastError);
+}
+
+// hard code the row key for this example
+$keyID = 'PAddressStore';
+
+// default keyspace in storage.conf
+$keySpace = 'Keyspace1';
+
+// default Super Column name
+$columnFamilyName = 'Super1';
+
+class Address extends PandraSuperColumn {
+ public function init() {
+ $this->addColumn('city', 'string');
+ $this->addColumn('street', 'string');
+ $this->addColumn('zip', 'int');
+ }
+}
+
+class Addresses extends PandraSuperColumnFamily {
+
+ var $keySpace = 'Keyspace1';
+ var $name = 'Super1';
+
+ public function init() {
+ $this->addSuper(new Address('homeAddress'));
+ $this->addSuper(new Address('workAddress'));
+ }
+}
+
+$addrs = new Addresses();
+$addrs->keyID = $keyID;
+
+// home address
+$homeAddr = $addrs->getColumn('homeAddress');
+$homeAddr->setColumn('city', 'san francisco');
+$homeAddr->setColumn('street', '1234 x street');
+$homeAddr->setColumn('zip', '94107');
+
+// work address
+$workAddr = $addrs->getColumn('workAddress');
+$workAddr->setColumn('city', 'san jose');
+$workAddr->setColumn('street', '9876 y drive');
+
+// custom labelled supercolumn
+$customAddr = new Address('customAddress');
+$customAddr->setColumn('city', 'another city');
+$addrs->addSuper($customAddr);
+
+if (!$addrs->save()) {
+ die($addrs->lastError());
+}
+
+unset($addrs);
+
+// Load the saved Addresses
+$addrs = new Addresses();
+if (!$addrs->load($keyID)) {
+ die($addrs->lastError());
+}
+
+// Show our loaded CF in JSON format, including key path
+echo '<pre>'.$addrs->toJSON(TRUE).'</pre>';
+
+// Example export -> import of JSON structure from loaded data into new object
+$addrsClone = new Addresses();
+
+// Populates data from JSON export (therefore must exclude key path)
+$addrsClone->populate($addrs->toJSON());
+
+// Set key
+$addrsClone->setKeyID($keyID);
+
+// Show the JSON for our CF with cloned data. This and the prior echo should match
+echo '<pre>'.$addrsClone->toJSON(TRUE).'</pre>';
+?>
@@ -11,7 +11,7 @@
/**
* @abstract
*/
-abstract class PandraColumnContainer extends ArrayObject {
+abstract class PandraColumnContainer implements ArrayAccess {
/* @var this column families name (table name) */
protected $_name = NULL;
@@ -182,6 +182,10 @@ public function getColumn($colName) {
return NULL;
}
+ public function getColumns() {
+ return $this->_columns;
+ }
+
/**
* Sets a columns value for this slice
* @param string $colName Column name to set
@@ -261,6 +265,9 @@ public function populate($data, $colAutoCreate = NULL) {
}
if (is_array($data) && count($data)) {
+
+ // Check depth, take first few keys as keyspace/columnfamily/key
+
foreach ($data as $idx => $colValue) {
if ($colValue instanceof cassandra_Column) {
@@ -277,7 +284,9 @@ public function populate($data, $colAutoCreate = NULL) {
// Set Value
if (array_key_exists($idx, $this->_columns)) {
- $this->_columns[$idx]->setValue($colValue);
+ if ($this->_columns[$idx] instanceof PandraColumn) {
+ $this->_columns[$idx]->setValue($colValue);
+ }
}
}
}
@@ -367,5 +376,28 @@ public function getModifiedColumns() {
}
return $modColumns;
}
+
+ public function toJSON($keyPath = FALSE) {
+ return json_encode($this->toArray($keyPath));
+ }
+
+ public function toArray($keyPath = FALSE) {
+ $retArr = array();
+ foreach ($this->_columns as $column) {
+ if ($column instanceof PandraColumn) {
+ $retArr[$column->name] = $column->value;
+ } else {
+ // keyspace/CF/key/{column or supercolumn}
+ if ($keyPath) {
+ $retArr[$this->getKeySpace()][$this->getName()][$this->keyID][$column->getName()] = $column->toArray();
+ } else {
+ $retArr[$column->getName()] = $column->toArray($keyPath);
+ }
+
+ }
+ }
+
+ return $retArr;
+ }
}
?>
@@ -15,9 +15,9 @@ class PandraSuperColumn extends PandraColumnContainer {
/**
* Supercolumn constructor
* @param string $superName Super Column name
- * @param PandraColumnFamilySuper $parentCF
+ * @param PandraSuperColumnFamily $parentCF
*/
- public function __construct($superName, PandraColumnFamilySuper $parentCF = NULL) {
+ public function __construct($superName, PandraSuperColumnFamily $parentCF = NULL) {
// SuperColumn name
$this->setName($superName);
@@ -83,7 +83,7 @@ public function save($consistencyLevel = NULL) {
*/
public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
- if ($this->_parentCF == NULL || !($this->_parentCF instanceof PandraColumnFamilySuper)) throw new RuntimeException('SuperColumn Requires a ColumnFamilySuper parent');
+ if ($this->_parentCF == NULL || !($this->_parentCF instanceof PandraSuperColumnFamily)) throw new RuntimeException('SuperColumn Requires a ColumnFamilySuper parent');
$this->_parentCF->checkCFState();
@@ -106,7 +106,7 @@ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
* Sets parent ColumnFamily or
* @param PandraColumnContainer $parentCF
*/
- public function setParentCF(PandraColumnFamilySuper $parentCF) {
+ public function setParentCF(PandraSuperColumnFamily $parentCF) {
$this->_parentCF = $parentCF;
}
@@ -11,7 +11,7 @@
/**
* @abstract
*/
-class PandraColumnFamilySuper extends PandraColumnFamily {
+class PandraSuperColumnFamily extends PandraColumnFamily {
/* @var string magic get/set prefix for Super Columns */
const _columnNamePrefix = 'super_';
@@ -124,5 +124,39 @@ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
return $this->_loaded;
}
+
+ /**
+ * Populates container object (ColumnFamily, ColumnFamilySuper or SuperColumn)
+ * @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, $colAutoCreate = NULL) {
+ if (is_string($data)) {
+ $data = json_decode($data, TRUE);
+ }
+
+ if (is_array($data) && count($data)) {
+
+ foreach ($data as $idx => $colValue) {
+
+ // Allow named SuperColumns to be populated into this CF
+ if ($colValue instanceof PandraSuperColumn) {
+ if ($this->getAutoCreate($colAutoCreate) || array_key_exists($idx, $this->_columns)) {
+ $this->_columns[$idx] = $colValue;
+ }
+
+ } else {
+ if ($this->getAutoCreate($colAutoCreate) || array_key_exists($idx, $this->_columns)) {
+ $this->addSuper(new PandraSuperColumn($idx), $this);
+ $this->getSuper($idx)->populate($colValue);
+ }
+ }
+ }
+ } else {
+ return FALSE;
+ }
+
+ return empty($this->errors);
+ }
}
?>
@@ -1,7 +1,7 @@
<?php
require_once 'PHPUnit/Framework.php';
require_once(dirname(__FILE__).'/../../config.php');
-require_once dirname(__FILE__).'/../../lib/ColumnFamilySuper.class.php';
+require_once dirname(__FILE__).'/../../lib/SuperColumnFamily.class.php';
// SuperColumn wrapper
class TestSuperColumn extends PandraSuperColumn {
@@ -13,7 +13,7 @@ public function init() {
}
// ColumnFamily (SuperColumn Wrapper)
-class TestCFSuper extends PandraColumnFamilySuper {
+class TestCFSuper extends PandraSuperColumnFamily {
public function init() {
$this->setKeySpace('Keyspace1');
@@ -25,12 +25,12 @@ public function init() {
}
/**
- * Test class for PandraColumnFamilySuper.
+ * Test class for PandraSuperColumnFamily.
* Generated by PHPUnit on 2010-01-09 at 11:52:22.
*/
-class PandraColumnFamilySuperTest extends PHPUnit_Framework_TestCase {
+class PandraSuperColumnFamilyTest extends PHPUnit_Framework_TestCase {
/**
- * @var PandraColumnFamilySuper
+ * @var PandraSuperColumnFamily
* @access protected
*/
protected $obj;
@@ -154,7 +154,7 @@ public function testNotations() {
// --------- Magic Methods
// Test Super Add
- $superPath = PandraColumnFamilySuper::_columnNamePrefix.$superName;
+ $superPath = PandraSuperColumnFamily::_columnNamePrefix.$superName;
$columnPath = PandraColumnFamily::_columnNamePrefix.$colName;
$this->obj->$superPath = new TestSuperColumn($superName);
@@ -181,7 +181,7 @@ public function testNotations() {
// --------- Accessors/Mutators
// Test Super Add
- $superPath = PandraColumnFamilySuper::_columnNamePrefix.$superName;
+ $superPath = PandraSuperColumnFamily::_columnNamePrefix.$superName;
$columnPath = PandraColumnFamily::_columnNamePrefix.$colName;
$this->obj->addSuper(new TestSuperColumn($superName));
@@ -12,7 +12,7 @@ public function init() {
}
// ColumnFamily (SuperColumn Wrapper)
-class TestCFSuper extends PandraColumnFamilySuper {
+class TestCFSuper extends PandraSuperColumnFamily {
public function init() {
$this->setKeySpace('Keyspace1');

0 comments on commit 49af9c7

Please sign in to comment.