Skip to content
Browse files

moved global defines into class statics

  • Loading branch information...
1 parent 37a275c commit 1970b9cc9ecd196c37cd57b895e5667d8c25db29 @mjpearson committed Jan 30, 2010
View
44 config.php
@@ -18,43 +18,23 @@
// Config xml path for Cassandra
define('CASSANDRA_CONF_PATH', '/usr/local/src/apache-cassandra-incubating-0.5.0/conf/storage-conf.xml');
-// Default Thrift port
-define('PANDRA_PORT_DEFAULT', 9160);
-
-// read/write modes (can be configured independently)
-define('PANDRA_MODE_ACTIVE', 0); // Active client only
-define('PANDRA_MODE_ROUND', 1); // sequentially select configured clients
-define('PANDRA_MODE_ROUND_APC', 1); // sequentially select between interpreter instances w/APC
-define('PANDRA_MODE_RANDOM', 2); // select random node
-
-// Column Family/Column Types (Standard or Super)
-define('PANDRA_STANDARD', 0);
-define('PANDRA_SUPER', 1);
-
-// When loading data from Cassandra, Pandra can either honour the existing columns
-// created in the object 1:1 on load, or create whatever columns exist in the row
-// as needed. This can be overriden for specific CF child objects via setAutoCreate(bool)
-define('PANDRA_DEFAULT_CREATE_MODE', TRUE);
-
-// Consistency level can be overloaded on load or save operations, but this is the default
-define('PANDRA_DEFAULT_CONSISTENCY', cassandra_ConsistencyLevel::ONE);
-
function _pandraAutoLoad($className) {
- if (!preg_match("/^pandra/i", $className)) return;
+ if (!preg_match("/^pandra/i", $className)) return;
+
+ if ($className != 'Pandra') $className = preg_replace('/^pandra/i', '', $className);
- if ($className != 'Pandra') $className = preg_replace('/^pandra/i', '', $className);
+ // class path relative to config
+ $classPath = dirname(__FILE__)."/lib/";
- // class path relative to config
- $classPath = dirname(__FILE__)."/lib/";
+ // class file suffix
+ $cSuffix = ".class.php";
- // class file suffix
- $cSuffix = ".class.php";
+ $classFile = $classPath.$className.$cSuffix;
- $classFile = $classPath.$className.$cSuffix;
- if (file_exists($classFile)) {
- require_once($classFile);
- }
+ if (file_exists($classFile)) {
+ require_once($classFile);
+ }
}
-spl_autoload_register('_pandraAutoload');
+spl_autoload_register('_pandraAutoLoad');
?>
View
2 examples/formvalidator/index.php
@@ -131,7 +131,7 @@ function ajaxResult($fieldName = '', $errorMessages = array()) {
}
} else {
$_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
- $_SESSION['StartTime'] = mktime();
+ $_SESSION['StartTime'] = time();
// init
foreach ($cColumns as $colName) $_POST[$colName] = '';
View
13 lib/Column.class.php
@@ -42,9 +42,12 @@ class PandraColumn extends cassandra_Column {
* @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()) {
+ public function __construct($name, $parentCF = NULL, $typeDef = array()) {
parent::__construct(array('name' => $name));
- $this->setParentCF($parentCF);
+ if ($parentCF instanceof PandraColumnContainer) {
+ $this->setParentCF($parentCF);
+ }
+
$this->typeDef = $typeDef;
}
@@ -84,7 +87,9 @@ public function bindTime($timeOverride = NULL) {
public function setValue($value, $validate = TRUE) {
if ($validate && !empty($this->typeDef)) {
if (!PandraValidator::check($value, $this->name, $this->typeDef, $this->errors)) {
- $this->_parentCF->errors[] = $this->errors[0];
+ if ($this->_parentCF instanceof PandraColumnContainer) {
+ $this->_parentCF->errors[] = $this->errors[0];
+ }
return FALSE;
}
}
@@ -163,7 +168,7 @@ public function save($keyID, $keySpace, $columnFamily, $consistencyLevel = NULL)
public function registerError($errorStr) {
if (!empty($errorStr)) {
array_push($this->errors, $errorStr);
- if ($this->_parentCF !== NULL) $this->_parentCF->registerError($errorStr);
+ if ($this->_parentCF instanceof PandraColumnContainer) $this->_parentCF->registerError($errorStr);
}
}
View
26 lib/ColumnContainer.class.php
@@ -11,7 +11,7 @@
/**
* @abstract
*/
-abstract class PandraColumnContainer implements ArrayAccess {
+abstract class PandraColumnContainer extends ArrayObject {
/* @var this column families name (table name) */
protected $_name = NULL;
@@ -22,9 +22,6 @@
/* @var array container for column objects, indexed to field name */
protected $_columns = array();
- /* @var int column family type (standard or super) */
- const TYPE = PANDRA_STANDARD;
-
/* @var array complete list of errors for this object instance */
public $errors = array();
@@ -38,7 +35,7 @@
protected $_loaded = FALSE;
/* @var bool auto create columns/containers loaded from Cassandra which do not exist in the local container */
- protected $_autoCreate = PANDRA_DEFAULT_CREATE_MODE;
+ protected $_autoCreate = TRUE;
/**
* Constructor, calls init()
@@ -306,16 +303,13 @@ protected function _gsMutable(&$colName) {
* @param string $colName field name to get
* @return string value
*/
- protected function __get($colName) {
+ public function __get($colName) {
if ($this->_gsMutable($colName)) {
- if ($this->_columns[$colName] instanceof PandraColumnContainer && $this->_columns[$colName]->getType() == PANDRA_SUPER) {
- return $this->_columns[$colName];
-
- } else if ($this->_columns[$colName] instanceof PandraColumn) {
+ if ($this->_columns[$colName] instanceof PandraColumn) {
return $this->_columns[$colName]->value;
- } else {
+ } else if ($this->_columns[$colName] instanceof PandraColumnContainer) {
return $this->_columns[$colName];
}
}
@@ -329,7 +323,7 @@ protected function __get($colName) {
* @param string $value value to set for field
* @return bool field set ok
*/
- protected function __set($colName, $value) {
+ public function __set($colName, $value) {
if (!$this->_gsMutable($colName)) {
$this->addColumn($colName);
}
@@ -340,14 +334,6 @@ protected function __set($colName, $value) {
}
/**
- * Helper to determine if containr is a standard or super type
- * @return int PANDRA_SUPER or PANDRA_STANDARD
- */
- public function getType() {
- return constant(get_class($this)."::TYPE");
- }
-
- /**
* accessor, checks if container has been explicitly modified, or it sub columns
* @return <type>
*/
View
2 lib/ColumnFamily.class.php
@@ -11,7 +11,7 @@
/**
* @abstract
*/
-abstract class PandraColumnFamily extends PandraColumnContainer {
+class PandraColumnFamily extends PandraColumnContainer {
/* @var string keyspace (or database) for this column family */
protected $keySpace = NULL;
View
5 lib/ColumnFamilySuper.class.php
@@ -11,10 +11,7 @@
/**
* @abstract
*/
-abstract class PandraColumnFamilySuper extends PandraColumnFamily {
-
- /* @var int column family type (standard or super) */
- const TYPE = PANDRA_SUPER;
+class PandraColumnFamilySuper extends PandraColumnFamily {
/* @var string magic get/set prefix for Super Columns */
const _columnNamePrefix = 'super_';
View
32 lib/Pandra.class.php
@@ -1,4 +1,5 @@
<?php
+
/**
* (c) 2010 phpgrease.net
*
@@ -11,21 +12,28 @@ class Pandra {
static public $lastError = '';
- static private $_consistencyLevel = PANDRA_DEFAULT_CONSISTENCY;
+ static private $_consistencyLevel = cassandra_ConsistencyLevel::ONE;
static private $_nodeConns = array();
static private $_activeNode = NULL;
- static private $readMode = PANDRA_MODE_ACTIVE;
+ const MODE_ACTIVE = 0; // Active client only
+
+ const MODE_ROUND = 1; // sequentially select configured clients
+
+ const MODE_RANDOM = 2; // select random node
+
+ const THRIFT_PORT_DEFAULT = 9160;
+
+ static private $readMode = self::MODE_ACTIVE;
- static private $writeMode = PANDRA_MODE_ACTIVE;
+ static private $writeMode = self::MODE_ACTIVE;
static private $_supportedModes = array(
- PANDRA_MODE_ACTIVE,
- PANDRA_MODE_ROUND,
- //PANDRA_MODE_ROUND_APC,
- PANDRA_MODE_RANDOM,
+ self::MODE_ACTIVE,
+ self::MODE_ROUND,
+ self::MODE_RANDOM,
);
static public function getSupportedModes() {
@@ -95,7 +103,7 @@ static public function disconnectAll() {
* @param int $port TCP port of connecting node
* @return bool connected ok
*/
- static public function connect($connectionID, $host = NULL, $port = PANDRA_PORT_DEFAULT) {
+ static public function connect($connectionID, $host = NULL, $port = self::THRIFT_PORT_DEFAULT) {
try {
// if the connection exists but it is closed, then re-open
if (array_key_exists($connectionID, self::$_nodeConns)) {
@@ -133,19 +141,17 @@ static public function getClient($writeMode = FALSE) {
if (empty(self::$_activeNode)) throw new Exception('Not Connected');
$useMode = ($writeMode) ? self::$writeMode : self::$readMode;
switch ($useMode) {
- // @todo, APC store of activeNode
- case PANDRA_MODE_ROUND_APC :
- case PANDRA_MODE_ROUND :
+ case self::MODE_ROUND :
if (!current(self::$_nodeConns)) reset(self::$_nodeConns);
$curConn = each(self::$_nodeConns);
self::$_activeNode = $curConn['key']; // store current working node
return self::$_nodeConns[self::$_activeNode]['client'];
break;
- case PANDRA_MODE_RANDOM :
+ case self::MODE_RANDOM :
$randConn =& array_rand(self::$_nodeConns);
return self::$_nodeConns[$randConn]['client'];
break;
- case PANDRA_MODE_ACTIVE :
+ case self::MODE_ACTIVE :
default :
return self::$_nodeConns[self::$_activeNode]['client'];
break;
View
3 lib/SuperColumn.class.php
@@ -12,9 +12,6 @@ class PandraSuperColumn extends PandraColumnContainer {
/* @var PandraColumnFamily column family parent reference */
private $_parentCF = NULL;
- /* @var int column family type (standard or super) */
- const TYPE = PANDRA_SUPER;
-
/**
* Supercolumn constructor
* @param string $superName Super Column name

0 comments on commit 1970b9c

Please sign in to comment.
Something went wrong with that request. Please try again.