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