Continued:
[core.git] / inc / classes / main / 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                 // Now add the value to the file stack (which returns a hash)
238                 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
239
240                 // Add the hash and gap position to the index
241                 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
242         }
243
244         /**
245          * Get last value from named stacker
246          *
247          * @param       $stackerName    Name of the stack
248          * @return      $value                  Value of last added value
249          * @throws      EmptyStackerException   If the stack is empty
250          */
251         protected function getLastValue ($stackerName) {
252                 // Is the stack not yet initialized or full?
253                 if ($this->isStackEmpty($stackerName)) {
254                         // Throw an exception
255                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
256                 } // END - if
257
258                 // Now get the last value
259                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
260                 $value = NULL;
261
262                 // Return it
263                 return $value;
264         }
265
266         /**
267          * Get first value from named stacker
268          *
269          * @param       $stackerName    Name of the stack
270          * @return      $value                  Value of last added value
271          * @throws      EmptyStackerException   If the stack is empty
272          */
273         protected function getFirstValue ($stackerName) {
274                 // Is the stack not yet initialized or full?
275                 if ($this->isStackEmpty($stackerName)) {
276                         // Throw an exception
277                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
278                 } // END - if
279
280                 // Now get the first value
281                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
282                 $value = NULL;
283
284                 // Return it
285                 return $value;
286         }
287
288         /**
289          * "Pops" last entry from stack
290          *
291          * @param       $stackerName    Name of the stack
292          * @return      $value                  Value "poped" from array
293          * @throws      EmptyStackerException   If the stack is empty
294          */
295         protected function popLast ($stackerName) {
296                 // Is the stack not yet initialized or full?
297                 if ($this->isStackEmpty($stackerName)) {
298                         // Throw an exception
299                         throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
300                 } // END - if
301
302                 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
303                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
304                 return NULL;
305         }
306
307         /**
308          * "Pops" first entry from stack
309          *
310          * @param       $stackerName    Name of the stack
311          * @return      $value                  Value "shifted" from array
312          * @throws      EmptyStackerException   If the named stacker is empty
313          */
314         protected function popFirst ($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, remove the last entry, we don't care about the return value here, see elseif() block above
322                 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
323                 return NULL;
324         }
325
326         /**
327          * Checks whether the given stack is full
328          *
329          * @param       $stackerName    Name of the stack
330          * @return      $isFull                 Whether the stack is full
331          */
332         protected function isStackFull ($stackerName) {
333                 // File-based stacks will only run full if the disk space is low.
334                 // @TODO Please implement this, returning FALSE
335                 $isFull = FALSE;
336
337                 // Return result
338                 return $isFull;
339         }
340
341         /**
342          * Checks whether the given stack is empty
343          *
344          * @param       $stackerName            Name of the stack
345          * @return      $isEmpty                        Whether the stack is empty
346          * @throws      NoStackerException      If given stack is missing
347          */
348         public function isStackEmpty ($stackerName) {
349                 // So, is the stack empty?
350                 $isEmpty = (($this->getStackCount($stackerName)) == 0);
351
352                 // Return result
353                 return $isEmpty;
354         }
355
356         /**
357          * Initializes given stacker
358          *
359          * @param       $stackerName    Name of the stack
360          * @param       $forceReInit    Force re-initialization
361          * @return      void
362          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
363          */
364         public function initStack ($stackerName, $forceReInit = FALSE) {
365                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
366         }
367
368         /**
369          * Initializes all stacks
370          *
371          * @return      void
372          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
373          */
374         public function initStacks (array $stacks, $forceReInit = FALSE) {
375                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
376         }
377
378         /**
379          * Checks whether the given stack is initialized (set in array $stackers)
380          *
381          * @param       $stackerName    Name of the stack
382          * @return      $isInitialized  Whether the stack is initialized
383          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
384          */
385         public function isStackInitialized ($stackerName) {
386                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
387         }
388
389         /**
390          * Determines whether the EOF has been reached
391          *
392          * @return      $isEndOfFileReached             Whether the EOF has been reached
393          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
394          */
395         public function isEndOfFileReached () {
396                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
397         }
398
399         /**
400          * Getter for file name
401          *
402          * @return      $fileName       The current file name
403          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
404          */
405         public function getFileName () {
406                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
407         }
408
409         /**
410          * Getter for size of given stack (array count)
411          *
412          * @param       $stackerName    Name of the stack
413          * @return      $count                  Size of stack (array count)
414          */
415         public function getStackCount ($stackerName) {
416                 // Now, simply return the found count value, this must be up-to-date then!
417                 return $this->getIteratorInstance()->getCounter();
418         }
419
420         /**
421          * Calculates minimum length for one entry/block
422          *
423          * @return      $length         Minimum length for one entry/block
424          */
425         public function calculateMinimumBlockLength () {
426                 // Calulcate it
427                 $length =
428                         // Length of entry group
429                         BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
430                         // Hash + value
431                         self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
432                         // Final separator
433                         strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
434
435                 // Return it
436                 return $length;
437         }
438
439         /**
440          * Initializes counter for valid entries, arrays for damaged entries and
441          * an array for gap seek positions. If you call this method on your own,
442          * please re-analyze the file structure. So you are better to call
443          * analyzeFile() instead of this method.
444          *
445          * @return      void
446          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
447          */
448         public function initCountersGapsArray () {
449                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
450         }
451
452         /**
453          * Getter for header size
454          *
455          * @return      $totalEntries   Size of file header
456          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
457          */
458         public final function getHeaderSize () {
459                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
460         }
461
462         /**
463          * Setter for header size
464          *
465          * @param       $headerSize             Size of file header
466          * @return      void
467          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
468          */
469         public final function setHeaderSize ($headerSize) {
470                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
471         }
472
473         /**
474          * Getter for header array
475          *
476          * @return      $totalEntries   Size of file header
477          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
478          */
479         public final function getHeader () {
480                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
481         }
482
483         /**
484          * Setter for header
485          *
486          * @param       $header         Array for a file header
487          * @return      void
488          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
489          */
490         public final function setHeader (array $header) {
491                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
492         }
493
494         /**
495          * Updates seekPosition attribute from file to avoid to much access on file.
496          *
497          * @return      void
498          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
499          */
500         public function updateSeekPosition () {
501                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
502         }
503
504         /**
505          * Getter for total entries
506          *
507          * @return      $totalEntries   Total entries in this file
508          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
509          */
510         public final function getCounter () {
511                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
512         }
513
514         /**
515          * Writes data at given position
516          *
517          * @param       $seekPosition   Seek position
518          * @param       $data                   Data to be written
519          * @param       $flushHeader    Whether to flush the header (default: flush)
520          * @return      void
521          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
522          */
523         public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
524                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader)));
525                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
526         }
527
528         /**
529          * Writes given value to the file and returns a hash and gap position for it
530          *
531          * @param       $groupId        Group identifier
532          * @param       $value          Value to be added to the stack
533          * @return      $data           Hash and gap position
534          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
535          */
536         public function writeValueToFile ($groupId, $value) {
537                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,value[%s]=%s', __METHOD__, __LINE__, $groupId, gettype($value), print_r($value, TRUE)));
538                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
539         }
540
541         /**
542          * Searches for next suitable gap the given length of data can fit in
543          * including padding bytes.
544          *
545          * @param       $length                 Length of raw data
546          * @return      $seekPosition   Found next gap's seek position
547          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
548          */
549         public function searchNextGap ($length) {
550                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] length=%s', __METHOD__, __LINE__, $length));
551                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
552         }
553
554         /**
555          * "Getter" for file size
556          *
557          * @return      $fileSize       Size of currently loaded file
558          */
559         public function getFileSize () {
560                 // Call iterator's method
561                 return $this->getIteratorInstance()->getFileSize();
562         }
563
564         /**
565          * Writes given raw data to the file and returns a gap position and length
566          *
567          * @param       $groupId        Group identifier
568          * @param       $hash           Hash from encoded value
569          * @param       $encoded        Encoded value to be written to the file
570          * @return      $data           Gap position and length of the raw data
571          */
572         public function writeDataToFreeGap ($groupId, $hash, $encoded) {
573                 // Debug message
574                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,encoded()=%s - CALLED!', __METHOD__, __LINE__, $groupId, $hash, strlen($encoded)));
575
576                 // Raw data been written to the file
577                 $rawData = sprintf('%s%s%s%s%s',
578                         $groupId,
579                         BaseBinaryFile::SEPARATOR_GROUP_HASH,
580                         hex2bin($hash),
581                         BaseBinaryFile::SEPARATOR_HASH_VALUE,
582                         $encoded
583                 );
584
585                 // Debug message
586                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%s', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
587
588                 // Search for next free gap
589                 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
590
591                 // Gap position cannot be smaller than header length + 1
592                 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
593
594                 // Debug message
595                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $hash, $gapPosition));
596
597                 // Then write the data at that gap
598                 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
599
600                 // Debug message
601                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%s - EXIT!', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
602
603                 // Return gap position, hash and length of raw data
604                 return array(
605                         self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
606                         self::ARRAY_INDEX_HASH         => $hash,
607                         self::ARRAY_INDEX_DATA_LENGTH  => strlen($rawData)
608                 );
609         }
610 }
611
612 // [EOF]
613 ?>