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