Introduced CalculatableBlock + basic implementation for valid().
authorRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 19:12:07 +0000 (21:12 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 19:12:07 +0000 (21:12 +0200)
Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/interfaces/calculatable/.htaccess [new file with mode: 0644]
inc/classes/interfaces/calculatable/class_CalculatableBlock.php [new file with mode: 0644]
inc/classes/interfaces/index/stack/class_IndexableStack.php
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/factories/index/class_FileStackIndexFactory.php
inc/classes/main/index/class_BaseIndex.php
inc/classes/main/index/file_stack/class_FileStackIndex.php
inc/classes/main/iterator/io/class_FileIoIterator.php
inc/classes/main/stacker/file/class_BaseFileStack.php
inc/classes/main/stacker/file/fifo/class_FiFoFileStack.php

diff --git a/inc/classes/interfaces/calculatable/.htaccess b/inc/classes/interfaces/calculatable/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/calculatable/class_CalculatableBlock.php b/inc/classes/interfaces/calculatable/class_CalculatableBlock.php
new file mode 100644 (file)
index 0000000..ba98085
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * A CalculatableBlock interface
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface CalculatableBlock extends FrameworkInterface {
+       /**
+        * Calculates minimum length for one entry/block
+        *
+        * @return      $length         Minimum length for one entry/block
+        */
+       function caluclateMinimumBlockLength ();
+}
+
+// [EOF]
+?>
index b6ba4b0..f50b972 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface IndexableStack extends Indexable {
+interface IndexableStack extends Indexable, CalculatableBlock {
 }
 
 // [EOF]
index 8b6707d..707f560 100644 (file)
@@ -189,10 +189,15 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        private $pointerInstance = NULL;
 
        /**
-        * An instance of an index
+        * An instance of an Indexable class
         */
        private $indexInstance = NULL;
 
+       /**
+        * An instance of a CalculatableBlock class
+        */
+       private $blockInstance = NULL;
+
        /**
         * Thousands separator
         */
@@ -1311,6 +1316,25 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                return $this->indexInstance;
        }
 
+       /**
+        * Setter for CalculatableBlock instance
+        *
+        * @param       $blockInstance  An instance of an CalculatableBlock class
+        * @return      void
+        */
+       protected final function setBlockInstance (CalculatableBlock $blockInstance) {
+               $this->blockInstance = $blockInstance;
+       }
+
+       /**
+        * Getter for CalculatableBlock instance
+        *
+        * @return      $blockInstance  An instance of an CalculatableBlock class
+        */
+       public final function getBlockInstance () {
+               return $this->blockInstance;
+       }
+
        /**
         * Checks whether an object equals this object. You should overwrite this
         * method to implement own equality checks
@@ -3133,8 +3157,8 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        protected function preAllocateFile ($type) {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
 
-               // caluclateMinimumFileEntryLength() must be callable
-               assert(is_callable(array($this, 'caluclateMinimumFileEntryLength')));
+               // caluclateMinimumBlockLength() must be callable
+               assert(is_callable(array($this, 'caluclateMinimumBlockLength')));
 
                // Is it enabled?
                if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
@@ -3149,7 +3173,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
 
                // Calculate minimum length for one entry
-               $minLengthEntry = $this->caluclateMinimumFileEntryLength();
+               $minLengthEntry = $this->caluclateMinimumBlockLength();
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
 
                // Calulcate seek position
index 8143840..3ce37f0 100644 (file)
@@ -47,6 +47,9 @@ class FileStackIndexFactory extends ObjectFactory {
                        // Get the handler instance
                        $indexInstance = self::createObjectByConfiguredName($type . '_file_stack_index_class', array($fileName));
 
+                       // Add check for interface
+                       assert($indexInstance instanceof IndexableStack);
+
                        // Add it to the registry
                        Registry::getRegistry()->addInstance($type . '_index', $indexInstance);
                }
index bca12f4..a427c5b 100644 (file)
@@ -186,11 +186,11 @@ class BaseIndex extends BaseFrameworkSystem {
        }
 
        /**
-        * Calculates minimum length for one entry
+        * Calculates minimum length for one entry/block
         *
-        * @return      $length         Minimum length for one entry
+        * @return      $length         Minimum length for one entry/block
         */
-       protected function caluclateMinimumFileEntryLength () {
+       public function caluclateMinimumBlockLength () {
                // Calulcate it
                // @TODO Not finished yet
                $length = 0;
@@ -214,7 +214,7 @@ class BaseIndex extends BaseFrameworkSystem {
                $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName));
 
                // Get iterator instance
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance));
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance, $this));
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);
index 3df30f8..ebd5fe2 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
+class FileStackIndex extends BaseIndex implements IndexableStack, CalculatableBlock, Registerable {
        /**
         * Protected constructor
         *
index 3437d11..12cf82e 100644 (file)
@@ -36,15 +36,19 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * Creates an instance of this class
         *
         * @param       $pointerInstance        An instance of a InputOutputPointer class
+        * @param       $blockInstance          An instance of a CalculatableBlock class
         * @return      $iteratorInstance       An instance of a Iterator class
         */
-       public final static function createFileIoIterator (InputOutputPointer $pointerInstance) {
+       public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
                // Get new instance
                $iteratorInstance = new FileIoIterator();
 
                // Set the instance here
                $iteratorInstance->setPointerInstance($pointerInstance);
 
+               // Set the block instance here
+               $iteratorInstance->setBlockInstance($blockInstance);
+
                // Return the prepared instance
                return $iteratorInstance;
        }
@@ -97,10 +101,29 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * 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      void
+        * @return      $isValid        Whether the next entry is valid
         */
        public function valid () {
-               $this->partialStub('Please implement this method.');
+               // First calculate minimum block length
+               $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
+
+               // Short be more than zero!
+               assert($length > 0);
+
+               // Get current seek position
+               $seekPosition = $this->key();
+
+               // Then try to read it
+               $data = $this->read($length);
+
+               // If some bytes could be read, all is fine
+               $isValid = ((is_string($data)) && (strlen($data) > 0));
+
+               // Seek back to old position
+               $this->seek($seekPosition);
+
+               // Return result
+               return $isValid;
        }
 
        /**
index 56c3b99..f6819a0 100644 (file)
@@ -220,7 +220,7 @@ class BaseFileStack extends BaseStacker {
                $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileName));
 
                // Get iterator instance
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance));
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance, $this));
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);
@@ -254,11 +254,11 @@ class BaseFileStack extends BaseStacker {
        }
 
        /**
-        * Calculates minimum length for one entry
+        * Calculates minimum length for one entry/block
         *
-        * @return      $length         Minimum length for one entry
+        * @return      $length         Minimum length for one entry/block
         */
-       protected function caluclateMinimumFileEntryLength () {
+       public function caluclateMinimumBlockLength () {
                // Calulcate it
                $length = self::getHashLength() + strlen(self::SEPARATOR_HASH_NAME) + self::LENGTH_NAME + 1;
 
index 899338d..a143b04 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class FiFoFileStack extends BaseFileStack implements StackableFile, Registerable {
+class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
        /**
         * Protected constructor
         *