X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ffile_directories%2Fclass_BaseFile.php;h=5fade9baeeaaded5a0f87f0af175e2fed130e20d;hp=632cbdcec73bce2dfe7dffaf79711c2de2178988;hb=d4a9ba3bac64cc833de7b5af32bca2880b6cd542;hpb=55a804571db8f1fade4c0cd3149ea847a3a7002d diff --git a/inc/classes/main/file_directories/class_BaseFile.php b/inc/classes/main/file_directories/class_BaseFile.php index 632cbdce..5fade9ba 100644 --- a/inc/classes/main/file_directories/class_BaseFile.php +++ b/inc/classes/main/file_directories/class_BaseFile.php @@ -141,6 +141,16 @@ y * @return void $this->currentBlock = $currentBlock; } + /** + * Gets currently read data + * + * @return $current Currently read data + */ + public function getCurrentBlock () { + // Return it + return $this->currentBlock; + } + /** * Initializes this file class * @@ -155,6 +165,154 @@ y * @return void $this->setPointerInstance($pointerInstance); } + /** + * Writes data at given position + * + * @param $seekPosition Seek position + * @param $data Data to be written + * @param $flushHeader Whether to flush the header (default: flush) + * @return void + */ + protected function writeData ($seekPosition, $data, $flushHeader = TRUE) { + //* 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->getPointerInstance()->writeAtPosition($seekPosition, $data); + + // Update seek position + $this->updateSeekPosition(); + + // Flush the header? + if ($flushHeader === TRUE) { + // Flush header + $this->flushFileHeader(); + + // Seek to old position + $this->seekToOldPosition(); + } // END - if + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Checks whether the file header is initialized + * + * @return $isInitialized Whether the file header is initialized + */ + public 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. + $rewindStatus = $this->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(); + + // The above method does already check the header + $isInitialized = TRUE; + } // END - if + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + return $isInitialized; + } + + /** + * Checks whether the assigned file has been initialized + * + * @return $isInitialized Whether the file's size is zero + */ + public 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->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 + * that the pointer class does not work correctly. + */ + assert(is_int($fileSize)); + + // Is more than 0 returned? + $isInitialized = ($fileSize > 0); + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + return $isInitialized; + } + + /** + * Creates the assigned file + * + * @return void + */ + public 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()); + + // Simple flush file header which will create it. + $this->flushFileHeader(); + + // Rewind seek position (to beginning of file) and update/flush file header + $this->rewineUpdateSeekPosition(); + + //* 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. + * + * @param $type Type of the file + * @return void + */ + public function preAllocateFile ($type) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Is it enabled? + if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { + // Not enabled + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__)); + + // Don't continue here. + return; + } // END - if + + // Message to user + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__)); + + // Calculate minimum length for one entry + $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry)); + + // Calulcate seek position + $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); + + // Now simply write a NUL there. This will pre-allocate the file. + $this->writeData($seekPosition, chr(0)); + + // Rewind seek position (to beginning of file) and update/flush file header + $this->rewineUpdateSeekPosition(); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + /** * Close a file source and set it's instance to null and the file name * to empty @@ -175,6 +333,7 @@ y * @return void * @return $seekPosition Current seek position */ public function determineSeekPosition () { + // Call pointer instance return $this->getPointerInstance()->determineSeekPosition(); } @@ -186,6 +345,7 @@ y * @return void * @return $status Status of file seek: 0 = success, -1 = failed */ public function seek ($offset, $whence = SEEK_SET) { + // Call pointer instance return $this->getPointerInstance()->seek($offset, $whence); } @@ -196,6 +356,7 @@ y * @return void * @todo Handle seekStatus */ public function size () { + // Call pointer instance return $this->getPointerInstance()->size(); } @@ -208,6 +369,7 @@ y * @return void * @throws InvalidResourceException If there is being set */ public function readFromFile () { + // Call pointer instance return $this->getPointerInstance()->readFromFile(); } @@ -218,6 +380,7 @@ y * @return void * @return $data Data read from file */ public function read ($bytes) { + // Call pointer instance return $this->getPointerInstance()->read($bytes); } @@ -232,6 +395,7 @@ y * @return void * an invalid file resource */ public function writeToFile ($dataStream) { + // Call pointer instance return $this->getPointerInstance()->writeToFile($dataStream); } @@ -241,6 +405,7 @@ y * @return void * @return $status Status of this operation */ public function rewind () { + // Call pointer instance return $this->getPointerInstance()->rewind(); } @@ -250,7 +415,8 @@ y * @return void * @return $isEndOfFileReached Whether the EOF has been reached */ public final function isEndOfFileReached () { - return $this->isEndOfFileReached(); + // Call pointer instance + return $this->getPointerInstance()->isEndOfFileReached(); } /** @@ -281,10 +447,10 @@ y * @return void $this->next(); // Get current entry - $current = $this->current(); + $current = $this->getCurrentBlock(); - // Simply output it - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE))); + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current))); } // END - while //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); @@ -304,17 +470,17 @@ y * @return void } // END - if // Make sure the block instance is set - assert($this->getBlockInstance() instanceof CalculatableBlock); + assert($this->getBlockInstance() instanceof Block); // First calculate minimum block length - $length = $this->getBlockInstance()->caluclateMinimumBlockLength(); + $length = $this->getBlockInstance()->calculateMinimumBlockLength(); // Short be more than zero! assert($length > 0); // Wait until a entry/block separator has been found $data = $this->getBackBuffer(); - while ((!$this->isEndOfFileReached()) && (!$this->getBlockInstance()->isBlockSeparatorFound($data))) { + while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) { // Then read the block $data .= $this->read($length); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data)); @@ -357,10 +523,10 @@ y * @return void */ public function valid () { // Make sure the block instance is set - assert($this->getBlockInstance() instanceof CalculatableBlock); + assert($this->getBlockInstance() instanceof Block); // First calculate minimum block length - $length = $this->getBlockInstance()->caluclateMinimumBlockLength(); + $length = $this->getBlockInstance()->calculateMinimumBlockLength(); // Short be more than zero! assert($length > 0); @@ -389,6 +555,42 @@ y * @return void // Return result return $isValid; } + + /** + * Gets current seek position ("key"). + * + * @return $key Current key in iteration + */ + public function key () { + // Call pointer instance + return $this->getPointerInstance()->determineSeekPosition(); + } + + /** + * Reads the file header + * + * @return void + */ + public function readFileHeader () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof Block); + + // Call block instance + $this->getBlockInstance()->readFileHeader(); + } + + /** + * Flushes the file header + * + * @return void + */ + public function flushFileHeader () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof Block); + + // Call block instance + $this->getBlockInstance()->flushFileHeader(); + } } // [EOF]