]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/file/class_BaseFileStack.php
5ddc1ca0d77371d4ea997cd5da8f21702faa3820
[core.git] / framework / main / classes / stacker / file / class_BaseFileStack.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stacker\Filesystem;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Filesystem\Stack\FileStackIndexFactory;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Index\Indexable;
11 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
12 use Org\Mxchange\CoreFramework\Stacker\BaseStacker;
13
14 // Import SPL stuff
15 use \SplFileInfo;
16
17 /**
18  * A general file-based stack class
19  *
20  * @author              Roland Haeder <webmaster@ship-simu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.ship-simu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38  */
39 abstract class BaseFileStack extends BaseStacker {
40         /**
41          * Magic for this stack
42          */
43         const STACK_MAGIC = 'STACKv0.1';
44
45         /**
46          * Name of array index for gap position
47          */
48         const ARRAY_INDEX_GAP_POSITION = 'gap';
49
50         /**
51          * Name of array index for hash
52          */
53         const ARRAY_INDEX_HASH = 'hash';
54
55         /**
56          * Name of array index for length of raw data
57          */
58         const ARRAY_INDEX_DATA_LENGTH = 'length';
59
60         /**
61          * An instance of an Indexable class
62          */
63         private $indexInstance = NULL;
64
65         /**
66          * Protected constructor
67          *
68          * @param       $className      Name of the class
69          * @return      void
70          */
71         protected function __construct (string $className) {
72                 // Call parent constructor
73                 parent::__construct($className);
74         }
75
76         /**
77          * Setter for Indexable instance
78          *
79          * @param       $indexInstance  An instance of an Indexable class
80          * @return      void
81          */
82         protected final function setIndexInstance (Indexable $indexInstance) {
83                 $this->indexInstance = $indexInstance;
84         }
85
86         /**
87          * Getter for Indexable instance
88          *
89          * @return      $indexInstance  An instance of an Indexable class
90          */
91         public final function getIndexInstance () {
92                 return $this->indexInstance;
93         }
94
95         /**
96          * Reads the file header
97          *
98          * @return      void
99          * @todo        To hard assertions here, better rewrite them to exceptions
100          */
101         public function readFileHeader () {
102                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
103
104                 // First rewind to beginning as the header sits at the beginning ...
105                 $this->getIteratorInstance()->rewind();
106
107                 // Then read it (see constructor for calculation)
108                 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
109                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
110
111                 // Have all requested bytes been read?
112                 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
113                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
114
115                 // Last character must be the separator
116                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1)))));
117                 assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
118                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
119
120                 // Okay, then remove it
121                 $data = substr($data, 0, -1);
122
123                 // And update seek position
124                 $this->getIteratorInstance()->updateSeekPosition();
125
126                 /*
127                  * Now split it:
128                  *
129                  * 0 => magic
130                  * 1 => total entries
131                  * 2 => current seek position
132                  */
133                 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
134
135                 // Set header here
136                 $this->getIteratorInstance()->setHeader($header);
137
138                 // Check if the array has only 3 elements
139                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
140                 assert(count($header) == 3);
141                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
142
143                 // Check magic
144                 assert($header[0] == self::STACK_MAGIC);
145                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
146
147                 // Check length of count and seek position
148                 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
149                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
150                 assert(strlen($header[2]) == BaseBinaryFile::LENGTH_POSITION);
151                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
152
153                 // Decode count and seek position
154                 $header[1] = hex2bin($header[1]);
155                 $header[2] = hex2bin($header[2]);
156
157                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
158         }
159
160         /**
161          * Flushes the file header
162          *
163          * @return      void
164          */
165         public function flushFileHeader () {
166                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
167
168                 // Put all informations together
169                 $header = sprintf('%s%s%s%s%s%s',
170                         // Magic
171                         self::STACK_MAGIC,
172
173                         // Separator magic<->count
174                         chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
175
176                         // Total entries (will be zero) and pad it to 20 chars
177                         str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
178
179                         // Separator count<->seek position
180                         chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
181
182                         // Position (will be zero)
183                         str_pad($this->dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
184
185                         // Separator position<->entries
186                         chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
187                 );
188
189                 // Write it to disk (header is always at seek position 0)
190                 $this->getIteratorInstance()->writeData(0, $header, false);
191
192                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
193         }
194
195         /**
196          * Initializes this file-based stack.
197          *
198          * @param       $fileInfoInstance       An instance of a SplFileInfo class
199          * @param       $type           Type of this stack (e.g. url_source for URL sources)
200          * @return      void
201          * @todo        Currently the stack file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole stack file.
202          */
203         protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) {
204                 // Get a stack file instance
205                 $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
206
207                 // Get iterator instance
208                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
209
210                 // Is the instance implementing the right interface?
211                 assert($iteratorInstance instanceof SeekableWritableFileIterator);
212
213                 // Set iterator here
214                 $this->setIteratorInstance($iteratorInstance);
215
216                 // Calculate header size
217                 $this->getIteratorInstance()->setHeaderSize(
218                         strlen(self::STACK_MAGIC) +
219                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
220                         BaseBinaryFile::LENGTH_COUNT +
221                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
222                         BaseBinaryFile::LENGTH_POSITION +
223                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
224                 );
225
226                 // Init counters and gaps array
227                 $this->getIteratorInstance()->initCountersGapsArray();
228
229                 // Is the file's header initialized?
230                 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
231                         // No, then create it (which may pre-allocate the stack)
232                         $this->getIteratorInstance()->createFileHeader();
233
234                         // And pre-allocate a bit
235                         $this->getIteratorInstance()->preAllocateFile('file_stack');
236                 } // END - if
237
238                 // Load the file header
239                 $this->readFileHeader();
240
241                 // Count all entries in file
242                 $this->getIteratorInstance()->analyzeFile();
243
244                 /*
245                  * Get stack index instance. This can be used for faster
246                  * "defragmentation" and startup.
247                  */
248                 $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
249
250                 // And set it here
251                 $this->setIndexInstance($indexInstance);
252         }
253
254         /**
255          * Adds a value to given stack
256          *
257          * @param       $stackerName    Name of the stack
258          * @param       $value                  Value to add to this stacker
259          * @return      void
260          * @throws      FullStackerException    If the stack is full
261          */
262         protected function addValue (string $stackerName, $value) {
263                 // Do some tests
264                 if ($this->isStackFull($stackerName)) {
265                         // Stacker is full
266                         throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
267                 } // END - if
268
269                 // No objects/resources are allowed as their serialization takes to long
270                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true));
271                 assert(!is_object($value));
272                 assert(!is_resource($value));
273
274                 /*
275                  * Now add the value to the file stack which returns gap position, a
276                  * hash and length of the raw data.
277                  */
278                 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
279
280                 // Add the hash and gap position to the index
281                 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
282         }
283
284         /**
285          * Get last value from named stacker
286          *
287          * @param       $stackerName    Name of the stack
288          * @return      $value                  Value of last added value
289          * @throws      EmptyStackerException   If the stack is empty
290          */
291         protected function getLastValue (string $stackerName) {
292                 // Is the stack not yet initialized or full?
293                 if ($this->isStackEmpty($stackerName)) {
294                         // Throw an exception
295                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
296                 } // END - if
297
298                 // Now get the last value
299                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
300                 $value = NULL;
301
302                 // Return it
303                 return $value;
304         }
305
306         /**
307          * Get first value from named stacker
308          *
309          * @param       $stackerName    Name of the stack
310          * @return      $value                  Value of last added value
311          * @throws      EmptyStackerException   If the stack is empty
312          */
313         protected function getFirstValue (string $stackerName) {
314                 // Is the stack not yet initialized or full?
315                 if ($this->isStackEmpty($stackerName)) {
316                         // Throw an exception
317                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
318                 } // END - if
319
320                 // Now get the first value
321                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
322                 $value = NULL;
323
324                 // Return it
325                 return $value;
326         }
327
328         /**
329          * "Pops" last entry from stack
330          *
331          * @param       $stackerName    Name of the stack
332          * @return      $value                  Value "poped" from array
333          * @throws      EmptyStackerException   If the stack is empty
334          */
335         protected function popLast (string $stackerName) {
336                 // Is the stack not yet initialized or full?
337                 if ($this->isStackEmpty($stackerName)) {
338                         // Throw an exception
339                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
340                 } // END - if
341
342                 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
343                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
344                 return NULL;
345         }
346
347         /**
348          * "Pops" first entry from stack
349          *
350          * @param       $stackerName    Name of the stack
351          * @return      $value                  Value "shifted" from array
352          * @throws      EmptyStackerException   If the named stacker is empty
353          */
354         protected function popFirst (string $stackerName) {
355                 // Is the stack not yet initialized or full?
356                 if ($this->isStackEmpty($stackerName)) {
357                         // Throw an exception
358                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
359                 } // END - if
360
361                 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
362                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
363                 return NULL;
364         }
365
366         /**
367          * Checks whether the given stack is full
368          *
369          * @param       $stackerName    Name of the stack
370          * @return      $isFull                 Whether the stack is full
371          */
372         protected function isStackFull (string $stackerName) {
373                 // File-based stacks will only run full if the disk space is low.
374                 // @TODO Please implement this, returning false
375                 $isFull = false;
376
377                 // Return result
378                 return $isFull;
379         }
380
381         /**
382          * Checks whether the given stack is empty
383          *
384          * @param       $stackerName            Name of the stack
385          * @return      $isEmpty                        Whether the stack is empty
386          * @throws      NoStackerException      If given stack is missing
387          */
388         public function isStackEmpty (string $stackerName) {
389                 // So, is the stack empty?
390                 $isEmpty = (($this->getStackCount($stackerName)) == 0);
391
392                 // Return result
393                 return $isEmpty;
394         }
395
396         /**
397          * Initializes given stacker
398          *
399          * @param       $stackerName    Name of the stack
400          * @param       $forceReInit    Force re-initialization
401          * @return      void
402          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
403          */
404         public function initStack (string $stackerName, bool $forceReInit = false) {
405                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
406         }
407
408         /**
409          * Initializes all stacks
410          *
411          * @return      void
412          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
413          */
414         public function initStacks (array $stacks, bool $forceReInit = false) {
415                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
416         }
417
418         /**
419          * Checks whether the given stack is initialized (set in array $stackers)
420          *
421          * @param       $stackerName    Name of the stack
422          * @return      $isInitialized  Whether the stack is initialized
423          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
424          */
425         public function isStackInitialized (string $stackerName) {
426                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
427         }
428
429         /**
430          * Determines whether the EOF has been reached
431          *
432          * @return      $isEndOfFileReached             Whether the EOF has been reached
433          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
434          */
435         public function isEndOfFileReached () {
436                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
437         }
438
439         /**
440          * Getter for size of given stack (array count)
441          *
442          * @param       $stackerName    Name of the stack
443          * @return      $count                  Size of stack (array count)
444          */
445         public function getStackCount (string $stackerName) {
446                 // Now, simply return the found count value, this must be up-to-date then!
447                 return $this->getIteratorInstance()->getCounter();
448         }
449
450         /**
451          * Calculates minimum length for one entry/block
452          *
453          * @return      $length         Minimum length for one entry/block
454          */
455         public function calculateMinimumBlockLength () {
456                 // Calulcate it
457                 $length =
458                         // Length of entry group
459                         BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
460                         // Hash + value
461                         self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
462                         // Final separator
463                         strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
464
465                 // Return it
466                 return $length;
467         }
468
469         /**
470          * Initializes counter for valid entries, arrays for damaged entries and
471          * an array for gap seek positions. If you call this method on your own,
472          * please re-analyze the file structure. So you are better to call
473          * analyzeFile() instead of this method.
474          *
475          * @return      void
476          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
477          */
478         public function initCountersGapsArray () {
479                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
480         }
481
482         /**
483          * Getter for header size
484          *
485          * @return      $totalEntries   Size of file header
486          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
487          */
488         public final function getHeaderSize () {
489                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
490         }
491
492         /**
493          * Setter for header size
494          *
495          * @param       $headerSize             Size of file header
496          * @return      void
497          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
498          */
499         public final function setHeaderSize (int $headerSize) {
500                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
501         }
502
503         /**
504          * Getter for header array
505          *
506          * @return      $totalEntries   Size of file header
507          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
508          */
509         public final function getHeader () {
510                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
511         }
512
513         /**
514          * Setter for header
515          *
516          * @param       $header         Array for a file header
517          * @return      void
518          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
519          */
520         public final function setHeader (array $header) {
521                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
522         }
523
524         /**
525          * Updates seekPosition attribute from file to avoid to much access on file.
526          *
527          * @return      void
528          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
529          */
530         public function updateSeekPosition () {
531                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
532         }
533
534         /**
535          * Getter for total entries
536          *
537          * @return      $totalEntries   Total entries in this file
538          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
539          */
540         public final function getCounter () {
541                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
542         }
543
544         /**
545          * Writes data at given position
546          *
547          * @param       $seekPosition   Seek position
548          * @param       $data                   Data to be written
549          * @param       $flushHeader    Whether to flush the header (default: flush)
550          * @return      void
551          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
552          */
553         public function writeData ($seekPosition, $data, bool $flushHeader = true) {
554                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader)));
555                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
556         }
557
558         /**
559          * Writes given value to the file and returns a hash and gap position for it
560          *
561          * @param       $groupId        Group identifier
562          * @param       $value          Value to be added to the stack
563          * @return      $data           Hash and gap position
564          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
565          */
566         public function writeValueToFile ($groupId, $value) {
567                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true)));
568                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
569         }
570
571         /**
572          * Searches for next suitable gap the given length of data can fit in
573          * including padding bytes.
574          *
575          * @param       $length                 Length of raw data
576          * @return      $seekPosition   Found next gap's seek position
577          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
578          */
579         public function searchNextGap (int $length) {
580                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%s', $length));
581                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
582         }
583
584         /**
585          * "Getter" for file size
586          *
587          * @return      $fileSize       Size of currently loaded file
588          */
589         public function getFileSize () {
590                 // Call iterator's method
591                 return $this->getIteratorInstance()->getFileSize();
592         }
593
594         /**
595          * Writes given raw data to the file and returns a gap position and length
596          *
597          * @param       $groupId        Group identifier
598          * @param       $hash           Hash from encoded value
599          * @param       $encoded        Encoded value to be written to the file
600          * @return      $data           Gap position and length of the raw data
601          */
602         public function writeDataToFreeGap ($groupId, string $hash, $encoded) {
603                 // Raw data been written to the file
604                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
605                 $rawData = sprintf('%s%s%s%s%s',
606                         $groupId,
607                         BaseBinaryFile::SEPARATOR_GROUP_HASH,
608                         hex2bin($hash),
609                         BaseBinaryFile::SEPARATOR_HASH_VALUE,
610                         $encoded
611                 );
612
613                 // Search for next free gap
614                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d', $groupId, $hash, strlen($rawData)));
615                 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
616
617                 // Gap position cannot be smaller than header length + 1
618                 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
619
620                 // Then write the data at that gap
621                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
622                 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
623
624                 // Return gap position, hash and length of raw data
625                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
626                 return [
627                         self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
628                         self::ARRAY_INDEX_HASH         => $hash,
629                         self::ARRAY_INDEX_DATA_LENGTH  => strlen($rawData),
630                 ];
631         }
632
633 }