]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/iterator/file/class_FileIterator.php
Continued:
[core.git] / framework / main / classes / iterator / file / class_FileIterator.php
index 98aae71f26f73052519bb16dd2956abb0ab58fed..a246927c02e4585c810e0af7eb4d7743d8f12c6f 100644 (file)
@@ -7,6 +7,10 @@ use Org\Mxchange\CoreFramework\Filesystem\Block;
 use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
 
+// Import SPL stuff
+use \BadMethodCallException;
+use \InvalidArgumentException;
+
 /**
  * A file iterator
  *
@@ -48,18 +52,19 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
        /**
         * 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;
        }
 
@@ -86,20 +91,44 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         * 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;
        }
 
        /**
@@ -109,7 +138,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -119,7 +152,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -130,7 +168,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -138,10 +181,22 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         *
         * @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;
        }
 
        /**
@@ -151,9 +206,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
@@ -164,8 +221,19 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         * @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;
        }
 
        /**
@@ -177,7 +245,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -187,7 +259,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -197,7 +274,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -205,10 +286,21 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         *
         * @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!');
        }
 
        /**
@@ -221,7 +313,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -231,7 +327,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -242,17 +343,26 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -263,7 +373,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -273,7 +387,11 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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!');
        }
 
        /**
@@ -283,7 +401,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -293,7 +416,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -303,10 +431,24 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         * @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!');
        }
 
        /**
@@ -316,7 +458,12 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        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;
        }
 
        /**
@@ -325,10 +472,25 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         * @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;
        }
 
        /**
@@ -340,8 +502,15 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         * @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;
        }
 
        /**
@@ -350,11 +519,22 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         *
         * @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;
        }
 
 }