]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/index/class_BaseIndex.php
Introduced interface Pointer, valid() is now used.
[core.git] / inc / classes / main / index / class_BaseIndex.php
index ea2484b0a306aae676d415b5bffd6291ff3e0ac5..bca12f4a0093d24c92f8adcfbb70c1f8c389147c 100644 (file)
@@ -64,18 +64,18 @@ class BaseIndex extends BaseFrameworkSystem {
         *
         * @return      void
         */
-       private function readFileHeader () {
+       protected function readFileHeader () {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
 
                // First rewind to beginning as the header sits at the beginning ...
                $this->getIteratorInstance()->rewind();
 
                // Then read it (see constructor for calculation)
-               $data = $this->getIteratorInstance()->read($this->headerSize);
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->headerSize));
+               $data = $this->getIteratorInstance()->read($this->getHeaderSize());
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getHeaderSize()));
 
                // Have all requested bytes been read?
-               assert(strlen($data) == $this->headerSize);
+               assert(strlen($data) == $this->getHeaderSize());
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
 
                // Last character must be the separator
@@ -95,23 +95,26 @@ class BaseIndex extends BaseFrameworkSystem {
                 * 0 => magic
                 * 1 => total entries
                 */
-               $this->header = explode(chr(self::SEPARATOR_HEADER_DATA), $data);
+               $header = explode(chr(self::SEPARATOR_HEADER_DATA), $data);
+
+               // Set it here
+               $this->setHeader($header);
 
                // Check if the array has only 3 elements
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($this->header), print_r($this->header, TRUE)));
-               assert(count($this->header) == 2);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, TRUE)));
+               assert(count($header) == 2);
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
 
                // Check magic
-               assert($this->header[0] == self::INDEX_MAGIC);
+               assert($header[0] == self::INDEX_MAGIC);
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
 
                // Check length of count
-               assert(strlen($this->header[1]) == self::LENGTH_COUNT);
+               assert(strlen($header[1]) == self::LENGTH_COUNT);
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
 
                // Decode count
-               $this->header[1] = hex2bin($this->header[1]);
+               $header[1] = hex2bin($header[1]);
 
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
@@ -121,20 +124,26 @@ class BaseIndex extends BaseFrameworkSystem {
         *
         * @return      void
         */
-       private function flushFileHeader () {
+       protected function flushFileHeader () {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
 
                // Put all informations together
-               $header = sprintf('%s%s',
+               $header = sprintf('%s%s%s%s',
                        // Magic
                        self::INDEX_MAGIC,
 
-                       // Separator position<->entries
+                       // Separator header data
+                       chr(self::SEPARATOR_HEADER_DATA),
+
+                       // Total entries
+                       str_pad($this->dec2hex($this->getCounter()), self::LENGTH_COUNT, '0', STR_PAD_LEFT),
+
+                       // Separator header<->entries
                        chr(self::SEPARATOR_HEADER_ENTRIES)
                );
 
                // Write it to disk (header is always at seek position 0)
-               $this->writeData(0, $header);
+               $this->writeData(0, $header, FALSE);
 
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
@@ -153,26 +162,59 @@ class BaseIndex extends BaseFrameworkSystem {
                assert($this->isFileInitialized());
 
                // Init counters and gaps array
-               $this->initGapsArray();
+               $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__));
        }
 
+       /**
+        * Calculates minimum length for one entry
+        *
+        * @return      $length         Minimum length for one entry
+        */
+       protected function caluclateMinimumFileEntryLength () {
+               // Calulcate it
+               // @TODO Not finished yet
+               $length = 0;
+
+               // Return it
+               return $length;
+       }
+
        /**
         * Initializes this index
         *
         * @param       $fileName       File name of this index
         * @return      void
+        * @todo        Currently the index file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole index file.
         */
        protected function initIndex ($fileName) {
+               // Append index file extension
+               $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
+
                // Get a file i/o pointer instance for index file
-               $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
+               $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName));
 
                // Get iterator instance
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance));
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);