]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 17 Jan 2021 00:42:17 +0000 (01:42 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 17 Jan 2021 00:42:17 +0000 (01:42 +0100)
- added parameter-validation and thrown IAE when not valid
- added more noisy debug lines

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/file_directories/binary/class_BaseBinaryFile.php
framework/main/classes/file_directories/binary/index/class_IndexFile.php
framework/main/classes/file_directories/binary/stack/class_StackFile.php
framework/main/classes/stacker/file/class_BaseFileStack.php

index 8fcaeca9dcba94f78af136793548859d83bcfb26..43963e47b0b9e3661e933d0a9c59294f20480e71 100644 (file)
@@ -8,8 +8,6 @@ use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
-use Org\Mxchange\CoreFramework\Index\Indexable;
-use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
 use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
 
@@ -791,19 +789,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        }
 
        /**
-        * Reads next "block" of bytes into $currentBlock field. THis method loads
-        * the whole file into memory when the file is just freshly initialized
-        * (only zeros in it).
+        * Reads next "block" of given bytes into $currentBlock field. THis method
+        * loads the whole file into memory when the file is just freshly
+        * initialized (only zeros in it).
         *
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       private function readNextBlock () {
-               // First calculate minimum block length
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
-               $length = $this->getIndexInstance()->calculateMinimumBlockLength();
+       protected function readNextBlockByLength (int $length) {
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d - CALLED!', $this->getSeekPosition(), $length));
+               if ($length < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               }
 
                // Read possibly back-buffered bytes from previous call of next().
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d', $this->getSeekPosition(), $length));
                $data = $this->getBackBuffer();
 
                /*
@@ -970,31 +971,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                return $isValid;
        }
 
+       /**
+        * Reads next "block" of bytes into $currentBlock field. THis method loads
+        * the whole file into memory when the file is just freshly initialized
+        * (only zeros in it).
+        *
+        * @return      void
+        */
+       protected abstract function readNextBlock ();
+
        /**
         * Reads the file header
         *
         * @return      void
         * @throws      LogicException  If both instances are not set
         */
-       public function readFileHeader () {
-               // Is index set or stack?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               if ($this->getIndexInstance() instanceof Indexable) {
-                       // Call index instance
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexInstance->readIndexHeader() ...');
-                       $this->getIndexInstance()->readIndexHeader();
-               } elseif ($this->getStackInstance() instanceof StackableFile) {
-                       // Call stacke instance
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->stackInstance->readStackHeader() ...');
-                       $this->getStackInstance()->readStackHeader();
-               } else {
-                       // Bad logic?
-                       throw new LogicException('Wether indexInstance nor stackInstance are set');
-               }
-
-               // Trace message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
-       }
+       public abstract function readFileHeader ();
 
        /**
         * Searches for next suitable gap the given length of data can fit in
index 3de99584a81b123af3dae12f319a7f7ca0eb359f..fb95380523bde897f9ec60a1b30f44c10b1128b9 100644 (file)
@@ -160,6 +160,40 @@ class IndexFile extends BaseBinaryFile implements IndexableFile {
                return $isValid;
        }
 
+       /**
+        * Reads next "block" of bytes into $currentBlock field. THis method loads
+        * the whole file into memory when the file is just freshly initialized
+        * (only zeros in it).
+        *
+        * @return      void
+        */
+       protected function readNextBlock () {
+               // First calculate minimum block length
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
+               $length = $this->getIndexInstance()->calculateMinimumBlockLength();
+
+               // Call protected method
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: Calling parent::readNextBlockByLength(%d) ...', $length));
+               parent::readNextBlockByLength($length);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
+       }
+
+       /**
+        * Reads the file header
+        *
+        * @return      void
+        */
+       public function readFileHeader () {
+               // Call index class' method
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->readIndexHeader() - CALLED!');
+               $this->getIndexInstance()->readIndexHeader();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
+       }
+
        /**
         * Writes given value to the file and returns a hash and gap position for it
         *
index 42a2412f007c4e28bb154f726f33fad5f2496ae9..590a7fca5f90683467552cbf23ef626b0dcdcea7 100644 (file)
@@ -158,6 +158,41 @@ class StackFile extends BaseBinaryFile implements FileStacker {
                return $isValid;
        }
 
+       /**
+        * Reads the file header
+        *
+        * @return      void
+        * @throws      LogicException  If both instances are not set
+        */
+       public function readFileHeader () {
+               // Call stacke instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->readStackHeader() - CALLED!');
+               $this->getStackInstance()->readStackHeader();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+       }
+
+       /**
+        * Reads next "block" of bytes into $currentBlock field. THis method loads
+        * the whole file into memory when the file is just freshly initialized
+        * (only zeros in it).
+        *
+        * @return      void
+        */
+       protected function readNextBlock () {
+               // First calculate minimum block length
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
+               $length = $this->getStackInstance()->calculateMinimumBlockLength();
+
+               // Call protected method
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling parent::readNextBlockByLength(%d) ...', $length));
+               parent::readNextBlockByLength($length);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+       }
+
        /**
         * Writes given value to the file and returns a hash and gap position for it
         *
@@ -178,15 +213,19 @@ class StackFile extends BaseBinaryFile implements FileStacker {
                }
 
                // Encode/convert the value into a "binary format"
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling StringUtils::encodeData(value[]=%s) ...', gettype($value)));
                $encoded = StringUtils::encodeData($value);
 
                // Get a strong hash for the "encoded" data
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling CryptoUtils::hash(%s) ...', $encoded));
                $hash = CryptoUtils::hash($encoded);
 
                // Then write it to the next free gap
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->stackInstance->writeDataToFreeGap(%s,%s,%s) ...', $stackName, $hash, $encoded));
                $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
 
                // Return info
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: data[]=%s - EXIT!', gettype($data)));
                return $data;
        }
 
index 634eda306301907318ec9cdd369c1fccdf5e3368..abb9c5e7e93ae4db347dd6a13128f69c93fb5d52 100644 (file)
@@ -114,7 +114,7 @@ abstract class BaseFileStack extends BaseStacker {
                $data = substr($data, 0, -1);
 
                // And update seek position
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->updateSeekPosition() ...');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->binaryFileInstance->updateSeekPosition() ...');
                $this->getIteratorInstance()->getBinaryFileInstance()->updateSeekPosition();
 
                /*