/** * Loads a SuperColumn for key * * Load will generate RuntimeException if parent column family has not been set ( * * @param string $keyID row key * @param bool $colAutoCreate create columns in the object instance which have not been defined * @param int $consistencyLevel cassandra consistency level * @return bool loaded OK */ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) { if ($this->_parentCF == NULL || !$this->_parentCF instanceof PandraSuperColumnFamily) { throw new RuntimeException('SuperColumn Requires a ColumnFamilySuper parent'); } $this->_parentCF->checkCFState(); $this->_loaded = FALSE; $result = PandraCore::getCFSlice($keyID, $this->_parentCF->getKeySpace(), $this->_parentCF->getName(), $this->getName(), PandraCore::getConsistency($consistencyLevel)); if ($result !== NULL) { $this->init(); $this->_loaded = $this->populate($result, $this->getAutoCreate($colAutoCreate)); if ($this->_loaded) { $this->keyID = $keyID; } } else { $this->registerError(PandraCore::$lastError); } return $this->_loaded; }
public function testSaveLoadDelete() { $keyID = 'PandraColumnTest'; $keySpace = 'Keyspace1'; $columnFamily = 'Standard1'; $value = 'test value'; $this->obj->setValue($value); // Save by explicitly setting keyid/keyspace/cf $this->obj->setKeyID($keyID); $this->obj->setKeyspace($keySpace); $this->obj->setColumnFamilyName($columnFamily); $this->assertTrue($this->obj->isModified()); $this->assertTrue($this->obj->save(), $this->obj->getLastError()); //$column = array_pop(PandraCore::getCFSlice($keySpace, $keyID, $columnFamily))->column; $this->assertTrue($this->obj->load()); $column = $this->obj; $this->assertTrue($column->value == $value && $column->name == $this->obj->getName() && empty(PandraCore::$lastError), PandraCore::$lastError); $this->obj->delete(); $this->assertTrue($this->obj->isModified() && $this->obj->isDeleted()); $this->assertTrue($this->obj->save(), $this->obj->getLastError()); $columnParent = new cassandra_ColumnParent(array('column_family' => $columnFamily)); $predicate = new PandraSlicePredicate('Column', array('column' => $this->obj->getName())); $result = PandraCore::getCFSlice($keySpace, $keyID, $columnParent, $predicate); $this->assertTrue(empty($result) && empty(PandraCore::$lastError), PandraCore::$lastError); // save using parent $this->obj->setKeyID(NULL); $this->obj->setKeyspace(NULL); $this->obj->setColumnFamilyName(NULL); $parent = clone $this->parent; // Wait so we don't have a delete/insert timestamp in the same second (32 bit systems) if (PHP_INT_SIZE == 4) { sleep(1); } $parent->setKeyID($keyID); $parent->setKeySpace($keySpace); $parent->setName($columnFamily); $this->obj->setParent($parent); $value = 'new test value'; $this->obj->setValue($value); $this->assertTrue($this->obj->isModified()); $this->assertTrue($this->obj->save(), $this->obj->getLastError()); $columnParent = new cassandra_ColumnParent(array('column_family' => $columnFamily)); $predicate = new PandraSlicePredicate('Column', array('column' => $this->obj->getName())); $column = array_pop(PandraCore::getCFSlice($keySpace, $keyID, $columnParent, $predicate))->column; $this->assertTrue($column->value == $value && ($column->name = $this->obj->name && empty(PandraCore::$lastError)), PandraCore::$lastError); $this->obj->delete(); $this->assertTrue($this->obj->isModified() && $this->obj->isDeleted()); $this->assertTrue($this->obj->save(), $this->obj->getLastError()); }
// Make sure the same two characters don't appear next to each other if ($r != $string[$i - 1]) { $string .= $r; } } // Return the string return $string; } // generate 5 timestamped supercolumns for ($i = 1; $i <= 5; $i++) { $bp = new BlogPost(UUID::v1()); $bp->column_title = rand_str(); $bp->column_body = rand_str(); $scf->addSuper($bp); } echo 'Saving SuperColumnFamily...<br>'; print_r($scf->toJSON()); $scf->save(); // get slice of the 5 most recent entries (count = 5, reversed = true) echo '<br><br>Loading via SuperColumnFamily container...<br>'; $scNew = new PandraSuperColumnFamily($keyID, $ks, $cfName, PandraColumnContainer::TYPE_UUID); $scNew->limit(5)->load(); echo '<br>Loaded...<br>'; print_r($scNew->toJSON()); echo '<br><br>Loading SuperColumn Slice...<br>'; $result = PandraCore::getCFSlice($ks, $keyID, new cassandra_ColumnParent(array('column_family' => $cfName)), new PandraSlicePredicate(PandraSlicePredicate::TYPE_RANGE, array('start' => '', 'finish' => '', 'count' => 5, 'reversed' => true))); $scNew = new PandraSuperColumnFamily($keyID, $ks, $cfName, PandraColumnContainer::TYPE_UUID); var_dump($result); $scNew->populate($result); echo '<br>Imported...<br>'; print_r($scNew->toJSON());
public function memcached() { if (!PandraCore::getMemcachedvailable()) { PandraLog::warn('Memcached Unavailable'); } else { $this->_cacheScheme = self::CACHE_MEM; } return $this; }
<?php /** * (c) 2010 phpgrease.net * * @author Michael Pearson <*****@*****.**> */ /** * Example dynamic Address Book construct using Column Family (Super) and Super Columns */ error_reporting(E_ALL); require_once dirname(__FILE__) . '/../config.php'; if (!PandraCore::auto('localhost')) { die(PandraCore::$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
/** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. * * @access protected */ protected function tearDown() { PandraCore::disconnectAll(); }
<?php /** * (c) 2010 phpgrease.net * * @author Michael Pearson <*****@*****.**> */ /** * 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 (!PandraCore::connect('default', 'localhost')) { die(PandraCore::$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'); } }
/** * Save this column family and any modified columns to Cassandra * @param cassandra_ColumnPath $columnPath * @param int $consistencyLevel Cassandra consistency level * @return bool save ok */ public function save($consistencyLevel = NULL) { $ok = $this->pathOK(); if ($ok) { if ($this->getDelete()) { $columnPath = new cassandra_ColumnPath(); $columnPath->column_family = $this->getName(); $ok = PandraCore::deleteColumnPath($this->getKeySpace(), $this->getKeyID(), $columnPath, NULL, PandraCore::getConsistency($consistencyLevel)); if (!$ok) { $this->registerError(PandraCore::$lastError); } } else { // @todo have this use thrift batch_insert method in core $modifiedColumns = $this->getModifiedColumns(); foreach ($modifiedColumns as &$cObj) { if (!$cObj->save(PandraCore::getConsistency($consistencyLevel))) { $this->registerError($cObj->getLastError()); return FALSE; } } $ok = TRUE; } if ($ok) { $this->reset(); } } return $ok; }
/** * Saves this individual column path * @param string $keyID row key * @param sring $keySpace key space * @param string $columnFamily column family name * @param int $consistencyLevel cassandra save consistency level * @return bool save ok */ public function save($keyID, $keySpace, $columnFamily, $consistencyLevel = NULL) { if (!$this->isModified()) { return TRUE; } // Build the column path for modifying this individual column $columnPath = new cassandra_ColumnPath(); $columnPath->column_family = $columnFamily; $columnPath->column = $this->name; $ok = FALSE; if ($this->_delete) { $ok = PandraCore::deleteColumnPath($keySpace, $keyID, $columnPath, $this->bindTime(), PandraCore::getConsistency($consistencyLevel)); } else { $ok = PandraCore::saveColumnPath($keySpace, $keyID, $columnPath, $this->callback === NULL ? $this->value : $this->callbackvalue(), $this->bindTime(), PandraCore::getConsistency($consistencyLevel)); } if (!$ok) { if (empty(PandraCore::$errors)) { $errorStr = 'Unkown Error'; } else { $errorStr = PandraCore::$errors; } $this->registerError($errorStr); } if ($ok) { $this->reset(); } return $ok; }
/** * Loads a SuperColumn for key * * @param string $keyID optional row key * @param int $consistencyLevel cassandra consistency level * @return bool loaded OK */ public function load($keyID = NULL, $consistencyLevel = NULL) { if ($keyID === NULL) { $keyID = $this->getKeyID(); } $ok = $this->pathOK($keyID); $this->setLoaded(FALSE); if ($ok) { $autoCreate = $this->getAutoCreate(); $predicate = new cassandra_SlicePredicate(); // if autocreate is turned on, get latest limited everything if ($autoCreate) { $predicate->slice_range = new cassandra_SliceRange(); $predicate->slice_range->start = ''; $predicate->slice_range->finish = ''; $predicate->slice_range->count = $this->getLimit(); $predicate->slice_range->reversed = TRUE; $result = PandraCore::getCFSlice($this->getKeySpace(), $keyID, new cassandra_ColumnParent(array('column_family' => $this->getColumnFamilyName(), 'super_column' => $this->getName())), $predicate, PandraCore::getConsistency($consistencyLevel)); // otherwise by defined columns (slice query) } else { $predicate->column_names = $this->getColumnNames(); $result = PandraCore::getCFSliceMulti($this->getKeySpace(), array($keyID), $predicate, new cassandra_ColumnParent(array('column_family' => $this->getColumnFamilyName(), 'super_column' => $this->getName())), PandraCore::getConsistency($consistencyLevel)); $result = $result[$keyID]; } if (!empty($result)) { $this->init(); $this->setLoaded($this->populate($result, $autoCreate)); if ($this->isLoaded()) { $this->setKeyID($keyID); } } else { $this->registerError(PandraCore::$lastError); } } return $ok && $this->isLoaded(); }
/** * Gets complete slice of Thrift cassandra_Column objects for keyID * * @return array cassandra_Column objects */ public function getCFSlice($keyID, $keySpace, $cfName, $superName = NULL, $consistencyLevel = NULL) { $client = self::getClient(); // build the column path $columnParent = new cassandra_ColumnParent(); $columnParent->column_family = $cfName; $columnParent->super_column = $superName; $predicate = new cassandra_SlicePredicate(); $predicate->slice_range = new cassandra_SliceRange(); $predicate->slice_range->start = ''; $predicate->slice_range->finish = ''; try { if (is_array($keyID)) { return $client->multiget_slice($keySpace, $keyID, $columnParent, $predicate, self::getConsistency($consistencyLevel)); } else { return $client->get_slice($keySpace, $keyID, $columnParent, $predicate, self::getConsistency($consistencyLevel)); } } catch (TException $te) { var_dump($te); self::$lastError = 'TException: ' . $te->getMessage() . "\n"; return NULL; } return NULL; }
/** * Loads an entire columnfamily by keyid * @param string $keyID optional row key * @param int $consistencyLevel cassandra consistency level * @return bool loaded OK */ public function load($keyID = NULL, $consistencyLevel = NULL) { if ($keyID === NULL) { $keyID = $this->getKeyID(); } $ok = $this->pathOK($keyID); $this->setLoaded(FALSE); if ($ok) { $autoCreate = $this->getAutoCreate(); $predicate = new cassandra_SlicePredicate(); // if autocreate is turned on, get latest limited everything if ($autoCreate) { $predicate->slice_range = new cassandra_SliceRange(); $predicate->slice_range->start = ''; $predicate->slice_range->finish = ''; $predicate->slice_range->count = $this->getLimit(); $predicate->slice_range->reversed = TRUE; $result = PandraCore::getCFSlice($this->getKeySpace(), $keyID, new cassandra_ColumnParent(array('column_family' => $this->getName())), $predicate, PandraCore::getConsistency($consistencyLevel)); // otherwise by defined columns (slice query) } else { $predicate->column_names = $this->getColumnNames(); $result = PandraCore::getCFSliceMulti($this->getKeySpace(), array($keyID), new cassandra_ColumnParent(array('column_family' => $this->getName())), $predicate, PandraCore::getConsistency($consistencyLevel)); $result = $result[$keyID]; } if ($result !== NULL) { $this->init(); foreach ($result as $superColumn) { $sc = $superColumn->super_column; $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 { $this->setLoaded(FALSE); break; } } if ($this->isLoaded()) { $this->setKeyID($keyID); } } else { $this->registerError(PandraCore::$lastError); } } return $ok && $this->isLoaded(); }
/** * Saves this individual column path, where a parent has been set (setParent()) keyid, keyspace, columnfamily or supercolumn * will be inherited for the save. * @return bool save ok */ public function save($consistencyLevel = NULL) { if (!$this->isModified()) { $this->registerError("Column " . $this->name . " is not modified"); return FALSE; } // Build the column path for modifying this individual column $columnPath = new cassandra_ColumnPath(); $columnPath->column_family = $this->getColumnFamilyName(); $columnPath->super_column = $this->getSuperColumnName(); $columnPath->column = $this->getName(); $ok = FALSE; if ($this->isDeleted()) { $ok = PandraCore::deleteColumnPath($this->getKeySpace(), $this->getKeyID(), $columnPath, $this->bindTime(), PandraCore::getConsistency($consistencyLevel)); } else { $ok = PandraCore::saveColumnPath($this->getKeySpace(), $this->getKeyID(), $columnPath, $this->callbackvalue(), $this->bindTime(), PandraCore::getConsistency($consistencyLevel)); } if (!$ok) { if (empty(PandraCore::$lastError)) { $errorStr = 'Unknown Error'; } else { $errorStr = PandraCore::$lastError; } $this->registerError($errorStr); } if ($ok) { $this->reset(); } return $ok; }
* @package pandra */ define('PANDRA_64', PHP_INT_SIZE == 8); function _pandraAutoLoad($className) { // seperate classes and interfaces for clarity $fExt = array('.class.php', '.interface.php'); // strip prefix $className = preg_replace('/^pandra/i', '', $className); // class path relative to config $classPath = dirname(__FILE__) . "/"; if (preg_match('/^(Query|Clause)/', $className)) { $classPath .= 'query/'; } elseif (preg_match('/^Log/', $className)) { $classPath .= 'logging/'; } elseif (preg_match('/^UUID/', $className)) { $classPath .= 'uuid/'; } foreach ($fExt as $ext) { $classFile = $classPath . $className . $ext; if (file_exists($classFile)) { require_once $classFile; break; } } } spl_autoload_register('_pandraAutoLoad'); // Setup our capabilities PandraCore::setMemcachedAvailable(class_exists('Memcached')); PandraCore::setAPCAvailable(function_exists('apc_sma_info') && apc_sma_info() !== FALSE);
public function getSetConsistency() { $newC = cassandra_ConsistencyLevel::QUORUM; PandraCore::setConsistency($newC); $this->assertTrue(PandraCore::getConsistency() == $newC); $this->assertTrue(PandraCore::getConsistency(cassandra_ConsistencyLevel::ONE) == cassandra_ConsistencyLevel::ONE); }
/** * consistency mutator * @param int $consistencyLevel new consistency level */ public static function setConsistency($consistencyLevel) { self::$_consistencyLevel = $consistencyLevel; }
/** * Save this column family and any modified columns to Cassandra * @param cassandra_ColumnPath $columnPath * @param int $consistencyLevel Cassandra consistency level * @return bool save ok */ public function save($consistencyLevel = NULL) { $this->checkCFState(); $ok = FALSE; if ($this->getDelete()) { $columnPath = new cassandra_ColumnPath(); $columnPath->column_family = $this->getName(); $ok = PandraCore::deleteColumnPath($this->getKeySpace(), $this->keyID, $columnPath, time()); if (!$ok) { $this->registerError(PandraCore::$lastError); } } else { foreach ($this->_columns as &$cObj) { if (!$cObj->isModified()) { continue; } if (!$cObj->save($this->keyID, $this->getKeySpace(), $this->getName(), PandraCore::getConsistency($consistencyLevel))) { $this->registerError($cObj->getLastError()); return FALSE; } } $ok = TRUE; } if ($ok) { $this->reset(); } return $ok; }
/** * Loads an entire columnfamily by keyid * @param string $keyID row key * @param bool $colAutoCreate create columns in the object instance which have not been defined * @param int $consistencyLevel cassandra consistency level * @return bool loaded OK */ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) { $this->_loaded = FALSE; $result = PandraCore::getCFSlice($keyID, $this->getKeySpace(), $this->getName(), NULL, PandraCore::getConsistency($consistencyLevel)); if ($result !== NULL) { $this->init(); foreach ($result as $superColumn) { $sc = $superColumn->super_column; // @todo Should at least 1 successful super load really indicate a successful load state? $this->_loaded = $this->addSuper(new PandraSuperColumn($sc->name))->populate($sc->columns, $this->getAutoCreate($colAutoCreate)); } if ($this->_loaded) { $this->setKeyID($keyID); } } else { $this->registerError(PandraCore::$lastError); } return $this->_loaded; }