From: Roland Haeder Date: Sat, 17 May 2014 20:41:22 +0000 (+0200) Subject: Continued with file-based stacks: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=2724091df3f002f07609232a74ad5620934dbb47 Continued with file-based stacks: - added flushFileHeader() - still there will be a lot stub messages Signed-off-by: Roland Häder --- diff --git a/contrib/file_stack/format.txt b/contrib/file_stack/format.txt index 40044386..637baf6f 100644 --- a/contrib/file_stack/format.txt +++ b/contrib/file_stack/format.txt @@ -5,11 +5,11 @@ This is the format for a file-based stack which works as any Stackable class. General made up: -Purpose: | "magic" | separator | count | position | separator | ----------+-----------+-----------+----------+---------------+-----------+ -Bytes: | 9 | 1 | 20 (hex) | 2 - n^2 (hex) | 1 | ----------+-----------+-----------+----------+---------------+-----------+ -Example: | STACKv1.0 | 00 | 00...ff | 05 | ff | +Purpose: | "magic" | separator | count | position | separator | +---------+-----------+-----------+----------+----------+-----------+ +Bytes: | 9 | 1 | 20 (hex) | 20 (hex) | 1 | +---------+-----------+-----------+----------+----------+-----------+ +Example: | STACKv1.0 | 00 | 00...ff | 00...ff | ff | Continued: diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index f0539e10..8491479d 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -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(); } }