3 namespace Org\Mxchange\CoreFramework\Iterator\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Block;
7 use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
8 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
13 * @author Roland Haeder <webmaster@ship-simu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.ship-simu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
34 * An instance of a Block class
36 private $blockInstance = NULL;
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this class
51 * @param $pointerInstance An instance of a Block class
52 * @return $iteratorInstance An instance of a Iterator class
54 public final static function createFileIterator (Block $blockInstance) {
56 $iteratorInstance = new FileIterator();
58 // Set the instance here
59 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
60 $iteratorInstance->setBlockInstance($blockInstance);
62 // Return the prepared instance
63 return $iteratorInstance;
67 * Setter for Block instance
69 * @param $blockInstance An instance of an Block class
72 protected final function setBlockInstance (Block $blockInstance) {
73 $this->blockInstance = $blockInstance;
77 * Getter for Block instance
79 * @return $blockInstance An instance of an Block class
81 public final function getBlockInstance () {
82 return $this->blockInstance;
86 * Gets currently read data
88 * @return $current Currently read data
90 public function current () {
91 // Call block instance
92 return $this->getBlockInstance()->current();
96 * Gets current seek position ("key").
98 * @return $key Current key in iteration
100 public function key () {
102 return $this->getBlockInstance()->determineSeekPosition();
106 * Advances to next "block" of bytes
110 public function next () {
111 // Call block instance
112 $this->getBlockInstance()->next();
116 * Rewinds to the beginning of the file
118 * @return $status Status of this operation
120 public function rewind () {
121 // Call block instance
122 return $this->getBlockInstance()->rewind();
126 * Checks wether the current entry is valid (not at the end of the file).
127 * This method will return true if an emptied (nulled) entry has been found.
129 * @return $isValid Whether the next entry is valid
131 public function valid () {
132 // Call block instance
133 return $this->getBlockInstance()->valid();
137 * Seeks to given position
139 * @param $seekPosition Seek position in file
140 * @return $status Status of this operation
142 public function seek (int $seekPosition) {
143 // Call block instance
144 return $this->getBlockInstance()->seek($seekPosition);
150 * @return $size Size (in bytes) of file
152 public function size () {
153 // Call the block object
154 $size = $this->getBlockInstance()->size();
161 * Reads given amount of bytes from file.
163 * @param $bytes Amount of bytes to read
164 * @return $data Data read from file
166 public function read (int $bytes = NULL) {
167 // Call block instance
168 return $this->getBlockInstance()->read($bytes);
172 * Analyzes entries in index file. This will count all found (and valid)
173 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
174 * only gaps are found, the file is considered as "virgin" (no entries).
178 public function analyzeFile () {
179 // Just call the block instance
180 $this->getBlockInstance()->analyzeFile();
184 * Checks whether the file header is initialized
186 * @return $isInitialized Whether the file header is initialized
188 public function isFileHeaderInitialized () {
189 // Just call the block instance
190 return $this->getBlockInstance()->isFileHeaderInitialized();
194 * Creates the assigned file
198 public function createFileHeader () {
199 // Just call the block instance
200 $this->getBlockInstance()->createFileHeader();
204 * Pre-allocates file (if enabled) with some space for later faster write access.
206 * @param $type Type of the file
209 public function preAllocateFile (string $type) {
210 // Just call the block instance
211 $this->getBlockInstance()->preAllocateFile($type);
215 * Initializes counter for valid entries, arrays for damaged entries and
216 * an array for gap seek positions. If you call this method on your own,
217 * please re-analyze the file structure. So you are better to call
218 * analyzeFile() instead of this method.
222 public function initCountersGapsArray () {
223 // Call block instance
224 $this->getBlockInstance()->initCountersGapsArray();
228 * Getter for header size
230 * @return $totalEntries Size of file header
232 public final function getHeaderSize () {
233 // Call block instance
234 return $this->getBlockInstance()->getHeaderSize();
238 * Setter for header size
240 * @param $headerSize Size of file header
243 public final function setHeaderSize (int $headerSize) {
244 // Call block instance
245 $this->getBlockInstance()->setHeaderSize($headerSize);
249 * Getter for header array
251 * @return $totalEntries Size of file header
253 public final function getHeader () {
254 // Call block instance
255 return $this->getBlockInstance()->getHeader();
261 * @param $header Array for a file header
264 public final function setHeader (array $header) {
265 // Call block instance
266 $this->getBlockInstance()->setHeader($header);
270 * Updates seekPosition attribute from file to avoid to much access on file.
274 public function updateSeekPosition () {
275 // Call block instance
276 $this->getBlockInstance()->updateSeekPosition();
280 * Getter for total entries
282 * @return $totalEntries Total entries in this file
284 public final function getCounter () {
285 // Call block instance
286 return $this->getBlockInstance()->getCounter();
290 * "Getter" for file size
292 * @return $fileSize Size of currently loaded file
294 public function getFileSize () {
295 // Call block instance
296 return $this->getBlockInstance()->getFileSize();
300 * Writes data at given position
302 * @param $seekPosition Seek position
303 * @param $data Data to be written
304 * @param $flushHeader Whether to flush the header (default: flush)
307 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
308 // Call block instance
309 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
313 * Getter for seek position
315 * @return $seekPosition Current seek position (stored here in object)
317 public function getSeekPosition () {
318 // Call block instance
319 return $this->getBlockInstance()->getSeekPosition();
323 * Writes given value to the file and returns a hash and gap position for it
325 * @param $groupId Group identifier
326 * @param $value Value to be added to the stack
327 * @return $data Hash and gap position
329 public function writeValueToFile (string $groupId, $value) {
330 // Call block instance
331 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
335 * Writes given raw data to the file and returns a gap position and length
337 * @param $groupId Group identifier
338 * @param $hash Hash from encoded value
339 * @param $encoded Encoded value to be written to the file
340 * @return $data Gap position and length of the raw data
342 public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
343 // Call block instance
344 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
348 * Searches for next suitable gap the given length of data can fit in
349 * including padding bytes.
351 * @param $length Length of raw data
352 * @return $seekPosition Found next gap's seek position
354 public function searchNextGap (int $length) {
355 // Call block instance
356 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: length=%d - CALLED!', $length));
357 return $this->getBlockInstance()->searchNextGap($length);