From: Roland Häder Date: Sat, 9 Feb 2013 18:52:14 +0000 (+0000) Subject: Introduced (cached) method ifNodeDataIsFound() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=469c544dbf73fad85eda15d35775842b392929b6;p=hub.git Introduced (cached) method ifNodeDataIsFound() --- diff --git a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php index 40f96ace8..70b5a58a3 100644 --- a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -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__]; } /** diff --git a/application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php index 03447d81d..4491ef222 100644 --- a/application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php @@ -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... diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index cbccd5006..842c85a13 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -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))) { /*