]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/index/file_stack/class_FileStackIndex.php
Continued:
[core.git] / framework / main / classes / index / file_stack / class_FileStackIndex.php
index 2a0efa3a944a309f59e376f152b9ba6f221ef81a..efb563cc2da5cfe9032ec99f1496c91ce24a989a 100644 (file)
@@ -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