]> 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 0e2c415c875f7a3bcc66fe2371268fb65f3f5cff..6ec08504771406c680ab15d4027e332ca39ca9cf 100644 (file)
@@ -61,6 +61,11 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         */
        const SEPARATOR_GAP_LENGTH = 0x03;
 
+       /**
+        * Minimum block length
+        */
+       private static $minimumBlockLength = 0;
+
        /**
         * Protected constructor
         *
@@ -76,7 +81,7 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         * Reads the file header
         *
         * @return      void
-        * @throws      UnexpectedValueException        If header length is invalid
+        * @throws      UnexpectedValueException        If header length or count of elements is invalid
         */
        public function readFileHeader () {
                // First rewind to beginning as the header sits at the beginning ...
@@ -87,7 +92,7 @@ abstract class BaseIndex extends BaseFrameworkSystem {
                $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
 
                // Have all requested bytes been read?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
+               /* 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',
@@ -117,22 +122,26 @@ 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('header(%d)=%s', count($header), print_r($header, true)));
-               assert(count($header) == 2);
-
-               // Check magic
-               assert($header[0] == self::INDEX_MAGIC);
-
-               // Check length of count
-               assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
+               /* 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]);
 
+               // Set it here
+               $this->getIteratorInstance()->setHeader($header);
+
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
        }
@@ -175,7 +184,7 @@ 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));
+               /* 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
@@ -220,13 +229,21 @@ abstract class BaseIndex extends BaseFrameworkSystem {
         * @return      $length         Minimum length for one entry/block
         */
        public function calculateMinimumBlockLength () {
-               // Calulcate it
+               // Is it "cached"?
                /* 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));
+               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
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: length=%d - EXIT!', $length));
-               return $length;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength));
+               return self::$minimumBlockLength;
        }
 
        /**