]> git.mxchange.org Git - hub.git/commitdiff
Updating node data in local DHT (not yet distributed here, that will do an other...
authorRoland Haeder <roland@mxchange.org>
Sat, 8 Feb 2014 16:10:13 +0000 (17:10 +0100)
committerRoland Haeder <roland@mxchange.org>
Sat, 8 Feb 2014 16:10:13 +0000 (17:10 +0100)
Signed-off-by: Roland Haeder <roland@mxchange.org>
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/nodes/class_BaseHubNode.php

index f3224f329cba405e1e3690ebc96f39a17cc246dc..daea599f608bb99568d1d27a9814e21ed42177eb 100644 (file)
@@ -65,6 +65,49 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                return $wrapperInstance;
        }
 
+       /**
+        * Static getter for an array of all DHT database entries
+        *
+        * @return      $elements       All elements for the DHT dabase
+        */
+       public static final function getAllElements () {
+               // Create array and ...
+               $elements = array(
+                       self::DB_COLUMN_NODE_ID,
+                       self::DB_COLUMN_SESSION_ID,
+                       self::DB_COLUMN_EXTERNAL_IP,
+                       self::DB_COLUMN_LISTEN_PORT,
+                       self::DB_COLUMN_PRIVATE_KEY_HASH,
+                       self::DB_COLUMN_NODE_MODE,
+                       self::DB_COLUMN_ACCEPTED_OBJECTS,
+                       self::DB_COLUMN_NODE_LIST
+               );
+
+               // ... return it
+               return $elements;
+       }
+
+       /**
+        * Prepares a search instance for given node data
+        *
+        * @param       $nodeData                       An array with valid node data
+        * @return      $searchInstance         An instance of a SearchCriteria class
+        */
+       private function prepareSearchInstance (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+               // Get instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Search for node id and limit it to one entry
+               $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
+               $searchInstance->setLimit(1);
+
+               // Return it
+               return $searchInstance;
+       }
+
        /**
         * Prepares a "local" instance of a StoreableCriteria class with all node
         * data for insert/update queries. This data set contains data from *this*
@@ -220,7 +263,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * Registeres a node by given message data.
         *
         * @param       $messageData            An array of all message data
-        * @param       $handlerInstance        An instance of a HandleableMessage class
+        * @param       $handlerInstance        An instance of a Handleable class
         * @return      void
         */
        public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
@@ -244,7 +287,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * Updates an existing entry in node list
         *
         * @param       $messageData            An array of all message data
-        * @param       $handlerInstance        An instance of a HandleableMessage class
+        * @param       $handlerInstance        An instance of a Handleable class
         * @param       $searchInstance         An instance of LocalSearchCriteria class
         * @return      void
         */
@@ -279,11 +322,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
 
                // Get search criteria
-               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
-               // Search for node id and limit it to one entry
-               $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
-               $searchInstance->setLimit(1);
+               $searchInstance = $this->prepareSearchInstance($nodeData);
 
                // Query database and get a result instance back
                $resultInstance = $this->doSelectByCriteria(
@@ -313,6 +352,9 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * @throws      NodeAlreadyRegisteredException  If the node is already registered
         */
        public function registerNode (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
                // Is the node registered?
                if ($this->isNodeRegistered($nodeData)) {
                        // Throw an exception
@@ -333,14 +375,38 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * @throws      NodeDataMissingException        If the node's data is missing
         */
        public function updateNode (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
                // Is the node registered?
                if (!$this->isNodeRegistered($nodeData)) {
                        // No, then throw an exception
                        throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
                } // END - if
 
-               // @TODO Unimplemented part
-               $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
+               // Get a search instance
+               $searchInstance = $this->prepareSearchInstance($nodeData);
+
+               // Get a data set instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+               // Add search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Set primary key (session id)
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
+
+               // Get node instance
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+               // Add all array elements
+               $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData);
+
+               // Remove 'node_list'
+               $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
+
+               // Run the "UPDATE" query
+               $this->queryUpdateDataSet($dataSetInstance);
        }
 }
 
index 027091571d8abc8d12dc1a0f183067eb84a14d73..a30164e9c5fab16716da71bbcb454e64918ef53b 100644 (file)
@@ -779,6 +779,27 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria {
                // ... and return it
                return $objectList;
        }
+
+       /**
+        * Adds all required elements from given array into data set instance
+        *
+        * @param       $dataSetInstance        An instance of a StoreableCriteria class
+        * @param       $nodeData                       An array with valid node data
+        * @return      void
+        */
+       public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $nodeData) {
+               // Add all data the array provides
+               foreach (NodeDistributedHashTableDatabaseWrapper::getAllElements() as $element) {
+                       // Is the element there?
+                       if (isset($nodeData[$element])) {
+                               // Add it
+                               $dataSetInstance->addCriteria($element, $nodeData[$element]);
+                       } else {
+                               // Output warning message
+                               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in nodeData array.');
+                       }
+               } // END - foreac
+       }
 }
 
 // [EOF]