From: Roland Häder Date: Sun, 17 Jan 2021 00:42:17 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=2f843ffb7276e4511e7c5e3f4636aab8cf29bb23;hp=1ff6ba0042c5121b5899c8e42ead7a20c7a13c56 Continued: - added parameter-validation and thrown IAE when not valid - added more noisy debug lines Signed-off-by: Roland Häder --- diff --git a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php index 8fcaeca9..43963e47 100644 --- a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php +++ b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php @@ -8,8 +8,6 @@ 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\Traits\Index\IndexableTrait; use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait; @@ -791,19 +789,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(); /* @@ -970,31 +971,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 diff --git a/framework/main/classes/file_directories/binary/index/class_IndexFile.php b/framework/main/classes/file_directories/binary/index/class_IndexFile.php index 3de99584..fb953805 100644 --- a/framework/main/classes/file_directories/binary/index/class_IndexFile.php +++ b/framework/main/classes/file_directories/binary/index/class_IndexFile.php @@ -160,6 +160,40 @@ class IndexFile extends BaseBinaryFile implements IndexableFile { 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 function readNextBlock () { + // First calculate minimum block length + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition())); + $length = $this->getIndexInstance()->calculateMinimumBlockLength(); + + // Call protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: Calling parent::readNextBlockByLength(%d) ...', $length)); + parent::readNextBlockByLength($length); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!'); + } + + /** + * Reads the file header + * + * @return void + */ + public function readFileHeader () { + // Call index class' method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->readIndexHeader() - CALLED!'); + $this->getIndexInstance()->readIndexHeader(); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!'); + } + /** * Writes given value to the file and returns a hash and gap position for it * diff --git a/framework/main/classes/file_directories/binary/stack/class_StackFile.php b/framework/main/classes/file_directories/binary/stack/class_StackFile.php index 42a2412f..590a7fca 100644 --- a/framework/main/classes/file_directories/binary/stack/class_StackFile.php +++ b/framework/main/classes/file_directories/binary/stack/class_StackFile.php @@ -158,6 +158,41 @@ class StackFile extends BaseBinaryFile implements FileStacker { return $isValid; } + /** + * Reads the file header + * + * @return void + * @throws LogicException If both instances are not set + */ + public function readFileHeader () { + // Call stacke instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->readStackHeader() - CALLED!'); + $this->getStackInstance()->readStackHeader(); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!'); + } + + /** + * 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 function readNextBlock () { + // First calculate minimum block length + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition())); + $length = $this->getStackInstance()->calculateMinimumBlockLength(); + + // Call protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling parent::readNextBlockByLength(%d) ...', $length)); + parent::readNextBlockByLength($length); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!'); + } + /** * Writes given value to the file and returns a hash and gap position for it * @@ -178,15 +213,19 @@ class StackFile extends BaseBinaryFile implements FileStacker { } // Encode/convert the value into a "binary format" + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling StringUtils::encodeData(value[]=%s) ...', gettype($value))); $encoded = StringUtils::encodeData($value); // Get a strong hash for the "encoded" data + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling CryptoUtils::hash(%s) ...', $encoded)); $hash = CryptoUtils::hash($encoded); // Then write it to the next free gap + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->stackInstance->writeDataToFreeGap(%s,%s,%s) ...', $stackName, $hash, $encoded)); $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded); // Return info + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: data[]=%s - EXIT!', gettype($data))); return $data; } diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 634eda30..abb9c5e7 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -114,7 +114,7 @@ abstract class BaseFileStack extends BaseStacker { $data = substr($data, 0, -1); // And update seek position - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->updateSeekPosition() ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->binaryFileInstance->updateSeekPosition() ...'); $this->getIteratorInstance()->getBinaryFileInstance()->updateSeekPosition(); /*