3 namespace CoreFramework\Database\Backend\Lfdb;
6 * Database backend class for storing objects in locally created files.
8 * This class serializes arrays stored in the dataset instance and saves them
9 * to local files. Every file (except 'info') represents a single line. Every
10 * directory within the 'db' (base) directory represents a table.
12 * A configurable 'file_io_class' is being used as "storage backend".
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
35 * The file's extension
37 private $fileExtension = 'serialized';
40 * The last read file's name
42 private $lastFile = '';
45 * The last read file's content including header information
47 private $lastContents = array();
50 * Whether the "connection is already up
52 private $alreadyConnected = FALSE;
55 * Table information array
57 private $tableInfo = array();
62 private $indexKey = '__idx';
65 * The protected constructor. Do never instance from outside! You need to
66 * set a local file path. The class will then validate it.
70 protected function __construct () {
71 // Call parent constructor
72 parent::__construct(__CLASS__);
76 * Create an object of CachedLocalFileDatabase and set the save path from
77 * configuration for local files.
79 * @return $databaseInstance An instance of CachedLocalFileDatabase
81 public static final function createCachedLocalFileDatabase () {
83 $databaseInstance = new CachedLocalFileDatabase();
85 // Get a new compressor channel instance
86 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
88 // Set the compressor channel
89 $databaseInstance->setCompressorChannel($compressorInstance);
91 // Get a file IO handler
92 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
95 $databaseInstance->setFileIoInstance($fileIoInstance);
97 // "Connect" to the database
98 $databaseInstance->connectToDatabase();
100 // Return database instance
101 return $databaseInstance;
105 * Setter for the last read file
107 * @param $fqfn The FQFN of the last read file
110 private final function setLastFile ($fqfn) {
111 // Cast string and set it
112 $this->lastFile = (string) $fqfn;
116 * Getter for last read file
118 * @return $lastFile The last read file's name with full path
120 public final function getLastFile () {
121 return $this->lastFile;
125 * Setter for contents of the last read file
127 * @param $contents An array with header and data elements
130 private final function setLastFileContents (array $contents) {
132 $this->lastContents = $contents;
136 * Getter for last read file's content as an array
138 * @return $lastContent The array with elements 'header' and 'data'.
140 public final function getLastContents () {
141 return $this->lastContents;
145 * Getter for file extension
147 * @return $fileExtension The array with elements 'header' and 'data'.
149 public final function getFileExtension () {
150 return $this->fileExtension;
154 * Getter for index key
156 * @return $indexKey Index key
158 public final function getIndexKey () {
159 return $this->indexKey;
163 * Reads a local data file and returns it's contents in an array
165 * @param $fqfn The FQFN for the requested file
168 private function getDataArrayFromFile ($fqfn) {
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
172 // Init compressed data
173 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
174 $compressedData = $compressedData['data'];
176 // Close the file and throw the instance away
177 unset($fileInstance);
180 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
183 $dataArray = json_decode($serializedData, TRUE);
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
194 * Writes data array to local file
196 * @param $fqfn The FQFN of the local file
197 * @param $dataArray An array with all the data we shall write
200 private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
203 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
205 // Serialize and compress it
206 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
209 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
211 // Write this data BASE64 encoded to the file
212 $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
215 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
219 * Getter for table information file contents or an empty if info file was not created
221 * @param $dataSetInstance An instance of a database set class
222 * @return $infoArray An array with all table informations
224 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
225 // Default content is no data
226 $infoArray = array();
228 // Create FQFN for getting the table information file
229 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
231 // Get the file contents
233 $infoArray = $this->getDataArrayFromFile($fqfn);
234 } catch (FileNotFoundException $e) {
235 // Not found, so ignore it here
243 * Generates an FQFN from given dataset instance and string
245 * @param $dataSetInstance An instance of a database set class
246 * @param $rowName Name of the row
247 * @return $fqfn The FQFN for this row
249 private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
251 $fqfn = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/' . $rowName . '.' . $this->getFileExtension();
258 * Creates the table info file from given dataset instance
260 * @param $dataSetInstance An instance of a database set class
263 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
264 // Create FQFN for creating the table information file
265 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
267 // Get the data out from dataset in a local array
268 $this->tableInfo[$dataSetInstance->getTableName()] = array(
269 'primary' => $dataSetInstance->getPrimaryKey(),
271 'last_updated' => time()
274 // Write the data to the file
275 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
279 * Updates the table info file from given dataset instance
281 * @param $dataSetInstance An instance of a database set class
284 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
285 // Get table name from criteria
286 $tableName = $dataSetInstance->getTableName();
288 // Create FQFN for creating the table information file
289 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
291 // Get the data out from dataset in a local array
292 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
293 $this->tableInfo[$tableName]['last_updated'] = time();
295 // Write the data to the file
296 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
300 * Updates the primary key information or creates the table info file if not found
302 * @param $dataSetInstance An instance of a database set class
305 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
306 // Get table name from criteria
307 $tableName = $dataSetInstance->getTableName();
309 // Get the information array from lower method
310 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
312 // Is the primary key there?
313 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, TRUE));
314 if (!isset($this->tableInfo[$tableName]['primary'])) {
315 // Then create the info file
316 $this->createTableInfoFile($dataSetInstance);
317 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
318 // Set the array element
319 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
322 $this->updateTableInfoFile($dataSetInstance);
327 * Makes sure that the database connection is alive
330 * @todo Do some checks on the database directory and files here
332 public function connectToDatabase () {
336 * Starts a SELECT query on the database by given return type, table name
337 * and search criteria
339 * @param $tableName Name of the database table
340 * @param $searchInstance Local search criteria class
341 * @return $resultData Result data of the query
342 * @throws UnsupportedCriteriaException If the criteria is unsupported
343 * @throws SqlException If an 'SQL error' occurs
345 public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
347 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
349 // The result is null by any errors
352 // Create full path name
353 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
356 * A 'select' query is not that easy on local files, so first try to
357 * find the 'table' which is in fact a directory on the server
360 // Get a directory pointer instance
361 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
363 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
365 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
366 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
369 // Initialize limit/skip
374 // Read the directory with some exceptions
375 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
377 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',this->getFileExtension()=' . $this->getFileExtension());
379 // Does the extension match?
380 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
386 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
387 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
390 if (is_array($dataArray)) {
391 // Default is nothing found
394 // Search in the criteria with FMFW (First Matches, First Wins)
395 foreach ($dataArray as $key => $value) {
396 // Make sure value is not bool
397 assert(!is_bool($value));
400 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
401 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
405 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
406 if ($isFound === TRUE) {
407 // Shall we skip this entry?
408 if ($searchInstance->getSkip() > 0) {
409 // We shall skip some entries
410 if ($skipFound < $searchInstance->getSkip()) {
418 $dataArray[$this->getIndexKey()] = $idx;
421 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, TRUE));
422 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
424 // Count found entries up
428 // Throw an exception here
429 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
436 // Close directory and throw the instance away
437 $directoryInstance->closeDirectory();
438 unset($directoryInstance);
440 // Reset last exception
441 $this->resetLastException();
442 } catch (PathIsNoDirectoryException $e) {
443 // Path not found means "table not found" for real databases...
444 $this->setLastException($e);
446 // So throw an SqlException here with faked error message
447 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
448 } catch (FrameworkException $e) {
449 // Catch all exceptions and store them in last error
450 $this->setLastException($e);
453 // Return the gathered result
458 * "Inserts" a data set instance into a local file database folder
460 * @param $dataSetInstance A storeable data set
462 * @throws SqlException If an SQL error occurs
464 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
465 // Try to save the request away
467 // Create full path name
468 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
470 // Write the data away
471 $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
473 // Update the primary key
474 $this->updatePrimaryKey($dataSetInstance);
476 // Reset last exception
477 $this->resetLastException();
478 } catch (FrameworkException $e) {
479 // Catch all exceptions and store them in last error
480 $this->setLastException($e);
482 // Throw an SQL exception
483 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);
488 * "Updates" a data set instance with a database layer
490 * @param $dataSetInstance A storeable data set
492 * @throws SqlException If an SQL error occurs
494 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
495 // Create full path name
496 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/';
498 // Try all the requests
500 // Get a file pointer instance
501 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
503 // Initialize limit/skip
507 // Get the criteria array from the dataset
508 $searchArray = $dataSetInstance->getCriteriaArray();
510 // Get search criteria
511 $searchInstance = $dataSetInstance->getSearchInstance();
513 // Read the directory with some exceptions
514 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
515 // Does the extension match?
516 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
518 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
523 // Open this file for reading
524 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
525 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
528 if (is_array($dataArray)) {
529 // Default is nothing found
532 // Search in the criteria with FMFW (First Matches, First Wins)
533 foreach ($dataArray as $key => $value) {
534 // Make sure value is not bool
535 assert(!is_bool($value));
538 $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
542 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: isFound=' . intval($isFound));
543 if ($isFound === TRUE) {
544 // Shall we skip this entry?
545 if ($searchInstance->getSkip() > 0) {
546 // We shall skip some entries
547 if ($skipFound < $searchInstance->getSkip()) {
549 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Found entry, but skipping ...');
555 // Entry found, so update it
556 foreach ($searchArray as $searchKey => $searchValue) {
557 // Make sure the value is not bool again
558 assert(!is_bool($searchValue));
559 assert($searchKey != $this->indexKey);
561 // Debug message + add/update it
562 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
563 $dataArray[$searchKey] = $searchValue;
566 // Write the data to a local file
567 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $dataFile . ' ...');
568 $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
570 // Count found entries up
576 // Close the file pointer
577 $directoryInstance->closeDirectory();
579 // Update the primary key
580 $this->updatePrimaryKey($dataSetInstance);
582 // Reset last exception
583 $this->resetLastException();
584 } catch (FrameworkException $e) {
585 // Catch all exceptions and store them in last error
586 $this->setLastException($e);
588 // Throw an SQL exception
589 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);
594 * Getter for primary key of specified table or if not found null will be
595 * returned. This must be database-specific.
597 * @param $tableName Name of the table we need the primary key from
598 * @return $primaryKey Primary key column of the given table
600 public function getPrimaryKeyOfTable ($tableName) {
601 // Default key is null
604 // Does the table information exist?
605 if (isset($this->tableInfo[$tableName])) {
606 // Then return the primary key
607 $primaryKey = $this->tableInfo[$tableName]['primary'];
615 * Removes non-public data from given array.
617 * @param $data An array with possible non-public data that needs to be removed.
618 * @return $data A cleaned up array with only public data.
619 * @todo Add more generic non-public data for removal
621 public function removeNonPublicDataFromArray (array $data) {
623 unset($data[$this->indexKey]);
626 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, TRUE));
631 * Counts total rows of given table
633 * @param $tableName Table name
634 * @return $count Total rows of given table
636 public function countTotalRows($tableName) {
637 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
639 // Create full path name
640 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
642 // Try all the requests
644 // Get a file pointer instance
645 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
647 // Initialize counter
650 // Read the directory with some exceptions
651 while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
652 // Does the extension match?
653 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
655 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
661 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
664 } catch (FrameworkException $e) {
665 // Catch all exceptions and store them in last error
666 $this->setLastException($e);
668 // Throw an SQL exception
669 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);
673 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');