From 5c227d3d4b639d9e5141bb218c414eb877250cf4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 24 Nov 2020 09:19:31 +0100 Subject: [PATCH] Continued: - WIP: ops, stack file and its index file were accidentally mixed into the stack which has caused reading errors. Still position 4 is written - added/commented in more noisy debug lines - added primitive type-hints MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../index/class_FileStackIndexFactory.php | 2 +- .../binary/class_BaseBinaryFile.php | 191 +++++++++--------- .../binary/index/class_IndexFile.php | 11 +- .../binary/stack/class_StackFile.php | 2 +- .../main/classes/index/class_BaseIndex.php | 67 +++--- .../index/file_stack/class_FileStackIndex.php | 20 +- .../iterator/file/class_FileIterator.php | 16 +- .../stacker/file/class_BaseFileStack.php | 24 +-- .../main/interfaces/block/class_Block.php | 6 +- .../index/stack/class_IndexableStack.php | 2 +- .../class_SeekableWritableFileIterator.php | 12 +- 11 files changed, 179 insertions(+), 174 deletions(-) diff --git a/framework/main/classes/factories/index/class_FileStackIndexFactory.php b/framework/main/classes/factories/index/class_FileStackIndexFactory.php index cee5e717..1049200e 100644 --- a/framework/main/classes/factories/index/class_FileStackIndexFactory.php +++ b/framework/main/classes/factories/index/class_FileStackIndexFactory.php @@ -49,7 +49,7 @@ class FileStackIndexFactory extends ObjectFactory { * @param $infoInstance An instance of a SplFileInfo class * @return $indexInstance An instance of a IndexableStack class */ - public static final function createFileStackIndexInstance (SplFileInfo $infoInstance, $type) { + public static final function createFileStackIndexInstance (SplFileInfo $infoInstance, string $type) { // If there is no handler? if (GenericRegistry::getRegistry()->instanceExists($type . '_index')) { // Get handler from registry diff --git a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php index 1c4dcff6..819e78a6 100644 --- a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php +++ b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php @@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock; use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile; // Import SPL stuff +use \BadMethodCallException; use \SplFileInfo; /** @@ -181,8 +182,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile { /* Only for debugging if (isset($this->gaps[0])) { // Output first and last gap - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', __METHOD__, __LINE__, print_r($this->gaps[0], true), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], true))); - } // END - if + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', print_r($this->gaps[0], true), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], true))); + } */ // Now count every gap @@ -190,10 +191,10 @@ abstract class BaseBinaryFile extends BaseAbstractFile { foreach ($this->gaps as $gap) { // Calculate size of found gap: end-start including both $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]); - } // END - if + } // Debug output - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', $gapsSize, $this->getHeaderSize())); // Total gap size + header size must be same as file size $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize()); @@ -236,7 +237,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $headerSize Size of file header * @return void */ - public final function setHeaderSize ($headerSize) { + public final function setHeaderSize (int $headerSize) { // Set it $this->headerSize = $headerSize; } @@ -278,7 +279,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $seekPosition Current seek position (stored here in object) * @return void */ - protected final function setSeekPosition ($seekPosition) { + protected final function setSeekPosition (int $seekPosition) { // And set it $this->seekPosition = $seekPosition; } @@ -289,16 +290,16 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ public function updateSeekPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!')); // Get key (= seek position) $seekPosition = $this->key(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', $seekPosition)); // And set it here $this->setSeekPosition($seekPosition); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!')); } /** @@ -308,7 +309,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ protected function rewindUpdateSeekPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!')); // flushFileHeader must be callable assert(is_callable(array($this, 'flushFileHeader'))); @@ -322,7 +323,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { // ... to write it back into the file $this->flushFileHeader(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!')); } /** @@ -331,12 +332,12 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ protected function seekToOldPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!')); // Seek to currently ("old") saved position $this->seek($this->getSeekPosition()); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!')); } /** @@ -369,10 +370,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $backBuffer Characters to "store" in back-buffer * @return void */ - private function setBackBuffer ($backBuffer) { - // Cast to string (so no arrays or objects) - $backBuffer = (string) $backBuffer; - + private function setBackBuffer (string $backBuffer) { // ... and set it $this->backBuffer = $backBuffer; } @@ -392,10 +390,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $currentBlock Characters to set a currently loaded block * @return void */ - private function setCurrentBlock ($currentBlock) { - // Cast to string (so no arrays or objects) - $currentBlock = (string) $currentBlock; - + private function setCurrentBlock (string $currentBlock) { // ... and set it $this->currentBlock = $currentBlock; } @@ -418,10 +413,14 @@ abstract class BaseBinaryFile extends BaseAbstractFile { */ protected function initFile (SplFileInfo $infoInstance) { // Get a file i/o pointer instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: infoInstance=%s - CALLED!', $infoInstance)); $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance)); // ... and set it here $this->setPointerInstance($pointerInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -432,10 +431,9 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $flushHeader Whether to flush the header (default: flush) * @return void */ - public function writeData ($seekPosition, $data, $flushHeader = true) { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data))); - + public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { // Write data at given position + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%s,data()=%d,flushHeader=%d - CALLED!', $seekPosition, strlen($data), intval($flushHeader))); $this->getPointerInstance()->writeAtPosition($seekPosition, $data); // Increment counter @@ -451,9 +449,10 @@ abstract class BaseBinaryFile extends BaseAbstractFile { // Seek to old position $this->seekToOldPosition(); - } // END - if + } - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -462,8 +461,9 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $length Length of the block * @return void */ - protected function markCurrentBlockAsEmpty ($length) { + protected function markCurrentBlockAsEmpty (int $length) { // Get current seek position + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length)); $currentPosition = $this->key(); // Now add it as gap entry @@ -471,6 +471,9 @@ abstract class BaseBinaryFile extends BaseAbstractFile { self::GAPS_INDEX_START => ($currentPosition - $length), self::GAPS_INDEX_END => $currentPosition, )); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -479,32 +482,31 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return $isInitialized Whether the file header is initialized */ public function isFileHeaderInitialized () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - // Default is not initialized + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); $isInitialized = false; // Is the file initialized? if ($this->isFileInitialized()) { // Some bytes has been written, so rewind to start of it. $rewindStatus = $this->rewind(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('rewindStatus=%s', $rewindStatus)); // Is the rewind() call successfull? if ($rewindStatus != 1) { // Something bad happened - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__)); - } // END - if + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Could not rewind().')); + } // Read file header $this->readFileHeader(); // The above method does already check the header $isInitialized = true; - } // END - if + } // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized))); return $isInitialized; } @@ -514,11 +516,11 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return $isInitialized Whether the file's size is zero */ public function isFileInitialized () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); // Get it from iterator which holds the pointer instance. If false is returned $fileSize = $this->size(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileSize=%s', $fileSize)); /* * The returned file size should not be false or NULL as this means @@ -530,7 +532,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { $isInitialized = ($fileSize > 0); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized))); return $isInitialized; } @@ -540,7 +542,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ public function createFileHeader () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); // The file's header should not be initialized here assert(!$this->isFileHeaderInitialized()); @@ -551,7 +553,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { // Rewind seek position (to beginning of file) and update/flush file header $this->rewindUpdateSeekPosition(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -560,36 +562,36 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $type Type of the file * @return void */ - public function preAllocateFile ($type) { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - + public function preAllocateFile (string $type) { // Is it enabled? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { // Not enabled - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__)); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Not pre-allocating file.')); // Don't continue here. return; - } // END - if + } // Message to user - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__)); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Pre-allocating file ...')); // Calculate minimum length for one entry $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry)); // Calulcate seek position + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('minLengthEntry=%s', $minLengthEntry)); $seekPosition = $minLengthEntry * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); // Now simply write a NUL there. This will pre-allocate the file. + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s', $seekPosition)); $this->writeData($seekPosition, chr(0)); // Rewind seek position (to beginning of file) and update/flush file header $this->rewindUpdateSeekPosition(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -609,7 +611,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, $whence = SEEK_SET) { // Call pointer instance return $this->getPointerInstance()->seek($offset, $whence); } @@ -620,10 +622,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read ($bytes = NULL) { - // $bytes shall be integer - assert(is_int($bytes)); - + public function read (int $bytes = NULL) { // Call pointer instance return $this->getPointerInstance()->read($bytes); } @@ -644,18 +643,21 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * only gaps are found, the file is considered as "virgin" (no entries). * * @return void + * @throws BadMethodCallException If this method is called but file is not initialized */ public function analyzeFile () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - // Make sure the file is initialized - assert($this->isFileInitialized()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); + if (!$this->isFileInitialized()) { + // Bad method call + throw new BadMethodCallException('Method called but file is not initialized.'); + } // Init counters and gaps array $this->initCountersGapsArray(); // Output message (as this may take some time) - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Analyzing file structure ... (this may take some time)')); // First rewind to the begining $this->rewind(); @@ -675,22 +677,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile { if (empty($current)) { // Then skip this part continue; - } // END - if + } // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current))); - } // END - while + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('current()=%d', strlen($current))); + } // If the last read block is empty, check gaps if (empty($current)) { // Output message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps))); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Found a total of %s gaps.', count($this->gaps))); // Check gaps, if the whole file is empty. if ($this->isFileOnlyGaps()) { // Only gaps, so don't continue here. return; - } // END - if + } /* * The above call has calculated a total size of all gaps. If the @@ -701,35 +703,29 @@ abstract class BaseBinaryFile extends BaseAbstractFile { if ($this->isDefragmentationNeeded()) { // Run "defragmentation" $this->doRunDefragmentation(); - } // END - if - } // END - if - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + } + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** * Advances to next "block" of bytes * * @return void + * @throws UnexpectedValueException If some unexpected value was found */ public function next () { // Is there nothing to read? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); if (!$this->valid()) { // Nothing to read return; - } // END - if - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key())); - - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof CalculatableBlock); + } // First calculate minimum block length + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: key()=%d', $this->key())); $length = $this->getBlockInstance()->calculateMinimumBlockLength(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length)); - - // Short be more than zero! - assert($length > 0); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%s', $length)); // Read possibly back-buffered bytes from previous call of next(). $data = $this->getBackBuffer(); @@ -739,39 +735,40 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * "block" may not fit, so this loop will continue until the EOB or EOF * has been reached whatever comes first. */ + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data))); while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) { // Then read the next possible block $block = $this->read($length); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length)); - // Is it all empty? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: block()=%d,length=%s', strlen($block), $length)); if (strlen(trim($block)) == 0) { // Mark this block as empty + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling markCurrentBlockAsEmpty(%d) ...', strlen($block))); $this->markCurrentBlockAsEmpty(strlen($block)); // Skip to next block + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CONTINUE!'); continue; - } // END - if + } // At this block then $data .= $block; // A debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data))); - } // END - while + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data))); + } // EOF reached? if ($this->isEndOfFileReached()) { // Set whole data as current read block - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling setCurrentBlock(%d) ...', strlen($data))); $this->setCurrentBlock($data); // Then abort here silently - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EOF reached - EXIT!'); return; - } // END - if + } /* * Init back-buffer which is the data that has been found beyond the @@ -783,12 +780,18 @@ abstract class BaseBinaryFile extends BaseAbstractFile { $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data); // This array must contain two elements - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, true)); - assert(count($dataArray) == 2); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: dataArray=' . print_r($dataArray, true)); + if (count($dataArray) != 2) { + // Bad count + throw new UnexpectedValueException(sprintf('dataArray()=%d is not expected, want 2', count($dataArray))); + } // Left part is the actual block, right one the back-buffer data $this->setCurrentBlock($dataArray[0]); $this->setBackBuffer($dataArray[1]); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } /** @@ -798,10 +801,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return $isValid Whether the next entry is valid */ public function valid () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - // First calculate minimum block length + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); $length = $this->getBlockInstance()->calculateMinimumBlockLength(); // Short be more than zero! @@ -848,9 +849,6 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ public function readFileHeader () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - // Call block instance $this->getBlockInstance()->readFileHeader(); } @@ -861,9 +859,6 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ public function flushFileHeader () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - // Call block instance $this->getBlockInstance()->flushFileHeader(); } @@ -875,12 +870,12 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $length Length of raw data * @return $seekPosition Found next gap's seek position */ - public function searchNextGap ($length) { + public function searchNextGap (int $length) { // If the file is only gaps, no need to seek if ($this->isFileOnlyGaps()) { // The first empty block is the first one right after the header return ($this->getHeaderSize() + 1); - } // END - if + } // @TODO Unfinished $this->partialStub('length=' . $length); diff --git a/framework/main/classes/file_directories/binary/index/class_IndexFile.php b/framework/main/classes/file_directories/binary/index/class_IndexFile.php index 35fc1e13..cca972d1 100644 --- a/framework/main/classes/file_directories/binary/index/class_IndexFile.php +++ b/framework/main/classes/file_directories/binary/index/class_IndexFile.php @@ -52,15 +52,20 @@ class IndexFile extends BaseBinaryFile implements Block { */ public final static function createIndexFile (SplFileInfo $fileInfoInstance, Block $blockInstance) { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInfoInstance=%s,blockInstance=%s - CALLED!', $fileInfoInstance, $blockInstance->__toString())); $fileInstance = new IndexFile(); // Set block instance here for callbacks $fileInstance->setBlockInstance($blockInstance); + // Expand file name with .idx + $indexInfoInstance = new SplFileInfo(sprintf('%s.idx', $fileInfoInstance->__toString())); + // Init this abstract file - $fileInstance->initFile($fileInfoInstance); + $fileInstance->initFile($indexInfoInstance); // Return the prepared instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInstance=%s - EXIT!', $fileInstance->__toString())); return $fileInstance; } @@ -72,7 +77,7 @@ class IndexFile extends BaseBinaryFile implements Block { * @return $data Hash and gap position * @throws UnsupportedOperationException If this method is called */ - public function writeValueToFile ($groupId, $value) { + public function writeValueToFile (string $groupId, $value) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, true)); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -86,7 +91,7 @@ class IndexFile extends BaseBinaryFile implements Block { * @return $data Gap position and length of the raw data * @throws UnsupportedOperationException If this method is called */ - public function writeDataToFreeGap ($groupId, $hash, $encoded) { + public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',encoded()=' . strlen($encoded)); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/file_directories/binary/stack/class_StackFile.php b/framework/main/classes/file_directories/binary/stack/class_StackFile.php index d55a06e4..f1422cf9 100644 --- a/framework/main/classes/file_directories/binary/stack/class_StackFile.php +++ b/framework/main/classes/file_directories/binary/stack/class_StackFile.php @@ -99,7 +99,7 @@ class StackFile extends BaseBinaryFile implements Block { * @return $data Gap position and length of the raw data * @throws UnsupportedOperationException If this method is called */ - public function writeDataToFreeGap ($groupId, $hash, $encoded) { + public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',hash=' . $hash . ',encoded()=' . strlen($encoded)); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/index/class_BaseIndex.php b/framework/main/classes/index/class_BaseIndex.php index c3dec50c..2b3d13a5 100644 --- a/framework/main/classes/index/class_BaseIndex.php +++ b/framework/main/classes/index/class_BaseIndex.php @@ -12,6 +12,7 @@ use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \SplFileInfo; +use \UnexpectedValueException; /** * A general index class @@ -71,25 +72,32 @@ abstract class BaseIndex extends BaseFrameworkSystem { * Reads the file header * * @return void + * @throws UnexpectedValueException If header length is invalid */ public function readFileHeader () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - // First rewind to beginning as the header sits at the beginning ... + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); $this->getIteratorInstance()->rewind(); // Then read it (see constructor for calculation) $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize()); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize())); // Have all requested bytes been read? - assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize()); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__)); - - // Last character must be the separator - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1))))); - assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize())); + if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) { + // Invalid header length + throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d', + strlen($data), + $data, + $this->getIteratorInstance()->getHeaderSize() + )); + } elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) { + // Bad last character + throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"', + $data, + chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES) + )); + } // Okay, then remove it $data = substr($data, 0, -1); @@ -109,22 +117,20 @@ abstract class BaseIndex extends BaseFrameworkSystem { $this->getIteratorInstance()->setHeader($header); // Check if the array has only 3 elements - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, true))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('header(%d)=%s', count($header), print_r($header, true))); assert(count($header) == 2); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__)); // Check magic assert($header[0] == self::INDEX_MAGIC); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__)); // Check length of count assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__)); // Decode count $header[1] = hex2bin($header[1]); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** @@ -133,9 +139,8 @@ abstract class BaseIndex extends BaseFrameworkSystem { * @return void */ public function flushFileHeader () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - // Put all informations together + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); $header = sprintf('%s%s%s%s', // Magic self::INDEX_MAGIC, @@ -153,7 +158,8 @@ abstract class BaseIndex extends BaseFrameworkSystem { // Write it to disk (header is always at seek position 0) $this->getIteratorInstance()->writeData(0, $header, false); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** @@ -165,14 +171,12 @@ abstract class BaseIndex extends BaseFrameworkSystem { */ protected function initIndex (SplFileInfo $fileInfoInstance) { // Get a file i/o pointer instance for index file + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileInfoInstance=%s - CALLED!', $fileInfoInstance)); $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this)); // Get iterator instance $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance)); - // Is the instance implementing the right interface? - assert($iteratorInstance instanceof SeekableWritableFileIterator); - // Set iterator here $this->setIteratorInstance($iteratorInstance); @@ -194,13 +198,16 @@ abstract class BaseIndex extends BaseFrameworkSystem { // And pre-allocate a bit $this->getIteratorInstance()->preAllocateFile('index'); - } // END - if + } // Load the file header $this->readFileHeader(); // Count all entries in file $this->getIteratorInstance()->analyzeFile(); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } /** @@ -210,9 +217,11 @@ abstract class BaseIndex extends BaseFrameworkSystem { */ public function calculateMinimumBlockLength () { // Calulcate it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!'); $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES)); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: length=%d - EXIT!', $length)); return $length; } @@ -256,7 +265,7 @@ abstract class BaseIndex extends BaseFrameworkSystem { * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ - public final function setHeaderSize ($headerSize) { + public final function setHeaderSize (int $headerSize) { throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -320,8 +329,8 @@ abstract class BaseIndex extends BaseFrameworkSystem { * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ - public function writeData ($seekPosition, $data, $flushHeader = true) { - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader))); + public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s,data[]=%s,flushHeader=%d - CALLED!', $seekPosition, gettype($data), intval($flushHeader))); throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -333,8 +342,8 @@ abstract class BaseIndex extends BaseFrameworkSystem { * @return $data Hash and gap position * @throws UnsupportedOperationException If this method is called */ - public function writeValueToFile ($groupId, $value) { - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, true)); + public function writeValueToFile (string $groupId, $value) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,value[%s]=%s - CALLED!', $groupId, gettype($value), print_r($value, true))); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -346,8 +355,8 @@ abstract class BaseIndex extends BaseFrameworkSystem { * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ - public function writeDataToFreeGap ($groupId, $hash, $encoded) { - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',hash=' . $hash . ',encoded()=' . strlen($encoded)); + public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded))); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/index/file_stack/class_FileStackIndex.php b/framework/main/classes/index/file_stack/class_FileStackIndex.php index a274dc46..0a104984 100644 --- a/framework/main/classes/index/file_stack/class_FileStackIndex.php +++ b/framework/main/classes/index/file_stack/class_FileStackIndex.php @@ -53,12 +53,14 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { */ public final static function createFileStackIndex (SplFileInfo $fileInfoInstance) { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: fileInfoInstance=%s - CALLED!', $fileInfoInstance)); $indexInstance = new FileStackIndex(); // Initialize index $indexInstance->initIndex($fileInfoInstance); // Return the prepared instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: indexInstance=%s - EXIT!', $indexInstance->__toString())); return $indexInstance; } @@ -69,11 +71,9 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { * @param $data Hash and gap position to be added to the index * @return void */ - public function addHashToIndex ($groupId, array $data) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gap=%d,length=%d - CALLED!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH])); - + public function addHashToIndex (string $groupId, array $data) { // Raw data been written to the file + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,gap=%d,length=%d - CALLED!', $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH])); $rawData = sprintf('%s%s%s%s%s%s%s', $groupId, self::SEPARATOR_GROUP_HASH, @@ -85,7 +85,7 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { ); // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,rawData()=%d', $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); // Search for next free gap $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData)); @@ -93,14 +93,12 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { // Gap position cannot be smaller than header length + 1 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize()); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $gapPosition)); - // Then write the data at that gap + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,gapPosition=%s', $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $gapPosition)); $this->getIteratorInstance()->writeData($gapPosition, $rawData); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d - EXIT!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); } /** @@ -110,7 +108,7 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { * @param $length Length of raw data * @return $seekPosition Found next gap's seek position */ - public function searchNextGap ($length) { + public function searchNextGap (int $length) { $this->partialStub('length=' . $length); } diff --git a/framework/main/classes/iterator/file/class_FileIterator.php b/framework/main/classes/iterator/file/class_FileIterator.php index ad9338a2..ff14542e 100644 --- a/framework/main/classes/iterator/file/class_FileIterator.php +++ b/framework/main/classes/iterator/file/class_FileIterator.php @@ -139,7 +139,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $seekPosition Seek position in file * @return $status Status of this operation */ - public function seek ($seekPosition) { + public function seek (int $seekPosition) { // Call block instance return $this->getBlockInstance()->seek($seekPosition); } @@ -163,7 +163,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read ($bytes = NULL) { + public function read (int $bytes = NULL) { // Call block instance return $this->getBlockInstance()->read($bytes); } @@ -206,7 +206,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $type Type of the file * @return void */ - public function preAllocateFile ($type) { + public function preAllocateFile (string $type) { // Just call the block instance $this->getBlockInstance()->preAllocateFile($type); } @@ -240,7 +240,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $headerSize Size of file header * @return void */ - public final function setHeaderSize ($headerSize) { + public final function setHeaderSize (int $headerSize) { // Call block instance $this->getBlockInstance()->setHeaderSize($headerSize); } @@ -304,7 +304,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $flushHeader Whether to flush the header (default: flush) * @return void */ - public function writeData ($seekPosition, $data, $flushHeader = true) { + public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { // Call block instance $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader); } @@ -326,7 +326,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $value Value to be added to the stack * @return $data Hash and gap position */ - public function writeValueToFile ($groupId, $value) { + public function writeValueToFile (string $groupId, $value) { // Call block instance return $this->getBlockInstance()->writeValueToFile($groupId, $value); } @@ -339,7 +339,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ - public function writeDataToFreeGap ($groupId, $hash, $encoded) { + public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { // Call block instance return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded); } @@ -351,7 +351,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $length Length of raw data * @return $seekPosition Found next gap's seek position */ - public function searchNextGap ($length) { + public function searchNextGap (int $length) { // Call block instance print $this->getBlockInstance()->__toString() . PHP_EOL; return $this->getBlockInstance()->searchNextGap($length); diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 6c1f3d1c..faa1b0dc 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -236,14 +236,12 @@ abstract class BaseFileStack extends BaseStacker { */ protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) { // Get a stack file instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance=%s,type=%s - CALLED!', $fileInfoInstance, $type)); $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this)); // Get iterator instance $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance)); - // Is the instance implementing the right interface? - assert($iteratorInstance instanceof SeekableWritableFileIterator); - // Set iterator here $this->setIteratorInstance($iteratorInstance); @@ -267,7 +265,7 @@ abstract class BaseFileStack extends BaseStacker { // And pre-allocate a bit $this->getIteratorInstance()->preAllocateFile('file_stack'); - } // END - if + } // Load the file header $this->readFileHeader(); @@ -292,18 +290,18 @@ abstract class BaseFileStack extends BaseStacker { * @param $value Value to add to this stacker * @return void * @throws FullStackerException If the stack is full + * @throws InvalidArgumentException Not all variable types are wanted here */ protected function addValue (string $stackerName, $value) { // Do some tests + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true))); if ($this->isStackFull($stackerName)) { // Stacker is full throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL); - } // END - if - - // No objects/resources are allowed as their serialization takes to long - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true)); - assert(!is_object($value)); - assert(!is_resource($value)); + } elseif (is_resource($value) || is_object($value)) { + // Not wanted type + throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($value))); + } /* * Now add the value to the file stack which returns gap position, a @@ -584,7 +582,7 @@ abstract class BaseFileStack extends BaseStacker { * @return void * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ - public function writeData ($seekPosition, $data, bool $flushHeader = true) { + public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader))); throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -597,7 +595,7 @@ abstract class BaseFileStack extends BaseStacker { * @return $data Hash and gap position * @throws UnsupportedOperationException This method is not (and maybe never will be) supported */ - public function writeValueToFile ($groupId, $value) { + public function writeValueToFile (string $groupId, $value) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true))); throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -633,7 +631,7 @@ abstract class BaseFileStack extends BaseStacker { * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ - public function writeDataToFreeGap ($groupId, string $hash, $encoded) { + public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) { // Raw data been written to the file /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded))); $rawData = sprintf('%s%s%s%s%s', diff --git a/framework/main/interfaces/block/class_Block.php b/framework/main/interfaces/block/class_Block.php index 3f3785f0..9f593ecc 100644 --- a/framework/main/interfaces/block/class_Block.php +++ b/framework/main/interfaces/block/class_Block.php @@ -117,7 +117,7 @@ interface Block extends FrameworkInterface { * @param $value Value to be added to the stack * @return $data Hash and gap position */ - function writeValueToFile ($groupId, $rawData); + function writeValueToFile (string $groupId, string $rawData); /** * Writes given raw data to the file and returns a gap position and length @@ -127,7 +127,7 @@ interface Block extends FrameworkInterface { * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ - function writeDataToFreeGap ($groupId, string $hash, $encoded); + function writeDataToFreeGap (string $groupId, string $hash, string $encoded); /** * Writes data at given position @@ -137,7 +137,7 @@ interface Block extends FrameworkInterface { * @param $flushHeader Whether to flush the header (default: flush) * @return void */ - function writeData ($seekPosition, $data, bool $flushHeader = true); + function writeData (int $seekPosition, string $data, bool $flushHeader = true); /** * Searches for next suitable gap the given length of data can fit in diff --git a/framework/main/interfaces/index/stack/class_IndexableStack.php b/framework/main/interfaces/index/stack/class_IndexableStack.php index 3254dffb..e00925e4 100644 --- a/framework/main/interfaces/index/stack/class_IndexableStack.php +++ b/framework/main/interfaces/index/stack/class_IndexableStack.php @@ -36,6 +36,6 @@ interface IndexableStack extends Indexable, CalculatableBlock { * @param $data Hash and gap position to be added to the index * @return void */ - function addHashToIndex ($stackName, array $data); + function addHashToIndex (string $stackName, array $data); } diff --git a/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php b/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php index e2c9fe47..b93ace41 100644 --- a/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php +++ b/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php @@ -35,7 +35,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $seekPosition Seek position in file * @return $status Status of this operation */ - function seek ($seekPosition); + function seek (int $seekPosition); /** * Size of file stack @@ -50,7 +50,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $bytes Amount of bytes to read * @return $data Data read from file */ - function read ($bytes); + function read (int $bytes); /** * Analyzes entries in index file. This will count all found (and valid) @@ -81,7 +81,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $type Type of the file * @return void */ - function preAllocateFile ($type); + function preAllocateFile (string $type); /** * Initializes counter for valid entries, arrays for damaged entries and @@ -152,7 +152,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $flushHeader Whether to flush the header (default: flush) * @return void */ - function writeData ($seekPosition, $data, bool $flushHeader = true); + function writeData (int $seekPosition, string $data, bool $flushHeader = true); /** * Getter for seek position @@ -168,7 +168,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $value Value to be added to the stack * @return $data Hash and gap position */ - function writeValueToFile ($groupId, $value); + function writeValueToFile (string $groupId, $value); /** * Writes given raw data to the file and returns a gap position and length @@ -178,7 +178,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $encoded Encoded value to be written to the file * @return $data Gap position and length of the raw data */ - function writeDataToFreeGap ($groupId, string $hash, $encoded); + function writeDataToFreeGap (string $groupId, string $hash, string $encoded); /** * Searches for next suitable gap the given length of data can fit in -- 2.39.5