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