Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 12 Dec 2021 09:09:22 +0000 (10:09 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 12 Dec 2021 09:09:22 +0000 (10:09 +0100)
- "cached" more configuration entries to class fields to avoid "expensive"
  invocations on FrameworkConfiguration->getConfigEntries()
- rewrote some "soft" assertions to hard exceptions as this cannot be let through

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
framework/main/classes/database/frontend/class_BaseDatabaseFrontend.php
framework/main/classes/handler/tasks/class_TaskHandler.php
framework/main/classes/stacker/class_BaseStacker.php

index 5c865041259a19e57f943b4bcab0ed138e9ca847..1c652b901835f8adc88b0fd34df959529e549f3b 100644 (file)
@@ -19,6 +19,7 @@ use Org\Mxchange\CoreFramework\Traits\Handler\Io\IoHandlerTrait;
 // Import SPL stuff
 use \InvalidArgumentException;
 use \SplFileInfo;
+use \UnexpectedValueException;
 
 /**
  * Database backend class for storing objects in locally created files.
@@ -83,6 +84,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         */
        private $indexKey = '__idx';
 
+       /**
+        * Cached file names based on table name to avoid "expensive" invocations
+        * of FrameworkConfiguration->getConfigEntry().
+        */
+       private $pathNames = [];
+
        /**
         * The protected constructor. Do never instance from outside! You need to
         * set a local file path. The class will then validate it.
@@ -362,22 +369,22 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                if (empty($tableName)) {
                        // Throw IAE
                        throw new InvalidArgumentException('Parameter "tableName" is empty');
+               } elseif (!isset($this->pathNames[$tableName])) {
+                       // "Cache" is not present, so create and assign it
+                       $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
                }
 
                // The result is null by any errors
                $resultData = NULL;
 
-               // Create full path name
-               $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
-
                /*
                 * A 'select' query is not that easy on local files, so first try to
                 * find the 'table' which is in fact a directory on the server
                 */
                try {
                        // Get a directory pointer instance
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for pathName=%s ...', $pathName));
-                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for this->pathNames[%s]=%s ...', $tableName, $this->pathNames[$tableName]));
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
 
                        // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
                        $resultData = [
@@ -414,7 +421,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: data[]=%d', count($dataArray)));
                                        foreach ($dataArray as $key => $value) {
                                                // Found one entry?
-                                               $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
+                                               $isFound = ($isFound && $searchInstance->isCriteriaMatching($key, $value));
                                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
                                        }
 
@@ -422,15 +429,11 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
                                        if ($isFound === true) {
                                                // Shall we skip this entry?
-                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d', $searchInstance->getSkip()));
-                                               if ($searchInstance->getSkip() > 0) {
-                                                       // We shall skip some entries
-                                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: skipFound=%s', $skipFound));
-                                                       if ($skipFound < $searchInstance->getSkip()) {
-                                                               // Skip this entry
-                                                               $skipFound++;
-                                                               break;
-                                                       }
+                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d,skipFound=%d', $searchInstance->getSkip(), $skipFound));
+                                               if ($searchInstance->getSkip() > 0 && $skipFound < $searchInstance->getSkip()) {
+                                                       // Skip this entry
+                                                       $skipFound++;
+                                                       break;
                                                }
 
                                                // Set id number
@@ -522,16 +525,26 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         *
         * @param       $dataSetInstance        An instance of a StorableCriteria class
         * @return      void
+        * @throws      UnexpectedValueException        If $tableName is empty
         * @throws      SqlException    If an SQL error occurs
         */
        public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
-               // Create full path name
-               $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
+               // Get table name
+               $tableName = $dataSetInstance->getTableName();
+
+               // Is "cache" there?
+               if (empty($tableName)) {
+                       // Should never be an empty string
+                       throw new UnexpectedValueException('Class field dataSetInstance->tableName is empty');
+               } elseif (!isset($this->pathNames[$tableName])) {
+                       // "Cache" is not present, so create and assign it
+                       $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
+               }
 
                // Try all the requests
                try {
                        // Get a file pointer instance
-                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
 
                        // Initialize limit/skip
                        $limitFound = 0;
@@ -682,17 +695,16 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                if (empty($tableName)) {
                        // Throw IAE
                        throw new InvalidArgumentException('Parameter "tableName" is empty');
+               } elseif (!isset($this->pathNames[$tableName])) {
+                       // "Cache" is not present, so create and assign it
+                       $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
                }
 
-
-               // Create full path name
-               $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
-
                // Try all the requests
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: pathName=' . $pathName);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: this->pathNames[%s]=%s', $tableName, $this->pathNames[$tableName]));
                try {
                        // Get a file pointer instance
-                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
 
                        // Initialize counter
                        $count = 0;
index 455213ec393fcfba4a27a87b25ab50f967e241ea..ccf8befea7077cdeacb8efaf854ddbf49c7fd6ab 100644 (file)
@@ -43,6 +43,10 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        private $tableName = 'unknown';
 
+       /**
+        * "Cached" value 'database_cache_enabled' from configuration
+        */
+       private $databaseCacheEnabled = false;
        /**
         * Protected constructor
         *
@@ -63,8 +67,11 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         * @return      void
         */
        private final function initCacheInstance () {
+               // Set "cache" attributes
+               $this->databaseCacheEnabled = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled');
+
                // Is the cache enabled?
-               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
+               if ($this->databaseCacheEnabled === true) {
                        // Set the new instance
                        $this->setCacheInstance(ObjectFactory::createObjectByConfiguredName('cache_class'));
                }
@@ -122,7 +129,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                $cacheKey = NULL;
 
                // Is cache enabled?
-               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
+               if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
                        //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
@@ -130,7 +137,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
 
                // Does this key exists in cache?
                //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
-               if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
+               if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
                        // Purge the cache
                        //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Calling this->cacheInstance->purgeOffset(%s) ...', $cacheKey));
                        $this->getCacheInstance()->purgeOffset($cacheKey);
@@ -155,14 +162,14 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                $cacheKey = NULL;
 
                // Is cache enabled?
-               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
+               if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
                        //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
                }
 
                // Does this key exists in cache?
-               if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
+               if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
                        // Purge the cache
                        $this->getCacheInstance()->purgeOffset($cacheKey);
                }
@@ -204,14 +211,14 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                $result = [];
 
                // Is the cache enabled?
-               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
+               if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
                }
 
                // Does this key exists in cache?
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
-               if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey, BaseDatabaseResult::RESULT_NAME_ROWS, 1))) {
+               if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey, BaseDatabaseResult::RESULT_NAME_ROWS, 1))) {
                        // Then use this result
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Cache used for cacheKey=%s', $cacheKey));
                        $result = $this->getCacheInstance()->offsetGet($cacheKey);
@@ -224,7 +231,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: result[]=%s', gettype($result)));
                        if (!is_null($result)) {
                                // Is cache enabled?
-                               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
+                               if ($this->databaseCacheEnabled === true) {
                                        // A valid result has returned from the database layer
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Setting cacheKey=%s with result()=%d entries', $cacheKey, count($result)));
                                        $this->getCacheInstance()->offsetSet($cacheKey, $result);
index 026bcebf17722edbafb7d919df7c0cea2161458f..ccd17aedc8fdd546a2fd41850e626fa50ac18fe3 100644 (file)
@@ -13,6 +13,10 @@ use Org\Mxchange\CoreFramework\Traits\Lists\ListableTrait;
 use Org\Mxchange\CoreFramework\Traits\Visitor\VisitorTrait;
 use Org\Mxchange\CoreFramework\Visitor\Visitable;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+use \UnexpectedValueException;
+
 /**
  * A Task handler
  *
@@ -231,16 +235,38 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
         * @param       $taskName               A task name to register the task on
         * @param       $taskInstance   An instance of a Taskable class
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is not valid
+        * @throws      UnexpectedValueException        If an unexpected value has been configured
         */
        public function registerTask (string $taskName, Taskable $taskInstance) {
-               // Get interval delay
+               // Is the parameter valid
+               if (empty($taskName)) {
+                       // Task name cannot be empty
+                       throw new InvalidArgumentException('Parameter "taskName" cannot be empty.');
+               }
+
+               // Get interval delay, startup delay and max runs
                $intervalDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
-               $startupDelay  = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
+               $startupDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
+               $maxRuns = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs');
 
                // If the task is 'idle_loop', a deplay of zero seconds is fine
-               assert($intervalDelay >= 0);
-               assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($intervalDelay > 0)));
-               assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($startupDelay > 0)));
+               if ($intervalDelay < 0) {
+                       // Invalid configuration value
+                       throw new UnexpectedValueException(sprintf('taskName=%s has intervalDelay=%d below zero', $taskName, $intervalDelay));
+               } elseif ($startupDelay < 0) {
+                       // Invalid configuration value
+                       throw new UnexpectedValueException(sprintf('taskName=%s has startupDelay=%d below zero', $taskName, $startupDelay));
+               } elseif ($maxRuns < 0) {
+                       // Invalid configuration value
+                       throw new UnexpectedValueException(sprintf('taskName=%s has maxRuns=%d below zero', $taskName, $maxRuns));
+               } elseif ($taskName != 'idle_loop' && $intervalDelay == 0) {
+                       // Only idle_loop can have a zero interval delay
+                       throw new UnexpectedValueException(sprintf('taskName=%s has zero interval delay which is only valid for "idle_loop" task', $taskName));
+               } elseif ($taskName != 'idle_loop' && $startupDelay == 0) {
+                       // Only idle_loop can have a zero interval delay
+                       throw new UnexpectedValueException(sprintf('taskName=%s has zero startup delay which is only valid for "idle_loop" task', $taskName));
+               }
 
                // Create the entry
                $taskEntry = [
@@ -265,7 +291,7 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                        // Interval time (delay) in milliseconds before this task is executed again
                        'task_interval_delay' => $intervalDelay,
                        // How often should this task run?
-                       'task_max_runs'       => FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
+                       'task_max_runs'       => $maxRuns,
                ];
 
                // Add the entry
index 853b0b4c3c8e5b31f896163e9311ccbd5fe7d509..b8562ca3d5a25e2e3518cde2bbe300f020c69984 100644 (file)
@@ -39,6 +39,12 @@ abstract class BaseStacker extends BaseFrameworkSystem {
        const EXCEPTION_NO_STACKER_FOUND            = 0x052;
        const EXCEPTION_STACKER_IS_EMPTY            = 0x053;
 
+       /**
+        * Array "caches" configuration entries for saving "expensive" method
+        * invocations
+        */
+       private $cachedMaxStackSizes = [];
+
        /**
         * Protected constructor
         *
@@ -71,8 +77,9 @@ abstract class BaseStacker extends BaseFrameworkSystem {
                        throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
                }
 
-               // Initialize the given stack
+               // Initialize the given stack and "cache" configuration entry
                $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit);
+               $this->cachedMaxStackSizes[$stackerName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('stacker_%s_max_size', $stackerName));
 
                // Trace message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
@@ -146,7 +153,7 @@ abstract class BaseStacker extends BaseFrameworkSystem {
                }
 
                // So, is the stack full?
-               $isFull = (($this->getStackCount($stackerName)) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'));
+               $isFull = (($this->getStackCount($stackerName)) == $this->cachedMaxStackSizes[$stackerName]);
 
                // Return result
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: isFull=%d - EXIT!', intval($isFull)));