Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored php mnam

  • Loading branch information...
commit 9bc531e8662aa6c80de0e1e2bc2163f4357a236f 1 parent 6455736
@jeffgirard authored
Showing with 163 additions and 66 deletions.
  1. +36 −66 php/Mnam.php
  2. +88 −0 php/MnamCass.php
  3. +39 −0 php/MnamGroup.php
View
102 php/Mnam.php
@@ -1,39 +1,29 @@
<?php
+require 'MnamCass.php';
/**
+ *
* Mnam: Messaging, not a mess!
* PHP library port
*
+ * Static class to send message into specific group, and get it written into Cassandra
+ *
* @author jeffgirard
*
*/
-
class Mnam{
- private static $_MnamInst;
- public $_config;
- private $_cassandraInst;
-
- protected function __construct()
- {
- $this->_LoadConfiguration();
- $this->_LoadAndConnectToCass();
- }
-
- public function getCassandra()
- {
- return $this->_cassandraInst;
- }
+ private static $_MnamCass;
/**
* Will create Cassandra required columnFamily required for a group
* @param string $groupName
* @param Array $columns
*/
- public static function InitGroup($groupName, Array $columns)
+ public static function InitGroup($keyspace, $groupName, Array $columns)
{
self::_Init();
- self::$_MnamInst->getCassandra()->createStandardColumnFamily(self::$_MnamInst->_config['default']['keyspace'], $groupName, $columns);
+ self::$_MnamCass->getCassandra()->createStandardColumnFamily($keyspace, $groupName, $columns);
}
/**
@@ -48,7 +38,12 @@ public static function Write($groupName, $key, Array $fields)
if(is_array($key)) $key = implode('.', $key);
- self::$_MnamInst->getCassandra()->set("{$groupName}.{$key}", $fields);
+// if(self::$_MnamCass->getCassandra()->getConnection()->isOpen()){
+ self::$_MnamCass->getCassandra()->set("{$groupName}.{$key}", $fields);
+
+ // }else{
+ // throw new Exception("No fallback implemented yet when cassandra isnt open");
+ // }
}
/**
@@ -74,7 +69,7 @@ public static function Read($groupName, $key)
if(is_array($key)) $key = implode('.', $key);
- return self::$_MnamInst->getCassandra()->get("{$groupName}.{$key}");
+ return self::$_MnamCass->getCassandra()->get("{$groupName}.{$key}");
}
/**
@@ -82,56 +77,11 @@ public static function Read($groupName, $key)
*/
private static function _Init()
{
- if(!self::$_MnamInst) self::$_MnamInst = new self();
- }
-
- /**
- * Load everything needed to work with messages
- * - Load yaml config file
- * - Call Cassandra setup and connection process
- */
- private function _LoadConfiguration()
- {
- # TODO: Yaml config loading
- $this->_config = array(
- 'cassandra' => array(
- 'host' => '127.0.0.1',
- 'port' => 9160),
-
- 'default' => array(
- 'keyspace' => 'mnam'),
-
- 'php' => array(
- 'client_include' => 'cass-php-client/Cassandra.php')
- );
-
- self::_LoadAndConnectToCass();
- }
-
- /**
- * Prepare Cassandra required configuration and connect to node
- */
- private function _LoadAndConnectToCass()
- {
- $cassConf = $this->_config['cassandra'];
- if(!$cassConf) throw new Exception('Missing cassandra config');
-
- $this->_cassandraInst = Cassandra::createInstance(array(
- array(
- 'host' => $cassConf['host'],
- 'port' => $cassConf['port'],
- 'use-framed-transport' => $cassConf['use_framed_transport'],
- 'send-timeout-ms' => $cassConf['send_timeout_ms'],
- 'receive-timeout-ms' => $cassConf['receive_timeout_ms']
- )
- ));
-
- $this->_cassandraInst->useKeyspace($this->_config['default']['keyspace']);
- $this->_cassandraInst->setMaxCallRetries(5);
+ if(!self::$_MnamCass) self::$_MnamCass = new MnamCass();
}
}
-require_once('cass-php-client/Cassandra.php');
+//require_once('cass-php-client/Cassandra.php');
/*Mnam::InitGroup('login',
array(
@@ -142,4 +92,24 @@ private function _LoadAndConnectToCass()
'index-name' => 'NameIdx'
)));*/
+/*Mnam::InitGroup('sold',
+ array(
+ array(
+ 'name' => 'val1',
+ 'type' => Cassandra::TYPE_UTF8,
+ 'index-type' => Cassandra::INDEX_KEYS, // create secondary index
+ 'index-name' => 'val1Idx'
+ ),
+ array(
+ 'name' => 'val2',
+ 'type' => Cassandra::TYPE_UTF8,
+ 'index-type' => Cassandra::INDEX_KEYS, // create secondary index
+ 'index-name' => 'val2Idx'
+ )));*/
+
Mnam::Write('login', 'jeff', array('name' => 'Jeff'));
+Mnam::Write('sold', 'jeff', array('val1' => '1', 'val2' => '2'));
+
+for($i=1;$i<=2000;$i++){
+ Mnam::Write('sold', "jeff{$i}", array('val1' => '1', 'val2' => '2'));
+}
View
88 php/MnamCass.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ *
+ * Mnam: Messaging, not a mess!
+ * PHP library port
+ *
+ * @author jeffgirard
+ *
+ */
+class MnamCass{
+
+ private $_cassInst;
+ private $_configFilePath;
+ private $_config = array();
+
+ public function __construct($configFilePath=null)
+ {
+ $this->_configFilePath = $configFilePath;
+
+ $this->_prepare();
+ $this->_connect();
+ }
+
+ public function getCassandra()
+ {
+ return $this->_cassInst;
+ }
+
+ private function _prepare()
+ {
+ # TODO: Yaml config loading
+ $this->_config = array(
+ 'cassandra' => array(
+ 'host' => '127.0.0.1',
+ 'port' => 9160,
+ 'use_framed_transport' => true,
+ 'send_timeout_ms' => 1000,
+ 'receive_timeout_ms' => 1000),
+
+ 'default' => array(
+ 'keyspace' => 'mnam'),
+
+ 'php' => array(
+ 'client_include' => 'cass-php-client/Cassandra.php')
+ );
+ }
+
+ private function _connect()
+ {
+ $cassConf = $this->_config['cassandra'];
+ if(!$cassConf) throw new Exception('Missing cassandra config');
+
+ require_once($this->_config['php']['client_include']);
+
+ $this->_cassInst = Cassandra::createInstance(array(
+ array(
+ 'host' => $cassConf['host'],
+ 'port' => $cassConf['port'],
+ 'use-framed-transport' => $cassConf['use_framed_transport'],
+ 'send-timeout-ms' => $cassConf['send_timeout_ms'],
+ 'receive-timeout-ms' => $cassConf['receive_timeout_ms']
+ )
+ ));
+
+ $this->_cassInst->useKeyspace($this->_config['default']['keyspace']);
+ $this->_cassInst->setMaxCallRetries(5);
+ }
+
+ /**
+ * Prepare fallback storage configuration, ready to serve if required!
+ */
+ /*private function _LoadAndConnectToFallback()
+ {
+ $fallbackConf = $this->_config['fallback'];
+ if($fallbackConf){
+
+ switch ($fallbackConf['engine']) {
+ case 'mysql':
+ throw new Exception("Fallback engine mysql connection not yet implemented");
+ break;
+
+ default:
+ throw new Exception("Fallback engine unrecognized");
+ }
+
+ }
+ }*/
+}
View
39 php/MnamGroup.php
@@ -0,0 +1,39 @@
+<?php
+require 'MnamCass.php';
+
+/**
+ *
+ * Mnam: Messaging, not a mess!
+ * PHP library port
+ *
+ * Class to create/manage message groups
+ *
+ * @author jeffgirard
+ *
+ */
+class MnamGroup{
+
+ private $_name;
+ private $_mnamFields=array();
+
+ public function __construct($groupName, $fieldNames=null)
+ {
+ $this->$_name = $groupName;
+
+ if($fieldNames && is_array($fieldNames)){
+ foreach ($fieldNames as $field) $this->addField($field);
+ }
+ }
+
+ public function addField($fieldName)
+ {
+ $this->$_mnamFields[] = array(
+ 'name' => $fieldName,
+ 'type' => Cassandra::TYPE_UTF8);
+ }
+
+ public function commit()
+ {
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.