--- /dev/null
+Deny from all
--- /dev/null
+<?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]
+?>
* 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]
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
*/
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
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') {
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
// 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);
}
}
/**
- * 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;
$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);
* 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
*
* 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;
}
* 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;
}
/**
$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);
}
/**
- * 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;
* 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
*