3 namespace CoreFramework\Database\Backend\Lfdb;
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Generic\FrameworkException;
11 * Database backend class for storing objects in locally created files.
13 * This class serializes arrays stored in the dataset instance and saves them
14 * to local files. Every file (except 'info') represents a single line. Every
15 * directory within the 'db' (base) directory represents a table.
17 * A configurable 'file_io_class' is being used as "storage backend".
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
40 * The file's extension
42 private $fileExtension = 'serialized';
45 * The last read file's name
47 private $lastFile = '';
50 * The last read file's content including header information
52 private $lastContents = array();
55 * Whether the "connection is already up
57 private $alreadyConnected = FALSE;
60 * Table information array
62 private $tableInfo = array();
67 private $indexKey = '__idx';
70 * The protected constructor. Do never instance from outside! You need to
71 * set a local file path. The class will then validate it.
75 protected function __construct () {
76 // Call parent constructor
77 parent::__construct(__CLASS__);
81 * Create an object of CachedLocalFileDatabase and set the save path from
82 * configuration for local files.
84 * @return $databaseInstance An instance of CachedLocalFileDatabase
86 public static final function createCachedLocalFileDatabase () {
88 $databaseInstance = new CachedLocalFileDatabase();
90 // Get a new compressor channel instance
91 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
93 // Set the compressor channel
94 $databaseInstance->setCompressorChannel($compressorInstance);
96 // Get a file IO handler
97 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
100 $databaseInstance->setFileIoInstance($fileIoInstance);
102 // "Connect" to the database
103 $databaseInstance->connectToDatabase();
105 // Return database instance
106 return $databaseInstance;
110 * Setter for the last read file
112 * @param $fqfn The FQFN of the last read file
115 private final function setLastFile ($fqfn) {
116 // Cast string and set it
117 $this->lastFile = (string) $fqfn;
121 * Getter for last read file
123 * @return $lastFile The last read file's name with full path
125 public final function getLastFile () {
126 return $this->lastFile;
130 * Setter for contents of the last read file
132 * @param $contents An array with header and data elements
135 private final function setLastFileContents (array $contents) {
137 $this->lastContents = $contents;
141 * Getter for last read file's content as an array
143 * @return $lastContent The array with elements 'header' and 'data'.
145 public final function getLastContents () {
146 return $this->lastContents;
150 * Getter for file extension
152 * @return $fileExtension The array with elements 'header' and 'data'.
154 public final function getFileExtension () {
155 return $this->fileExtension;
159 * Getter for index key
161 * @return $indexKey Index key
163 public final function getIndexKey () {
164 return $this->indexKey;
168 * Reads a local data file and returns it's contents in an array
170 * @param $fqfn The FQFN for the requested file
173 private function getDataArrayFromFile ($fqfn) {
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
177 // Init compressed data
178 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
179 $compressedData = $compressedData['data'];
181 // Close the file and throw the instance away
182 unset($fileInstance);
185 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
188 $dataArray = json_decode($serializedData, TRUE);
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
199 * Writes data array to local file
201 * @param $fqfn The FQFN of the local file
202 * @param $dataArray An array with all the data we shall write
205 private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
208 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
210 // Serialize and compress it
211 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
214 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
216 // Write this data BASE64 encoded to the file
217 $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
224 * Getter for table information file contents or an empty if info file was not created
226 * @param $dataSetInstance An instance of a database set class
227 * @return $infoArray An array with all table informations
229 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
230 // Default content is no data
231 $infoArray = array();
233 // Create FQFN for getting the table information file
234 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
236 // Get the file contents
238 $infoArray = $this->getDataArrayFromFile($fqfn);
239 } catch (FileNotFoundException $e) {
240 // Not found, so ignore it here
248 * Generates an FQFN from given dataset instance and string
250 * @param $dataSetInstance An instance of a database set class
251 * @param $rowName Name of the row
252 * @return $fqfn The FQFN for this row
254 private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
256 $fqfn = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/' . $rowName . '.' . $this->getFileExtension();
263 * Creates the table info file from given dataset instance
265 * @param $dataSetInstance An instance of a database set class
268 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
269 // Create FQFN for creating the table information file
270 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
272 // Get the data out from dataset in a local array
273 $this->tableInfo[$dataSetInstance->getTableName()] = array(
274 'primary' => $dataSetInstance->getPrimaryKey(),
276 'last_updated' => time()
279 // Write the data to the file
280 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
284 * Updates the table info file from given dataset instance
286 * @param $dataSetInstance An instance of a database set class
289 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
290 // Get table name from criteria
291 $tableName = $dataSetInstance->getTableName();
293 // Create FQFN for creating the table information file
294 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
296 // Get the data out from dataset in a local array
297 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
298 $this->tableInfo[$tableName]['last_updated'] = time();
300 // Write the data to the file
301 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
305 * Updates the primary key information or creates the table info file if not found
307 * @param $dataSetInstance An instance of a database set class
310 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
311 // Get table name from criteria
312 $tableName = $dataSetInstance->getTableName();
314 // Get the information array from lower method
315 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
317 // Is the primary key there?
318 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, TRUE));
319 if (!isset($this->tableInfo[$tableName]['primary'])) {
320 // Then create the info file
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 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
327 $this->updateTableInfoFile($dataSetInstance);
332 * Makes sure that the database connection is alive
335 * @todo Do some checks on the database directory and files here
337 public function connectToDatabase () {
341 * Starts a SELECT query on the database by given return type, table name
342 * and search criteria
344 * @param $tableName Name of the database table
345 * @param $searchInstance Local search criteria class
346 * @return $resultData Result data of the query
347 * @throws UnsupportedCriteriaException If the criteria is unsupported
348 * @throws SqlException If an 'SQL error' occurs
350 public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
352 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
354 // The result is null by any errors
357 // Create full path name
358 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
361 * A 'select' query is not that easy on local files, so first try to
362 * find the 'table' which is in fact a directory on the server
365 // Get a directory pointer instance
366 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
368 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
370 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
371 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
374 // Initialize limit/skip
379 // Read the directory with some exceptions
380 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
382 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',this->getFileExtension()=' . $this->getFileExtension());
384 // Does the extension match?
385 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
391 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
392 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
395 if (is_array($dataArray)) {
396 // Default is nothing found
399 // Search in the criteria with FMFW (First Matches, First Wins)
400 foreach ($dataArray as $key => $value) {
401 // Make sure value is not bool
402 assert(!is_bool($value));
405 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
406 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
410 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
411 if ($isFound === TRUE) {
412 // Shall we skip this entry?
413 if ($searchInstance->getSkip() > 0) {
414 // We shall skip some entries
415 if ($skipFound < $searchInstance->getSkip()) {
423 $dataArray[$this->getIndexKey()] = $idx;
426 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, TRUE));
427 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
429 // Count found entries up
433 // Throw an exception here
434 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
441 // Close directory and throw the instance away
442 $directoryInstance->closeDirectory();
443 unset($directoryInstance);
445 // Reset last exception
446 $this->resetLastException();
447 } catch (PathIsNoDirectoryException $e) {
448 // Path not found means "table not found" for real databases...
449 $this->setLastException($e);
451 // So throw an SqlException here with faked error message
452 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
453 } catch (FrameworkException $e) {
454 // Catch all exceptions and store them in last error
455 $this->setLastException($e);
458 // Return the gathered result
463 * "Inserts" a data set instance into a local file database folder
465 * @param $dataSetInstance A storeable data set
467 * @throws SqlException If an SQL error occurs
469 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
470 // Try to save the request away
472 // Create full path name
473 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
475 // Write the data away
476 $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
478 // Update the primary key
479 $this->updatePrimaryKey($dataSetInstance);
481 // Reset last exception
482 $this->resetLastException();
483 } catch (FrameworkException $e) {
484 // Catch all exceptions and store them in last error
485 $this->setLastException($e);
487 // Throw an SQL exception
488 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);
493 * "Updates" a data set instance with a database layer
495 * @param $dataSetInstance A storeable data set
497 * @throws SqlException If an SQL error occurs
499 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
500 // Create full path name
501 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/';
503 // Try all the requests
505 // Get a file pointer instance
506 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
508 // Initialize limit/skip
512 // Get the criteria array from the dataset
513 $searchArray = $dataSetInstance->getCriteriaArray();
515 // Get search criteria
516 $searchInstance = $dataSetInstance->getSearchInstance();
518 // Read the directory with some exceptions
519 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
520 // Does the extension match?
521 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
523 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
528 // Open this file for reading
529 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
530 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
533 if (is_array($dataArray)) {
534 // Default is nothing found
537 // Search in the criteria with FMFW (First Matches, First Wins)
538 foreach ($dataArray as $key => $value) {
539 // Make sure value is not bool
540 assert(!is_bool($value));
543 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
547 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
548 if ($isFound === TRUE) {
549 // Shall we skip this entry?
550 if ($searchInstance->getSkip() > 0) {
551 // We shall skip some entries
552 if ($skipFound < $searchInstance->getSkip()) {
554 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
560 // Entry found, so update it
561 foreach ($searchArray as $searchKey => $searchValue) {
562 // Make sure the value is not bool again
563 assert(!is_bool($searchValue));
564 assert($searchKey != $this->indexKey);
566 // Debug message + add/update it
567 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
568 $dataArray[$searchKey] = $searchValue;
571 // Write the data to a local file
572 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $dataFile . ' ...');
573 $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
575 // Count found entries up
581 // Close the file pointer
582 $directoryInstance->closeDirectory();
584 // Update the primary key
585 $this->updatePrimaryKey($dataSetInstance);
587 // Reset last exception
588 $this->resetLastException();
589 } catch (FrameworkException $e) {
590 // Catch all exceptions and store them in last error
591 $this->setLastException($e);
593 // Throw an SQL exception
594 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);
599 * Getter for primary key of specified table or if not found null will be
600 * returned. This must be database-specific.
602 * @param $tableName Name of the table we need the primary key from
603 * @return $primaryKey Primary key column of the given table
605 public function getPrimaryKeyOfTable ($tableName) {
606 // Default key is null
609 // Does the table information exist?
610 if (isset($this->tableInfo[$tableName])) {
611 // Then return the primary key
612 $primaryKey = $this->tableInfo[$tableName]['primary'];
620 * Removes non-public data from given array.
622 * @param $data An array with possible non-public data that needs to be removed.
623 * @return $data A cleaned up array with only public data.
624 * @todo Add more generic non-public data for removal
626 public function removeNonPublicDataFromArray (array $data) {
628 unset($data[$this->indexKey]);
631 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, TRUE));
636 * Counts total rows of given table
638 * @param $tableName Table name
639 * @return $count Total rows of given table
641 public function countTotalRows($tableName) {
642 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
644 // Create full path name
645 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
647 // Try all the requests
649 // Get a file pointer instance
650 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
652 // Initialize counter
655 // Read the directory with some exceptions
656 while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
657 // Does the extension match?
658 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
660 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
666 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
669 } catch (FrameworkException $e) {
670 // Catch all exceptions and store them in last error
671 $this->setLastException($e);
673 // Throw an SQL exception
674 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);
678 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');