]> git.mxchange.org Git - core.git/commitdiff
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 b6ba4b02c2c3fd5e84c376c69ae3273afdc8fdd0..f50b972d41c5da85c08b2e6ceb81bd46d5cb984c 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 8b6707dd2c7a282d62b8efa8b360fa5953811551..707f5600a6ae6b8bccfa74d914905939973003dd 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 8143840a5d5c9457c37205e03d90dae0647dadba..3ce37f01aac262acd835343232e3d7b8436990d9 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 bca12f4a0093d24c92f8adcfbb70c1f8c389147c..a427c5b7c74632a34ff501afb147d06ab6b7bb7f 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 3df30f84f67c12397b95468fd774bfbbc614031b..ebd5fe25dcee457de8038f31d46bf4a60beb0cd0 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 3437d1165902af54a1cc557527ec90cdd924844a..12cf82ea461e8f32fc5e3f32e4b11b6627dbf57a 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 56c3b99d4b7539cfce89f814448953fa90d6f68d..f6819a02deb9b5b2f8870409e121d0e72682adbb 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 899338d5983f60d03a959bf63fe239075b9eeb06..a143b04dfad981ca6643637846b3dba4e3ee4804 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
         *