Permalink
Browse files

tweakable auto create mode

  • Loading branch information...
1 parent 5f09761 commit 4a1072d916bd8b546af208087aa551bd272ab438 @mjpearson committed Jan 26, 2010
Showing with 43 additions and 15 deletions.
  1. +4 −4 README
  2. +4 −1 config.php
  3. +29 −3 lib/ColumnContainer.class.php
  4. +2 −2 lib/ColumnFamily.class.php
  5. +2 −2 lib/ColumnFamilySuper.class.php
  6. +0 −1 lib/Pandra.class.php
  7. +2 −2 lib/SuperColumn.class.php
View
8 README
@@ -44,12 +44,12 @@ The Pandra homepage is http://www.phpgrease.net/projects/Pandra
* Flexible arrangement of Keyspaces, Column Families and Super Columns (implements ArrayAccess)
- * Hard schema aware, or build factory objects on the fly
+ * Schema aware, or build factory objects on the fly
- Dependencies
- * Cassandra >=0.4
+ * Cassandra >= 0.5.0
- * Thrift Interface (thrift binary + cassandra.thrift, or pre generated php sources)
+ * Thrift Interface (cassandra.thrift and pre-generated files are packaged)
- * PHP >=5.2 (requires APC module)
+ * PHP >= 5.3
View
@@ -31,11 +31,14 @@
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;
@@ -37,6 +37,9 @@
/* @var bool container columns have been loaded from Cassandra */
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;
+
/**
* Constructor, calls init()
*/
@@ -228,11 +231,34 @@ public function destroyColumn($colName) {
}
/**
+ * Get working autocreate mode (either this set autocreate or overriden)
+ * @param bool $override
+ * @return bool working autocreate mode
+ */
+ public function getAutoCreate($override = NULL) {
+ $autoCreate = $this->_autoCreate;
+
+ if ($override !== NULL) {
+ $autoCreate = $override;
+ }
+
+ return $autoCreate;
+ }
+
+ /**
+ * autoCreate mutator
+ * @param bool $autoCreate new mode
+ */
+ public function setAutoCreate(bool $autoCreate) {
+ $this->$_autoCreate = $autoCreate;
+ }
+
+ /**
* Populates container object (ColumnFamily, ColumnFamilySuper or SuperColumn)
* @param mixed $data associative string array, array of cassandra_Column's or JSON string of key => values.
* @return bool column values set without error
*/
- public function populate($data, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE) {
+ public function populate($data, $colAutoCreate = NULL) {
if (is_string($data)) {
$data = json_decode($data, TRUE);
}
@@ -241,14 +267,14 @@ public function populate($data, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE) {
foreach ($data as $idx => $colValue) {
if ($colValue instanceof cassandra_Column) {
- if ($colAutoCreate || array_key_exists($colValue->name, $this->_columns)) {
+ if ($this->getAutoCreate($colAutoCreate) || array_key_exists($colValue->name, $this->_columns)) {
$this->_columns[$colValue->name] = PandraColumn::cast($colValue, $this);
}
} else {
$colExists = array_key_exists($idx, $this->_columns);
// Create a new named column object
- if ($colAutoCreate && !array_key_exists($idx, $this->_columns)) {
+ if ($this->getAutoCreate($colAutoCreate) && !array_key_exists($idx, $this->_columns)) {
$this->addColumn($idx);
}
@@ -73,15 +73,15 @@ public function isDeleted() {
* @param int $consistencyLevel cassandra consistency level
* @return bool loaded OK
*/
- public function load($keyID, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE, $consistencyLevel = NULL) {
+ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
$this->_loaded = FALSE;
$result = Pandra::getCFSlice($keyID, $this->getKeySpace(), $this->getName(), NULL, Pandra::getConsistency($consistencyLevel));
if ($result !== NULL) {
$this->init();
- $this->_loaded = $this->populate($result, $colAutoCreate);
+ $this->_loaded = $this->populate($result, $this->getAutoCreate($colAutoCreate));
if ($this->_loaded) $this->setKeyID($keyID);
} else {
$this->registerError(Pandra::$lastError);
@@ -105,7 +105,7 @@ public function save($consistencyLevel = NULL) {
* @param int $consistencyLevel cassandra consistency level
* @return bool loaded OK
*/
- public function load($keyID, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE, $consistencyLevel = NULL) {
+ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
$this->_loaded = FALSE;
@@ -117,7 +117,7 @@ public function load($keyID, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE, $consi
$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, PANDRA_DEFAULT_CREATE_MODE);
+ $this->_loaded = $this->addSuper(new PandraSuperColumn($sc->name))->populate($sc->columns, $this->getAutoCreate($colAutoCreate));
}
if ($this->_loaded) $this->setKeyID($keyID);
View
@@ -164,7 +164,6 @@ static public function getConsistency($override = NULL) {
}
return $consistency;
-
}
static public function setConsistency(int $consistencyLevel) {
@@ -84,7 +84,7 @@ public function save($consistencyLevel = NULL) {
* @param int $consistencyLevel cassandra consistency level
* @return bool loaded OK
*/
- public function load($keyID, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE, $consistencyLevel = NULL) {
+ public function load($keyID, $colAutoCreate = NULL, $consistencyLevel = NULL) {
if ($this->_parentCF == NULL || !($this->_parentCF instanceof PandraColumnFamilySuper)) throw new RuntimeException('SuperColumn Requires a ColumnFamilySuper parent');
@@ -96,7 +96,7 @@ public function load($keyID, $colAutoCreate = PANDRA_DEFAULT_CREATE_MODE, $consi
if ($result !== NULL) {
$this->init();
- $this->_loaded = $this->populate($result, $colAutoCreate);
+ $this->_loaded = $this->populate($result, $this->getAutoCreate($colAutoCreate));
if ($this->_loaded) $this->keyID = $keyID;
} else {
$this->registerError(Pandra::$lastError);

0 comments on commit 4a1072d

Please sign in to comment.