3 namespace CoreFramework\Database\Backend\Lfdb;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
9 * Database backend class for storing objects in locally created files.
11 * This class serializes arrays stored in the dataset instance and saves them
12 * to local files. Every file (except 'info') represents a single line. Every
13 * directory within the 'db' (base) directory represents a table.
15 * A configurable 'file_io_class' is being used as "storage backend".
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
38 * The file's extension
40 private $fileExtension = 'serialized';
43 * The last read file's name
45 private $lastFile = '';
48 * The last read file's content including header information
50 private $lastContents = array();
53 * Whether the "connection is already up
55 private $alreadyConnected = FALSE;
58 * Table information array
60 private $tableInfo = array();
65 private $indexKey = '__idx';
68 * The protected constructor. Do never instance from outside! You need to
69 * set a local file path. The class will then validate it.
73 protected function __construct () {
74 // Call parent constructor
75 parent::__construct(__CLASS__);
79 * Create an object of CachedLocalFileDatabase and set the save path from
80 * configuration for local files.
82 * @return $databaseInstance An instance of CachedLocalFileDatabase
84 public static final function createCachedLocalFileDatabase () {
86 $databaseInstance = new CachedLocalFileDatabase();
88 // Get a new compressor channel instance
89 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
91 // Set the compressor channel
92 $databaseInstance->setCompressorChannel($compressorInstance);
94 // Get a file IO handler
95 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
98 $databaseInstance->setFileIoInstance($fileIoInstance);
100 // "Connect" to the database
101 $databaseInstance->connectToDatabase();
103 // Return database instance
104 return $databaseInstance;
108 * Setter for the last read file
110 * @param $fqfn The FQFN of the last read file
113 private final function setLastFile ($fqfn) {
114 // Cast string and set it
115 $this->lastFile = (string) $fqfn;
119 * Getter for last read file
121 * @return $lastFile The last read file's name with full path
123 public final function getLastFile () {
124 return $this->lastFile;
128 * Setter for contents of the last read file
130 * @param $contents An array with header and data elements
133 private final function setLastFileContents (array $contents) {
135 $this->lastContents = $contents;
139 * Getter for last read file's content as an array
141 * @return $lastContent The array with elements 'header' and 'data'.
143 public final function getLastContents () {
144 return $this->lastContents;
148 * Getter for file extension
150 * @return $fileExtension The array with elements 'header' and 'data'.
152 public final function getFileExtension () {
153 return $this->fileExtension;
157 * Getter for index key
159 * @return $indexKey Index key
161 public final function getIndexKey () {
162 return $this->indexKey;
166 * Reads a local data file and returns it's contents in an array
168 * @param $fqfn The FQFN for the requested file
171 private function getDataArrayFromFile ($fqfn) {
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
175 // Init compressed data
176 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
177 $compressedData = $compressedData['data'];
179 // Close the file and throw the instance away
180 unset($fileInstance);
183 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
186 $dataArray = json_decode($serializedData, TRUE);
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
197 * Writes data array to local file
199 * @param $fqfn The FQFN of the local file
200 * @param $dataArray An array with all the data we shall write
203 private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
206 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
208 // Serialize and compress it
209 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
212 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
214 // Write this data BASE64 encoded to the file
215 $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
218 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
222 * Getter for table information file contents or an empty if info file was not created
224 * @param $dataSetInstance An instance of a database set class
225 * @return $infoArray An array with all table informations
227 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
228 // Default content is no data
229 $infoArray = array();
231 // Create FQFN for getting the table information file
232 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
234 // Get the file contents
236 $infoArray = $this->getDataArrayFromFile($fqfn);
237 } catch (FileNotFoundException $e) {
238 // Not found, so ignore it here
246 * Generates an FQFN from given dataset instance and string
248 * @param $dataSetInstance An instance of a database set class
249 * @param $rowName Name of the row
250 * @return $fqfn The FQFN for this row
252 private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
254 $fqfn = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/' . $rowName . '.' . $this->getFileExtension();
261 * Creates the table info file from given dataset instance
263 * @param $dataSetInstance An instance of a database set class
266 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
267 // Create FQFN for creating the table information file
268 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
270 // Get the data out from dataset in a local array
271 $this->tableInfo[$dataSetInstance->getTableName()] = array(
272 'primary' => $dataSetInstance->getPrimaryKey(),
274 'last_updated' => time()
277 // Write the data to the file
278 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
282 * Updates the table info file from given dataset instance
284 * @param $dataSetInstance An instance of a database set class
287 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
288 // Get table name from criteria
289 $tableName = $dataSetInstance->getTableName();
291 // Create FQFN for creating the table information file
292 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
294 // Get the data out from dataset in a local array
295 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
296 $this->tableInfo[$tableName]['last_updated'] = time();
298 // Write the data to the file
299 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
303 * Updates the primary key information or creates the table info file if not found
305 * @param $dataSetInstance An instance of a database set class
308 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
309 // Get table name from criteria
310 $tableName = $dataSetInstance->getTableName();
312 // Get the information array from lower method
313 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
315 // Is the primary key there?
316 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, TRUE));
317 if (!isset($this->tableInfo[$tableName]['primary'])) {
318 // Then create the info file
319 $this->createTableInfoFile($dataSetInstance);
320 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
321 // Set the array element
322 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
325 $this->updateTableInfoFile($dataSetInstance);
330 * Makes sure that the database connection is alive
333 * @todo Do some checks on the database directory and files here
335 public function connectToDatabase () {
339 * Starts a SELECT query on the database by given return type, table name
340 * and search criteria
342 * @param $tableName Name of the database table
343 * @param $searchInstance Local search criteria class
344 * @return $resultData Result data of the query
345 * @throws UnsupportedCriteriaException If the criteria is unsupported
346 * @throws SqlException If an 'SQL error' occurs
348 public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
350 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
352 // The result is null by any errors
355 // Create full path name
356 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
359 * A 'select' query is not that easy on local files, so first try to
360 * find the 'table' which is in fact a directory on the server
363 // Get a directory pointer instance
364 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
366 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
368 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
369 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
372 // Initialize limit/skip
377 // Read the directory with some exceptions
378 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
380 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',this->getFileExtension()=' . $this->getFileExtension());
382 // Does the extension match?
383 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
389 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
390 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
393 if (is_array($dataArray)) {
394 // Default is nothing found
397 // Search in the criteria with FMFW (First Matches, First Wins)
398 foreach ($dataArray as $key => $value) {
399 // Make sure value is not bool
400 assert(!is_bool($value));
403 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
404 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
408 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
409 if ($isFound === TRUE) {
410 // Shall we skip this entry?
411 if ($searchInstance->getSkip() > 0) {
412 // We shall skip some entries
413 if ($skipFound < $searchInstance->getSkip()) {
421 $dataArray[$this->getIndexKey()] = $idx;
424 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, TRUE));
425 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
427 // Count found entries up
431 // Throw an exception here
432 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
439 // Close directory and throw the instance away
440 $directoryInstance->closeDirectory();
441 unset($directoryInstance);
443 // Reset last exception
444 $this->resetLastException();
445 } catch (PathIsNoDirectoryException $e) {
446 // Path not found means "table not found" for real databases...
447 $this->setLastException($e);
449 // So throw an SqlException here with faked error message
450 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
451 } catch (FrameworkException $e) {
452 // Catch all exceptions and store them in last error
453 $this->setLastException($e);
456 // Return the gathered result
461 * "Inserts" a data set instance into a local file database folder
463 * @param $dataSetInstance A storeable data set
465 * @throws SqlException If an SQL error occurs
467 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
468 // Try to save the request away
470 // Create full path name
471 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
473 // Write the data away
474 $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
476 // Update the primary key
477 $this->updatePrimaryKey($dataSetInstance);
479 // Reset last exception
480 $this->resetLastException();
481 } catch (FrameworkException $e) {
482 // Catch all exceptions and store them in last error
483 $this->setLastException($e);
485 // Throw an SQL exception
486 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);
491 * "Updates" a data set instance with a database layer
493 * @param $dataSetInstance A storeable data set
495 * @throws SqlException If an SQL error occurs
497 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
498 // Create full path name
499 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/';
501 // Try all the requests
503 // Get a file pointer instance
504 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
506 // Initialize limit/skip
510 // Get the criteria array from the dataset
511 $searchArray = $dataSetInstance->getCriteriaArray();
513 // Get search criteria
514 $searchInstance = $dataSetInstance->getSearchInstance();
516 // Read the directory with some exceptions
517 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
518 // Does the extension match?
519 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
521 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
526 // Open this file for reading
527 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
528 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
531 if (is_array($dataArray)) {
532 // Default is nothing found
535 // Search in the criteria with FMFW (First Matches, First Wins)
536 foreach ($dataArray as $key => $value) {
537 // Make sure value is not bool
538 assert(!is_bool($value));
541 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
545 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
546 if ($isFound === TRUE) {
547 // Shall we skip this entry?
548 if ($searchInstance->getSkip() > 0) {
549 // We shall skip some entries
550 if ($skipFound < $searchInstance->getSkip()) {
552 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
558 // Entry found, so update it
559 foreach ($searchArray as $searchKey => $searchValue) {
560 // Make sure the value is not bool again
561 assert(!is_bool($searchValue));
562 assert($searchKey != $this->indexKey);
564 // Debug message + add/update it
565 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
566 $dataArray[$searchKey] = $searchValue;
569 // Write the data to a local file
570 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $dataFile . ' ...');
571 $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
573 // Count found entries up
579 // Close the file pointer
580 $directoryInstance->closeDirectory();
582 // Update the primary key
583 $this->updatePrimaryKey($dataSetInstance);
585 // Reset last exception
586 $this->resetLastException();
587 } catch (FrameworkException $e) {
588 // Catch all exceptions and store them in last error
589 $this->setLastException($e);
591 // Throw an SQL exception
592 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);
597 * Getter for primary key of specified table or if not found null will be
598 * returned. This must be database-specific.
600 * @param $tableName Name of the table we need the primary key from
601 * @return $primaryKey Primary key column of the given table
603 public function getPrimaryKeyOfTable ($tableName) {
604 // Default key is null
607 // Does the table information exist?
608 if (isset($this->tableInfo[$tableName])) {
609 // Then return the primary key
610 $primaryKey = $this->tableInfo[$tableName]['primary'];
618 * Removes non-public data from given array.
620 * @param $data An array with possible non-public data that needs to be removed.
621 * @return $data A cleaned up array with only public data.
622 * @todo Add more generic non-public data for removal
624 public function removeNonPublicDataFromArray (array $data) {
626 unset($data[$this->indexKey]);
629 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, TRUE));
634 * Counts total rows of given table
636 * @param $tableName Table name
637 * @return $count Total rows of given table
639 public function countTotalRows($tableName) {
640 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
642 // Create full path name
643 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
645 // Try all the requests
647 // Get a file pointer instance
648 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
650 // Initialize counter
653 // Read the directory with some exceptions
654 while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
655 // Does the extension match?
656 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
658 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
664 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
667 } catch (FrameworkException $e) {
668 // Catch all exceptions and store them in last error
669 $this->setLastException($e);
671 // Throw an SQL exception
672 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);
676 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');