]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/binary/stack/class_StackFile.php
WIP:
[core.git] / framework / main / classes / file_directories / binary / stack / class_StackFile.php
index ed3497d51e3521c5466e0c7970eef2008dba65db..d211aa1f1bef63abb10efec0e02306c0601c7ee5 100644 (file)
@@ -3,6 +3,7 @@
 namespace Org\Mxchange\CoreFramework\Stack\File;
 
 // Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Filesystem\Stack\FileStacker;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
@@ -10,6 +11,7 @@ use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
 
 // Import SPL stuff
+use \BadMethodCallException;
 use \InvalidArgumentException;
 use \SplFileInfo;
 
@@ -51,22 +53,108 @@ class StackFile extends BaseBinaryFile implements FileStacker {
         *
         * @param       $infoInstance   An instance of a SplFileInfo class
         * @param       $stackInstance  An instance of a StackableFile class
-        * @return      $fileInstance   An instance of this File class
+        * @return      $stackFileInstance      An instance of this File class
         */
        public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
                // Get a new instance
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
-               $fileInstance = new StackFile();
+               $stackFileInstance = new StackFile();
 
                // Set stack instance here for callbacks
-               $fileInstance->setStackInstance($stackInstance);
+               $stackFileInstance->setStackInstance($stackInstance);
 
                // Init this abstract file
-               $fileInstance->initFile($infoInstance);
+               $stackFileInstance->initFile($infoInstance);
+
+               // Init counters and gaps array
+               $stackFileInstance->initCountersGapsArray();
 
                // Return the prepared instance
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: fileInstance=%s - EXIT!', $fileInstance->__toString()));
-               return $fileInstance;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackFileInstance=%s - EXIT!', $stackFileInstance->__toString()));
+               return $stackFileInstance;
+       }
+
+       /**
+        * Flushes the file header
+        *
+        * @return      void
+        * @throws      BadMethodCallException  If this->stackInstance is not properly set
+        */
+       public function flushFileHeader () {
+               // Validate call
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
+               if (!($this->getStackInstance() instanceof StackableFIle)) {
+                       // Index instance not set
+                       throw new BadMethodCallException('this->stackInstance[] is not properly set.');
+               }
+
+               // Call block instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->indexInstance->flushFileHeader() ...');
+               $this->getStackInstance()->flushFileHeader();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+       }
+
+       /**
+        * Pre-allocates file (if enabled) with some space for later faster write access.
+        *
+        * @param       $type   Type of the file
+        * @return      void
+        * @throws      InvalidArgumentException        If a parameter is empty
+        * @throws      BadMethodCallException  If this->stackInstance is not properly set
+        */
+       public function preAllocateFile (string $type) {
+               // Is it enabled?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: type=%s - CALLED!', $type));
+               if (empty($type)) {
+                       // Empty type
+                       throw new InvalidArgumentException('Parameter "type" is empty');
+               } elseif (!($this->getStackInstance() instanceof StackableFile) && !($this->getStackInstance() instanceof StackableFile)) {
+                       // Index instance not set
+                       throw new BadMethodCallException('this->stackInstance[] and this->pointerInstance are not properly set.');
+               }
+
+               // Message to user
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Pre-allocating file ...');
+
+               // Calculate minimum block length and get file size
+               $minimumBlockLength = $this->getStackInstance()->calculateMinimumBlockLength();
+
+               // Call protected method
+               $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+       }
+
+       /**
+        * Checks wether the current entry is valid (not at the end of the file).
+        * This method will return true if an emptied (nulled) entry has been found.
+        *
+        * @return      $isValid        Whether the next entry is valid
+        * @throws      UnexpectedValueException        If some value is not expected
+        * @throws      BadMethodCallException  If this->stackInstance is not properly set
+        */
+       public function isValid () {
+               // Validate call
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
+               if (!($this->getStackInstance() instanceof StackableFile)) {
+                       // Index instance not set
+                       throw new BadMethodCallException('this->stackInstance[] is not properly set.');
+               }
+
+               // Get length from index
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->calculateMinimumBlockLength() ...');
+               $length = $this->getStackInstance()->calculateMinimumBlockLength();
+
+               // Call protected method
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->isValidByLength(%d) ...', $length));
+               $isValid = $this->isValidByLength($length);
+
+               // Return result
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: isValid=%d - EXIT!', intval($isValid)));
+               return $isValid;
        }
 
        /**