From 0af07e30d2e346b21cbb57b53c7550ce93baac06 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sat, 24 May 2014 13:26:18 +0200 Subject: [PATCH] Moved analyzeFile() to BaseFile where a much better place is (and duplicate code is avoided, too). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/classes/interfaces/io/class_Pointer.php | 9 ++++ .../class_SeekableWritableFileIterator.php | 9 ++++ .../main/file_directories/class_BaseFile.php | 41 ++++++++++++++++++- inc/classes/main/index/class_BaseIndex.php | 39 +----------------- .../main/iterator/io/class_FileIoIterator.php | 13 ++++++ .../main/stacker/file/class_BaseFileStack.php | 40 +----------------- 6 files changed, 74 insertions(+), 77 deletions(-) diff --git a/inc/classes/interfaces/io/class_Pointer.php b/inc/classes/interfaces/io/class_Pointer.php index c17d881c..b0882b7a 100644 --- a/inc/classes/interfaces/io/class_Pointer.php +++ b/inc/classes/interfaces/io/class_Pointer.php @@ -52,6 +52,15 @@ interface Pointer extends FrameworkInterface { * @return $isEndOfFileReached Whether the EOF has been reached */ function isEndOfFileReached (); + + /** + * Analyzes entries in index file. This will count all found (and valid) + * entries, mark invalid as damaged and count gaps ("fragmentation"). If + * only gaps are found, the file is considered as "virgin" (no entries). + * + * @return void + */ + function analyzeFile (); } // [EOF] diff --git a/inc/classes/interfaces/iterator/class_SeekableWritableFileIterator.php b/inc/classes/interfaces/iterator/class_SeekableWritableFileIterator.php index abd1e583..263bba68 100644 --- a/inc/classes/interfaces/iterator/class_SeekableWritableFileIterator.php +++ b/inc/classes/interfaces/iterator/class_SeekableWritableFileIterator.php @@ -54,6 +54,15 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @return $data Data read from file */ function read ($bytes); + + /** + * Analyzes entries in index file. This will count all found (and valid) + * entries, mark invalid as damaged and count gaps ("fragmentation"). If + * only gaps are found, the file is considered as "virgin" (no entries). + * + * @return void + */ + function analyzeFile (); } // [EOF] diff --git a/inc/classes/main/file_directories/class_BaseFile.php b/inc/classes/main/file_directories/class_BaseFile.php index 7816e866..fb601626 100644 --- a/inc/classes/main/file_directories/class_BaseFile.php +++ b/inc/classes/main/file_directories/class_BaseFile.php @@ -36,6 +36,9 @@ class BaseFile extends BaseFrameworkSystem { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + + // Init counters and gaps array + $this->initCountersGapsArray(); } /** @@ -58,7 +61,6 @@ class BaseFile extends BaseFrameworkSystem { * @throws UnsupportedOperationException If this method is called */ public final function getPointer () { - self::createDebugInstance(__CLASS__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.'); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } @@ -193,6 +195,43 @@ class BaseFile extends BaseFrameworkSystem { public final function isEndOfFileReached () { return $this->getPointerInstance()->isEndOfFileReached(); } + + /** + * Analyzes entries in index file. This will count all found (and valid) + * entries, mark invalid as damaged and count gaps ("fragmentation"). If + * only gaps are found, the file is considered as "virgin" (no entries). + * + * @return void + */ + public function analyzeFile () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Make sure the file is initialized + assert($this->isFileInitialized()); + + // Init counters and gaps array + $this->initCountersGapsArray(); + + // Output message (as this may take some time) + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); + + // First rewind to the begining + $this->rewind(); + + // Then try to load all entries + while ($this->valid()) { + // Go to next entry + $this->next(); + + // Get current entry + $current = $this->current(); + + // Simply output it + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE))); + } // END - while + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } } // [EOF] diff --git a/inc/classes/main/index/class_BaseIndex.php b/inc/classes/main/index/class_BaseIndex.php index de0e5629..0ee7b77c 100644 --- a/inc/classes/main/index/class_BaseIndex.php +++ b/inc/classes/main/index/class_BaseIndex.php @@ -138,43 +138,6 @@ class BaseIndex extends BaseFrameworkSystem { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } - /** - * Analyzes entries in index file. This will count all found (and valid) - * entries, mark invalid as damaged and count gaps ("fragmentation"). If - * only gaps are found, the file is considered as "virgin" (no entries). - * - * @return void - */ - private function analyzeFile () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Make sure the file is initialized - assert($this->isFileInitialized()); - - // Init counters and gaps array - $this->initCountersGapsArray(); - - // Output message (as this may take some time) - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); - - // First rewind to the begining - $this->getIteratorInstance()->rewind(); - - // Then try to load all entries - while ($this->getIteratorInstance()->valid()) { - // Go to next entry - $this->getIteratorInstance()->next(); - - // Get current entry - $current = $this->getIteratorInstance()->current(); - - // Simply output it - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE))); - } // END - while - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - /** * Initializes this index * @@ -211,7 +174,7 @@ class BaseIndex extends BaseFrameworkSystem { $this->readFileHeader(); // Count all entries in file - $this->analyzeFile(); + $this->getIteratorInstance()->analyzeFile(); } /** diff --git a/inc/classes/main/iterator/io/class_FileIoIterator.php b/inc/classes/main/iterator/io/class_FileIoIterator.php index 20d351f5..f2158c63 100644 --- a/inc/classes/main/iterator/io/class_FileIoIterator.php +++ b/inc/classes/main/iterator/io/class_FileIoIterator.php @@ -134,6 +134,7 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato * Advances to next "block" of bytes * * @return void + * @todo This method will load large but empty files in a whole */ public function next () { // Is there nothing to read? @@ -282,6 +283,18 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato // Call pointer instance return $this->getPointerInstance()->read($bytes); } + + /** + * Analyzes entries in index file. This will count all found (and valid) + * entries, mark invalid as damaged and count gaps ("fragmentation"). If + * only gaps are found, the file is considered as "virgin" (no entries). + * + * @return void + */ + public function analyzeFile () { + // Just call the pointer instance + $this->getPointerInstance()->analyzeFile(); + } } // [EOF] diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index d16068d3..8e3ae4d8 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -55,6 +55,7 @@ class BaseFileStack extends BaseStacker { * Reads the file header * * @return void + * @todo To hard assertions here, better rewrite them to exceptions */ protected function readFileHeader () { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); @@ -150,43 +151,6 @@ class BaseFileStack extends BaseStacker { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } - /** - * Analyzes entries in stack file. This will count all found (and valid) - * entries, mark invalid as damaged and count gaps ("fragmentation"). If - * only gaps are found, the file is considered as "virgin" (no entries). - * - * @return void - */ - private function analyzeFile () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Make sure the file is initialized - assert($this->isFileInitialized()); - - // Init counters and gaps array - $this->initCountersGapsArray(); - - // Output message (as this may take some time) - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); - - // First rewind to the begining - $this->getIteratorInstance()->rewind(); - - // Then try to load all entries - while ($this->getIteratorInstance()->valid()) { - // Go to next entry - $this->getIteratorInstance()->next(); - - // Get current entry - $current = $this->getIteratorInstance()->current(); - - // Simply output it - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current(%s)=%s', __METHOD__, __LINE__, strlen($current), print_r($current, TRUE))); - } // END - while - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - /** * Initializes this file-based stack. * @@ -221,7 +185,7 @@ class BaseFileStack extends BaseStacker { $this->readFileHeader(); // Count all entries in file - $this->analyzeFile(); + $this->getIteratorInstance()->analyzeFile(); /* * Get stack index instance. This can be used for faster -- 2.39.2