]> git.mxchange.org Git - hub.git/commitdiff
Introduced (cached) method ifNodeDataIsFound()
authorRoland Häder <roland@mxchange.org>
Sat, 9 Feb 2013 18:52:14 +0000 (18:52 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 9 Feb 2013 18:52:14 +0000 (18:52 +0000)
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php
application/hub/main/nodes/class_BaseHubNode.php

index 40f96ace88ee29fbd224a06118e270bebdbee169..70b5a58a380bb2d44621c54d5e6997c352b96db8 100644 (file)
@@ -63,24 +63,27 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * @return      $isRegistered   Whether *this* node is registered in the DHT
         */
        public function isLocalNodeRegistered () {
-               // Get a search criteria instance
-               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+               // Is there cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Get a search criteria instance
+                       $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
-               // Get node instance
-               $nodeInstance = Registry::getRegistry()->getInstance('node');
+                       // Get node instance
+                       $nodeInstance = Registry::getRegistry()->getInstance('node');
 
-               // Add ip:port as criteria
-               $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
-               $searchInstance->setLimit(1);
+                       // Add ip:port as criteria
+                       $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
+                       $searchInstance->setLimit(1);
 
-               // Query database and get a result instance back
-               $resultInstance = $this->doSelectByCriteria($searchInstance);
+                       // Query database and get a result instance back
+                       $resultInstance = $this->doSelectByCriteria($searchInstance);
 
-               // Is there an entry?
-               $isRegistered = $resultInstance->next();
+                       // Cache result of if there is an entry, next() will tell us if the next entry is valid
+                       $GLOBALS[__METHOD__] = $resultInstance->next();
+               } // END - if
 
                // Return result
-               return $isRegistered;
+               return $GLOBALS[__METHOD__];
        }
 
        /**
index 03447d81d5b9815499dc9406bdffe1693c26d53e..4491ef22284e9b0290a2b67a4fa538aabcda137b 100644 (file)
@@ -59,6 +59,37 @@ class NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements Node
                return $wrapperInstance;
        }
 
+       /**
+        * Checks whether there is an entry for given node instance
+        *
+        * @param       $nodeInstance   An instance of a NodeHelper class
+        * @return      $isFound                Whether a node id has been found for this node
+        */
+       public function ifNodeDataIsFound (NodeHelper $nodeInstance) {
+               // Is there cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Now get a search criteria instance
+                       $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Search for the node number one which is hard-coded the default
+                       $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
+                       $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $nodeInstance->getRequestInstance()->getRequestElement('mode'));
+                       $searchInstance->setLimit(1);
+
+                       // Get a result back
+                       $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+                       // Set result instance in node instance
+                       $nodeInstance->setResultInstance($resultInstance);
+
+                       // Is it valid?
+                       $GLOBALS[__METHOD__] = $resultInstance->next();
+               } // END - if
+
+               // Return it
+               return $GLOBALS[__METHOD__];
+       }
+
        /**
         * 'Registers' a new node id along with data provided in the node instance.
         * This may sound confusing but avoids double code very nicely...
index cbccd5006bcf299c01a19bede5014b340e227a7c..842c85a1331ba8cd234f710dbdde83644751fd91 100644 (file)
@@ -277,22 +277,8 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria {
         * @return      void
         */
        public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) {
-               // Now get a search criteria instance
-               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
-               // Search for the node number one which is hard-coded the default
-               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
-               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
-               $searchInstance->setLimit(1);
-
-               // Get a result back
-               $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
-
-               // Is it valid?
-               if ($resultInstance->next()) {
-                       // Save the result instance in this class
-                       $this->setResultInstance($resultInstance);
-
+               // Is there a node id?
+               if ($this->getWrapperInstance()->ifNodeDataIsFound($this)) {
                        // Get the node id from result and set it
                        $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
 
@@ -349,22 +335,8 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria {
         * @return      void
         */
        public function bootstrapGeneratePrivateKey () {
-               // Now get a search criteria instance
-               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
-               // Search for the node number one which is hard-coded the default
-               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
-               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
-               $searchInstance->setLimit(1);
-
-               // Get a result back
-               $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
-
                // Is it valid?
-               if ($resultInstance->next()) {
-                       // Save the result instance in this class
-                       $this->setResultInstance($resultInstance);
-
+               if ($this->getWrapperInstance()->ifNodeDataIsFound($this)) {
                        // Is the element set?
                        if (is_null($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY))) {
                                /*