3 namespace Org\Mxchange\CoreFramework\Database\Backend\Lfdb;
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;
19 * Database backend class for storing objects in locally created files.
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.
25 * A configurable 'file_io_class' is being used as "storage backend".
27 * @author Roland Haeder <webmaster@shipsimu.org>
29 <<<<<<< HEAD:framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
30 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
32 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
33 >>>>>>> Some updates::inc/main/classes/database/backend/class_CachedLocalFileDatabase.php
34 * @license GNU GPL 3.0 or any newer version
35 * @link http://www.shipsimu.org
37 * This program is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, either version 3 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program. If not, see <http://www.gnu.org/licenses/>.
50 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
52 * The file's extension
54 private $fileExtension = 'serialized';
57 * The last read file's name
59 private $lastFile = '';
62 * The last read file's content including header information
64 private $lastContents = array();
67 * Whether the "connection is already up
69 private $alreadyConnected = false;
72 * Table information array
74 private $tableInfo = array();
79 private $indexKey = '__idx';
82 * The protected constructor. Do never instance from outside! You need to
83 * set a local file path. The class will then validate it.
87 protected function __construct () {
88 // Call parent constructor
89 parent::__construct(__CLASS__);
93 * Create an object of CachedLocalFileDatabase and set the save path from
94 * configuration for local files.
96 * @return $databaseInstance An instance of CachedLocalFileDatabase
98 public static final function createCachedLocalFileDatabase () {
100 $databaseInstance = new CachedLocalFileDatabase();
102 // Get a new compressor channel instance
103 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
105 // Set the compressor channel
106 $databaseInstance->setCompressorChannel($compressorInstance);
108 // Get a file IO handler
109 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
112 $databaseInstance->setFileIoInstance($fileIoInstance);
114 // "Connect" to the database
115 $databaseInstance->connectToDatabase();
117 // Return database instance
118 return $databaseInstance;
122 * Setter for the last read file
124 * @param $infoInstance The FQFN of the last read file
127 private final function setLastFile (SplFileInfo $infoInstance) {
128 // Cast string and set it
129 $this->lastFile = $infoInstance;
133 * Getter for last read file
135 * @return $lastFile The last read file's name with full path
137 public final function getLastFile () {
138 return $this->lastFile;
142 * Setter for contents of the last read file
144 * @param $contents An array with header and data elements
147 private final function setLastFileContents (array $contents) {
149 $this->lastContents = $contents;
153 * Getter for last read file's content as an array
155 * @return $lastContent The array with elements 'header' and 'data'.
157 public final function getLastContents () {
158 return $this->lastContents;
162 * Getter for file extension
164 * @return $fileExtension The array with elements 'header' and 'data'.
166 public final function getFileExtension () {
167 return $this->fileExtension;
171 * Getter for index key
173 * @return $indexKey Index key
175 public final function getIndexKey () {
176 return $this->indexKey;
180 * Reads a local data file and returns it's contents in an array
182 * @param $infoInstance An instance of a SplFileInfo class
185 private function getDataArrayFromFile (SplFileInfo $infoInstance) {
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $infoInstance . ' ...');
189 // Init compressed data
190 $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
191 $compressedData = $compressedData['data'];
194 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
197 $dataArray = json_decode($serializedData, true);
200 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
201 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
208 * Writes data array to local file
210 * @param $infoInstance An instance of a SplFileInfo class
211 * @param $dataArray An array with all the data we shall write
214 private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
216 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
217 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
219 // Serialize and compress it
220 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
225 // Write this data BASE64 encoded to the file
226 $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
229 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
233 * Getter for table information file contents or an empty if info file was not created
235 * @param $dataSetInstance An instance of a database set class
236 * @return $infoArray An array with all table informations
238 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
239 // Default content is no data
240 $infoArray = array();
242 // Create FQFN for getting the table information file
243 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
245 // Get the file contents
247 $infoArray = $this->getDataArrayFromFile($infoInstance);
248 } catch (FileNotFoundException $e) {
249 // Not found, so ignore it here
257 * Generates a file info class from given dataset instance and string
259 * @param $dataSetInstance An instance of a database set class
260 * @param $rowName Name of the row
261 * @return $infoInstance An instance of a SplFileInfo class
263 private function generateFileFromDataSet (Criteria $dataSetInstance, $rowName) {
264 // Instanciate new file object
265 $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
268 return $infoInstance;
272 * Creates the table info file from given dataset instance
274 * @param $dataSetInstance An instance of a database set class
277 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
278 // Create FQFN for creating the table information file
279 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
281 // Get the data out from dataset in a local array
282 $this->tableInfo[$dataSetInstance->getTableName()] = array(
283 'primary' => $dataSetInstance->getPrimaryKey(),
285 'last_updated' => time()
288 // Write the data to the file
289 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
293 * Updates the table info file from given dataset instance
295 * @param $dataSetInstance An instance of a database set class
298 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
299 // Get table name from criteria
300 $tableName = $dataSetInstance->getTableName();
302 // Create FQFN for creating the table information file
303 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
305 // Get the data out from dataset in a local array
306 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
307 $this->tableInfo[$tableName]['last_updated'] = time();
309 // Write the data to the file
310 $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
314 * Updates the primary key information or creates the table info file if not found
316 * @param $dataSetInstance An instance of a database set class
319 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
320 // Get table name from criteria
321 $tableName = $dataSetInstance->getTableName();
323 // Get the information array from lower method
324 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
326 // Is the primary key there?
327 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, true));
328 if (!isset($this->tableInfo[$tableName]['primary'])) {
329 // Then create the info file
330 $this->createTableInfoFile($dataSetInstance);
331 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
332 // Set the array element
333 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
336 $this->updateTableInfoFile($dataSetInstance);
341 * Makes sure that the database connection is alive
344 * @todo Do some checks on the database directory and files here
346 public function connectToDatabase () {
350 * Starts a SELECT query on the database by given return type, table name
351 * and search criteria
353 * @param $tableName Name of the database table
354 * @param $searchInstance Local search criteria class
355 * @return $resultData Result data of the query
356 * @throws UnsupportedCriteriaException If the criteria is unsupported
357 * @throws SqlException If an 'SQL error' occurs
359 public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
361 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
363 // The result is null by any errors
366 // Create full path name
367 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
370 * A 'select' query is not that easy on local files, so first try to
371 * find the 'table' which is in fact a directory on the server
374 // Get a directory pointer instance
375 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
377 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
379 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
380 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
383 // Initialize limit/skip
388 // Read the directory with some exceptions
389 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
391 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
393 // Does the extension match?
394 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
400 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
401 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
404 if (is_array($dataArray)) {
405 // Default is nothing found
408 // Search in the criteria with FMFW (First Matches, First Wins)
409 foreach ($dataArray as $key => $value) {
410 // Make sure value is not bool
411 assert(!is_bool($value));
414 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
415 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
419 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
420 if ($isFound === true) {
421 // Shall we skip this entry?
422 if ($searchInstance->getSkip() > 0) {
423 // We shall skip some entries
424 if ($skipFound < $searchInstance->getSkip()) {
432 $dataArray[$this->getIndexKey()] = $idx;
435 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, true));
436 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
438 // Count found entries up
442 // Throw an exception here
443 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
450 // Close directory and throw the instance away
451 $directoryInstance->closeDirectory();
452 unset($directoryInstance);
454 // Reset last exception
455 $this->resetLastException();
456 } catch (PathIsNoDirectoryException $e) {
457 // Path not found means "table not found" for real databases...
458 $this->setLastException($e);
460 // So throw an SqlException here with faked error message
461 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
462 } catch (FrameworkException $e) {
463 // Catch all exceptions and store them in last error
464 $this->setLastException($e);
467 // Return the gathered result
472 * "Inserts" a data set instance into a local file database folder
474 * @param $dataSetInstance A storeable data set
476 * @throws SqlException If an SQL error occurs
478 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
479 // Try to save the request away
481 // Create full path name
482 $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
484 // Write the data away
485 $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
487 // Update the primary key
488 $this->updatePrimaryKey($dataSetInstance);
490 // Reset last exception
491 $this->resetLastException();
492 } catch (FrameworkException $e) {
493 // Catch all exceptions and store them in last error
494 $this->setLastException($e);
496 // Throw an SQL exception
497 throw new SqlException(array($this, sprintf('Cannot write data to table '%s', is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
502 * "Updates" a data set instance with a database layer
504 * @param $dataSetInstance A storeable data set
506 * @throws SqlException If an SQL error occurs
508 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
509 // Create full path name
510 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
512 // Try all the requests
514 // Get a file pointer instance
515 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
517 // Initialize limit/skip
521 // Get the criteria array from the dataset
522 $searchArray = $dataSetInstance->getCriteriaArray();
524 // Get search criteria
525 $searchInstance = $dataSetInstance->getSearchInstance();
527 // Read the directory with some exceptions
528 while (($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
530 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
532 // Does the extension match?
533 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
535 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
540 // Open this file for reading
541 $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
542 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',dataArray='.print_r($dataArray, true));
545 if (is_array($dataArray)) {
546 // Default is nothing found
549 // Search in the criteria with FMFW (First Matches, First Wins)
550 foreach ($dataArray as $key => $value) {
551 // Make sure value is not bool
552 assert(!is_bool($value));
555 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
559 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
560 if ($isFound === true) {
561 // Shall we skip this entry?
562 if ($searchInstance->getSkip() > 0) {
563 // We shall skip some entries
564 if ($skipFound < $searchInstance->getSkip()) {
566 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
572 // Entry found, so update it
573 foreach ($searchArray as $searchKey => $searchValue) {
574 // Make sure the value is not bool again
575 assert(!is_bool($searchValue));
576 assert($searchKey != $this->indexKey);
578 // Debug message + add/update it
579 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
580 $dataArray[$searchKey] = $searchValue;
583 // Write the data to a local file
584 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
585 $this->writeDataArrayToFqfn($fileInfoInstance, $dataArray);
587 // Count found entries up
593 // Close the file pointer
594 $directoryInstance->closeDirectory();
596 // Update the primary key
597 $this->updatePrimaryKey($dataSetInstance);
599 // Reset last exception
600 $this->resetLastException();
601 } catch (FrameworkException $e) {
602 // Catch all exceptions and store them in last error
603 $this->setLastException($e);
605 // Throw an SQL exception
606 throw new SqlException(array($this, sprintf('Cannot write data to table '%s', is the table created? Exception: %s, message:%s', $dataSetInstance->getTableName(), $e->__toString(), $e->getMessage()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
611 * Getter for primary key of specified table or if not found null will be
612 * returned. This must be database-specific.
614 * @param $tableName Name of the table we need the primary key from
615 * @return $primaryKey Primary key column of the given table
617 public function getPrimaryKeyOfTable ($tableName) {
618 // Default key is null
621 // Does the table information exist?
622 if (isset($this->tableInfo[$tableName])) {
623 // Then return the primary key
624 $primaryKey = $this->tableInfo[$tableName]['primary'];
632 * Removes non-public data from given array.
634 * @param $data An array with possible non-public data that needs to be removed.
635 * @return $data A cleaned up array with only public data.
636 * @todo Add more generic non-public data for removal
638 public function removeNonPublicDataFromArray (array $data) {
640 unset($data[$this->indexKey]);
643 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, true));
648 * Counts total rows of given table
650 * @param $tableName Table name
651 * @return $count Total rows of given table
653 public function countTotalRows($tableName) {
654 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
656 // Create full path name
657 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
659 // Try all the requests
661 // Get a file pointer instance
662 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
664 // Initialize counter
667 // Read the directory with some exceptions
668 while ($fileInfoInstance = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
670 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInstance.extension=' . $fileInfoInstance->getExtension() . ',this->getFileExtension()=' . $this->getFileExtension());
672 // Does the extension match?
673 if ($fileInfoInstance->getExtension() !== $this->getFileExtension()) {
675 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
681 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
684 } catch (FrameworkException $e) {
685 // Catch all exceptions and store them in last error
686 $this->setLastException($e);
688 // Throw an SQL exception
689 throw new SqlException(array($this, sprintf('Cannot count on table '%s', is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_NOT_FOUND), self::EXCEPTION_SQL_QUERY);
693 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');