]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/stacker/file/class_BaseFileStack.php
Continued with file-based hash:
[core.git] / inc / classes / main / stacker / file / class_BaseFileStack.php
index 8491479d9a41ae7efc474d374b68a688218a1434..70bd7006f5c1c345e273f59790c256470af3a0f8 100644 (file)
@@ -52,6 +52,16 @@ class BaseFileStack extends BaseStacker {
         */
        const COUNT_POSITION = 20;
 
         */
        const COUNT_POSITION = 20;
 
+       /**
+        * Counter for total entries
+        */
+       private $totalEntries = 0;
+
+       /**
+        * Current seek position
+        */
+       private $seekPosition = 0;
+
        /**
         * Protected constructor
         *
        /**
         * Protected constructor
         *
@@ -63,6 +73,60 @@ class BaseFileStack extends BaseStacker {
                parent::__construct($className);
        }
 
                parent::__construct($className);
        }
 
+       /**
+        * Getter for total entries
+        *
+        * @return      $totalEntries   Total entries in this stack
+        */
+       private function getCounter () {
+               // Get it
+               return $this->totalEntries;
+       }
+
+       /**
+        * Increment counter
+        *
+        * @return      void
+        */
+       private function incrementCounter () {
+               // Get it
+               $this->totalEntries++;
+       }
+
+       /**
+        * Getter for seek position
+        *
+        * @return      $seekPosition   Current seek position (stored here in object)
+        */
+       private function getSeekPosition () {
+               // Get it
+               return $this->seekPosition;
+       }
+
+       /**
+        * Setter for seek position
+        *
+        * @param       $seekPosition   Current seek position (stored here in object)
+        * @return      void
+        */
+       private function setSeekPosition ($seekPosition) {
+               // And set it
+               $this->seekPosition = $seekPosition;
+       }
+
+       /**
+        * Updates seekPosition attribute from file to avoid to much access on file.
+        *
+        * @return      void
+        */
+       private function updateSeekPosition () {
+               // Get key (= seek position)
+               $seekPosition = $this->getIteratorInstance()->key();
+
+               // And set it here
+               $this->setSeekPosition($seekPosition);
+       }
+
        /**
         * Checks whether the file header is initialized
         *
        /**
         * Checks whether the file header is initialized
         *
@@ -122,9 +186,6 @@ class BaseFileStack extends BaseStacker {
                // The file's header should not be initialized here
                assert(!$this->isFileHeaderInitialized());
 
                // The file's header should not be initialized here
                assert(!$this->isFileHeaderInitialized());
 
-               // Init  counter
-               $this->getIteratorInstance()->initCounter();
-
                // Flush file header
                $this->flushFileHeader();
        }
                // Flush file header
                $this->flushFileHeader();
        }
@@ -144,7 +205,7 @@ class BaseFileStack extends BaseStacker {
                        chr(self::SEPARATOR_MAGIC_COUNT),
 
                        // Total entries (will be zero) and pad it to 20 chars
                        chr(self::SEPARATOR_MAGIC_COUNT),
 
                        // Total entries (will be zero) and pad it to 20 chars
-                       str_pad($this->dec2hex($this->getIteratorInstance()->getCount()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
+                       str_pad($this->dec2hex($this->getCounter()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
 
                        // Position (will be zero)
                        str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
 
                        // Position (will be zero)
                        str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
@@ -153,8 +214,11 @@ class BaseFileStack extends BaseStacker {
                        chr(self::SEPARATOR_SEEK_POS_ENTRIES)
                );
 
                        chr(self::SEPARATOR_SEEK_POS_ENTRIES)
                );
 
-               // Write it to disk
+               // Write it to disk (header is always at seek position 0)
                $this->getIteratorInstance()->writeAtPosition(0, $header);
                $this->getIteratorInstance()->writeAtPosition(0, $header);
+
+               // Update seek position
+               $this->updateSeekPosition();
        }
 
        /**
        }
 
        /**