X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fstacker%2Ffile%2Fclass_BaseFileStack.php;h=6f55182793b19a9c459f96bbc9a013381d5a7635;hp=21315929272717a46af5bb5dae3c5a56e0b1feb8;hb=3f642576006694216befb445e22e2e4b79e3ffbd;hpb=aea999914f021557cf9d207a84ca4e65871adb45 diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index 21315929..6f551827 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -42,6 +42,11 @@ class BaseFileStack extends BaseStacker { */ const SEPARATOR_HASH_NAME = 0x05; + /** + * Length of name + */ + const COUNT_NAME = 10; + /** * Length of count */ @@ -120,11 +125,16 @@ class BaseFileStack extends BaseStacker { * @return void */ private function updateSeekPosition () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + // Get key (= seek position) $seekPosition = $this->getIteratorInstance()->key(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); // And set it here $this->setSeekPosition($seekPosition); + + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } /** @@ -133,19 +143,28 @@ class BaseFileStack extends BaseStacker { * @return $isInitialized Whether the file header is initialized */ private function isFileHeaderInitialized () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); // Default is not initialized $isInitialized = FALSE; // Is the file initialized? if ($this->isFileInitialized()) { // Some bytes has been written, so rewind to start of it. - $this->getIteratorInstance()->rewind(); + $rewindStatus = $this->getIteratorInstance()->rewind(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus)); + + // Is the rewind() call successfull? + if ($rewindStatus != 1) { + // Something bad happened + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__)); + } // END - if // Read file header $this->readFileHeader(); } // END - if // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); return $isInitialized; } @@ -155,8 +174,11 @@ class BaseFileStack extends BaseStacker { * @return $isInitialized Whether the file's size is zero */ private function isFileInitialized () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + // Get it from iterator which holds the pointer instance. If FALSE is returned $fileSize = $this->getIteratorInstance()->size(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize)); /* * The returned file size should not be FALSE or NULL as this means @@ -168,6 +190,7 @@ class BaseFileStack extends BaseStacker { $isInitialized = ($fileSize > 0); // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); return $isInitialized; } @@ -177,11 +200,14 @@ class BaseFileStack extends BaseStacker { * @return void */ private function createFileHeader () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); // The file's header should not be initialized here assert(!$this->isFileHeaderInitialized()); // Flush file header $this->flushFileHeader(); + + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); } /** @@ -190,6 +216,8 @@ class BaseFileStack extends BaseStacker { * @return void */ private function flushFileHeader () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + // Put all informations together $header = sprintf('%s%s%s%s%s', // Magic @@ -213,6 +241,45 @@ class BaseFileStack extends BaseStacker { // Update seek position $this->updateSeekPosition(); + + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Pre-allocates file (if enabled) with some space for later faster write access. + * + * @return void + */ + private function preAllocateFile () { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Is it enabled? + if ($this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_enabled') != 'Y') { + // Not enabled + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating stack file.', __METHOD__, __LINE__)); + + // Don't continue here. + return; + } // END - if + + // Message to user + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating stack file ...', __METHOD__, __LINE__)); + + /* + * Calculate minimum length for one entry: + * minimum length = hash length + separator + name + minimum entry size = ?? + 1 + 10 + 1 = ?? + */ + $minLengthEntry = self::getHashLength() + strlen(self::SEPARATOR_HASH_NAME) + self::COUNT_NAME + 1; + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry)); + + // Calulcate seek position + $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); + + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } /**