Fixed handling of cache keys as empty results caused cache keys to exist. :(
authorRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 07:36:43 +0000 (07:36 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 07:36:43 +0000 (07:36 +0000)
inc/classes/interfaces/cache/class_Cacheable.php
inc/classes/main/cache/class_MemoryCache.php
inc/classes/main/criteria/class_BaseCriteria.php
inc/classes/main/criteria/search/class_SearchCriteria.php
inc/classes/main/database/class_BaseDatabaseWrapper.php
inc/classes/main/database/databases/class_LocalFileDatabase.php
inc/classes/main/result/class_DatabaseResult.php

index de4c30aeb7500e6b3b9cddf407313aee3937821d..d02c35a0ec909d6d74e0b22a79d2ed1d1d90628e 100644 (file)
@@ -25,10 +25,12 @@ interface Cacheable extends FrameworkInterface {
        /**
         * Does the specified offset exist in cache?
         *
-        * @param       $offset         The offset we are looking for
-        * @return      $exists         Whether the offset exists
+        * @param       $offset                 The offset we are looking for
+        * @param       $arrayElement   If type is array, then this element must be found
+        * @param       $minimumCount   If array element is found then this count must at least match
+        * @return      $exists                 Whether the offset exists
         */
-       function offsetExists($offset);
+       function offsetExists($offset, $arrayElement = NULL, $minimumCount = 0);
 
        /**
         * Setter for cache offset
index 55bf24db2e2ee14d1ff67a76ec5dba9847c6b061..7f254d3367f2abd2d722be7a4c8f1ea819f82972 100644 (file)
@@ -66,11 +66,31 @@ class MemoryCache extends BaseFrameworkSystem implements Cacheable {
        /**
         * Does the specified offset exist in cache?
         *
-        * @param       $offset         The offset we are looking for
-        * @return      $exists         Whether the offset exists
+        * @param       $offset                 The offset we are looking for
+        * @param       $arrayElement   If type is array, then this element must be found
+        * @param       $minimumCount   If array element is found then this count must at least match
+        * @return      $exists                 Whether the offset exists
         */
-       public function offsetExists ($offset) {
+       public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
+               // Is it there?
                $exists = $this->dataCache->offsetExists($offset);
+
+               // So look for array element?
+               if (($exists === TRUE) && (!is_null($arrayElement))) {
+                       // Get it
+                       $array = $this->offetget($offset);
+
+                       // Is it an array and element is found?
+                       if ((is_array($array)) && (isset($array[$arrayElement]))) {
+                               // Is an array and element is found, so check count
+                               $exists = (count($array[$arrayElement]) >= $minimumCount);
+                       } else {
+                               // Not found
+                               $exists = FALSE;
+                       }
+               } // END - if
+
+               // Return status
                return $exists;
        }
 
index b0edb27dc270a17c2be4bb7cf46b110a04a9dc5f..d02c32fca8d75024538929a1fde57d7fec5c93d1 100644 (file)
@@ -154,7 +154,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria {
                $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
 
                // Is it already there?
                if ($this->isKeySet($criteriaType, $criteriaKey)) {
index 02f3083dc55d80b91d50eb7ae642d8b42a50c895..df1f60ef17196e3e4bad395eafaa9f55d8c0f274 100644 (file)
@@ -131,19 +131,19 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
                $valueArray = explode($separator, $value);
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - ENTERED!');
 
                // Get 'default' search value
                $searchDefault = $this->getCriteriaElemnent($key);
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
 
                // 'default' check
                $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE));
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
 
                // Get 'choice' search value (can be NULL or $separator-separated string)
                $searchChoice = $this->getCriteriaChoiceElemnent($key);
@@ -152,7 +152,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
                assert(($searchChoice === FALSE) || (is_array($searchChoice)));
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
 
                // 'choice' check
                if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
@@ -160,41 +160,41 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
                        $isMatching = ((($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value)))) && (in_array($value, $searchChoice)));
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
                } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
 
                        // $value is choice-search value, so check all entries
                        $isMatching = (($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value))));
                        $idx = 0;
                        foreach ($valueArray as $idx => $match) {
                                // Debug message
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
 
                                // Is it found? (one is okay)
                                $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
 
                                // Debug message
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
                        } // END - foreach
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
                } else {
                        // Choice-match is FALSE
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH');
                }
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
 
                // Get 'exclude' search value
                $searchExclude = $this->getCriteriaExcludeElemnent($key);
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
 
                // 'exclude' check
                $isMatching = (
@@ -218,7 +218,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
                );
 
                // Return result
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');
                return $isMatching;
        }
 }
index 63d121c90af517c30ac50756b04071d1fee63c78..271849d8502c16a947acf5f2189c4c346669d514 100644 (file)
@@ -149,9 +149,9 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem {
                $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
 
                // Does this key exists in cache?
-               if ($this->cacheInstance->offsetExists($cacheKey)) {
+               if ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1)) {
                        // Debug message
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey);
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
 
                        // Then use this result
                        $result = $this->cacheInstance->offsetGet($cacheKey);
index 7aa3fb359f983740957752b205021412b220bbd4..c4c67ee5a565c6b583d8d4e64d73691eb77be963 100644 (file)
@@ -272,6 +272,27 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
        }
 
+       /**
+        * Updates the table info file from given dataset instance
+        *
+        * @param       $dataSetInstance        An instance of a database set class
+        * @return      void
+        */
+       private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
+               // "Cache" table name
+               $tableName = $dataSetInstance->getTableName();
+
+               // Create FQFN for creating the table information file
+               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+
+               // Get the data out from dataset in a local array
+               $this->tableInfo[$tableName]['primary']      = $dataSetInstance->getPrimaryKey();
+               $this->tableInfo[$tableName]['last_updated'] = time();
+
+               // Write the data to the file
+               $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
+       }
+
        /**
         * Updates the primary key information or creates the table info file if not found
         *
@@ -279,16 +300,20 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
         * @return      void
         */
        private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
+               // "Cache" table name
+               $tableName = $dataSetInstance->getTableName();
+
                // Get the information array from lower method
                $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
 
                // Is the primary key there?
-               if (!isset($this->tableInfo['primary'])) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, TRUE));
+               if (!isset($this->tableInfo[$tableName]['primary'])) {
                        // Then create the info file
                        $this->createTableInfoFile($dataSetInstance);
-               } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo['primary'])) {
+               } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
                        // Set the array element
-                       $this->tableInfo[$dataSetInstance->getTableName()]['primary'] = $dataSetInstance->getPrimaryKey();
+                       $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
 
                        // Update the entry
                        $this->updateTableInfoFile($dataSetInstance);
@@ -348,7 +373,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
 
                                // Read the file
                                $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
 
                                // Is this an array?
                                if (is_array($dataArray)) {
@@ -362,11 +387,11 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
 
                                                // Found one entry?
                                                $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
-                                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
+                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
                                        } // END - foreach
 
                                        // Is all found?
-                                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
                                        if ($isFound === TRUE) {
                                                // Shall we skip this entry?
                                                if ($searchInstance->getSkip() > 0) {
@@ -518,6 +543,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                                                foreach ($searchArray as $searchKey => $searchValue) {
                                                        // Make sure the value is not bool again
                                                        assert(!is_bool($searchValue));
+                                                       assert($searchKey != $this->indexKey);
 
                                                        // Debug message + add/update it
                                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
index f143a540b50999a8eda8b899f077ee3e4a49951b..11143105a5456f37c4002b24dc773ace17417a17 100644 (file)
@@ -193,6 +193,16 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                return $isValid;
        }
 
+       /**
+        * Returns count of entries
+        *
+        * @return      $isValid Whether the next/rewind entry is valid
+        */
+       public function count () {
+               // Return it
+               return count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
+       }
+
        /**
         * Determines whether the status of the query was fine (BaseDatabaseBackend::RESULT_OKAY)
         *