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