3 namespace CoreFramework\Database\Backend\Lfdb;
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use CoreFramework\Criteria\Storing\StoreableCriteria;
9 use CoreFramework\Database\Backend\BaseDatabaseBackend;
10 use CoreFramework\Database\Backend\DatabaseBackend;
11 use CoreFramework\Factory\ObjectFactory;
12 use CoreFramework\Filesystem\FileNotFoundException;
13 use CoreFramework\Generic\FrameworkException;
16 * Database backend class for storing objects in locally created files.
18 * This class serializes arrays stored in the dataset instance and saves them
19 * to local files. Every file (except 'info') represents a single line. Every
20 * directory within the 'db' (base) directory represents a table.
22 * A configurable 'file_io_class' is being used as "storage backend".
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
45 * The file's extension
47 private $fileExtension = 'serialized';
50 * The last read file's name
52 private $lastFile = '';
55 * The last read file's content including header information
57 private $lastContents = array();
60 * Whether the "connection is already up
62 private $alreadyConnected = false;
65 * Table information array
67 private $tableInfo = array();
72 private $indexKey = '__idx';
75 * The protected constructor. Do never instance from outside! You need to
76 * set a local file path. The class will then validate it.
80 protected function __construct () {
81 // Call parent constructor
82 parent::__construct(__CLASS__);
86 * Create an object of CachedLocalFileDatabase and set the save path from
87 * configuration for local files.
89 * @return $databaseInstance An instance of CachedLocalFileDatabase
91 public static final function createCachedLocalFileDatabase () {
93 $databaseInstance = new CachedLocalFileDatabase();
95 // Get a new compressor channel instance
96 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
98 // Set the compressor channel
99 $databaseInstance->setCompressorChannel($compressorInstance);
101 // Get a file IO handler
102 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
105 $databaseInstance->setFileIoInstance($fileIoInstance);
107 // "Connect" to the database
108 $databaseInstance->connectToDatabase();
110 // Return database instance
111 return $databaseInstance;
115 * Setter for the last read file
117 * @param $fqfn The FQFN of the last read file
120 private final function setLastFile ($fqfn) {
121 // Cast string and set it
122 $this->lastFile = (string) $fqfn;
126 * Getter for last read file
128 * @return $lastFile The last read file's name with full path
130 public final function getLastFile () {
131 return $this->lastFile;
135 * Setter for contents of the last read file
137 * @param $contents An array with header and data elements
140 private final function setLastFileContents (array $contents) {
142 $this->lastContents = $contents;
146 * Getter for last read file's content as an array
148 * @return $lastContent The array with elements 'header' and 'data'.
150 public final function getLastContents () {
151 return $this->lastContents;
155 * Getter for file extension
157 * @return $fileExtension The array with elements 'header' and 'data'.
159 public final function getFileExtension () {
160 return $this->fileExtension;
164 * Getter for index key
166 * @return $indexKey Index key
168 public final function getIndexKey () {
169 return $this->indexKey;
173 * Reads a local data file and returns it's contents in an array
175 * @param $fqfn The FQFN for the requested file
178 private function getDataArrayFromFile ($fqfn) {
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
182 // Init compressed data
183 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
184 $compressedData = $compressedData['data'];
186 // Close the file and throw the instance away
187 unset($fileInstance);
190 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
193 $dataArray = json_decode($serializedData, true);
196 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
197 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
204 * Writes data array to local file
206 * @param $fqfn The FQFN of the local file
207 * @param $dataArray An array with all the data we shall write
210 private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
212 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
213 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
215 // Serialize and compress it
216 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
221 // Write this data BASE64 encoded to the file
222 $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
225 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
229 * Getter for table information file contents or an empty if info file was not created
231 * @param $dataSetInstance An instance of a database set class
232 * @return $infoArray An array with all table informations
234 private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
235 // Default content is no data
236 $infoArray = array();
238 // Create FQFN for getting the table information file
239 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
241 // Get the file contents
243 $infoArray = $this->getDataArrayFromFile($fqfn);
244 } catch (FileNotFoundException $e) {
245 // Not found, so ignore it here
253 * Generates an FQFN from given dataset instance and string
255 * @param $dataSetInstance An instance of a database set class
256 * @param $rowName Name of the row
257 * @return $fqfn The FQFN for this row
259 private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
261 $fqfn = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension();
268 * Creates the table info file from given dataset instance
270 * @param $dataSetInstance An instance of a database set class
273 private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
274 // Create FQFN for creating the table information file
275 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
277 // Get the data out from dataset in a local array
278 $this->tableInfo[$dataSetInstance->getTableName()] = array(
279 'primary' => $dataSetInstance->getPrimaryKey(),
281 'last_updated' => time()
284 // Write the data to the file
285 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
289 * Updates the table info file from given dataset instance
291 * @param $dataSetInstance An instance of a database set class
294 private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
295 // Get table name from criteria
296 $tableName = $dataSetInstance->getTableName();
298 // Create FQFN for creating the table information file
299 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
301 // Get the data out from dataset in a local array
302 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
303 $this->tableInfo[$tableName]['last_updated'] = time();
305 // Write the data to the file
306 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
310 * Updates the primary key information or creates the table info file if not found
312 * @param $dataSetInstance An instance of a database set class
315 private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
316 // Get table name from criteria
317 $tableName = $dataSetInstance->getTableName();
319 // Get the information array from lower method
320 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
322 // Is the primary key there?
323 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableInfo=' . print_r($this->tableInfo, true));
324 if (!isset($this->tableInfo[$tableName]['primary'])) {
325 // Then create the info file
326 $this->createTableInfoFile($dataSetInstance);
327 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo[$tableName]['primary'])) {
328 // Set the array element
329 $this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
332 $this->updateTableInfoFile($dataSetInstance);
337 * Makes sure that the database connection is alive
340 * @todo Do some checks on the database directory and files here
342 public function connectToDatabase () {
346 * Starts a SELECT query on the database by given return type, table name
347 * and search criteria
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
355 public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
357 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
359 // The result is null by any errors
362 // Create full path name
363 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
366 * A 'select' query is not that easy on local files, so first try to
367 * find the 'table' which is in fact a directory on the server
370 // Get a directory pointer instance
371 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
373 // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
375 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
376 BaseDatabaseBackend::RESULT_INDEX_ROWS => array()
379 // Initialize limit/skip
384 // Read the directory with some exceptions
385 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
387 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',this->getFileExtension()=' . $this->getFileExtension());
389 // Does the extension match?
390 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
396 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
397 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, true));
400 if (is_array($dataArray)) {
401 // Default is nothing found
404 // Search in the criteria with FMFW (First Matches, First Wins)
405 foreach ($dataArray as $key => $value) {
406 // Make sure value is not bool
407 assert(!is_bool($value));
410 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
411 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound));
415 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound) . ',limitFound=' . $limitFound . ',limit=' . $searchInstance->getLimit());
416 if ($isFound === true) {
417 // Shall we skip this entry?
418 if ($searchInstance->getSkip() > 0) {
419 // We shall skip some entries
420 if ($skipFound < $searchInstance->getSkip()) {
428 $dataArray[$this->getIndexKey()] = $idx;
431 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, true));
432 array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
434 // Count found entries up
438 // Throw an exception here
439 throw new SqlException(array($this, sprintf('File '%s' contains invalid data.', $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
446 // Close directory and throw the instance away
447 $directoryInstance->closeDirectory();
448 unset($directoryInstance);
450 // Reset last exception
451 $this->resetLastException();
452 } catch (PathIsNoDirectoryException $e) {
453 // Path not found means "table not found" for real databases...
454 $this->setLastException($e);
456 // So throw an SqlException here with faked error message
457 throw new SqlException (array($this, sprintf('Table '%s' not found', $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
458 } catch (FrameworkException $e) {
459 // Catch all exceptions and store them in last error
460 $this->setLastException($e);
463 // Return the gathered result
468 * "Inserts" a data set instance into a local file database folder
470 * @param $dataSetInstance A storeable data set
472 * @throws SqlException If an SQL error occurs
474 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
475 // Try to save the request away
477 // Create full path name
478 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
480 // Write the data away
481 $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
483 // Update the primary key
484 $this->updatePrimaryKey($dataSetInstance);
486 // Reset last exception
487 $this->resetLastException();
488 } catch (FrameworkException $e) {
489 // Catch all exceptions and store them in last error
490 $this->setLastException($e);
492 // Throw an SQL exception
493 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);
498 * "Updates" a data set instance with a database layer
500 * @param $dataSetInstance A storeable data set
502 * @throws SqlException If an SQL error occurs
504 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
505 // Create full path name
506 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
508 // Try all the requests
510 // Get a file pointer instance
511 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
513 // Initialize limit/skip
517 // Get the criteria array from the dataset
518 $searchArray = $dataSetInstance->getCriteriaArray();
520 // Get search criteria
521 $searchInstance = $dataSetInstance->getSearchInstance();
523 // Read the directory with some exceptions
524 while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
525 // Does the extension match?
526 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
528 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
533 // Open this file for reading
534 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
535 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, true));
538 if (is_array($dataArray)) {
539 // Default is nothing found
542 // Search in the criteria with FMFW (First Matches, First Wins)
543 foreach ($dataArray as $key => $value) {
544 // Make sure value is not bool
545 assert(!is_bool($value));
548 $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
552 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: isFound=' . intval($isFound));
553 if ($isFound === true) {
554 // Shall we skip this entry?
555 if ($searchInstance->getSkip() > 0) {
556 // We shall skip some entries
557 if ($skipFound < $searchInstance->getSkip()) {
559 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Found entry, but skipping ...');
565 // Entry found, so update it
566 foreach ($searchArray as $searchKey => $searchValue) {
567 // Make sure the value is not bool again
568 assert(!is_bool($searchValue));
569 assert($searchKey != $this->indexKey);
571 // Debug message + add/update it
572 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
573 $dataArray[$searchKey] = $searchValue;
576 // Write the data to a local file
577 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing dataArray()=' . count($dataArray) . ' to ' . $dataFile . ' ...');
578 $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
580 // Count found entries up
586 // Close the file pointer
587 $directoryInstance->closeDirectory();
589 // Update the primary key
590 $this->updatePrimaryKey($dataSetInstance);
592 // Reset last exception
593 $this->resetLastException();
594 } catch (FrameworkException $e) {
595 // Catch all exceptions and store them in last error
596 $this->setLastException($e);
598 // Throw an SQL exception
599 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);
604 * Getter for primary key of specified table or if not found null will be
605 * returned. This must be database-specific.
607 * @param $tableName Name of the table we need the primary key from
608 * @return $primaryKey Primary key column of the given table
610 public function getPrimaryKeyOfTable ($tableName) {
611 // Default key is null
614 // Does the table information exist?
615 if (isset($this->tableInfo[$tableName])) {
616 // Then return the primary key
617 $primaryKey = $this->tableInfo[$tableName]['primary'];
625 * Removes non-public data from given array.
627 * @param $data An array with possible non-public data that needs to be removed.
628 * @return $data A cleaned up array with only public data.
629 * @todo Add more generic non-public data for removal
631 public function removeNonPublicDataFromArray (array $data) {
633 unset($data[$this->indexKey]);
636 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, true));
641 * Counts total rows of given table
643 * @param $tableName Table name
644 * @return $count Total rows of given table
646 public function countTotalRows($tableName) {
647 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
649 // Create full path name
650 $pathName = $this->getConfigInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
652 // Try all the requests
654 // Get a file pointer instance
655 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
657 // Initialize counter
660 // Read the directory with some exceptions
661 while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
662 // Does the extension match?
663 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
665 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
671 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
674 } catch (FrameworkException $e) {
675 // Catch all exceptions and store them in last error
676 $this->setLastException($e);
678 // Throw an SQL exception
679 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);
683 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');