]> 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\Result\Database\BaseDatabaseResult;
17 use Org\Mxchange\CoreFramework\Traits\Compressor\Channel\CompressorChannelTrait;
18 use Org\Mxchange\CoreFramework\Traits\Handler\Io\IoHandlerTrait;
19
20 // Import SPL stuff
21 use \InvalidArgumentException;
22 use \SplFileInfo;
23 use \UnexpectedValueException;
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 - 2021 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()] = array(
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()->getConfigEntry('db_update_primary_forced') == 'Y') && ($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');
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                         ];
395
396                         // Initialize limit/skip
397                         $limitFound = 0;
398                         $skipFound = 0;
399                         $idx = 1;
400
401                         // Read the directory with some exceptions
402                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
403                                 // Does the extension match?
404                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: fileInfoInstance->extension=%s,this->fileExtension=%s', $fileInfoInstance->getExtension(), $this->getFileExtension()));
405                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
406                                         // Skip this file!
407                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
408                                         $directoryInstance->getDirectoryIteratorInstance()->next();
409                                         continue;
410                                 }
411
412                                 // Read the file
413                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
414
415                                 // Is this an array?
416                                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
417                                 if (is_array($dataArray)) {
418                                         // Default is nothing found
419                                         $isFound = true;
420
421                                         // Search in the criteria with FMFW (First Matches, First Wins)
422                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: data[]=%d', count($dataArray)));
423                                         foreach ($dataArray as $key => $value) {
424                                                 // Found one entry?
425                                                 $isFound = ($isFound && $searchInstance->isCriteriaMatching($key, $value));
426                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
427                                         }
428
429                                         // Is all found?
430                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
431                                         if ($isFound === true) {
432                                                 // Shall we skip this entry?
433                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d,skipFound=%d', $searchInstance->getSkip(), $skipFound));
434                                                 if ($searchInstance->getSkip() > 0 && $skipFound < $searchInstance->getSkip()) {
435                                                         // Skip this entry
436                                                         $skipFound++;
437                                                         break;
438                                                 }
439
440                                                 // Set id number
441                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
442                                                 $dataArray[$this->getIndexKey()] = $idx;
443
444                                                 // Entry found!
445                                                 array_push($resultData[BaseDatabaseResult::RESULT_NAME_ROWS], $dataArray);
446
447                                                 // Count found entries up
448                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseResult::RESULT_NAME_ROWS, count($resultData[BaseDatabaseResult::RESULT_NAME_ROWS])));
449                                                 $limitFound++;
450                                         }
451                                 } else {
452                                         // Throw an exception here
453                                         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);
454                                 }
455
456                                 // Count entry up
457                                 $idx++;
458
459                                 // Advance to next entry
460                                 $directoryInstance->getDirectoryIteratorInstance()->next();
461                         }
462
463                         // Close directory and throw the instance away
464                         $directoryInstance->closeDirectory();
465                         unset($directoryInstance);
466
467                         // Reset last exception
468                         $this->resetLastException();
469                 } catch (PathIsNoDirectoryException $e) {
470                         // Path not found means "table not found" for real databases...
471                         $this->setLastException($e);
472
473                         // So throw an SqlException here with faked error message
474                         throw new SqlException (array($this, sprintf('Table &#39;%s&#39; not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
475                 } catch (FrameworkException $e) {
476                         // Catch all exceptions and store them in last error
477                         $this->setLastException($e);
478                 }
479
480                 // Return the gathered result
481                 return $resultData;
482         }
483
484         /**
485          * "Inserts" a data set instance into a local file database folder
486          *
487          * @param       $dataSetInstance        A storeable data set
488          * @return      void
489          * @throws      SqlException    If an SQL error occurs
490          */
491         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
492                 // Try to save the request away
493                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: dataSetInstance=%s - CALLED!', $dataSetInstance->__toString()));
494                 try {
495                         // Create full path name
496                         $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
497
498                         // Write the data away
499                         $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
500
501                         // Update the primary key
502                         $this->updatePrimaryKey($dataSetInstance);
503
504                         // Reset last exception
505                         $this->resetLastException();
506                 } catch (FrameworkException $e) {
507                         // Catch all exceptions and store them in last error
508                         $this->setLastException($e);
509
510                         // Throw an SQL exception
511                         throw new SqlException([
512                                         $this,
513                                         sprintf('Cannot write data to table &#39;%s&#39;, is the table created? e=%s,e->message=%s',
514                                                 $dataSetInstance->getTableName(),
515                                                 $e->__toString(),
516                                                 $e->getMessage()
517                                         ),
518                                         self::DB_CODE_TABLE_UNWRITEABLE
519                                 ],
520                                 self::EXCEPTION_SQL_QUERY
521                         );
522                 }
523
524                 // Trace message
525                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: EXIT!');
526         }
527
528         /**
529          * "Updates" a data set instance with a database layer
530          *
531          * @param       $dataSetInstance        An instance of a StorableCriteria class
532          * @return      void
533          * @throws      UnexpectedValueException        If $tableName is empty
534          * @throws      SqlException    If an SQL error occurs
535          */
536         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
537                 // Get table name
538                 $tableName = $dataSetInstance->getTableName();
539
540                 // Is "cache" there?
541                 if (empty($tableName)) {
542                         // Should never be an empty string
543                         throw new UnexpectedValueException('Class field dataSetInstance->tableName is empty');
544                 } elseif (!isset($this->pathNames[$tableName])) {
545                         // "Cache" is not present, so create and assign it
546                         $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
547                 }
548
549                 // Try all the requests
550                 try {
551                         // Get a file pointer instance
552                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
553
554                         // Initialize limit/skip
555                         $limitFound = 0;
556                         $skipFound = 0;
557
558                         // Get the criteria array from the dataset
559                         $searchArray = $dataSetInstance->getCriteriaArray();
560
561                         // Get search criteria
562                         $searchInstance = $dataSetInstance->getSearchInstance();
563
564                         // Read the directory with some exceptions
565                         while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
566                                 // Does the extension match?
567                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
568                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
569                                         // Skip this file!
570                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
571                                         $directoryInstance->getDirectoryIteratorInstance()->next();
572                                         continue;
573                                 }
574
575                                 // Open this file for reading
576                                 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
577                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
578
579                                 // Is this an array?
580                                 if (is_array($dataArray)) {
581                                         // Default is nothing found
582                                         $isFound = true;
583
584                                         // Search in the criteria with FMFW (First Matches, First Wins)
585                                         foreach ($dataArray as $key => $value) {
586                                                 // Found one entry?
587                                                 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
588                                         }
589
590                                         // Is all found?
591                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
592                                         if ($isFound === true) {
593                                                 // Shall we skip this entry?
594                                                 if ($searchInstance->getSkip() > 0) {
595                                                         // We shall skip some entries
596                                                         if ($skipFound < $searchInstance->getSkip()) {
597                                                                 // Skip this entry
598                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
599                                                                 $skipFound++;
600                                                                 break;
601                                                         }
602                                                 }
603
604                                                 // Entry found, so update it
605                                                 foreach ($searchArray as $searchKey => $searchValue) {
606                                                         // Make sure the value is not bool again
607                                                         assert(!is_bool($searchValue));
608                                                         assert($searchKey != $this->indexKey);
609
610                                                         // Debug message + add/update it
611                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
612                                                         $dataArray[$searchKey] = $searchValue;
613                                                 }
614
615                                                 // Write the data to a local file
616                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing data[]=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
617                                                 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
618
619                                                 // Count found entries up
620                                                 $limitFound++;
621                                         }
622                                 }
623
624                                 // Advance to next entry
625                                 $directoryInstance->getDirectoryIteratorInstance()->next();
626                         }
627
628                         // Close the file pointer
629                         $directoryInstance->closeDirectory();
630
631                         // Update the primary key
632                         $this->updatePrimaryKey($dataSetInstance);
633
634                         // Reset last exception
635                         $this->resetLastException();
636                 } catch (FrameworkException $e) {
637                         // Catch all exceptions and store them in last error
638                         $this->setLastException($e);
639
640                         // Throw an SQL exception
641                         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);
642                 }
643         }
644
645         /**
646          * Getter for primary key of specified table or if not found null will be
647          * returned. This must be database-specific.
648          *
649          * @param       $tableName              Name of the table we need the primary key from
650          * @return      $primaryKey             Primary key column of the given table
651          * @throws      InvalidArgumentException        If a parameter is not valid
652          * @todo        Rename method to getPrimaryKeyFromTableInfo()
653          */
654         public function getPrimaryKeyOfTable (string $tableName) {
655                 // Validate parameter
656                 if (empty($tableName)) {
657                         // Throw IAE
658                         throw new InvalidArgumentException('Parameter "tableName" is empty');
659                 }
660
661                 // Default key is null
662                 $primaryKey = NULL;
663
664                 // Does the table information exist?
665                 if (isset($this->tableInfo[$tableName])) {
666                         // Then return the primary key
667                         $primaryKey = $this->tableInfo[$tableName]['primary'];
668                 }
669
670                 // Return the column
671                 return $primaryKey;
672         }
673
674         /**
675          * Removes non-public data from given array.
676          *
677          * @param       $data   An array with possible non-public data that needs to be removed.
678          * @return      $data   A cleaned up array with only public data.
679          * @todo        Add more generic non-public data for removal
680          */
681         public function removeNonPublicDataFromArray (array $data) {
682                 // Remove '__idx'
683                 unset($data[$this->indexKey]);
684
685                 // Return it
686                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: data[' . gettype($data) . ']='.print_r($data, true));
687                 return $data;
688         }
689
690         /**
691          * Counts total rows of given table
692          *
693          * @param       $tableName      Table name
694          * @return      $count          Total rows of given table
695          * @throws      InvalidArgumentException        If a parameter is not valid
696          */
697         public function countTotalRows (string $tableName) {
698                 // Validate parameter
699                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
700                 if (empty($tableName)) {
701                         // Throw IAE
702                         throw new InvalidArgumentException('Parameter "tableName" is empty');
703                 } elseif (!isset($this->pathNames[$tableName])) {
704                         // "Cache" is not present, so create and assign it
705                         $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
706                 }
707
708                 // Try all the requests
709                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: this->pathNames[%s]=%s', $tableName, $this->pathNames[$tableName]));
710                 try {
711                         // Get a file pointer instance
712                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
713
714                         // Initialize counter
715                         $count = 0;
716
717                         // Read the directory with some exceptions
718                         while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(['.gitkeep', 'info.' . $this->getFileExtension()])) {
719                                 // Does the extension match?
720                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance->extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
721                                 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
722                                         // Debug message
723                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
724                                         // Skip this file!
725                                         continue;
726                                 }
727
728                                 // Count this row up
729                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
730                                 $count++;
731                         }
732                 } catch (FrameworkException $e) {
733                         // Catch all exceptions and store them in last error
734                         $this->setLastException($e);
735
736                         // Throw an SQL exception
737                         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);
738                 }
739
740                 // Return count
741                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
742                 return $count;
743         }
744
745 }