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