]> git.mxchange.org Git - core.git/blob - framework/main/classes/iterator/file/class_FileIterator.php
360996c23476f309587c5573d73c1eb342663952
[core.git] / framework / main / classes / iterator / file / class_FileIterator.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Iterator\File;
4
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;
9
10 /**
11  * A file iterator
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
33         /**
34          * An instance of a Block class
35          */
36         private $blockInstance = NULL;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this class
50          *
51          * @param       $pointerInstance        An instance of a Block class
52          * @return      $iteratorInstance       An instance of a Iterator class
53          */
54         public final static function createFileIterator (Block $blockInstance) {
55                 // Get new instance
56                 $iteratorInstance = new FileIterator();
57
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);
61
62                 // Return the prepared instance
63                 return $iteratorInstance;
64         }
65
66         /**
67          * Setter for Block instance
68          *
69          * @param       $blockInstance  An instance of an Block class
70          * @return      void
71          */
72         protected final function setBlockInstance (Block $blockInstance) {
73                 $this->blockInstance = $blockInstance;
74         }
75
76         /**
77          * Getter for Block instance
78          *
79          * @return      $blockInstance  An instance of an Block class
80          */
81         public final function getBlockInstance () {
82                 return $this->blockInstance;
83         }
84
85         /**
86          * Gets currently read data
87          *
88          * @return      $current        Currently read data
89          */
90         public function current () {
91                 // Call block instance
92                 return $this->getBlockInstance()->current();
93         }
94
95         /**
96          * Gets current seek position ("key").
97          *
98          * @return      $key    Current key in iteration
99          */
100         public function key () {
101                 // Return it
102                 return $this->getBlockInstance()->determineSeekPosition();
103         }
104
105         /**
106          * Advances to next "block" of bytes
107          *
108          * @return      void
109          */
110         public function next () {
111                 // Call block instance
112                 $this->getBlockInstance()->next();
113         }
114
115         /**
116          * Rewinds to the beginning of the file
117          *
118          * @return      $status         Status of this operation
119          */
120         public function rewind () {
121                 // Call block instance
122                 return $this->getBlockInstance()->rewind();
123         }
124
125         /**
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.
128          *
129          * @return      $isValid        Whether the next entry is valid
130          */
131         public function valid () {
132                 // Call block instance
133                 return $this->getBlockInstance()->valid();
134         }
135
136         /**
137          * Seeks to given position
138          *
139          * @param       $seekPosition   Seek position in file
140          * @return      $status                 Status of this operation
141          */
142         public function seek (int $seekPosition) {
143                 // Call block instance
144                 return $this->getBlockInstance()->seek($seekPosition);
145         }
146
147         /**
148          * Size of file stack
149          *
150          * @return      $size   Size (in bytes) of file
151          */
152         public function size () {
153                 // Call the block object
154                 $size = $this->getBlockInstance()->size();
155
156                 // Return result
157                 return $size;
158         }
159
160         /**
161          * Reads given amount of bytes from file.
162          *
163          * @param       $bytes  Amount of bytes to read
164          * @return      $data   Data read from file
165          */
166         public function read (int $bytes = NULL) {
167                 // Call block instance
168                 return $this->getBlockInstance()->read($bytes);
169         }
170
171         /**
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).
175          *
176          * @return      void
177          */
178         public function analyzeFile () {
179                 // Just call the block instance
180                 $this->getBlockInstance()->analyzeFile();
181         }
182
183         /**
184          * Checks whether the file header is initialized
185          *
186          * @return      $isInitialized  Whether the file header is initialized
187          */
188         public function isFileHeaderInitialized () {
189                 // Just call the block instance
190                 return $this->getBlockInstance()->isFileHeaderInitialized();
191         }
192
193         /**
194          * Creates the assigned file
195          *
196          * @return      void
197          */
198         public function createFileHeader () {
199                 // Just call the block instance
200                 $this->getBlockInstance()->createFileHeader();
201         }
202
203         /**
204          * Pre-allocates file (if enabled) with some space for later faster write access.
205          *
206          * @param       $type   Type of the file
207          * @return      void
208          */
209         public function preAllocateFile (string $type) {
210                 // Just call the block instance
211                 $this->getBlockInstance()->preAllocateFile($type);
212         }
213
214         /**
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.
219          *
220          * @return      void
221          */
222         public function initCountersGapsArray () {
223                 // Call block instance
224                 $this->getBlockInstance()->initCountersGapsArray();
225         }
226
227         /**
228          * Getter for header size
229          *
230          * @return      $totalEntries   Size of file header
231          */
232         public final function getHeaderSize () {
233                 // Call block instance
234                 return $this->getBlockInstance()->getHeaderSize();
235         }
236
237         /**
238          * Setter for header size
239          *
240          * @param       $headerSize             Size of file header
241          * @return      void
242          */
243         public final function setHeaderSize (int $headerSize) {
244                 // Call block instance
245                 $this->getBlockInstance()->setHeaderSize($headerSize);
246         }
247
248         /**
249          * Getter for header array
250          *
251          * @return      $totalEntries   Size of file header
252          */
253         public final function getHeader () {
254                 // Call block instance
255                 return $this->getBlockInstance()->getHeader();
256         }
257
258         /**
259          * Setter for header
260          *
261          * @param       $header         Array for a file header
262          * @return      void
263          */
264         public final function setHeader (array $header) {
265                 // Call block instance
266                 $this->getBlockInstance()->setHeader($header);
267         }
268
269         /**
270          * Updates seekPosition attribute from file to avoid to much access on file.
271          *
272          * @return      void
273          */
274         public function updateSeekPosition () {
275                 // Call block instance
276                 $this->getBlockInstance()->updateSeekPosition();
277         }
278
279         /**
280          * Getter for total entries
281          *
282          * @return      $totalEntries   Total entries in this file
283          */
284         public final function getCounter () {
285                 // Call block instance
286                 return $this->getBlockInstance()->getCounter();
287         }
288
289         /**
290          * "Getter" for file size
291          *
292          * @return      $fileSize       Size of currently loaded file
293          */
294         public function getFileSize () {
295                 // Call block instance
296                 return $this->getBlockInstance()->getFileSize();
297         }
298
299         /**
300          * Writes data at given position
301          *
302          * @param       $seekPosition   Seek position
303          * @param       $data                   Data to be written
304          * @param       $flushHeader    Whether to flush the header (default: flush)
305          * @return      void
306          */
307         public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
308                 // Call block instance
309                 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
310         }
311
312         /**
313          * Getter for seek position
314          *
315          * @return      $seekPosition   Current seek position (stored here in object)
316          */
317         public function getSeekPosition () {
318                 // Call block instance
319                 return $this->getBlockInstance()->getSeekPosition();
320         }
321
322         /**
323          * Writes given value to the file and returns a hash and gap position for it
324          *
325          * @param       $groupId        Group identifier
326          * @param       $value          Value to be added to the stack
327          * @return      $data           Hash and gap position
328          */
329         public function writeValueToFile (string $groupId, $value) {
330                 // Call block instance
331                 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
332         }
333
334         /**
335          * Writes given raw data to the file and returns a gap position and length
336          *
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
341          */
342         public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
343                 // Call block instance
344                 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
345         }
346
347         /**
348          * Searches for next suitable gap the given length of data can fit in
349          * including padding bytes.
350          *
351          * @param       $length                 Length of raw data
352          * @return      $seekPosition   Found next gap's seek position
353          */
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);
358         }
359
360 }