Permalink
Browse files

Fixed bad calls vs core::delete (no delete function and bad keyspace …

…getter), updated unit tests and checked coverage.

Added Query stub + extensible clause for inline CF extraction, eg:

$c = new PandraColumnFamily();
$c['myblog'] = ' BLOG NAME ';
$c['homeAddress'] = ' MY HOUSE ';
$c['workAddress'] = ' MY WORK ';

$q = new PandraQuery();
var_dump($c[$q->Regex('!address$!i')]->get()); // returns just the address columns
  • Loading branch information...
1 parent c771eb8 commit 4d0a0070f2797f1ba3a9c298c82acdb5c461aa55 @mjpearson committed Feb 20, 2010
View
@@ -27,6 +27,8 @@ function _pandraAutoLoad($className) {
// class path relative to config
$classPath = dirname(__FILE__)."/lib/";
+ if (preg_match('/^(Query|Clause)/', $className)) $classPath .= 'query/';
+
// class file suffix
$cSuffix = ".class.php";
View
@@ -5,19 +5,11 @@
* For licensing terms, plese see license.txt which should distribute with this source
*
* @package Pandra
+ * @link http://www.phpgrease.net/projects/pandra
* @author Michael Pearson <pandra-support@phpgrease.net>
*/
class PandraColumn extends cassandra_Column {
- /* @var string column name */
- public $name = NULL;
-
- /* @var string column value */
- public $value = NULL;
-
- /* @var int last changed timestamp */
- public $timestamp = NULL;
-
/* @var array validator type definitions for this colun */
public $typeDef = array();
@@ -69,11 +61,12 @@ public function getParentCF() {
/**
* Binds a timestamp to the column, defaults to current time if no override defined
- * @param int $timeOverride new stamp
+ * @param int $time new time stamp, microtime assumed (optional)
* @return int new timestamp
*/
- public function bindTime($timeOverride = NULL) {
- $this->timestamp = ($timeOverride === NULL) ? time() : $timeOverride;
+ public function bindTime($time = NULL) {
+ //$this->timestamp = ($time === NULL) ? round(microtime(true) * 1000, 3) : intval($time);
+ $this->timestamp = ($time === NULL) ? time() : intval($time);
$this->setModified();
return $this->timestamp;
}
@@ -104,9 +97,7 @@ public function setValue($value, $validate = TRUE) {
* @return mixed result of callback eval
*/
public function callbackvalue() {
- $value = '';
- eval('$value = '.$this->callback.'("'.$this->value.'");');
- return $value;
+ return call_user_func($this->callback, $this->value);
}
/**
@@ -161,6 +152,8 @@ public function save($keyID, $keySpace, $columnFamily, $consistencyLevel = NULL)
return $ok;
}
+ // ----------------- ERROR HANDLING
+
/**
* Creates an error entry in this column and propogate to parent
* @param string $errorStr error string
@@ -191,6 +184,8 @@ public function getLastError() {
return NULL;
}
+ // ----------------- MODIFY/DELETE MUTATORS AND ACCESSORS
+
/**
* Removes any modified or delete flags, (does not revert values)
*/
@@ -86,6 +86,10 @@ protected function setDelete($delete) {
$this->_delete = $delete;
}
+ public function delete() {
+ $this->setDelete(TRUE);
+ }
+
/**
* accessor, delete
* @return bool container is marked for deletion
@@ -136,8 +140,11 @@ public function offsetUnset($offset) {
* @param string $offset column name
* @return mixed column value
*/
- public function offsetGet($offset) {
- return $this->__get($offset);
+ public function offsetGet($columnName) {
+ if ($columnName instanceof PandraClause) {
+ return $this->getColumn($columnName);
+ }
+ return $this->__get($columnName);
}
/**
@@ -153,31 +160,46 @@ public function lastError() {
/**
* Define a new column, type definition and callback
- * @param string $colName column name
+ * @param string $columnName column name
* @param array $typeDef validator type definitions
* @param string $callbackOnSave callback function pre-save
* @return PandraColumn reference to created column
*/
- public function addColumn($colName, $typeDef = array(), $callbackOnSave = NULL) {
- if (!array_key_exists($colName, $this->_columns)) {
- $this->_columns[$colName] = new PandraColumn($colName, $this, $typeDef);
+ public function addColumn($columnName, $typeDef = array(), $callbackOnSave = NULL) {
+ if (!array_key_exists($columnName, $this->_columns)) {
+ $this->_columns[$columnName] = new PandraColumn($columnName, $this, $typeDef);
}
// pre-save callback
- if (!empty($callbackOnSave)) $this->_columns[$colName]->callback = $callbackOnSave;
+ if (!empty($callbackOnSave)) $this->_columns[$columnName]->callback = $callbackOnSave;
- return $this->getColumn($colName);
+ return $this->getColumn($columnName);
}
/**
* Get reference to named PandraColumn
- * @param string $colName column name
+ * @param string $columnName column name
* @return PandraColumn
*/
- public function getColumn($colName) {
+ public function getColumn($columnMatch) {
+
+ // Extract matching named columns based on clause
+ if ($columnMatch instanceof PandraClause) {
+
+ $container = new PandraQuery();
+
+ $matches = array();
- if ($this->_gsMutable($colName)) {
- return $this->_columns[$colName];
+ foreach ($this->_columns as $columnName => &$column) {
+ if ($columnMatch->match($columnName)) {
+ $container->columns[$columnName] = $column;
+ }
+ }
+ return $container;
+ }
+
+ if ($this->_gsMutable($columnMatch)) {
+ return $this->_columns[$columnMatch];
}
return NULL;
}
@@ -188,18 +210,18 @@ public function getColumns() {
/**
* Sets a columns value for this slice
- * @param string $colName Column name to set
+ * @param string $columnName Column name to set
* @param string $value New value for column
* @param bool $validate opt validate from typeDef (default TRUE)
* @return bool column set ok
*/
- public function setColumn($colName, $value, $validate = TRUE) {
+ public function setColumn($columnName, $value, $validate = TRUE) {
if (is_object($value)) {
- $this->_columns[$colName] = $value;
+ $this->_columns[$columnName] = $value;
return TRUE;
}
- return (array_key_exists($colName, $this->_columns) && $this->_columns[$colName]->setValue($value, $validate));
+ return (array_key_exists($columnName, $this->_columns) && $this->_columns[$columnName]->setValue($value, $validate));
}
/**
@@ -223,11 +245,11 @@ public function reset() {
/**
* removes a column from the container (does not delete from Cassandra)
- * @param string $colName column name
+ * @param string $columnName column name
*/
- public function destroyColumn($colName) {
- if (array_key_exists($colName, $this->_columns)) {
- unset($this->_columns[$colName]);
+ public function destroyColumn($columnName) {
+ if (array_key_exists($columnName, $this->_columns)) {
+ unset($this->_columns[$columnName]);
}
}
@@ -260,6 +282,7 @@ public function setAutoCreate(bool $autoCreate) {
* @return bool column values set without error
*/
public function populate($data, $colAutoCreate = NULL) {
+
if (is_string($data)) {
$data = json_decode($data, TRUE);
}
@@ -299,27 +322,27 @@ public function populate($data, $colAutoCreate = NULL) {
/**
* determine if get/set field exists/is mutable, strips field prefix from magic get/setters
- * @param string $colName field name to check
+ * @param string $columnName field name to check
* @return bool field exists
*/
- protected function _gsMutable(&$colName) {
- $colName = preg_replace("/^".constant(get_class($this).'::_columnNamePrefix')."/", "", $colName);
- return array_key_exists($colName, $this->_columns);
+ protected function _gsMutable(&$columnName) {
+ $columnName = preg_replace("/^".constant(get_class($this).'::_columnNamePrefix')."/", "", $columnName);
+ return array_key_exists($columnName, $this->_columns);
}
/**
* Magic getter
- * @param string $colName field name to get
+ * @param string $columnName field name to get
* @return string value
*/
- public function __get($colName) {
- if ($this->_gsMutable($colName)) {
+ public function __get($columnName) {
+ if ($this->_gsMutable($columnName)) {
- if ($this->_columns[$colName] instanceof PandraColumn) {
- return $this->_columns[$colName]->value;
+ if ($this->_columns[$columnName] instanceof PandraColumn) {
+ return $this->_columns[$columnName]->value;
- } else if ($this->_columns[$colName] instanceof PandraColumnContainer) {
- return $this->_columns[$colName];
+ } else if ($this->_columns[$columnName] instanceof PandraColumnContainer) {
+ return $this->_columns[$columnName];
}
}
return NULL;
@@ -328,17 +351,17 @@ public function __get($colName) {
/**
* 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 $columnName field name to set
* @param string $value value to set for field
* @return bool field set ok
*/
- public function __set($colName, $value) {
- if (!$this->_gsMutable($colName) && $this->getAutoCreate()) {
- $this->addColumn($colName);
+ public function __set($columnName, $value) {
+ if (!$this->_gsMutable($columnName) && $this->getAutoCreate()) {
+ $this->addColumn($columnName);
}
- if (!$this->setColumn($colName, $value)) {
- throw new RuntimeException($colName.' set but does not exist in container');
+ if (!$this->setColumn($columnName, $value)) {
+ throw new RuntimeException($columnName.' set but does not exist in container');
}
}
View
@@ -6,6 +6,7 @@
* For licensing terms, plese see license.txt which should distribute with this source
*
* @package Pandra
+ * @link http://www.phpgrease.net/projects/pandra
* @author Michael Pearson <pandra-support@phpgrease.net>
*/
class PandraCore {
@@ -36,6 +37,10 @@ class PandraCore {
self::MODE_RANDOM,
);
+ static private $_memcachedAvailable = FALSE;
+
+ static private $_apcAvailable = FALSE;
+
static public function getSupportedModes() {
return self::$_supportedModes;
}
@@ -129,7 +134,22 @@ static public function connect($connectionID, $host = NULL, $port = self::THRIFT
self::$lastError = 'TException: '.$tx->getMessage() . "\n";
}
return FALSE;
+ }
+ static public function setmemcachedAvailable($memcachedAvailable) {
+ self::$_memcachedAvailable = $memcachedAvailable;
+ }
+
+ static public function getmemcachedAvailable() {
+ return self::$_memcachedAvailable;
+ }
+
+ static public function setAPCAvailable($apcAvailable) {
+ self::$_apcAvailable = $apcAvailable;
+ }
+
+ static public function getAPCAvailable() {
+ return self::$_apcAvailable;
}
/**
@@ -214,6 +234,8 @@ public function saveSuperColumn($keySpace, $keyID, $superCFName, $superName, arr
$scContainer->super_column->name = $superName;
$scContainer->super_column->columns = $columns;
+ //var_dump($scContainer); exit;
+
$mutation = array();
$mutations[$superCFName] = array($scContainer);
@@ -248,7 +270,7 @@ public function getCFSlice($keyID, $keySpace, $cfName, $superName = NULL, $consi
try {
if (is_array($keyID)) {
return $client->multiget_slice($keySpace, $keyID, $columnParent, $predicate, self::getConsistency($consistencyLevel));
- } else {
+ } else {
return $client->get_slice($keySpace, $keyID, $columnParent, $predicate, self::getConsistency($consistencyLevel));
}
} catch (TException $te) {
@@ -262,6 +284,7 @@ public function getCFSlice($keyID, $keySpace, $cfName, $superName = NULL, $consi
/**
* Grabs locally defined columnfamilies (debug tool)
*/
+ /*
static public function getConfColumnFamilies() {
$conf = self::loadConfigXML();
@@ -275,5 +298,51 @@ static public function getConfColumnFamilies() {
return $columnFamiles;
}
+
+ static public function buildModels() {
+ // check the schemas directory
+ if (file_exists(SCHEMA_PATH)) {
+
+ // Grab config, check our available keyspaces
+ $dir = scandir(SCHEMA_PATH);
+ foreach ($dir as $fileName) {
+ if (preg_match('/^\./', $fileName)) continue;
+ $filePath = SCHEMA_PATH.'/'.$fileName;
+ $schema = NULL;
+
+ list($keySpace, $extension) = explode('.', $fileName);
+
+ $extension = strtolower($extension);
+ if ($extension == 'json') {
+ $c = file_get_contents($filePath);
+ $schema = json_decode(file_get_contents($filePath));
+ } else if ($extension == 'yaml') {
+ if (!function_exists('syck_load')) {
+ throw new RuntimeException('YAML schema found but syck module not supported');
+ } else {
+ $schema = syck_load($filePath);
+ }
+ }
+
+ if ($schema === NULL) {
+ throw new RuntimeException('Schema failed to parse ('.$filePath.')');
+ } else {
+
+ }
+ }
+ } else {
+ throw new RuntimeException('Defined SCHEMA_PATH not found ('.SCHEMA_PATH.')');
+ }
+
+ // Check if syck module is available
+ //if (!function_exists('syck_load')) {
+ //}
+ }
+*/
}
+
+// Setup our capabilities
+PandraCore::setMemcachedAvailable(class_exists('Memcached'));
+
+PandraCore::setAPCAvailable(function_exists('apc_sma_info') && apc_sma_info() !== FALSE);
?>
Oops, something went wrong.

0 comments on commit 4d0a007

Please sign in to comment.