From: Roland Haeder Date: Fri, 23 May 2014 19:12:07 +0000 (+0200) Subject: Introduced CalculatableBlock + basic implementation for valid(). X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=5ffe42efd591f1d84d65a93b2965cdf6931f9442 Introduced CalculatableBlock + basic implementation for valid(). Signed-off-by: Roland Häder --- diff --git a/inc/classes/interfaces/calculatable/.htaccess b/inc/classes/interfaces/calculatable/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/interfaces/calculatable/.htaccess @@ -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 index 00000000..ba980857 --- /dev/null +++ b/inc/classes/interfaces/calculatable/class_CalculatableBlock.php @@ -0,0 +1,34 @@ + + * @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 . + */ +interface CalculatableBlock extends FrameworkInterface { + /** + * Calculates minimum length for one entry/block + * + * @return $length Minimum length for one entry/block + */ + function caluclateMinimumBlockLength (); +} + +// [EOF] +?> diff --git a/inc/classes/interfaces/index/stack/class_IndexableStack.php b/inc/classes/interfaces/index/stack/class_IndexableStack.php index b6ba4b02..f50b972d 100644 --- a/inc/classes/interfaces/index/stack/class_IndexableStack.php +++ b/inc/classes/interfaces/index/stack/class_IndexableStack.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface IndexableStack extends Indexable { +interface IndexableStack extends Indexable, CalculatableBlock { } // [EOF] diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 8b6707dd..707f5600 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -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 diff --git a/inc/classes/main/factories/index/class_FileStackIndexFactory.php b/inc/classes/main/factories/index/class_FileStackIndexFactory.php index 8143840a..3ce37f01 100644 --- a/inc/classes/main/factories/index/class_FileStackIndexFactory.php +++ b/inc/classes/main/factories/index/class_FileStackIndexFactory.php @@ -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); } diff --git a/inc/classes/main/index/class_BaseIndex.php b/inc/classes/main/index/class_BaseIndex.php index bca12f4a..a427c5b7 100644 --- a/inc/classes/main/index/class_BaseIndex.php +++ b/inc/classes/main/index/class_BaseIndex.php @@ -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); diff --git a/inc/classes/main/index/file_stack/class_FileStackIndex.php b/inc/classes/main/index/file_stack/class_FileStackIndex.php index 3df30f84..ebd5fe25 100644 --- a/inc/classes/main/index/file_stack/class_FileStackIndex.php +++ b/inc/classes/main/index/file_stack/class_FileStackIndex.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { +class FileStackIndex extends BaseIndex implements IndexableStack, CalculatableBlock, Registerable { /** * Protected constructor * diff --git a/inc/classes/main/iterator/io/class_FileIoIterator.php b/inc/classes/main/iterator/io/class_FileIoIterator.php index 3437d116..12cf82ea 100644 --- a/inc/classes/main/iterator/io/class_FileIoIterator.php +++ b/inc/classes/main/iterator/io/class_FileIoIterator.php @@ -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; } /** diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index 56c3b99d..f6819a02 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -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; diff --git a/inc/classes/main/stacker/file/fifo/class_FiFoFileStack.php b/inc/classes/main/stacker/file/fifo/class_FiFoFileStack.php index 899338d5..a143b04d 100644 --- a/inc/classes/main/stacker/file/fifo/class_FiFoFileStack.php +++ b/inc/classes/main/stacker/file/fifo/class_FiFoFileStack.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class FiFoFileStack extends BaseFileStack implements StackableFile, Registerable { +class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable { /** * Protected constructor *