3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Stack\File\FileStackIndexFactory;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
11 use Org\Mxchange\CoreFramework\Stack\BaseStacker;
12 use Org\Mxchange\CoreFramework\Stack\File\InvalidMagicException;
13 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
14 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
15 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
16 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
19 use \InvalidArgumentException;
21 use \UnexpectedValueException;
24 * A general file-based stack class
26 * @author Roland Haeder <webmaster@ship-simu.org>
28 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
29 * @license GNU GPL 3.0 or any newer version
30 * @link http://www.ship-simu.org
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 abstract class BaseFileStack extends BaseStacker implements StackableFile {
51 const EXCEPTION_BAD_MAGIC = 0xe100;
54 * Minimum block length
56 private static $minimumBlockLength = 0;
59 * Protected constructor
61 * @param $className Name of the class
64 protected function __construct (string $className) {
65 // Call parent constructor
66 parent::__construct($className);
70 * Reads the file header
73 * @todo To hard assertions here, better rewrite them to exceptions
74 * @throws UnexpectedValueException If header is not proper length
75 * @throws InvalidMagicException If a bad magic was found
77 public function readFileHeader () {
78 // First rewind to beginning as the header sits at the beginning ...
79 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
80 $this->getIteratorInstance()->rewind();
82 // Then read it (see constructor for calculation)
83 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
84 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
86 // Have all requested bytes been read?
87 if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
89 throw new UnexpectedValueException(sprintf('data(%d)=%s does not match iteratorInstance->headerSize=%d',
92 $this->getIteratorInstance()->getHeaderSize()
96 // Last character must be the separator
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1)))));
98 if (substr($data, -1, 1) !== chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
99 // Not valid separator
100 throw new UnexpectedValueException(sprintf('data=%s does not have separator=%s at the end.',
102 BaseBinaryFile::SEPARATOR_HEADER_ENTRIES
106 // Okay, then remove it
107 $data = substr($data, 0, -1);
109 // And update seek position
110 $this->getIteratorInstance()->updateSeekPosition();
117 * 2 => current seek position
119 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
121 // Check if the array has only 3 elements
122 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
123 if (count($header) != 3) {
124 // Header array count is not expected
125 throw new UnexpectedValueException(sprintf('data=%s has %d elements, expected 3',
129 } elseif ($header[0] != StackableFile::STACK_MAGIC) {
131 throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
134 // Check length of count and seek position
135 if (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
136 // Count length not valid
137 throw new UnexpectedValueException(sprintf('header[1](%d)=%s is not expected %d length',
140 BaseBinaryFile::LENGTH_COUNT
142 } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_POSITION) {
143 // Position length not valid
144 throw new UnexpectedValueException(sprintf('header[2](%d)=%s is not expected %d length',
147 BaseBinaryFile::LENGTH_POSITION
151 // Decode count and seek position
152 $header[1] = hex2bin($header[1]);
153 $header[2] = hex2bin($header[2]);
156 $this->getIteratorInstance()->setHeader($header);
159 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
163 * Flushes the file header
167 public function flushFileHeader () {
168 // Put all informations together
169 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
170 $header = sprintf('%s%s%s%s%s%s',
172 StackableFile::STACK_MAGIC,
174 // Separator magic<->count
175 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
177 // Total entries (will be zero) and pad it to 20 chars
178 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
180 // Separator count<->seek position
181 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
183 // Position (will be zero)
184 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
186 // Separator position<->entries
187 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
190 // Write it to disk (header is always at seek position 0)
191 $this->getIteratorInstance()->writeData(0, $header, false);
194 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
198 * Initializes this file-based stack.
200 * @param $fileInfoInstance An instance of a SplFileInfo class
201 * @param $type Type of this stack (e.g. url_source for URL sources)
203 * @throws InvalidArgumentException If a parameter is invalid
204 * @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.
206 protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) {
207 // Validate parameter
208 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
211 throw new InvalidArgumentException('Parameter "type" is empty');
214 // Get a stack file instance
215 $stackInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
217 // Get iterator instance
218 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($stackInstance));
221 $this->setIteratorInstance($iteratorInstance);
223 // Calculate header size
224 $this->getIteratorInstance()->setHeaderSize(
225 strlen(StackableFile::STACK_MAGIC) +
226 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
227 BaseBinaryFile::LENGTH_COUNT +
228 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
229 BaseBinaryFile::LENGTH_POSITION +
230 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
233 // Init counters and gaps array
234 $this->getIteratorInstance()->initCountersGapsArray();
236 // Is the file's header initialized?
237 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
238 // No, then create it (which may pre-allocate the stack)
239 $this->getIteratorInstance()->createFileHeader();
241 // And pre-allocate a bit
242 $this->getIteratorInstance()->preAllocateFile('file_stack');
245 // Load the file header
246 $this->readFileHeader();
248 // Count all entries in file
249 $this->getIteratorInstance()->analyzeFile();
252 * Get stack index instance. This can be used for faster
253 * "defragmentation" and startup.
255 $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
258 $this->setIndexInstance($indexInstance);
261 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
265 * Adds a value to given stack
267 * @param $stackerName Name of the stack
268 * @param $value Value to add to this stacker
270 * @throws FullStackerException If the stack is full
271 * @throws InvalidArgumentException If a parameter is not valid
272 * @throws InvalidArgumentException Not all variable types are wanted here
274 protected function addValue (string $stackerName, $value) {
275 // Validate parameter
276 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true)));
277 if (empty($stackerName)) {
278 // No empty stack name
279 throw new InvalidArgumentException('Parameter "stackerName" is empty');
280 } elseif ($this->isStackFull($stackerName)) {
282 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
283 } elseif (is_resource($value) || is_object($value)) {
285 throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($value)));
289 * Now add the value to the file stack which returns gap position, a
290 * hash and length of the raw data.
292 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
294 // Add the hash and gap position to the index
295 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
298 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
302 * Get last value from named stacker
304 * @param $stackerName Name of the stack
305 * @return $value Value of last added value
306 * @throws InvalidArgumentException If a parameter is not valid
307 * @throws BadMethodCallException If the stack is empty
309 protected function getLastValue (string $stackerName) {
310 // Validate parameter
311 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
312 if (empty($stackerName)) {
313 // No empty stack name
314 throw new InvalidArgumentException('Parameter "stackerName" is empty');
315 } elseif ($this->isStackEmpty($stackerName)) {
316 // Throw an exception
317 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
320 // Now get the last value
321 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
325 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
330 * Get first value from named stacker
332 * @param $stackerName Name of the stack
333 * @return $value Value of last added value
334 * @throws InvalidArgumentException If a parameter is not valid
335 * @throws BadMethodCallException If the stack is empty
337 protected function getFirstValue (string $stackerName) {
338 // Validate parameter
339 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
340 if (empty($stackerName)) {
341 // No empty stack name
342 throw new InvalidArgumentException('Parameter "stackerName" is empty');
343 } elseif ($this->isStackEmpty($stackerName)) {
344 // Throw an exception
345 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
348 // Now get the first value
349 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
353 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
358 * "Pops" last entry from stack
360 * @param $stackerName Name of the stack
361 * @return $value Value "poped" from array
362 * @throws InvalidArgumentException If a parameter is not valid
363 * @throws BadMethodCallException If the stack is empty
365 protected function popLast (string $stackerName) {
366 // Validate parameter
367 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
368 if (empty($stackerName)) {
369 // No empty stack name
370 throw new InvalidArgumentException('Parameter "stackerName" is empty');
371 } elseif ($this->isStackEmpty($stackerName)) {
372 // Throw an exception
373 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
376 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
377 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
382 * "Pops" first entry from stack
384 * @param $stackerName Name of the stack
385 * @return $value Value "shifted" from array
386 * @throws InvalidArgumentException If a parameter is not valid
387 * @throws BadMethodCallException If the named stacker is empty
389 protected function popFirst (string $stackerName) {
390 // Validate parameter
391 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
392 if (empty($stackerName)) {
393 // No empty stack name
394 throw new InvalidArgumentException('Parameter "stackerName" is empty');
395 } elseif ($this->isStackEmpty($stackerName)) {
396 // Throw an exception
397 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
400 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
401 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
406 * Checks whether the given stack is full
408 * @param $stackerName Name of the stack
409 * @return $isFull Whether the stack is full
410 * @throws InvalidArgumentException If a parameter is not valid
412 protected function isStackFull (string $stackerName) {
413 // Validate parameter
414 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
415 if (empty($stackerName)) {
416 // No empty stack name
417 throw new InvalidArgumentException('Parameter "stackerName" is empty');
420 // @TODO Please implement this, returning false
421 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
425 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isFull=%d - EXIT!', intval($isFull)));
430 * Checks whether the given stack is empty
432 * @param $stackerName Name of the stack
433 * @return $isEmpty Whether the stack is empty
434 * @throws InvalidArgumentException If a parameter is not valid
435 * @throws BadMethodCallException If given stack is missing
437 public function isStackEmpty (string $stackerName) {
438 // Validate parameter
439 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
440 if (empty($stackerName)) {
441 // No empty stack name
442 throw new InvalidArgumentException('Parameter "stackerName" is empty');
445 // So, is the stack empty?
446 $isEmpty = (($this->getStackCount($stackerName)) == 0);
449 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isEmpty=%d - EXIT!', intval($isEmpty)));
454 * Initializes given stacker
456 * @param $stackerName Name of the stack
457 * @param $forceReInit Force re-initialization
459 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
461 public function initStack (string $stackerName, bool $forceReInit = false) {
462 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
466 * Initializes all stacks
469 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
471 public function initStacks (array $stacks, bool $forceReInit = false) {
472 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
476 * Checks whether the given stack is initialized (set in array $stackers)
478 * @param $stackerName Name of the stack
479 * @return $isInitialized Whether the stack is initialized
480 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
482 public function isStackInitialized (string $stackerName) {
483 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
487 * Determines whether the EOF has been reached
489 * @return $isEndOfFileReached Whether the EOF has been reached
490 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
492 public function isEndOfFileReached () {
493 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
497 * Getter for size of given stack (array count)
499 * @param $stackerName Name of the stack
500 * @return $count Size of stack (array count)
502 public function getStackCount (string $stackerName) {
503 // Now, simply return the found count value, this must be up-to-date then!
504 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
505 $count = $this->getIteratorInstance()->getCounter();
508 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: count=%d - EXIT!', $count));
513 * Calculates minimum length for one entry/block
515 * @return $length Minimum length for one entry/block
517 public function calculateMinimumBlockLength () {
518 // Is the value "cached"?
519 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
520 if (self::$minimumBlockLength == 0) {
522 self::$minimumBlockLength =
523 // Length of entry group
524 BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
526 self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
528 strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
532 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength));
533 return self::$minimumBlockLength;
537 * Initializes counter for valid entries, arrays for damaged entries and
538 * an array for gap seek positions. If you call this method on your own,
539 * please re-analyze the file structure. So you are better to call
540 * analyzeFile() instead of this method.
543 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
545 public function initCountersGapsArray () {
546 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
550 * Getter for header size
552 * @return $totalEntries Size of file header
553 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
555 public final function getHeaderSize () {
556 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
560 * Setter for header size
562 * @param $headerSize Size of file header
564 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
566 public final function setHeaderSize (int $headerSize) {
567 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
571 * Getter for header array
573 * @return $totalEntries Size of file header
574 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
576 public final function getHeader () {
577 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
583 * @param $header Array for a file header
585 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
587 public final function setHeader (array $header) {
588 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
592 * Updates seekPosition attribute from file to avoid to much access on file.
595 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
597 public function updateSeekPosition () {
598 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
602 * Getter for total entries
604 * @return $totalEntries Total entries in this file
605 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
607 public final function getCounter () {
608 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
612 * Writes data at given position
614 * @param $seekPosition Seek position
615 * @param $data Data to be written
616 * @param $flushHeader Whether to flush the header (default: flush)
618 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
620 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
621 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader)));
622 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
626 * Writes given value to the file and returns a hash and gap position for it
628 * @param $groupId Group identifier
629 * @param $value Value to be added to the stack
630 * @return $data Hash and gap position
631 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
633 public function writeValueToFile (string $groupId, $value) {
634 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true)));
635 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
639 * Searches for next suitable gap the given length of data can fit in
640 * including padding bytes.
642 * @param $length Length of raw data
643 * @return $seekPosition Found next gap's seek position
644 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
646 public function searchNextGap (int $length) {
647 // Not supported here
648 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - CALLED!', $length));
649 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
653 * "Getter" for file size
655 * @return $fileSize Size of currently loaded file
657 public function getFileSize () {
658 // Call iterator's method
659 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
660 $size = $this->getIteratorInstance()->getFileSize();
663 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: size=%d - EXIT!', $size));
668 * Writes given raw data to the file and returns a gap position and length
670 * @param $groupId Group identifier
671 * @param $hash Hash from encoded value
672 * @param $encoded Encoded value to be written to the file
673 * @return $data Gap position and length of the raw data
675 public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
676 // Raw data been written to the file
677 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
678 $rawData = sprintf('%s%s%s%s%s',
680 BaseBinaryFile::SEPARATOR_GROUP_HASH,
682 BaseBinaryFile::SEPARATOR_HASH_VALUE,
686 // Search for next free gap
687 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d', $groupId, $hash, strlen($rawData)));
688 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
690 // Gap position cannot be smaller than header length + 1
691 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: gapPosition=%d', $gapPosition));
692 if ($gapPosition <= $this->getIteratorInstance()->getHeaderSize()) {
693 // Improper gap position
694 throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is not larger than headerSize=%d',
695 gettype($gapPosition),
697 $this->getIteratorInstance()->getHeaderSize()
701 // Then write the data at that gap
702 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
703 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
705 // Return gap position, hash and length of raw data
706 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
708 self::ARRAY_NAME_GAP_POSITION => $gapPosition,
709 self::ARRAY_NAME_HASH => $hash,
710 self::ARRAY_NAME_DATA_LENGTH => strlen($rawData),