* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseIndex extends BaseFrameworkSystem implements Indexable { // Load traits use IteratorTrait; /** * Minimum block length */ private static $minimumBlockLength = 0; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct (string $className) { // Call parent constructor parent::__construct($className); } /** * Reads the file header * * @return void * @throws UnexpectedValueException If header length or count of elements is invalid */ public function readFileHeader () { // First rewind to beginning as the header sits at the beginning ... /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); $this->getIteratorInstance()->rewind(); // Then read it (see constructor for calculation) $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize()); // Have all requested bytes been read? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize())); if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) { // Invalid header length throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d', strlen($data), $data, $this->getIteratorInstance()->getHeaderSize() )); } elseif (empty(trim($data, chr(0)))) { // Empty file header /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: File header is empty - EXIT!'); return; } elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) { // Bad last character throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"', $data, chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES) )); } // Okay, then remove it $data = substr($data, 0, -1); // And update seek position /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->updateSeekPosition() ...'); $this->getIteratorInstance()->updateSeekPosition(); /* * Now split it: * * 0 => magic * 1 => total entries */ $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data); // Check if the array has only 3 elements /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header()=%d', count($header))); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header(%d)=%s', count($header), print_r($header, true))); if (count($header) != 2) { // Bad header throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header))); } elseif ($header[0] !== Indexable::INDEX_MAGIC) { // Magic must be in first element throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], Indexable::INDEX_MAGIC)); } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) { // Length of total entries not matching throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BaseBinaryFile::LENGTH_COUNT)); } // Decode count $header[1] = hex2bin($header[1]); // Set it here $this->getIteratorInstance()->setHeader($header); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** * Flushes the file header * * @return void */ public function flushFileHeader () { // Put all informations together /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); $header = sprintf('%s%s%s%s', // Magic Indexable::INDEX_MAGIC, // Separator header data chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), // Total entries str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT), // Separator header<->entries chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES) ); // Write it to disk (header is always at seek position 0) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header)); $this->getIteratorInstance()->writeAtPosition(0, $header); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** * Initializes this index * * @param $fileInfoInstance An instance of a SplFileInfo class * @return void * @todo Currently the index file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole index file. */ protected function initIndex (SplFileInfo $fileInfoInstance) { // Get a file i/o pointer instance for index file /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this)); // Get iterator instance $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', [$fileInstance]); // Set iterator here $this->setIteratorInstance($iteratorInstance); // Calculate header size $headerSize = ( strlen(Indexable::INDEX_MAGIC) + strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) + BaseBinaryFile::LENGTH_COUNT + strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) ); // Set it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Setting headerSize=%d ...', $headerSize)); $this->getIteratorInstance()->setHeaderSize($headerSize); // Init counters and gaps array /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->initCountersGapsArray() ...'); $this->getIteratorInstance()->initCountersGapsArray(); // Default is not created $created = false; // Is the file's header initialized? if (!$this->getIteratorInstance()->isFileHeaderInitialized()) { // First pre-allocate a bit /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->preAllocateFile(index) ...'); $this->getIteratorInstance()->preAllocateFile('index'); // Then write file header /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->createFileHeader() ...'); $this->getIteratorInstance()->createFileHeader(); // Mark as freshly created $created = true; } // Load the file header /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->readFileHeader() ...'); $this->readFileHeader(); // Freshly created? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: created=%d', intval($created))); if (!$created) { // Analyze file structure /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->analyzeFileStructure() ...'); $this->getIteratorInstance()->analyzeFileStructure(); } // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** * Calculates minimum length for one entry/block * * @return $length Minimum length for one entry/block */ public function calculateMinimumBlockLength () { // Is it "cached"? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); if (self::$minimumBlockLength == 0) { // Calulcate it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calculating ...'); self::$minimumBlockLength = ( // Type BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + // Position BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES)) ); } // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength)); return self::$minimumBlockLength; } /** * Determines whether the EOF has been reached * * @return $isEndOfFileReached Whether the EOF has been reached * @throws UnsupportedOperationException If this method is called */ public function isEndOfFileReached () { throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Initializes counter for valid entries, arrays for damaged entries and * an array for gap seek positions. If you call this method on your own, * please re-analyze the file structure. So you are better to call * analyzeFileStructure() instead of this method. * * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public function initCountersGapsArray () { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Getter for header size * * @return $totalEntries Size of file header * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public final function getHeaderSize () { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Setter for header size * * @param $headerSize Size of file header * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public final function setHeaderSize (int $headerSize) { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Getter for header array * * @return $totalEntries Size of file header * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public final function getHeader () { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Setter for header * * @param $header Array for a file header * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public final function setHeader (array $header) { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Updates seekPosition attribute from file to avoid to much access on file. * * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public function updateSeekPosition () { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Getter for total entries * * @return $totalEntries Total entries in this file * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public final function getCounter () { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * "Getter" for file size * * @return $fileSize Size of currently loaded file */ public function getFileSize () { // Call iterator's method return $this->getIteratorInstance()->getFileSize(); } /** * Writes data at given position * * @param $seekPosition Seek position * @param $data Data to be written * @param $flushHeader Whether to flush the header (default: flush) * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s,data[]=%s,flushHeader=%d - CALLED!', $seekPosition, gettype($data), intval($flushHeader))); throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Writes given value to the file and returns a hash and gap position for it * * @param $groupId Group identifier * @param $value Value to be added to the stack * @return $data Hash and gap position * @throws UnsupportedOperationException If this method is called */ public function writeValueToFile (string $groupId, $value) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,value[%s]=%s - CALLED!', $groupId, gettype($value), print_r($value, true))); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } /** * Writes given raw data to the file and returns a gap position and length * * @param $groupId Group identifier * @param $hash Hash from encoded value * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded))); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } }