Continued with file-based stacks:
[core.git] / inc / classes / main / stacker / file / class_BaseFileStack.php
index f0539e10fc0f0130a222cae198b9ca370f48f295..8491479d9a41ae7efc474d374b68a688218a1434 100644 (file)
@@ -42,6 +42,16 @@ class BaseFileStack extends BaseStacker {
         */
        const SEPARATOR_HASH_NAME = 0x05;
 
+       /**
+        * Length of count
+        */
+       const COUNT_LENGTH = 20;
+
+       /**
+        * Length of position
+        */
+       const COUNT_POSITION = 20;
+
        /**
         * Protected constructor
         *
@@ -103,6 +113,50 @@ class BaseFileStack extends BaseStacker {
                return $isInitialized;
        }
 
+       /**
+        * Creates the file-stack's header
+        *
+        * @return      void
+        */
+       private function createFileHeader () {
+               // The file's header should not be initialized here
+               assert(!$this->isFileHeaderInitialized());
+
+               // Init  counter
+               $this->getIteratorInstance()->initCounter();
+
+               // Flush file header
+               $this->flushFileHeader();
+       }
+
+       /**
+        * Flushes the file header
+        *
+        * @return      void
+        */
+       private function flushFileHeader () {
+               // Put all informations together
+               $header = sprintf('%s%s%s%s%s',
+                       // Magic
+                       self::STACK_MAGIC,
+
+                       // Separator magic<->count
+                       chr(self::SEPARATOR_MAGIC_COUNT),
+
+                       // Total entries (will be zero) and pad it to 20 chars
+                       str_pad($this->dec2hex($this->getIteratorInstance()->getCount()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
+
+                       // Position (will be zero)
+                       str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
+
+                       // Separator position<->entries
+                       chr(self::SEPARATOR_SEEK_POS_ENTRIES)
+               );
+
+               // Write it to disk
+               $this->getIteratorInstance()->writeAtPosition(0, $header);
+       }
+
        /**
         * Initializes this file-based stack.
         *
@@ -129,6 +183,9 @@ class BaseFileStack extends BaseStacker {
                } else {
                        // No, then create it (which may pre-allocate the stack)
                        $this->createFileHeader();
+
+                       // And pre-allocate a bit
+                       $this->preAllocateFile();
                }
        }