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