Added missing setter.
[core.git] / inc / classes / main / stacker / file / class_BaseFileStack.php
index 879f19d0187b09700380ccd3bdf086dbcc883b4a..e3aeb4701fb9c3f192b7fa9cec788448e13cfb61 100644 (file)
@@ -77,6 +77,16 @@ class BaseFileStack extends BaseStacker {
         */
        private $header = array();
 
+       /**
+        * Seek positions for gaps ("fragmentation")
+        */
+       private $gaps = array();
+
+       /**
+        * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
+        */
+       private $damagedEntries = array();
+
        /**
         * Protected constructor
         *
@@ -96,6 +106,27 @@ class BaseFileStack extends BaseStacker {
                        self::LENGTH_POSITION +
                        strlen(self::SEPARATOR_HEADER_ENTRIES)
                );
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
+       }
+
+       /**
+        * Initializes counter for valid entries, arrays for damaged entries and
+        * an array for gap seek positions. If you call this method on your own,
+        * please re-analyze the file structure. So you are better to call
+        * analyzeStackFile() instead of this method.
+        *
+        * @return      void
+        */
+       private function initCountersGapsArray () {
+               // Init counter and seek position
+               $this->setCounter(0);
+               $this->setSeekPosition(0);
+
+               // Init arrays
+               $this->gaps = array();
+               $this->damagedEntries = array();
        }
 
        /**
@@ -103,17 +134,28 @@ class BaseFileStack extends BaseStacker {
         *
         * @return      $totalEntries   Total entries in this stack
         */
-       private function getCounter () {
+       private final function getCounter () {
                // Get it
                return $this->totalEntries;
        }
 
+       /**
+        * Setter for total entries
+        *
+        * @param       $totalEntries   Total entries in this stack
+        * @return      void
+        */
+       private final function setCounter ($counter) {
+               // Set it
+               $this->totalEntries = $counter;
+       }
+
        /**
         * Increment counter
         *
         * @return      void
         */
-       private function incrementCounter () {
+       private final function incrementCounter () {
                // Get it
                $this->totalEntries++;
        }
@@ -123,7 +165,7 @@ class BaseFileStack extends BaseStacker {
         *
         * @return      $seekPosition   Current seek position (stored here in object)
         */
-       private function getSeekPosition () {
+       private final function getSeekPosition () {
                // Get it
                return $this->seekPosition;
        }
@@ -134,7 +176,7 @@ class BaseFileStack extends BaseStacker {
         * @param       $seekPosition   Current seek position (stored here in object)
         * @return      void
         */
-       private function setSeekPosition ($seekPosition) {
+       private final function setSeekPosition ($seekPosition) {
                // And set it
                $this->seekPosition = $seekPosition;
        }
@@ -385,14 +427,37 @@ class BaseFileStack extends BaseStacker {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
+       /**
+        * Analyzes entries in stack file. This will count all found (and valid)
+        * entries, mark invalid as damaged and count gaps ("fragmentation"). If
+        * only gaps are found, the file is considered as "virgin" (no entries).
+        *
+        * @return      void
+        */
+       private function analyzeStackFile () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+
+               // Make sure the file is initialized
+               assert($this->isFileInitialized());
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
+
+               // Output message (as this may take some time)
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
+
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+       }
+
        /**
         * Initializes this file-based stack.
         *
         * @param       $fileName       File name of this stack
+        * @param       $type           Type of this stack (e.g. url_source for URL sources)
         * @return      void
         */
-       protected function initFileStack ($fileName) {
-               // Get a file i/o pointer instance
+       protected function initFileStack ($fileName, $type) {
+               // Get a file i/o pointer instance for stack file
                $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
 
                // Get iterator instance
@@ -415,6 +480,18 @@ class BaseFileStack extends BaseStacker {
 
                // Load the file header
                $this->readFileHeader();
+
+               // Count all entries in file
+               $this->analyzeStackFile();
+
+               /*
+                * Get stack index instance. This can be used for faster
+                * "defragmentation" and startup.
+                */
+               $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileName, $type);
+
+               // And set it here
+               $this->setIndexInstance($indexInstance);
        }
 
        /**
@@ -524,7 +601,7 @@ class BaseFileStack extends BaseStacker {
         * @param       $stackerName    Name of the stack
         * @return      $isFull                 Whether the stack is full
         */
-       protected final function isStackFull ($stackerName) {
+       protected function isStackFull ($stackerName) {
                // File-based stacks will only run full if the disk space is low.
                // @TODO Please implement this, returning FALSE
                $isFull = FALSE;
@@ -540,7 +617,7 @@ class BaseFileStack extends BaseStacker {
         * @return      $isEmpty                        Whether the stack is empty
         * @throws      NoStackerException      If given stack is missing
         */
-       public final function isStackEmpty ($stackerName) {
+       public function isStackEmpty ($stackerName) {
                // So, is the stack empty?
                $isEmpty = (($this->getStackCount($stackerName)) == 0);
 
@@ -588,12 +665,8 @@ class BaseFileStack extends BaseStacker {
         * @return      $count                  Size of stack (array count)
         */
        public function getStackCount ($stackerName) {
-               // Now, count the array of entries
-               $this->partialStub('stackerName=' . $stackerName);
-               $count = 0;
-
-               // Return result
-               return $count;
+               // Now, simply return the found count value, this must be up-to-date then!
+               return $this->getCounter();
        }
 }