Permalink
Browse files

fixed documentation & bugs, implemented ArrayAccess for associative a…

…rray format column path retrieval
  • Loading branch information...
1 parent f890d00 commit e9827d95f14c01c8fea8b54421a95a56ac58bb62 @mjpearson committed Jan 20, 2010
View
@@ -2,7 +2,7 @@ version: alpha
== Welcome to the Pandra Toolkit ==
-Copyright (C) 2009 PHPGrease.net
+Copyright (C) 2010 Michael Pearson <pandra-support@phpgrease.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -42,98 +42,14 @@ The Pandra homepage is http://www.phpgrease.net/projects/Pandra
* Extensible type definition, validation and callback layers
- * Flexible arrangement of Keyspaces, Column Families and Super Columns
+ * Flexible arrangement of Keyspaces, Column Families and Super Columns (implements ArrayAccess)
* Hard schema aware, or build factory objects on the fly
- * Provides paginated search functions
-
- * Bundles with browser based ad hoc predicate, scaffolding and input validation tools
-
- Dependencies
* Cassandra >=0.4
* Thrift Interface (thrift binary + cassandra.thrift, or pre generated php sources)
- * PHP >=5.2 (requires APC module)
-
-- Usage Samples
-
- * Simple connection handling and CRUD operations are currently supported, pending significant further development :
-
-<?php
-// -------- THE CONNECTOR MODEL
-// The basic connection handler to the Cassandra cluster, supporting round-robin reads and writes
-
-// create named connections against PANDRA_PORT_DEFAULT
-Pandra::Connect('default', 'host1');
-Pandra::Connect('default1', 'host2');
-Pandra::Connect('default2', 'host3'); // last connected becomes the active connection
-
-// to set active node
-Pandra::setActive('default');
-
-// read/write modes
-Pandra::setReadMode(PANDRA_MODE_ACTIVE); // PANDRA_MODE_ACTIVE, PANDRA_MODE_ROUND, PANDRA_MODE_ROUNDRANDOM. Default ACTIVE
-Pandra::setWriteMode(PANDRA_MODE_ROUND); // PANDRA_MODE_ACTIVE, PANDRA_MODE_ROUND, PANDRA_MODE_ROUNDRANDOM, Default ACTIVE
-
-// get active CassandraClient/Thrift object
-Pandra::getClient();
-
-// disconnections. Consistency level can be tweaked to replication factor - will detect < 3 nodes for a quorum read for example and downgrade consistency accordingly
-Pandra::disconnect('default'); // closes transport for a named node and drops from connection pool.
-Pandra::disconnectAll();
-
-// Sets global default consistency level for reads and writes
-Pandra::setConsistency(cassandra_ConsistencyLevel::ONE);
-
-// -------- FACTORY CREATE
-// create a slice object from an existing key
-$mySlice = new PandraSlice('Keyspace1', 'Standard1', '4afcdebc4a65e'); // Keyspace/ColumnFamily/Key ID
-// create a new column
-$mySlice->addColumn('dyn');
-$mySlice->coldyn = 'some data';
-// save
-$mySlice->save();
-
-// -------- BY CHILD OBJECT
-class Customer extends PandraColumnFamily {
-
- public function constructColumns() {
- $this->keySpace = 'Keyspace1';
- $this->columnFamily = 'Standard1';
-
- $this->addColumn('customer', array('notempty', 'maxlength=20'));
- $this->addColumn('username', array('notempty', 'maxlength=20'));
- $this->addColumn('password', array('notempty', 'maxlength=20'), 'md5'); // md5 crypt password prior to save
- }
-}
-
-$customer = new Customer();
-$keyID = $customer->keyID = uniqid();
-echo $keyID."\n";
-
-// Magic setters for column values
-$cName = $customer->colcustomer = 'Foo McBar';
-$cUsername = $customer->colusername = 'foologin';
-$cPassword = $customer->colpassword = 'abc1234';
-
-echo $keyID."\n";
-
-// Save new record (slice)
-$customer->save();
-
-unset($customer);
-$customer = new Customer();
-$customer->load($keyID);
-
-// confirm password callback-save matches what we'd expect
-if ($customer->password != md5($cPassword)) {
- echo "password w/md5 mismatch\n"; // authenticate
-} else {
- echo "'password' w/md5 callback OK\n";
-}
-
-// Delete
-$customer->delete();
+ * PHP >=5.2 (requires APC module)
View
@@ -1,26 +1,11 @@
<?
-/*
-Copyright (C) 2009 PHPGrease.net
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Lesser General Public
-License as published by the Free Software Foundation; either
-version 3 of the License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-Lesser General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-The Pandra homepage is :
-http://www.phpgrease.net/projects/pandra
-*/
/**
+ * (c) 2010 phpgrease.net
+ *
+ * For licensing terms, plese see license.txt which should distribute with this source
*
* @package Pandra
+ * @author Michael Pearson <pandra-support@phpgrease.net>
*/
$GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/thrift-php/';
@@ -42,11 +27,11 @@
define('PANDRA_MODE_ROUND_APC', 1); // sequentially select between interpreter instances w/APC
define('PANDRA_MODE_RANDOM', 2); // select random node
-// Column Family Types
-define('PANDRA_CF_STANDARD', 0);
-define('PANDRA_CF_SUPER', 1);
+// Column Family/Column Types (Standard or Super)
+define('PANDRA_STANDARD', 0);
+define('PANDRA_SUPER', 1);
-function pandraAutoLoad($className) {
+function _pandraAutoLoad($className) {
if (!preg_match("/^pandra/i", $className)) return;
@@ -63,5 +48,5 @@ function pandraAutoLoad($className) {
require_once($classFile);
}
}
-
-spl_autoload_register('pandraAutoload');
+spl_autoload_register('_pandraAutoload');
+?>
View
@@ -1,14 +1,20 @@
<?php
/**
+ * (c) 2010 phpgrease.net
+ *
+ * For licensing terms, plese see license.txt which should distribute with this source
*
* @package Pandra
+ * @author Michael Pearson <pandra-support@phpgrease.net>
*/
-class PandraColumn {
+class PandraColumn extends cassandra_Column {
+
/* @var string column name */
public $name = NULL;
/* @var string column value */
- private $_value = NULL;
+ //private $_value = NULL;
+ public $value = NULL;
/* @var int last changed timestamp */
public $timestamp = NULL;
@@ -31,69 +37,67 @@ class PandraColumn {
/* @var PandraColumnFamily column family parent reference */
private $_parentCF = NULL;
- public function __construct($name, $parentCF, $typeDef = array()) {
- //parent::__construct(array('name' => $name));
- $this->_parentCF = $parentCF;
+ /**
+ * Column constructor (extends cassandra_Column)
+ * @param string $name Column name
+ * @param PandraColumnContainer $parentCF parent column family (standard or super), or supercolumn
+ * @param array $typeDef validator type definitions
+ */
+ public function __construct($name, PandraColumnContainer $parentCF, $typeDef = array()) {
+ parent::__construct(array('name' => $name));
+ $this->setParentCF($parentCF);
$this->typeDef = $typeDef;
}
+ /**
+ * Sets parent ColumnFamily or
+ * @param PandraColumnContainer $parentCF
+ */
+ public function setParentCF(PandraColumnContainer $parentCF) {
+ $this->_parentCF = $parentCF;
+ }
+
+ /**
+ * Gets the current working parent column family
+ * @return <type>
+ */
+ public function getParentCF() {
+ return $this->_parentCF;
+ }
+
+ /**
+ * Binds a timestamp to the column, defaults to current time if no override defined
+ * @param int $timeOverride new stamp
+ * @return int new timestamp
+ */
public function bindTime($timeOverride = NULL) {
$this->timestamp = ($timeOverride === NULL) ? time() : $timeOverride;
+ $this->_modified = TRUE;
return $this->timestamp;
}
- public function setvalue($value, $validate = TRUE) {
+ /**
+ * 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)
+ */
+ 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];
return FALSE;
}
}
- $this->_value = $value;
+ $this->value = $value;
$this->_modified = TRUE;
return TRUE;
}
- /**
- * Magic getter
- * @param string $colName field name to get
- * @return string value
- */
- public function __get($colName) {
- if ($colName == 'value') {
- return $this->_value;
- }
- return NULL;
- }
-
- /**
- * Magic setter
- * @todo propogate an exception for setcolumn if it returns false. magic __set's are void return type
- * @param string $colName field name to set
- * @param string $value value to set for field
- * @return bool field set ok
- */
- public function __set($colName, $value) {
- if ($colName == 'value') {
- return $this->setvalue($value, $this->_validator);
- }
- return NULL;
- }
-
- public function reset() {
- $this->_modified = FALSE;
- $this->_delete = FALSE;
- }
-
- public function markDelete() {
- $this->_delete = TRUE;
- $this->_modified = TRUE;
- }
-
public function callbackvalue() {
$value = '';
- eval('$value = '.$this->callback.'("'.$this->_value.'");');
+ eval('$value = '.$this->callback.'("'.$this->value.'");');
return $value;
}
@@ -109,6 +113,8 @@ public function cast($object, $parentCF = NULL) {
public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
+ if (!$this->isModified()) return TRUE;
+
//if (!$this->_modified) return FALSE;
$this->_parentCF->checkCFState();
@@ -121,17 +127,11 @@ public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel
$columnPath->column_family = $this->_parentCF->name;
$columnPath->column = $this->name;
- // @todo super writes
- /*
- if ($this->_parentCF !== NULL && $this->_parentCF instanceof PandraSuperColumn) {
- $columnPath->super_column = $this->_parentCF->superColumn;
- }
- */
try {
if ($this->_delete) {
$client->remove($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->bindTime(), $consistencyLevel);
} else {
- $client->insert($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->_value, $this->bindTime(), $consistencyLevel);
+ $client->insert($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, ($this->callback === NULL) ? $this->value : $this->callbackvalue(), $this->bindTime(), $consistencyLevel);
}
} catch (TException $te) {
array_push($this->lastError, $te->getMessage());
@@ -141,15 +141,32 @@ public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel
return TRUE;
}
+ /**
+ * Removes any modified or delete flags, (does not revert values)
+ */
+ public function reset() {
+ $this->_modified = FALSE;
+ $this->_delete = FALSE;
+ }
+
+ /**
+ * Marks this column for deletion
+ */
public function delete() {
- $this->markDelete();
- //$this->save();
+ $this->_delete = TRUE;
+ $this->_modified = TRUE;
}
+ /**
+ * @return bool Column is marked for deletion
+ */
public function isDeleted() {
return $this->_delete;
}
+ /**
+ * @return bool column has been modified since instance construct/load
+ */
public function isModified() {
return $this->_modified;
}
Oops, something went wrong.

0 comments on commit e9827d9

Please sign in to comment.