]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/stacker/file/class_BaseFileStack.php
Continued:
[core.git] / framework / main / classes / stacker / file / class_BaseFileStack.php
index 63686de9f14a0640f8876a43b16670ff11cd2477..66345fd72112ad7f76bbc920d0912a4268df91af 100644 (file)
@@ -9,8 +9,6 @@ use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
 use Org\Mxchange\CoreFramework\Stack\BaseStacker;
-use Org\Mxchange\CoreFramework\Stack\File\InvalidMagicException;
-use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
@@ -42,7 +40,7 @@ use \UnexpectedValueException;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-abstract class BaseFileStack extends BaseStacker implements StackableFile {
+abstract class BaseFileStack extends BaseStacker {
        // Load traits
        use IndexableTrait;
        use IteratorTrait;
@@ -244,9 +242,6 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->initCountersGapsArray() ...');
                $this->getIteratorInstance()->initCountersGapsArray();
 
-               // Default is not created/already exists
-               $created = false;
-
                // Is the file's header initialized?
                if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
                        // First pre-allocate a bit
@@ -256,33 +251,39 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile {
                        // Then create file header
                        /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: this->iteratorInstance->createFileHeader() ...');
                        $this->getIteratorInstance()->createFileHeader();
-
-                       // Set flag
-                       $created = true;
                }
 
                // Load the file header
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->readFileHeader() ...');
                $this->readFileHeader();
 
-               // Not created/already exists?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: created=%d', intval($created)));
-               if (!$created) {
-                       // Count all entries in file
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->analyzeFileStructure() ...');
-                       $this->getIteratorInstance()->analyzeFileStructure();
-               }
-
                /*
                 * Get stack index instance. This can be used for faster
                 * "defragmentation" and startup.
                 */
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance[%s]=%s,type=%s', get_class($fileInfoInstance), $fileInfoInstance, $type));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Creating index instance for fileInfoInstance[%s]=%s,type=%s ...', get_class($fileInfoInstance), $fileInfoInstance, $type));
                $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
 
                // And set it here
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: indexInstance=%s', $indexInstance->__toString()));
                $this->setIndexInstance($indexInstance);
 
+               // Is the index loaded correctly and the stack file is just created?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->indexInstance->isIndexFileLoaded() ...');
+               if (!$this->getIndexInstance()->isIndexFileLoaded()) {
+                       /*
+                        * Something horrible has happened to the index as it should be
+                        * loaded at this point. The stack's file structure then needs to
+                        * be analyzed and the index rebuild.
+                        */
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->analyzeFileStructure() ...');
+                       $this->getIteratorInstance()->analyzeFileStructure();
+
+                       // Rebuild index from file
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Calling this->iteratorInstance->rebuildIndexFromStack(%s) ...', $this->__toString()));
+                       $this->getIndexInstance()->rebuildIndexFromStack($this);
+               }
+
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
        }
@@ -299,7 +300,8 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile {
         */
        protected function addValueToStack (string $stackerName, $value) {
                // Validate parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true)));
+               /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
+               //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[%s]=%s', $stackerName, gettype($value), print_r($value, true)));
                if (empty($stackerName)) {
                        // No empty stack name
                        throw new InvalidArgumentException('Parameter "stackerName" is empty');
@@ -318,7 +320,9 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile {
                $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
 
                // Add the hash and gap position to the index
-               $this->getIndexInstance()->addHashToIndex($stackerName, $data);
+               //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data=%s', print_r($data, true));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Calling this->indexInstance->addHashedDataToIndex(%s,data()=%d) ...', $stackerName, count($data)));
+               $this->getIndexInstance()->addHashedDataToIndex($stackerName, $data);
 
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
@@ -476,28 +480,6 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile {
                return $isEmpty;
        }
 
-       /**
-        * Initializes given stacker
-        *
-        * @param       $stackerName    Name of the stack
-        * @param       $forceReInit    Force re-initialization
-        * @return      void
-        * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
-        */
-       public function initStack (string $stackerName, bool $forceReInit = false) {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
-       }
-
-       /**
-        * Initializes all stacks
-        *
-        * @return      void
-        * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
-        */
-       public function initStacks (array $stacks, bool $forceReInit = false) {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
-       }
-
        /**
         * Checks whether the given stack is initialized (set in array $stackers)
         *