Skip to content
Browse files

abstracted ossp uuid constants into uuid class (so can be swapped out…

… for another implementation)
  • Loading branch information...
1 parent 9dbf88b commit 5d9e657f344dd36d2d26657772270c84ba2152dc @mjpearson committed Apr 5, 2010
Showing with 59 additions and 107 deletions.
  1. +22 −12 README
  2. +3 −2 config.php
  3. +10 −10 lib/ColumnContainer.class.php
  4. +6 −72 lib/Core.class.php
  5. +1 −1 lib/SlicePredicate.class.php
  6. +4 −4 lib/SuperColumnFamily.class.php
  7. +13 −6 lib/ext/UUID.class.php
View
34 README
@@ -15,18 +15,6 @@ Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Community
-
- The Pandra homepage is http://www.phpgrease.net/projects/Pandra
-
- For feedback, bugs and general support discussion see http://groups.google.com/group/pandra-user
-
- Developers : http://groups.google.com/group/pandra-dev
-
- Github users may also log bugs directly via http://github.com/mjpearson/pandra/issues
-
- Thankyou for your support.
-
- What is it?
Pandra (PHP-Cassandra) is a light-weight PHP based create/read/update/delete (CRUD) layer for the Cassandra distributed datastore.
@@ -50,6 +38,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* Extensible type definition, validation and callback layers
+ * UUID Column Families
+
* Flexible arrangement of Keyspaces, Column Families and Super Columns (implements ArrayAccess)
* Schema aware, or build factory objects on the fly
@@ -63,3 +53,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* Thrift Interface (tested cassandra.thrift and pre-generated files are packaged)
* PHP >= 5.3 (APC and Memcached suggested)
+
+ * OSSP PHP-UUID module
+
+ * scyk yaml (optional)
+
+- Download
+
+ Latest snapshot : git checkout git://github.com/mjpearson/Pandra.git .
+ - or -
+ Latest stable tag : http://github.com/mjpearson/Pandra/tarball/0.1
+
+- Community
+
+ The Pandra homepage is http://www.phpgrease.net/projects/Pandra
+
+ For feedback, bugs and general support discussion see http://groups.google.com/group/pandra-user
+
+ Developers : http://groups.google.com/group/pandra-dev
+
+ Github users may also log bugs directly via http://github.com/mjpearson/pandra/issues
View
5 config.php
@@ -21,8 +21,8 @@
define('CASSANDRA_CONF_PATH', '/usr/local/src/apache-cassandra-incubating-0.5.0/conf/storage-conf.xml');
define('THRIFT_PORT_DEFAULT', 9160);
-
-define('PANDRA_64', PHP_INT_SIZE == 8);
+define('DEFAULT_ROW_LIMIT', 10);
+define('PERSIST_CONNECTIONS', FALSE); // TSocket Persistence
// horrendous autoloader in the absense of namespace
function _pandraAutoLoad($className) {
@@ -55,4 +55,5 @@ function _pandraAutoLoad($className) {
}
}
spl_autoload_register('_pandraAutoLoad');
+define('PANDRA_64', PHP_INT_SIZE == 8);
?>
View
20 lib/ColumnContainer.class.php
@@ -53,7 +53,7 @@
protected $_containerType = self::TYPE_STRING;
- protected $_rangeLimit = PandraCore::DEFAULT_ROW_LIMIT;
+ protected $_rangeLimit = DEFAULT_ROW_LIMIT;
/**
* CF constructor, calls init()
@@ -348,7 +348,7 @@ public function getLimit() {
/**
* Converts the given column name to it's expected container type context (UUID or String)
* @param string $columnName column name
- * @param int $toFmt convert to type (UUID_FMT_BIN, UUID_FMT_STR)
+ * @param int $toFmt convert to type (UUID::UUID_FMT_BIN, UUID::UUID_FMT_STR)
* @return mixed converted column name
*/
protected function typeConvert($columnName, $toFmt) {
@@ -359,8 +359,8 @@ protected function typeConvert($columnName, $toFmt) {
$bin = UUID::isBinary($columnName);
// Save accidental double-conversions on binaries
- if (($bin && $toFmt == UUID_FMT_BIN) ||
- (!$bin && $toFmt == UUID_FMT_STR)) {
+ if (($bin && $toFmt == UUID::UUID_FMT_BIN) ||
+ (!$bin && $toFmt == UUID::UUID_FMT_STR)) {
return $columnName;
} elseif (!$bin && !UUID::validUUID($columnName)) {
throw new RuntimeException('Column Name ('.$columnName.') cannot be converted');
@@ -380,7 +380,7 @@ public function addColumn($columnName, $typeDef = array(), $callbackOnSave = NUL
if (!array_key_exists($columnName, $this->_columns)) {
$this->_columns[$columnName] =
- new PandraColumn($this->typeConvert($columnName, UUID_FMT_BIN), $typeDef);
+ new PandraColumn($this->typeConvert($columnName, UUID::UUID_FMT_BIN), $typeDef);
$this->_columns[$columnName]->setParent($this, FALSE);
}
@@ -396,7 +396,7 @@ public function addColumn($columnName, $typeDef = array(), $callbackOnSave = NUL
*/
public function addColumnObj(PandraColumn $columnObj) {
if ($columnObj->getName() === NULL) throw new RuntimeException('Column has no name');
- $this->_columns[$this->typeConvert($columnObj->name, UUID_FMT_STR)] = $columnObj;
+ $this->_columns[$this->typeConvert($columnObj->name, UUID::UUID_FMT_STR)] = $columnObj;
}
/**
@@ -450,7 +450,7 @@ public function getColumnNames() {
*/
public function columnIn($columnName) {
if (UUID::isBinary($columnName)) {
- $columnName = UUID::convert($columnName, UUID_FMT_STR);
+ $columnName = UUID::convert($columnName, UUID::UUID_FMT_STR);
}
return array_key_exists($columnName, $this->_columns);
@@ -556,15 +556,15 @@ public function populate($data, $colAutoCreate = NULL) {
// Check depth, take first few keys as keyspace/columnfamily/key
foreach ($data as $idx => $column) {
if ($column instanceof cassandra_Column) {
- $columnName = $this->typeConvert($column->name, UUID_FMT_STR);
+ $columnName = $this->typeConvert($column->name, UUID::UUID_FMT_STR);
if ($this->getAutoCreate($colAutoCreate) || array_key_exists($columnName, $this->_columns)) {
$this->_columns[$columnName] = PandraColumn::cast($column, $this);
}
// circular dependency?
} elseif ($column instanceof cassandra_ColumnOrSuperColumn && !empty($column->column)) {
- $columnName = $this->typeConvert($column->column->name, UUID_FMT_STR);
+ $columnName = $this->typeConvert($column->column->name, UUID::UUID_FMT_STR);
if ($this->getAutoCreate($colAutoCreate) || array_key_exists($columnName, $this->_columns)) {
$this->_columns[$columnName] = PandraColumn::cast($column->column, $this);
@@ -718,7 +718,7 @@ public function toArray($keyPath = FALSE) {
$retArr = array();
foreach ($this->_columns as $columnName => $column) {
- //$columnName = $this->typeConvert($column->getName(), UUID_FMT_STR);
+ //$columnName = $this->typeConvert($column->getName(), UUID::UUID_FMT_STR);
if ($column instanceof PandraColumn) {
$retArr[$columnName] = $column->value;
View
78 lib/Core.class.php
@@ -19,12 +19,8 @@ class PandraCore {
const MODE_RANDOM = 2; // select random node
- const DEFAULT_ROW_LIMIT = 100; // default max # of rows to return for ranging queries
-
const DEFAULT_POOL_NAME = 'default';
- const PERSIST_CONNECTIONS = FALSE; // TSocket Persistence
-
/* @var string Last internal error */
static public $lastError = '';
@@ -70,7 +66,9 @@ class PandraCore {
/* @var PandraCore instance of self */
static private $_instance = NULL;
- /* @var dummy constructor */
+ /**
+ * dummy constructor
+ */
private function __construct() {
}
@@ -212,7 +210,7 @@ static public function connect($connectionID, $host, $poolName = self::DEFAULT_P
if (!array_key_exists($poolName, self::$_socketPool)) self::$_socketPool[$poolName] = array();
// Create Thrift transport and binary protocol cassandra client
- $transport = new TBufferedTransport(new TSocket($host, $port, self::PERSIST_CONNECTIONS, 'PandraCore::registerError'), 1024, 1024);
+ $transport = new TBufferedTransport(new TSocket($host, $port, PERSIST_CONNECTIONS, 'PandraCore::registerError'), 1024, 1024);
$transport->open();
self::$_socketPool[$poolName][$connectionID] = array(
@@ -660,7 +658,7 @@ static public function getRangeKeys($keySpace,
array $keyRange,
cassandra_ColumnParent $columnParent,
cassandra_SlicePredicate $predicate,
- $numRows = self::DEFAULT_ROW_LIMIT,
+ $numRows = DEFAULT_ROW_LIMIT,
$consistencyLevel = NULL) {
$client = self::getClient();
@@ -678,71 +676,7 @@ static public function getRangeKeys($keySpace,
return NULL;
}
- }
-
-
- /**
- * Grabs locally defined columnfamilies (debug tool)
- */
-
- /*
- * @TODO!!!!!!!
- *
- *
- static public function getConfColumnFamilies() {
-
- $conf = self::loadConfigXML();
-
- $columnFamiles = array();
-
- foreach ($conf->Keyspaces as $keySpace) {
- $ksName = $keySpace->attributes()->Name;
- $columnFamilies[] = $keySpace->xpath('Keyspace/ColumnFamily');
- }
-
- 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
View
2 lib/SlicePredicate.class.php
@@ -74,7 +74,7 @@ public function buildPredicateAttribute(array $predicateAttribute) {
array('start' => '',
'finish' => '',
'reversed' => false,
- 'count' => PandraCore::DEFAULT_ROW_LIMIT)
+ 'count' => DEFAULT_ROW_LIMIT)
);
foreach ($predicateAttribute as $key => $value) {
View
8 lib/SuperColumnFamily.class.php
@@ -44,7 +44,7 @@ public function addSuper(PandraSuperColumn $scObj) {
$superName = $scObj->getName();
if ($this->getType() == self::TYPE_UUID && !UUID::isBinary($scObj->getName())) {
- $scObj->setName(UUID::convert($scObj->getName(), UUID_FMT_BIN));
+ $scObj->setName(UUID::convert($scObj->getName(), UUID::UUID_FMT_BIN));
}
$scObj->setParent($this, false);
@@ -65,7 +65,7 @@ public function addColumn($superName, $containerType = NULL) {
if (!array_key_exists($superName, $this->_columns)) {
$this->_columns[$superName] = new PandraSuperColumn(
//$superName,
- $this->typeConvert($superName, UUID_FMT_BIN),
+ $this->typeConvert($superName, UUID::UUID_FMT_BIN),
$this->getKeyID(),
$this->getKeySpace(),
$this,
@@ -195,7 +195,7 @@ public function load($keyID = NULL, $consistencyLevel = NULL) {
$this->init();
foreach ($result as $superColumn) {
$sc = $superColumn->super_column;
- $newSuper = new PandraSuperColumn($this->typeConvert($sc->name, UUID_FMT_STR), NULL, NULL, $this, $this->getType());
+ $newSuper = new PandraSuperColumn($this->typeConvert($sc->name, UUID::UUID_FMT_STR), NULL, NULL, $this, $this->getType());
if ($this->addSuper($newSuper)->populate($sc->columns, $autoCreate)) {
$this->setLoaded(TRUE);
} else {
@@ -234,7 +234,7 @@ public function populate($data, $colAutoCreate = NULL) {
}
} elseif ($colValue instanceof cassandra_ColumnOrSuperColumn && !empty($colValue->super_column)) {
- $columnName = $this->typeConvert($colValue->super_column->name, UUID_FMT_STR);
+ $columnName = $this->typeConvert($colValue->super_column->name, UUID::UUID_FMT_STR);
if ($this->getAutoCreate($colAutoCreate) || array_key_exists($columnName, $this->_columns)) {
$this->addSuper(new PandraSuperColumn($columnName))->populate($colValue->super_column->columns);
View
19 lib/ext/UUID.class.php
@@ -2,8 +2,7 @@
/**
* Generates v1 (Timestamp) and v5 (Lexical, SHA-1) UUID's for use with Cassandra
*
- * Core is a gracefully degrading static connection manager, Thrift API helper and
- * cache manager
+ * Requires OSSP PHP-UUID module
*
* @author Michael Pearson <pandra-support@phpgrease.net>
* @author Marius Karthaus
@@ -13,6 +12,14 @@ class UUID {
public static $_uuid;
+ const UUID_FMT_STR = UUID_FMT_STR;
+
+ const UUID_FMT_BIN = UUID_FMT_BIN;
+
+ const UUID_MAKE_V1 = UUID_MAKE_V1;
+
+ const UUID_MAKE_V5 = UUID_MAKE_V5;
+
private static function instance() {
if (!is_resource(self::$_uuid)) {
uuid_create(&self::$_uuid);
@@ -29,26 +36,26 @@ public static function generate() {
* @return string
*/
public static function v1() {
- return self::_generate(UUID_MAKE_V1);
+ return self::_generate(self::UUID_MAKE_V1);
}
/**
* returns a type 5 (SHA-1 hash) uuid
* @return string
*/
public static function v5() {
- return self::_generate(UUID_MAKE_V5);
+ return self::_generate(self::UUID_MAKE_V5);
}
private static function _generate($type) {
uuid_make ( self::instance(), $type );
- uuid_export ( self::instance(), UUID_FMT_STR, &$uuidstring );
+ uuid_export ( self::instance(), self::UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
public static function convert($uuid, $toFmt) {
$uuidConv = $uuid;
- $fromFmt = self::isBinary($uuid) ? UUID_FMT_BIN : UUID_FMT_STR;
+ $fromFmt = self::isBinary($uuid) ? self::UUID_FMT_BIN : self::UUID_FMT_STR;
uuid_import(&self::$_uuid, $fromFmt, $uuid);
uuid_export(self::instance(), $toFmt, &$uuidConv);
return $uuidConv;

0 comments on commit 5d9e657

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