X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Findex%2Ffile_stack%2Fclass_FileStackIndex.php;fp=framework%2Fmain%2Fclasses%2Findex%2Ffile_stack%2Fclass_FileStackIndex.php;h=efb563cc2da5cfe9032ec99f1496c91ce24a989a;hp=2a0efa3a944a309f59e376f152b9ba6f221ef81a;hb=728805fb9d82980b98f0f301410c1c500c7aac7d;hpb=cc783d97086d21b6a675b8b5be801ca1af426a42 diff --git a/framework/main/classes/index/file_stack/class_FileStackIndex.php b/framework/main/classes/index/file_stack/class_FileStackIndex.php index 2a0efa3a..efb563cc 100644 --- a/framework/main/classes/index/file_stack/class_FileStackIndex.php +++ b/framework/main/classes/index/file_stack/class_FileStackIndex.php @@ -12,6 +12,7 @@ use Org\Mxchange\CoreFramework\Stack\Index\IndexableStack; // Import SPL stuff use \InvalidArgumentException; use \SplFileInfo; +use \UnexpectedValueException; /** * A FileStack index class @@ -71,6 +72,7 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { * @param $groupId Name of stack to add hash for * @param $data Hash and gap position to be added to the index * @return void + * @throws UnexpectedValueException If an invalid gap position is being returned */ public function addHashToIndex (string $groupId, array $data) { // Raw data been written to the file @@ -85,17 +87,19 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { $data[StackableFile::ARRAY_NAME_DATA_LENGTH] ); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,rawData()=%d', $groupId, $data[StackableFile::ARRAY_NAME_HASH], strlen($rawData))); - // Search for next free gap + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,rawData()=%d', $groupId, $data[StackableFile::ARRAY_NAME_HASH], strlen($rawData))); $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData)); - // Gap position cannot be smaller than header length + 1 - assert($gapPosition > $this->getIteratorInstance()->getHeaderSize()); + // Gap position cannot be smaller or equal than header length + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,gapPosition=%s', $groupId, $data[StackableFile::ARRAY_NAME_HASH], $gapPosition)); + if ($gapPosition <= ($this->getIteratorInstance()->getHeaderSize() + 1)) { + // Not valid gap position returned + throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is smaller or equal headerSize+1=%d', gettype($gapPosition), $gapPosition, ($this->getIteratorInstance()->getHeaderSize() + 1))); + } // Then write the data at that gap - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,gapPosition=%s', $groupId, $data[StackableFile::ARRAY_NAME_HASH], $gapPosition)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: Calling this->iteratorInstance->writeData(%d,%s) ...', $gapPosition, $rawData)); $this->getIteratorInstance()->writeData($gapPosition, $rawData); // Trace message