]> git.mxchange.org Git - core.git/blob - framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
Continued:
[core.git] / framework / main / classes / database / backend / lfdb_legacy / class_CachedLocalFileDatabase.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\Criteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
10 use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend;
11 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
12 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
13 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
14 use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler;
15
16 // Import SPL stuff
17 use \SplFileInfo;
18
19 /**
20  * Database backend class for storing objects in locally created files.
21  *
22  * This class serializes arrays stored in the dataset instance and saves them
23  * to local files. Every file (except 'info') represents a single line. Every
24  * directory within the 'db' (base) directory represents a table.
25  *
26  * A configurable 'file_io_class' is being used as "storage backend".
27  *
28  * @author              Roland Haeder <webmaster@shipsimu.org>
29  * @version             0.0.0
30  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
31  * @license             GNU GPL 3.0 or any newer version
32  * @link                http://www.shipsimu.org
33  *
34  * This program is free software: you can redistribute it and/or modify
35  * it under the terms of the GNU General Public License as published by
36  * the Free Software Foundation, either version 3 of the License, or
37  * (at your option) any later version.
38  *
39  * This program is distributed in the hope that it will be useful,
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42  * GNU General Public License for more details.
43  *
44  * You should have received a copy of the GNU General Public License
45  * along with this program. If not, see <http://www.gnu.org/licenses/>.
46  */
47 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
48         /**
49          * The file's extension
50          */
51         private $fileExtension = 'serialized';
52
53         /**
54          * The last read file's name
55          */
56         private $lastFile = '';
57
58         /**
59          * The last read file's content including header information
60          */
61         private $lastContents = array();
62
63         /**
64          * Whether the "connection is already up
65          */
66         private $alreadyConnected = false;
67
68         /**
69          * Table information array
70          */
71         private $tableInfo = array();
72
73         /**
74          * Element for index
75          */
76         private $indexKey = '__idx';
77
78         /**
79          * The file I/O instance for the template loader
80          */
81         private $fileIoInstance = NULL;
82
83         /**
84          * The protected constructor. Do never instance from outside! You need to
85          * set a local file path. The class will then validate it.
86          *
87          * @return      void
88          */
89         protected function __construct () {
90                 // Call parent constructor
91                 parent::__construct(__CLASS__);
92         }
93
94         /**
95          * Create an object of CachedLocalFileDatabase and set the save path from
96          * configuration for local files.
97          *
98          * @return      $databaseInstance       An instance of CachedLocalFileDatabase
99          */
100         public static final function createCachedLocalFileDatabase () {
101                 // Get an instance
102                 $databaseInstance = new CachedLocalFileDatabase();
103
104                 // Get a new compressor channel instance
105                 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
106
107                 // Set the compressor channel
108                 $databaseInstance->setCompressorChannel($compressorInstance);
109
110                 // Get a file IO handler
111                 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
112
113                 // ... and set it
114                 $databaseInstance->setFileIoInstance($fileIoInstance);
115
116                 // "Connect" to the database
117                 $databaseInstance->connectToDatabase();
118
119                 // Return database instance
120                 return $databaseInstance;
121         }
122
123         /**
124          * Setter for the last read file
125          *
126          * @param       $infoInstance   The FQFN of the last read file
127          * @return      void
128          */
129         private final function setLastFile (SplFileInfo $infoInstance) {
130                 // Cast string and set it
131                 $this->lastFile = $infoInstance;
132         }
133
134         /**
135          * Getter for last read file
136          *
137          * @return      $lastFile       The last read file's name with full path
138          */
139         public final function getLastFile () {
140                 return $this->lastFile;
141         }
142
143         /**
144          * Setter for contents of the last read file
145          *
146          * @param               $contents       An array with header and data elements
147          * @return      void
148          */
149         private final function setLastFileContents (array $contents) {
150                 // Set array
151                 $this->lastContents = $contents;
152         }
153
154         /**
155          * Getter for last read file's content as an array
156          *
157          * @return      $lastContent    The array with elements 'header' and 'data'.
158          */
159         public final function getLastContents () {
160                 return $this->lastContents;
161         }
162
163         /**
164          * Getter for file extension
165          *
166          * @return      $fileExtension  The array with elements 'header' and 'data'.
167          */
168         public final function getFileExtension () {
169                 return $this->fileExtension;
170         }
171
172         /**
173          * Getter for index key
174          *
175          * @return      $indexKey       Index key
176          */
177         public final function getIndexKey () {
178                 return $this->indexKey;
179         }
180         /**
181          * Private getter for file IO instance
182          *
183          * @return      $fileIoInstance         An instance to the file I/O sub-system
184          */
185         protected final function getFileIoInstance () {
186                 return $this->fileIoInstance;
187         }
188
189         /**
190          * Setter for file I/O instance
191          *
192          * @param       $fileIoInstance         An instance to the file I/O sub-system
193          * @return      void
194          */
195         public final function setFileIoInstance (IoHandler $fileIoInstance) {
196                 $this->fileIoInstance = $fileIoInstance;
197         }
198
199         /**
200          * Reads a local data file  and returns it's contents in an array
201          *
202          * @param       $infoInstance   An instance of a SplFileInfo class
203          * @return      $dataArray
204          */
205         private function getDataArrayFromFile (SplFileInfo $infoInstance) {
206                 // Init compressed data
207                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Reading elements from database file ' . $infoInstance . ' ...');
208                 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
209                 $compressedData = $compressedData['data'];
210
211                 // Decompress it
212                 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
213
214                 // Unserialize it
215                 $dataArray = json_decode($serializedData, true);
216
217                 // Finally return it
218                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
219                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
220                 return $dataArray;
221         }
222
223         /**
224          * Writes data array to local file
225          *
226          * @param       $infoInstance   An instance of a SplFileInfo class
227          * @param       $dataArray      An array with all the data we shall write
228          * @return      void
229          */
230         private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
231                 // Serialize and compress it
232                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
233                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: dataArray=' . print_r($dataArray, true));
234                 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
235
236                 // Write this data BASE64 encoded to the file
237                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing ' . strlen($compressedData) . ' bytes ...');
238                 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
239
240                 // Debug message
241                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Flushing ' . count($dataArray) . ' elements to database file completed.');
242         }
243
244         /**
245          * Getter for table information file contents or an empty if info file was not created
246          *
247          * @param       $dataSetInstance        An instance of a database set class
248          * @return      $infoArray                      An array with all table informations
249          */
250         private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
251                 // Default content is no data
252                 $infoArray = array();
253
254                 // Create FQFN for getting the table information file
255                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
256
257                 // Get the file contents
258                 try {
259                         $infoArray = $this->getDataArrayFromFile($infoInstance);
260                 } catch (FileNotFoundException $e) {
261                         // Not found, so ignore it here
262                 }
263
264                 // ... and return it
265                 return $infoArray;
266         }
267
268         /**
269          * Generates a file info class from given dataset instance and string
270          *
271          * @param       $dataSetInstance        An instance of a database set class
272          * @param       $rowName                        Name of the row
273          * @return      $infoInstance           An instance of a SplFileInfo class
274          */
275         private function generateFileFromDataSet (Criteria $dataSetInstance, string $rowName) {
276                 // Instanciate new file object
277                 $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
278
279                 // Return it
280                 return $infoInstance;
281         }
282
283         /**
284          * Creates the table info file from given dataset instance
285          *
286          * @param       $dataSetInstance        An instance of a database set class
287          * @return      void
288          */
289         private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
290                 // Create FQFN for creating the table information file
291                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
292
293                 // Get the data out from dataset in a local array
294                 $this->tableInfo[$dataSetInstance->getTableName()] = array(
295                         'primary'      => $dataSetInstance->getPrimaryKey(),
296                         'created'      => time(),
297                         'last_updated' => time()
298                 );
299
300                 // Write the data to the file
301                 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
302         }
303
304         /**
305          * Updates the table info file from given dataset instance
306          *
307          * @param       $dataSetInstance        An instance of a database set class
308          * @return      void
309          */
310         private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
311                 // Get table name from criteria
312                 $tableName = $dataSetInstance->getTableName();
313
314                 // Create FQFN for creating the table information file
315                 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
316
317                 // Get the data out from dataset in a local array
318                 $this->tableInfo[$tableName]['primary']      = $dataSetInstance->getPrimaryKey();
319                 $this->tableInfo[$tableName]['last_updated'] = time();
320
321                 // Write the data to the file
322                 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
323         }
324
325         /**
326          * Updates the primary key information or creates the table info file if not found
327          *
328          * @param       $dataSetInstance        An instance of a database set class
329          * @return      void
330          */
331         private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
332                 // Get table name from criteria
333                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
334                 $tableName = $dataSetInstance->getTableName();
335
336                 // Get the information array from lower method
337                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s', $tableName));
338                 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
339
340                 // Is the primary key there?
341                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableInfo=' . print_r($this->tableInfo, true));
342                 if (!isset($this->tableInfo[$tableName]['primary'])) {
343                         // Then create the info file
344                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Creating info table for tableName=%s ...', $tableName));
345                         $this->createTableInfoFile($dataSetInstance);
346                 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
347                         // Set the array element
348                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting primaryKey=%s for tableName=%s ...', $dataSetInstance->getPrimaryKey(), $tableName));
349                         $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
350
351                         // Update the entry
352                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Updating info table for tableName=%s ...', $tableName));
353                         $this->updateTableInfoFile($dataSetInstance);
354                 }
355
356                 // Trace message
357                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
358         }
359
360         /**
361          * Makes sure that the database connection is alive
362          *
363          * @return      void
364          * @todo        Do some checks on the database directory and files here
365          */
366         public function connectToDatabase () {
367         }
368
369         /**
370          * Starts a SELECT query on the database by given return type, table name
371          * and search criteria
372          *
373          * @param       $tableName                      Name of the database table
374          * @param       $searchInstance         Local search criteria class
375          * @return      $resultData                     Result data of the query
376          * @throws      UnsupportedCriteriaException    If the criteria is unsupported
377          * @throws      SqlException                                    If an 'SQL error' occurs
378          */
379         public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
380                 // The result is null by any errors
381                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
382                 $resultData = NULL;
383
384                 // Create full path name
385                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
386
387                 /*
388                  * A 'select' query is not that easy on local files, so first try to
389                  * find the 'table' which is in fact a directory on the server
390                  */
391                 try {
392                         // Get a directory pointer instance
393                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for pathName=%s ...', $pathName));
394                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
395
396                         // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
397                         $resultData = array(
398                                 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
399                                 BaseDatabaseBackend::RESULT_INDEX_ROWS   => array()
400                         );
401
402                         // Initialize limit/skip
403                         $limitFound = 0;
404                         $skipFound = 0;
405                         $idx = 1;
406
407                         // Read the directory with some exceptions
408                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
409                                 // Does the extension match?
410                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
411                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
412                                         // Skip this file!
413                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
414                                         $directoryInstance->getDirectoryIteratorInstance()->next();
415                                         continue;
416                                 } // END - if
417
418                                 // Read the file
419                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
420
421                                 // Is this an array?
422                                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
423                                 if (is_array($dataArray)) {
424                                         // Default is nothing found
425                                         $isFound = true;
426
427                                         // Search in the criteria with FMFW (First Matches, First Wins)
428                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataArray()=%d', count($dataArray)));
429                                         foreach ($dataArray as $key => $value) {
430                                                 // Make sure value is not bool
431                                                 assert(!is_bool($value));
432
433                                                 // Found one entry?
434                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
435                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
436                                         } // END - foreach
437
438                                         // Is all found?
439                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
440                                         if ($isFound === true) {
441                                                 // Shall we skip this entry?
442                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d', $searchInstance->getSkip()));
443                                                 if ($searchInstance->getSkip() > 0) {
444                                                         // We shall skip some entries
445                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: skipFound=%s', $skipFound));
446                                                         if ($skipFound < $searchInstance->getSkip()) {
447                                                                 // Skip this entry
448                                                                 $skipFound++;
449                                                                 break;
450                                                         } // END - if
451                                                 } // END - if
452
453                                                 // Set id number
454                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
455                                                 $dataArray[$this->getIndexKey()] = $idx;
456
457                                                 // Entry found!
458                                                 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
459
460                                                 // Count found entries up
461                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseBackend::RESULT_INDEX_ROWS, count($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS])));
462                                                 $limitFound++;
463                                         } // END - if
464                                 } else {
465                                         // Throw an exception here
466                                         throw new SqlException(array($this, sprintf('File &#39;%s&#39; contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
467                                 }
468
469                                 // Count entry up
470                                 $idx++;
471
472                                 // Advance to next entry
473                                 $directoryInstance->getDirectoryIteratorInstance()->next();
474                         } // END - while
475
476                         // Close directory and throw the instance away
477                         $directoryInstance->closeDirectory();
478                         unset($directoryInstance);
479
480                         // Reset last exception
481                         $this->resetLastException();
482                 } catch (PathIsNoDirectoryException $e) {
483                         // Path not found means "table not found" for real databases...
484                         $this->setLastException($e);
485
486                         // So throw an SqlException here with faked error message
487                         throw new SqlException (array($this, sprintf('Table &#39;%s&#39; not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
488                 } catch (FrameworkException $e) {
489                         // Catch all exceptions and store them in last error
490                         $this->setLastException($e);
491                 }
492
493                 // Return the gathered result
494                 return $resultData;
495         }
496
497         /**
498          * "Inserts" a data set instance into a local file database folder
499          *
500          * @param       $dataSetInstance        A storeable data set
501          * @return      void
502          * @throws      SqlException    If an SQL error occurs
503          */
504         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
505                 // Try to save the request away
506                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
507                 try {
508                         // Create full path name
509                         $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
510
511                         // Write the data away
512                         $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
513
514                         // Update the primary key
515                         $this->updatePrimaryKey($dataSetInstance);
516
517                         // Reset last exception
518                         $this->resetLastException();
519                 } catch (FrameworkException $e) {
520                         // Catch all exceptions and store them in last error
521                         $this->setLastException($e);
522
523                         // Throw an SQL exception
524                         throw new SqlException(array(
525                                         $this,
526                                         sprintf('Cannot write data to table &#39;%s&#39;, is the table created?', $dataSetInstance->getTableName()),
527                                         self::DB_CODE_TABLE_UNWRITEABLE
528                                 ),
529                                 self::EXCEPTION_SQL_QUERY
530                         );
531                 }
532
533                 // Trace message
534                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
535         }
536
537         /**
538          * "Updates" a data set instance with a database layer
539          *
540          * @param       $dataSetInstance        A storeable data set
541          * @return      void
542          * @throws      SqlException    If an SQL error occurs
543          */
544         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
545                 // Create full path name
546                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
547
548                 // Try all the requests
549                 try {
550                         // Get a file pointer instance
551                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
552
553                         // Initialize limit/skip
554                         $limitFound = 0;
555                         $skipFound = 0;
556
557                         // Get the criteria array from the dataset
558                         $searchArray = $dataSetInstance->getCriteriaArray();
559
560                         // Get search criteria
561                         $searchInstance = $dataSetInstance->getSearchInstance();
562
563                         // Read the directory with some exceptions
564                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
565                                 // Does the extension match?
566                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
567                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
568                                         // Skip this file!
569                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
570                                         $directoryInstance->getDirectoryIteratorInstance()->next();
571                                         continue;
572                                 } // END - if
573
574                                 // Open this file for reading
575                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
576                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
577
578                                 // Is this an array?
579                                 if (is_array($dataArray)) {
580                                         // Default is nothing found
581                                         $isFound = true;
582
583                                         // Search in the criteria with FMFW (First Matches, First Wins)
584                                         foreach ($dataArray as $key => $value) {
585                                                 // Make sure value is not bool
586                                                 assert(!is_bool($value));
587
588                                                 // Found one entry?
589                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
590                                         } // END - foreach
591
592                                         // Is all found?
593                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
594                                         if ($isFound === true) {
595                                                 // Shall we skip this entry?
596                                                 if ($searchInstance->getSkip() > 0) {
597                                                         // We shall skip some entries
598                                                         if ($skipFound < $searchInstance->getSkip()) {
599                                                                 // Skip this entry
600                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
601                                                                 $skipFound++;
602                                                                 break;
603                                                         } // END - if
604                                                 } // END - if
605
606                                                 // Entry found, so update it
607                                                 foreach ($searchArray as $searchKey => $searchValue) {
608                                                         // Make sure the value is not bool again
609                                                         assert(!is_bool($searchValue));
610                                                         assert($searchKey != $this->indexKey);
611
612                                                         // Debug message + add/update it
613                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
614                                                         $dataArray[$searchKey] = $searchValue;
615                                                 } // END - foreach
616
617                                                 // Write the data to a local file
618                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing dataArray()=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
619                                                 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
620
621                                                 // Count found entries up
622                                                 $limitFound++;
623                                         } // END - if
624                                 } // END - if
625
626                                 // Advance to next entry
627                                 $directoryInstance->getDirectoryIteratorInstance()->next();
628                         } // END - while
629
630                         // Close the file pointer
631                         $directoryInstance->closeDirectory();
632
633                         // Update the primary key
634                         $this->updatePrimaryKey($dataSetInstance);
635
636                         // Reset last exception
637                         $this->resetLastException();
638                 } catch (FrameworkException $e) {
639                         // Catch all exceptions and store them in last error
640                         $this->setLastException($e);
641
642                         // Throw an SQL exception
643                         throw new SqlException(array($this, sprintf('Cannot write data to table &#39;%s&#39;, is the table created? Exception: %s, message:%s', $dataSetInstance->getTableName(), $e->__toString(), $e->getMessage()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
644                 }
645         }
646
647         /**
648          * Getter for primary key of specified table or if not found null will be
649          * returned. This must be database-specific.
650          *
651          * @param       $tableName              Name of the table we need the primary key from
652          * @return      $primaryKey             Primary key column of the given table
653          * @todo        Rename method to getPrimaryKeyFromTableInfo()
654          */
655         public function getPrimaryKeyOfTable (string $tableName) {
656                 // Default key is null
657                 $primaryKey = NULL;
658
659                 // Does the table information exist?
660                 if (isset($this->tableInfo[$tableName])) {
661                         // Then return the primary key
662                         $primaryKey = $this->tableInfo[$tableName]['primary'];
663                 } // END - if
664
665                 // Return the column
666                 return $primaryKey;
667         }
668
669         /**
670          * Removes non-public data from given array.
671          *
672          * @param       $data   An array with possible non-public data that needs to be removed.
673          * @return      $data   A cleaned up array with only public data.
674          * @todo        Add more generic non-public data for removal
675          */
676         public function removeNonPublicDataFromArray (array $data) {
677                 // Remove '__idx'
678                 unset($data[$this->indexKey]);
679
680                 // Return it
681                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: data[' . gettype($data) . ']='.print_r($data, true));
682                 return $data;
683         }
684
685         /**
686          * Counts total rows of given table
687          *
688          * @param       $tableName      Table name
689          * @return      $count          Total rows of given table
690          */
691         public function countTotalRows (string $tableName) {
692                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
693
694                 // Create full path name
695                 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
696
697                 // Try all the requests
698                 try {
699                         // Get a file pointer instance
700                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
701
702                         // Initialize counter
703                         $count = 0;
704
705                         // Read the directory with some exceptions
706                         while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
707                                 // Does the extension match?
708                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
709                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
710                                         // Debug message
711                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
712                                         // Skip this file!
713                                         continue;
714                                 } // END - if
715
716                                 // Count this row up
717                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
718                                 $count++;
719                         } // END - while
720                 } catch (FrameworkException $e) {
721                         // Catch all exceptions and store them in last error
722                         $this->setLastException($e);
723
724                         // Throw an SQL exception
725                         throw new SqlException(array($this, sprintf('Cannot count on table &#39;%s&#39;, is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_NOT_FOUND), self::EXCEPTION_SQL_QUERY);
726                 }
727
728                 // Return count
729                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
730                 return $count;
731         }
732
733 }