]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/index/class_BaseIndex.php
Continued:
[core.git] / framework / main / classes / index / class_BaseIndex.php
index dbc65fe8b79a46c5a40b5eb8705409ed1bf9d2b2..0a801bf109eb6f5c9369d6ae9921b091eba79acb 100644 (file)
@@ -3,21 +3,24 @@
 namespace Org\Mxchange\CoreFramework\Index;
 
 // Import framework stuff
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
+use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
 
 // Import SPL stuff
 use \SplFileInfo;
+use \UnexpectedValueException;
 
 /**
  * A general index class
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -35,6 +38,9 @@ use \SplFileInfo;
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 abstract class BaseIndex extends BaseFrameworkSystem {
+       // Load traits
+       use IteratorTrait;
+
        /**
         * Magic for this index
         */
@@ -55,13 +61,18 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         */
        const SEPARATOR_GAP_LENGTH = 0x03;
 
+       /**
+        * Minimum block length
+        */
+       private static $minimumBlockLength = 0;
+
        /**
         * Protected constructor
         *
         * @param       $className      Name of the class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
                parent::__construct($className);
        }
@@ -70,25 +81,32 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         * Reads the file header
         *
         * @return      void
+        * @throws      UnexpectedValueException        If header length or count of elements 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('BASE-INDEX: 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);
@@ -104,26 +122,28 @@ abstract class BaseIndex extends BaseFrameworkSystem {
                 */
                $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
 
-               // Set it here
-               $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)));
-               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__));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header()=%d', count($header)));
+               //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header(%d)=%s', count($header), print_r($header, true)));
+               if (count($header) != 2) {
+                       // Bad header
+                       throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header)));
+               } elseif ($header[0] !== self::INDEX_MAGIC) {
+                       // Magic must be in first element
+                       throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], self::INDEX_MAGIC));
+               } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
+                       // Length of total entries not matching
+                       throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BaseBinaryFile::LENGTH_COUNT));
+               }
 
                // Decode count
                $header[1] = hex2bin($header[1]);
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               // Set it here
+               $this->getIteratorInstance()->setHeader($header);
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
        }
 
        /**
@@ -132,9 +152,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,
@@ -143,7 +162,7 @@ abstract class BaseIndex extends BaseFrameworkSystem {
                        chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
 
                        // Total entries
-                       str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
+                       str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
 
                        // Separator header<->entries
                        chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
@@ -152,7 +171,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!');
        }
 
        /**
@@ -164,14 +184,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('BASE-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $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);
 
@@ -193,13 +211,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!');
        }
 
        /**
@@ -208,11 +229,21 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         * @return      $length         Minimum length for one entry/block
         */
        public function calculateMinimumBlockLength () {
-               // Calulcate it
-               $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
+               // Is it "cached"?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
+               if (self::$minimumBlockLength == 0) {
+                       // Calulcate it
+                       self::$minimumBlockLength = (
+                               // Type
+                               BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) +
+                               // Position
+                               BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES))
+                       );
+               }
 
                // Return it
-               return $length;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength));
+               return self::$minimumBlockLength;
        }
 
        /**
@@ -255,7 +286,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);
        }
 
@@ -319,8 +350,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);
        }
 
@@ -332,8 +363,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);
        }
 
@@ -345,8 +376,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);
        }