]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/stacker/file/class_BaseFileStack.php
Introduced writeData() (low-level method).
[core.git] / inc / classes / main / stacker / file / class_BaseFileStack.php
index b7597f0c9c4393c2f522be42078517c3dda90797..dd8570c45185aeb0f202bb0452221f093ae3c27a 100644 (file)
@@ -30,17 +30,22 @@ class BaseFileStack extends BaseStacker {
        /**
         * Separator magic->count
         */
-       const SEPARATOR_MAGIC_COUNT = 0x00;
+       const SEPARATOR_MAGIC_COUNT = 0x01;
+
+       /**
+        * Separator count->position
+        */
+       const SEPARATOR_COUNT_SEEK_POS = 0x02;
 
        /**
         * Separator position->entries
         */
-       const SEPARATOR_SEEK_POS_ENTRIES = 0xff;
+       const SEPARATOR_SEEK_POS_ENTRIES = 0x03;
 
        /**
         * Separator hash->name
         */
-       const SEPARATOR_HASH_NAME = 0x05;
+       const SEPARATOR_HASH_NAME = 0x04;
 
        /**
         * Length of name
@@ -204,7 +209,7 @@ class BaseFileStack extends BaseStacker {
                // The file's header should not be initialized here
                assert(!$this->isFileHeaderInitialized());
 
-               // Flush file header
+               // Simple flush file header which will create it.
                $this->flushFileHeader();
 
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
@@ -219,7 +224,7 @@ class BaseFileStack extends BaseStacker {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
 
                // Put all informations together
-               $header = sprintf('%s%s%s%s%s',
+               $header = sprintf('%s%s%s%s%s%s',
                        // Magic
                        self::STACK_MAGIC,
 
@@ -229,15 +234,34 @@ class BaseFileStack extends BaseStacker {
                        // Total entries (will be zero) and pad it to 20 chars
                        str_pad($this->dec2hex($this->getCounter()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
 
+                       // Separator count<->seek position
+                       chr(self::SEPARATOR_COUNT_SEEK_POS),
+
                        // Position (will be zero)
-                       str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
+                       str_pad($this->dec2hex($this->getSeekPosition(), 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
 
                        // Separator position<->entries
                        chr(self::SEPARATOR_SEEK_POS_ENTRIES)
                );
 
                // Write it to disk (header is always at seek position 0)
-               $this->getIteratorInstance()->writeAtPosition(0, $header);
+               $this->writeData(0, $header);
+
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+       }
+
+       /**
+        * Writes data at given position
+        *
+        * @param       $seekPosition   Seek position
+        * @param       $data                   Data to be written
+        * @return      void
+        */
+       private function writeData ($seekPosition, $data) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
+
+               // Write data at given position
+               $this->getIteratorInstance()->writeAtPosition($seekPosition, $data);
 
                // Update seek position
                $this->updateSeekPosition();
@@ -273,11 +297,11 @@ class BaseFileStack extends BaseStacker {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
 
                // Calulcate seek position
-               $seekPosition = $minLengthEntry * $this->getConfigInstanstance()->getConfigEntry('file_stack_pre_allocate_count');
+               $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_count');
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
 
-               // Now seek to the position
-               $this->getIteratorInstance()->seek($seekPosition);
+               // Now simply write a NUL there. This will pre-allocate the file.
+               $this->writeData($seekPosition, chr(0));
 
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }