X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ffile_directories%2Fbinary%2Fclass_BaseBinaryFile.php;h=0885e338ca03681f3b4cd9955a3441e714aa751e;hb=f861d6bc00f8a3ecf6b057f2aa93129572643896;hp=d2adfe87c170821c0672e1dc509384295cfaf5b1;hpb=deb07dbc679f4cc7fe0e84eeb352f87f21ccf0aa;p=core.git diff --git a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php index d2adfe87..0885e338 100644 --- a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php +++ b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php @@ -8,8 +8,8 @@ use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint; use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile; use Org\Mxchange\CoreFramework\Filesystem\FilePointer; -use Org\Mxchange\CoreFramework\Index\Indexable; -use Org\Mxchange\CoreFramework\Stack\File\StackableFile; +use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; +use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware; use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait; use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait; @@ -26,7 +26,7 @@ use \UnexpectedValueException; * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -49,66 +49,9 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { use IndexableTrait; /** - * Separator for header data + * Configuration cache */ - const SEPARATOR_HEADER_DATA = 0x01; - - /** - * Separator header->entries - */ - const SEPARATOR_HEADER_ENTRIES = 0x02; - - /** - * Separator group->hash - */ - const SEPARATOR_GROUP_HASH = 0x03; - - /** - * Separator hash->value - */ - const SEPARATOR_HASH_VALUE = 0x04; - - /** - * Separator entry->entry - */ - const SEPARATOR_ENTRIES = 0x05; - - /** - * Separator type->position - */ - const SEPARATOR_TYPE_POSITION = 0x06; - - /** - * Length of count - */ - const LENGTH_COUNT = 20; - - /** - * Length of position - */ - const LENGTH_POSITION = 20; - - /** - * Length of group - */ - const LENGTH_GROUP = 10; - - /** - * Maximum length of entry type - */ - const LENGTH_TYPE = 20; - - //***** Array elements for 'gaps' array ***** - - /** - * Start of gap - */ - const GAPS_INDEX_START = 'start'; - - /** - * End of gap - */ - const GAPS_INDEX_END = 'end'; + private static $configCache = []; /** * Current seek position @@ -275,8 +218,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->gaps()=%d', count($this->gaps))); foreach ($this->gaps as $gap) { // Calculate size of found gap: end-start including both - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gap[%s]=%d,ga[%s]=%d', self::GAPS_INDEX_START, $gap[self::GAPS_INDEX_START], self::GAPS_INDEX_END, $gap[self::GAPS_INDEX_END])); - $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gap[%s]=%d,ga[%s]=%d', BinaryFile::GAPS_INDEX_START, $gap[BinaryFile::GAPS_INDEX_START], BinaryFile::GAPS_INDEX_END, $gap[BinaryFile::GAPS_INDEX_END])); + $gapsSize += ($gap[BinaryFile::GAPS_INDEX_END] - $gap[BinaryFile::GAPS_INDEX_START]); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gapsSize=%d', $gapsSize)); @@ -309,9 +252,16 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { * @return void */ private function markFileGapsOnly (string $type, int $minimumBlockLength) { - // Very simple to do ... + // Is config cache there? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s,minimumBlockLength=%d - CALLED!', $type, $minimumBlockLength)); - for ($idx = 0; $idx < FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); $idx++) { + if (!isset(self::$configCache[$type . '_pre_allocate_count'])) { + // Then set it + self::$configCache[$type . '_pre_allocate_count'] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); + } + + // Very simple to do ... + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: self:configCache[%s_pre_allocate_count]()=%d', count(self::$configCache[$type . '_pre_allocate_count']))); + for ($idx = 0; $idx < self::$configCache[$type . '_pre_allocate_count']; $idx++) { // Calculate start/end positions $startPosition = $idx * $minimumBlockLength; $endPosition = $idx * $minimumBlockLength + $minimumBlockLength; @@ -336,8 +286,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { // Push to gaps array //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: startPosition=%d,endPosition=%d - CALLED!', $startPosition, $endPosition)); array_push($this->gaps, [ - self::GAPS_INDEX_START => $startPosition, - self::GAPS_INDEX_END => $endPosition, + BinaryFile::GAPS_INDEX_START => $startPosition, + BinaryFile::GAPS_INDEX_END => $endPosition, ]); // Trace message @@ -510,11 +460,11 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: str=%s - CALLED!', $str)); if (empty($str)) { // Throw IAE - throw new InvalidArgumentException('Parameter "str" is empty'); + throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Determine it - $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false); + $isFound = (strpos($str, chr(BinaryFile::SEPARATOR_ENTRIES)) !== false); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isFound=%d - EXIT!', intval($isFound))); @@ -539,7 +489,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition)); } elseif (empty($data)) { // Empty data is invalid, too - throw new InvalidArgumentException('Parameter "data" is empty'); + throw new InvalidArgumentException('Parameter "data" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Write data at given position @@ -587,7 +537,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition)); } elseif (empty($dataStream)) { // Empty dataStream - throw new InvalidArgumentException('Parameter "dataStream" is empty'); + throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Call pointer's method @@ -853,19 +803,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { } /** - * Reads next "block" of bytes into $currentBlock field. THis method loads - * the whole file into memory when the file is just freshly initialized - * (only zeros in it). + * Reads next "block" of given bytes into $currentBlock field. THis method + * loads the whole file into memory when the file is just freshly + * initialized (only zeros in it). * * @return void + * @throws InvalidArgumentException If a parameter is not valid */ - private function readNextBlock () { - // First calculate minimum block length - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition())); - $length = $this->getIndexInstance()->calculateMinimumBlockLength(); + protected function readNextBlockByLength (int $length) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d - CALLED!', $this->getSeekPosition(), $length)); + if ($length < 1) { + // Throw IAE + throw new InvalidArgumentException(sprintf('length=%d is not valid', $length)); + } // Read possibly back-buffered bytes from previous call of next(). - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d', $this->getSeekPosition(), $length)); $data = $this->getBackBuffer(); /* @@ -915,11 +868,11 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { } // Separate data - $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data); + $dataArray = explode(chr(BinaryFile::SEPARATOR_ENTRIES), $data); // Left part is the actual block, right one the back-buffer data, if found //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray()=%d', count($dataArray))); - /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray=%s', print_r($dataArray, true))); + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray=%s', print_r($dataArray, true))); $this->setCurrentBlock($dataArray[0]); // Is back buffere data found? @@ -946,7 +899,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s,minimumBlockLength=%d - CALLED!', $type, $minimumBlockLength)); if (empty($type)) { // Empty type - throw new InvalidArgumentException('Parameter "type" is empty'); + throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif ($minimumBlockLength < 1) { // Invalid block length throw new InvalidArgumentException(sprintf('Parameter minimumBlockLength=%d is not valid', $minimumBlockLength)); @@ -1032,31 +985,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { return $isValid; } + /** + * Reads next "block" of bytes into $currentBlock field. THis method loads + * the whole file into memory when the file is just freshly initialized + * (only zeros in it). + * + * @return void + */ + protected abstract function readNextBlock (); + /** * Reads the file header * * @return void * @throws LogicException If both instances are not set */ - public function readFileHeader () { - // Is index set or stack? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); - if ($this->getIndexInstance() instanceof Indexable) { - // Call index instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexInstance->readIndexHeader() ...'); - $this->getIndexInstance()->readIndexHeader(); - } elseif ($this->getStackInstance() instanceof StackableFile) { - // Call stacke instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->stackInstance->readStackHeader() ...'); - $this->getStackInstance()->readStackHeader(); - } else { - // Bad logic? - throw new LogicException('Wether indexInstance nor stackInstance are set'); - } - - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); - } + public abstract function readFileHeader (); /** * Searches for next suitable gap the given length of data can fit in @@ -1085,7 +1029,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile { } // @TODO Unfinished - $this->partialStub('length=' . $length); + DebugMiddleware::getSelfInstance()->partialStub('length=' . $length); } }