Rewritten:
[core.git] / framework / main / classes / database / backend / lfdb_legacy / class_CachedLocalFileDatabase.php
index e8a8555a03220a65b9db947efb7cd99938f86146..d232db1b460bd42f320d97fd3e70427ddf1edd65 100644 (file)
@@ -12,6 +12,9 @@ use CoreFramework\Factory\ObjectFactory;
 use CoreFramework\Filesystem\FileNotFoundException;
 use CoreFramework\Generic\FrameworkException;
 
 use CoreFramework\Filesystem\FileNotFoundException;
 use CoreFramework\Generic\FrameworkException;
 
+// Import SPL stuff
+use \SplFileInfo;
+
 /**
  * Database backend class for storing objects in locally created files.
  *
 /**
  * Database backend class for storing objects in locally created files.
  *
@@ -114,12 +117,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
        /**
         * Setter for the last read file
         *
        /**
         * Setter for the last read file
         *
-        * @param       $fqfn   The FQFN of the last read file
+        * @param       $infoInstance   The FQFN of the last read file
         * @return      void
         */
         * @return      void
         */
-       private final function setLastFile ($fqfn) {
+       private final function setLastFile (SplFileInfo $infoInstance) {
                // Cast string and set it
                // Cast string and set it
-               $this->lastFile = (string) $fqfn;
+               $this->lastFile = $infoInstance;
        }
 
        /**
        }
 
        /**
@@ -172,20 +175,17 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
        /**
         * Reads a local data file  and returns it's contents in an array
         *
        /**
         * Reads a local data file  and returns it's contents in an array
         *
-        * @param       $fqfn   The FQFN for the requested file
+        * @param       $infoInstance   An instance of a SplFileInfo class
         * @return      $dataArray
         */
         * @return      $dataArray
         */
-       private function getDataArrayFromFile ($fqfn) {
+       private function getDataArrayFromFile (SplFileInfo $infoInstance) {
                // Debug message
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $infoInstance . ' ...');
 
                // Init compressed data
 
                // Init compressed data
-               $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
+               $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
                $compressedData = $compressedData['data'];
 
                $compressedData = $compressedData['data'];
 
-               // Close the file and throw the instance away
-               unset($fileInstance);
-
                // Decompress it
                $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
 
                // Decompress it
                $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
 
@@ -193,7 +193,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                $dataArray = json_decode($serializedData, true);
 
                // Debug message
                $dataArray = json_decode($serializedData, true);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
 
                // Finally return it
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
 
                // Finally return it
@@ -203,13 +203,13 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
        /**
         * Writes data array to local file
         *
        /**
         * Writes data array to local file
         *
-        * @param       $fqfn           The FQFN of the local file
+        * @param       $infoInstance   An instance of a SplFileInfo class
         * @param       $dataArray      An array with all the data we shall write
         * @return      void
         */
         * @param       $dataArray      An array with all the data we shall write
         * @return      void
         */
-       private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
+       private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
                // Debug message
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
 
                // Serialize and compress it
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
 
                // Serialize and compress it
@@ -219,7 +219,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
 
                // Write this data BASE64 encoded to the file
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
 
                // Write this data BASE64 encoded to the file
-               $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
+               $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
 
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
 
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
@@ -236,11 +236,11 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                $infoArray = array();
 
                // Create FQFN for getting the table information file
                $infoArray = array();
 
                // Create FQFN for getting the table information file
-               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+               $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
 
                // Get the file contents
                try {
 
                // Get the file contents
                try {
-                       $infoArray = $this->getDataArrayFromFile($fqfn);
+                       $infoArray = $this->getDataArrayFromFile($infoInstance);
                } catch (FileNotFoundException $e) {
                        // Not found, so ignore it here
                }
                } catch (FileNotFoundException $e) {
                        // Not found, so ignore it here
                }
@@ -250,18 +250,18 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
        }
 
        /**
        }
 
        /**
-        * Generates an FQFN from given dataset instance and string
+        * Generates a file info class from given dataset instance and string
         *
         * @param       $dataSetInstance        An instance of a database set class
         * @param       $rowName                        Name of the row
         *
         * @param       $dataSetInstance        An instance of a database set class
         * @param       $rowName                        Name of the row
-        * @return      $fqfn                           The FQFN for this row
+        * @return      $infoInstance           An instance of a SplFileInfo class
         */
         */
-       private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
-               // This is the FQFN
-               $fqfn = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension();
+       private function generateFileFromDataSet (Criteria $dataSetInstance, $rowName) {
+               // Instanciate new file object
+               $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
 
                // Return it
 
                // Return it
-               return $fqfn;
+               return $infoInstance;
        }
 
        /**
        }
 
        /**
@@ -272,7 +272,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         */
        private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
                // Create FQFN for creating the table information file
         */
        private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
                // Create FQFN for creating the table information file
-               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+               $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
 
                // Get the data out from dataset in a local array
                $this->tableInfo[$dataSetInstance->getTableName()] = array(
 
                // Get the data out from dataset in a local array
                $this->tableInfo[$dataSetInstance->getTableName()] = array(
@@ -282,7 +282,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                );
 
                // Write the data to the file
                );
 
                // Write the data to the file
-               $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
+               $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
        }
 
        /**
        }
 
        /**
@@ -296,14 +296,14 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                $tableName = $dataSetInstance->getTableName();
 
                // Create FQFN for creating the table information file
                $tableName = $dataSetInstance->getTableName();
 
                // Create FQFN for creating the table information file
-               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+               $infoInstance = $this->generateFileFromDataSet($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
 
                // 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]);
+               $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
        }
 
        /**
        }
 
        /**
@@ -475,10 +475,10 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                // Try to save the request away
                try {
                        // Create full path name
                // Try to save the request away
                try {
                        // Create full path name
-                       $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
+                       $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
 
                        // Write the data away
 
                        // Write the data away
-                       $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
+                       $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
 
                        // Update the primary key
                        $this->updatePrimaryKey($dataSetInstance);
 
                        // Update the primary key
                        $this->updatePrimaryKey($dataSetInstance);