X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fdatabase%2Fbackend%2Flfdb_legacy%2Fclass_CachedLocalFileDatabase.php;h=7ba7a968b31ddeed72f742697abf7ad053ea390d;hb=84ca76444c3cc19221f5a4414b10b48e5bb14b76;hp=a9edcabff27b6a23e3198ae32e37e66d36333306;hpb=b2ea555ca8062721bba3bc565770a3da27577ced;p=core.git diff --git a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php index a9edcabf..7ba7a968 100644 --- a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php +++ b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Criteria\Criteria; use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria; use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria; @@ -11,6 +12,8 @@ use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException; use Org\Mxchange\CoreFramework\Generic\FrameworkException; +use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler; +use Org\Mxchange\CoreFramework\Middleware\Compressor\CompressorChannel; // Import SPL stuff use \SplFileInfo; @@ -74,6 +77,16 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac */ private $indexKey = '__idx'; + /** + * The file I/O instance for the template loader + */ + private $fileIoInstance = NULL; + + /** + * A Compressor instance + */ + private $compressorChannelInstance = NULL; + /** * The protected constructor. Do never instance from outside! You need to * set a local file path. The class will then validate it. @@ -96,10 +109,10 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $databaseInstance = new CachedLocalFileDatabase(); // Get a new compressor channel instance - $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class'); + $compressorChannelInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class'); // Set the compressor channel - $databaseInstance->setCompressorChannel($compressorInstance); + $databaseInstance->setCompressorChannelInstance($compressorChannelInstance); // Get a file IO handler $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class'); @@ -171,6 +184,43 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac public final function getIndexKey () { return $this->indexKey; } + /** + * Private getter for file IO instance + * + * @return $fileIoInstance An instance to the file I/O sub-system + */ + protected final function getFileIoInstance () { + return $this->fileIoInstance; + } + + /** + * Setter for file I/O instance + * + * @param $fileIoInstance An instance to the file I/O sub-system + * @return void + */ + public final function setFileIoInstance (IoHandler $fileIoInstance) { + $this->fileIoInstance = $fileIoInstance; + } + + /** + * Setter for compressor channel + * + * @param $compressorChannelInstance An instance of a CompressorChannel class + * @return void + */ + protected final function setCompressorChannelInstance (CompressorChannel $compressorChannelInstance) { + $this->compressorChannelInstance = $compressorChannelInstance; + } + + /** + * Getter for compressor channel + * + * @return $compressorChannelInstance An instance of a CompressorChannel class + */ + protected final function getCompressorChannelInstance () { + return $this->compressorChannelInstance; + } /** * Reads a local data file and returns it's contents in an array @@ -179,24 +229,20 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @return $dataArray */ private function getDataArrayFromFile (SplFileInfo $infoInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...'); - // Init compressed data + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...'); $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance); $compressedData = $compressedData['data']; // Decompress it - $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData); + $serializedData = $this->getCompressorChannelInstance()->getCompressor()->decompressStream($compressedData); // Unserialize it $dataArray = json_decode($serializedData, true); - // Debug message + // Finally return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.'); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true)); - - // Finally return it return $dataArray; } @@ -208,12 +254,10 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @return void */ private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) { - // Debug message + // Serialize and compress it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...'); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true)); - - // Serialize and compress it - $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray)); + $compressedData = $this->getCompressorChannelInstance()->getCompressor()->compressStream(json_encode($dataArray)); // Write this data BASE64 encoded to the file //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...'); @@ -254,9 +298,9 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @param $rowName Name of the row * @return $infoInstance An instance of a SplFileInfo class */ - private function generateFileFromDataSet (Criteria $dataSetInstance, $rowName) { + private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) { // Instanciate new file object - $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension()); + $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension()); // Return it return $infoInstance; @@ -312,23 +356,31 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac */ private function updatePrimaryKey (StoreableCriteria $dataSetInstance) { // Get table name from criteria + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString())); $tableName = $dataSetInstance->getTableName(); // Get the information array from lower method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName)); $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance); // Is the primary key there? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true)); + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true)); if (!isset($this->tableInfo[$tableName]['primary'])) { // Then create the info file + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName)); $this->createTableInfoFile($dataSetInstance); - } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) { + } elseif ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) { // Set the array element + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName)); $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey(); // Update the entry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName)); $this->updateTableInfoFile($dataSetInstance); } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!'); } /** @@ -350,13 +402,13 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @throws UnsupportedCriteriaException If the criteria is unsupported * @throws SqlException If an 'SQL error' occurs */ - public function querySelect ($tableName, LocalSearchCriteria $searchInstance) { + public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) { // The result is null by any errors //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString())); $resultData = NULL; // Create full path name - $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR; + $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR; /* * A 'select' query is not that easy on local files, so first try to @@ -364,6 +416,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac */ 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)); // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read @@ -380,9 +433,11 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // Read the directory with some exceptions while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) { // Does the extension match? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInstance.extension=%s,this->getFileExtension()=%s', $fileInfoInstance->getExtension(), $this->getFileExtension())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension())); if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) { // Skip this file! + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename())); + $directoryInstance->getDirectoryIteratorInstance()->next(); continue; } // END - if @@ -439,6 +494,9 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // Count entry up $idx++; + + // Advance to next entry + $directoryInstance->getDirectoryIteratorInstance()->next(); } // END - while // Close directory and throw the instance away @@ -511,7 +569,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac */ public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) { // Create full path name - $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR; + $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR; // Try all the requests try { @@ -530,14 +588,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // Read the directory with some exceptions while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension()); - // Does the extension match? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension()); if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!'); // Skip this file! + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!'); + $directoryInstance->getDirectoryIteratorInstance()->next(); continue; } // END - if @@ -592,6 +648,9 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $limitFound++; } // END - if } // END - if + + // Advance to next entry + $directoryInstance->getDirectoryIteratorInstance()->next(); } // END - while // Close the file pointer @@ -619,7 +678,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @return $primaryKey Primary key column of the given table * @todo Rename method to getPrimaryKeyFromTableInfo() */ - public function getPrimaryKeyOfTable ($tableName) { + public function getPrimaryKeyOfTable (string $tableName) { // Default key is null $primaryKey = NULL; @@ -655,11 +714,11 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac * @param $tableName Table name * @return $count Total rows of given table */ - public function countTotalRows($tableName) { + public function countTotalRows (string $tableName) { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!'); // Create full path name - $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR; + $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR; // Try all the requests try { @@ -671,10 +730,8 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // Read the directory with some exceptions while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension()); - // Does the extension match? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension()); if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');