use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
+// Import SPL stuff
+use \BadMethodCallException;
+use \InvalidArgumentException;
+
/**
* A file iterator
*
/**
* Creates an instance of this class
*
- * @param $pointerInstance An instance of a Block class
+ * @param $blockInstance An instance of a Block class
* @return $iteratorInstance An instance of a Iterator class
*/
public final static function createFileIterator (Block $blockInstance) {
// Get new instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: blockInstance=%s - CALLED!', $blockInstance->__toString()));
$iteratorInstance = new FileIterator();
// Set the instance here
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
$iteratorInstance->setBlockInstance($blockInstance);
// Return the prepared instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: iteratorInstance=%s - EXIT!', $iteratorInstance->__toString()));
return $iteratorInstance;
}
* Gets currently read data
*
* @return $current Currently read data
+ * @throws BadMethodCallException If valid() is FALSE
*/
public function current () {
+ // Is condition given?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ if (!$this->valid()) {
+ // Throw BMCE
+ throw new BadMethodCallException('Current key cannot be valid, forgot to invoke valid()?');
+ }
+
// Call block instance
- return $this->getBlockInstance()->current();
+ $current = $this->getBlockInstance()->current();
+
+ // Return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: current[]=%s - EXIT!', gettype($current)));
+ return $current;
}
/**
* Gets current seek position ("key").
*
* @return $key Current key in iteration
+ * @throws BadMethodCallException If valid() is FALSE
*/
public function key () {
- // Return it
- return $this->getBlockInstance()->determineSeekPosition();
+ // Is condition given?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ if (!$this->valid()) {
+ // Throw BMCE
+ throw new BadMethodCallException('Current key cannot be valid, forgot to invoke valid()?');
+ }
+
+ // Get key from block instance
+ $key = $this->getBlockInstance()->determineSeekPosition();
+
+ // Return key
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: key[%s]=%s - EXIT!', gettype($key), $key));
+ return $key;
}
/**
*/
public function next () {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$this->getBlockInstance()->next();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public function rewind () {
// Call block instance
- return $this->getBlockInstance()->rewind();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $status = $this->getBlockInstance()->rewind();
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: status=%d - EXIT!', intval($status)));
+ return $status;
}
/**
*/
public function valid () {
// Call block instance
- return $this->getBlockInstance()->valid();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $isValid = $this->getBlockInstance()->valid();
+
+ // Return flag
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: isValid=%d - EXIT!', intval($isValid)));
+ return $isValid;
}
/**
*
* @param $seekPosition Seek position in file
* @return $status Status of this operation
+ * @throws InvalidArgumentException If a parameter is not valid
*/
public function seek (int $seekPosition) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - CALLED!', $seekPosition));
+ if ($seekPosition < 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
+ }
+
// Call block instance
- return $this->getBlockInstance()->seek($seekPosition);
+ $status = $this->getBlockInstance()->seek($seekPosition);
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: status=%d - EXIT!', intval($status)));
+ return $status;
}
/**
*/
public function size () {
// Call the block object
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$size = $this->getBlockInstance()->size();
- // Return result
+ // Return size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: size=%d - EXIT!', $size));
return $size;
}
* @return $data Data read from file
*/
public function read (int $bytes = 0) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: bytes=%d - CALLED!', $bytes));
+ if ($bytes < 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('bytes=%d is not valid', $bytes));
+ }
+
// Call block instance
- return $this->getBlockInstance()->read($bytes);
+ $data = $this->getBlockInstance()->read($bytes);
+
+ // Return data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: data[]=%s - EXIT!', gettype($data)));
+ return $data;
}
/**
*/
public function analyzeFile () {
// Just call the block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$this->getBlockInstance()->analyzeFile();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public function isFileHeaderInitialized () {
// Just call the block instance
- return $this->getBlockInstance()->isFileHeaderInitialized();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $isInitialized = $this->getBlockInstance()->isFileHeaderInitialized();
+
+ // Return flag
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: isInitialized=%d - EXIT!', intval($isInitialized)));
+ return $isInitialized;
}
/**
*/
public function createFileHeader () {
// Just call the block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$this->getBlockInstance()->createFileHeader();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*
* @param $type Type of the file
* @return void
+ * @throws InvalidArgumentException If a parameter is not valid
*/
public function preAllocateFile (string $type) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: type=%s - CALLED!', $type));
+ if (empty($type)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "type" is empty');
+ }
+
// Just call the block instance
$this->getBlockInstance()->preAllocateFile($type);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public function initCountersGapsArray () {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$this->getBlockInstance()->initCountersGapsArray();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public final function getHeaderSize () {
// Call block instance
- return $this->getBlockInstance()->getHeaderSize();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $size = $this->getBlockInstance()->getHeaderSize();
+
+ // Return size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: size=%d - EXIT!', $size));
+ return $size;
}
/**
*/
public final function setHeaderSize (int $headerSize) {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: headerSize=%d - CALLED!', $headerSize));
$this->getBlockInstance()->setHeaderSize($headerSize);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
* Getter for header array
*
- * @return $totalEntries Size of file header
+ * @return $header Header array
*/
public final function getHeader () {
// Call block instance
- return $this->getBlockInstance()->getHeader();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $header = $this->getBlockInstance()->getHeader();
+
+ // Return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: header()=%d - EXIT!', count($header)));
+ return $header;
}
/**
*/
public final function setHeader (array $header) {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: header()=%d - CALLED!', count($header)));
$this->getBlockInstance()->setHeader($header);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public function updateSeekPosition () {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
$this->getBlockInstance()->updateSeekPosition();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public final function getCounter () {
// Call block instance
- return $this->getBlockInstance()->getCounter();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $counter = $this->getBlockInstance()->getCounter();
+
+ // Return counter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: counter=%d - EXIT!', $counter));
+ return $counter;
}
/**
*/
public function getFileSize () {
// Call block instance
- return $this->getBlockInstance()->getFileSize();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $size = $this->getBlockInstance()->getFileSize();
+
+ // Return size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: size=%d - EXIT!', $size));
+ return $size;
}
/**
* @param $data Data to be written
* @param $flushHeader Whether to flush the header (default: flush)
* @return void
+ * @throws InvalidArgumentException If a parameter is not valid
*/
public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d,data(%d)=%s,flushHeader=%d - CALLED!', $seekPosition, strlen($data), $data, intval($flushHeader)));
+ if ($seekPosition < 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
+ } elseif (empty($data)) {
+ // Throw it again
+ throw new InvalidArgumentException('Parameter "data" is empty');
+ }
+
// Call block instance
$this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
*/
public function getSeekPosition () {
// Call block instance
- return $this->getBlockInstance()->getSeekPosition();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
+ $seekPosition = $this->getBlockInstance()->getSeekPosition();
+
+ // Return position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - EXIT!', $seekPosition));
+ return $seekPosition;
}
/**
* @param $groupId Group identifier
* @param $value Value to be added to the stack
* @return $data Hash and gap position
+ * @throws InvalidArgumentException If a parameter is not valid
*/
public function writeValueToFile (string $groupId, $value) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: groupId=%s,value[]=%s - CALLED!', $groupId, gettype($value)));
+ if (empty($groupId)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "groupId" is empty');
+ } elseif (is_resource($value) || is_object($value)) {
+ // Resources and objects are nothing for file-based indexes (mostly)
+ throw new InvalidArgumentException(sprintf('value[]=%s is not supported by file-based indexes', gettype($value)));
+ }
+
// Call block instance
- return $this->getBlockInstance()->writeValueToFile($groupId, $value);
+ $data = $this->getBlockInstance()->writeValueToFile($groupId, $value);
+
+ // Return data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: data[]=%s - EXIT!', gettype($data)));
+ return $data;
}
/**
* @return $data Gap position and length of the raw data
*/
public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
+
// Call block instance
- return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
+ $data = $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
+
+ // Return data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: data[]=%s - EXIT!', gettype($data)));
+ return $data;
}
/**
*
* @param $length Length of raw data
* @return $seekPosition Found next gap's seek position
+ * @throws InvalidArgumentException If a parameter is invalid
*/
public function searchNextGap (int $length) {
- // Call block instance
+ // Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: length=%d - CALLED!', $length));
- return $this->getBlockInstance()->searchNextGap($length);
+ if ($length <= 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+ }
+
+ // Call block instance
+ $seekPosition = $this->getBlockInstance()->searchNextGap($length);
+
+ // Return position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - EXIT!', $seekPosition));
+ return $seekPosition;
}
}