// Import SPL stuff
use \InvalidArgumentException;
use \SplFileInfo;
+use \UnexpectedValueException;
/**
* A FileStack index class
* @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
$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