Continued with file-based stacks:
authorRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 22:34:50 +0000 (00:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 22:34:50 +0000 (00:34 +0200)
- implemented pre-allocation of the file (untested)
- renamed hashString() in BaseFrameworkSystem to hash() and made it static
- added static "getter" getHashLength() (this method's return value is "cached")

Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/interfaces/crypto/class_Cryptable.php
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/stacker/file/class_BaseFileStack.php
inc/config.php

index a3da66c..9080025 100644 (file)
@@ -30,9 +30,10 @@ interface Cryptable extends FrameworkInterface {
         *
         * @param       $str            Unhashed string
         * @param       $oldHash        A hash from previous hashed string
+        * @param       $withFixed      Whether to include a fixed salt (not recommended in p2p applications)
         * @return      $hashed         The hashed and salted string
         */
-       function hashString ($str, $oldHash = '');
+       function hashString ($str, $oldHash = '', $withFixed = TRUE);
 
        /**
         * Encrypt the string with fixed salt
index 0bd3909..47282b3 100644 (file)
@@ -203,6 +203,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        private $genericArray = array();
 
+       /**
+        * Length of output from hash()
+        */
+       private static $hashLength = NULL;
+
        /***********************
         * Exception codes.... *
         ***********************/
@@ -2090,19 +2095,37 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        }
 
        /**
-        * Hashes a given string with a simple but stronger hash function (no salts)
+        * Hashes a given string with a simple but stronger hash function (no salt)
+        * and hex-encode it.
         *
         * @param       $str    The string to be hashed
         * @return      $hash   The hash from string $str
         */
-       public function hashString ($str) {
+       public static final function hash ($str) {
                // Hash given string with (better secure) hasher
-               $hash = mhash(MHASH_SHA256, $str);
+               $hash = bin2hex(mhash(MHASH_SHA256, $str));
 
                // Return it
                return $hash;
        }
 
+       /**
+        * "Getter" for length of hash() output. This will be "cached" to speed up
+        * things.
+        *
+        * @return      $length         Length of hash() output
+        */
+       public static final function getHashLength () {
+               // Is it cashed?
+               if (is_null(self::$hashLength)) {
+                       // No, then hash a string and save its length.
+                       self::$hashLength = strlen(self::hash('abc123'));
+               } // END - if
+
+               // Return it
+               return self::$hashLength;
+       }
+
        /**
         * Checks whether the given number is really a number (only chars 0-9).
         *
index 95fb8c8..b7597f0 100644 (file)
@@ -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__));
        }
 
        /**
@@ -143,6 +153,12 @@ class BaseFileStack extends BaseStacker {
                        $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
@@ -158,6 +174,8 @@ 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));
@@ -182,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__));
        }
 
        /**
@@ -195,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
@@ -218,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->getConfigInstanstance()->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__));
        }
 
        /**
index 00fce07..6d7e3d3 100644 (file)
@@ -371,5 +371,11 @@ $cfg->setConfigEntry('file_raw_input_output_class', 'FrameworkFileInputOutputPoi
 // CFG: FILE-IO-ITERATOR-CLASS
 $cfg->setConfigEntry('file_io_iterator_class', 'FileIoIterator');
 
+// CFG: FILE-STACK-PRE-ALLOCATE-ENABLED
+$cfg->setConfigEntry('file_stack_pre_allocate_enabled', 'Y');
+
+// CFG: FILE-STACK-PRE-ALLOCATE-COUNT
+$cfg->setConfigEntry('file_stack_pre_allocate_count', 100);
+
 // [EOF]
 ?>