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 * Protected constructor
56 * @param $className Name of the class
59 protected function __construct (string $className) {
60 // Call parent constructor
61 parent::__construct($className);
65 * Reads the file header
68 * @todo To hard assertions here, better rewrite them to exceptions
69 * @throws UnexpectedValueException If header is not proper length
70 * @throws InvalidMagicException If a bad magic was found
72 public function readFileHeader () {
73 // First rewind to beginning as the header sits at the beginning ...
74 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
75 $this->getIteratorInstance()->rewind();
77 // Then read it (see constructor for calculation)
78 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
79 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
81 // Have all requested bytes been read?
82 if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
84 throw new UnexpectedValueException(sprintf('data(%d)=%s does not match iteratorInstance->headerSize=%d',
87 $this->getIteratorInstance()->getHeaderSize()
91 // Last character must be the separator
92 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1)))));
93 if (substr($data, -1, 1) !== chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
94 // Not valid separator
95 throw new UnexpectedValueException(sprintf('data=%s does not have separator=%s at the end.',
97 BaseBinaryFile::SEPARATOR_HEADER_ENTRIES
101 // Okay, then remove it
102 $data = substr($data, 0, -1);
104 // And update seek position
105 $this->getIteratorInstance()->updateSeekPosition();
112 * 2 => current seek position
114 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
116 // Check if the array has only 3 elements
117 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
118 if (count($header) != 3) {
119 // Header array count is not expected
120 throw new UnexpectedValueException(sprintf('data=%s has %d elements, expected 3',
124 } elseif ($header[0] != StackableFile::STACK_MAGIC) {
126 throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
129 // Check length of count and seek position
130 if (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
131 // Count length not valid
132 throw new UnexpectedValueException(sprintf('header[1](%d)=%s is not expected %d length',
135 BaseBinaryFile::LENGTH_COUNT
137 } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_POSITION) {
138 // Position length not valid
139 throw new UnexpectedValueException(sprintf('header[2](%d)=%s is not expected %d length',
142 BaseBinaryFile::LENGTH_POSITION
146 // Decode count and seek position
147 $header[1] = hex2bin($header[1]);
148 $header[2] = hex2bin($header[2]);
151 $this->getIteratorInstance()->setHeader($header);
154 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
158 * Flushes the file header
162 public function flushFileHeader () {
163 // Put all informations together
164 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
165 $header = sprintf('%s%s%s%s%s%s',
167 StackableFile::STACK_MAGIC,
169 // Separator magic<->count
170 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
172 // Total entries (will be zero) and pad it to 20 chars
173 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
175 // Separator count<->seek position
176 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
178 // Position (will be zero)
179 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
181 // Separator position<->entries
182 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
185 // Write it to disk (header is always at seek position 0)
186 $this->getIteratorInstance()->writeData(0, $header, false);
189 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
193 * Initializes this file-based stack.
195 * @param $fileInfoInstance An instance of a SplFileInfo class
196 * @param $type Type of this stack (e.g. url_source for URL sources)
198 * @throws InvalidArgumentException If a parameter is invalid
199 * @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.
201 protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) {
202 // Validate parameter
203 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
206 throw new InvalidArgumentException('Parameter "type" is empty');
209 // Get a stack file instance
210 $stackInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
212 // Get iterator instance
213 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($stackInstance));
216 $this->setIteratorInstance($iteratorInstance);
218 // Calculate header size
219 $this->getIteratorInstance()->setHeaderSize(
220 strlen(StackableFile::STACK_MAGIC) +
221 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
222 BaseBinaryFile::LENGTH_COUNT +
223 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
224 BaseBinaryFile::LENGTH_POSITION +
225 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
228 // Init counters and gaps array
229 $this->getIteratorInstance()->initCountersGapsArray();
231 // Is the file's header initialized?
232 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
233 // No, then create it (which may pre-allocate the stack)
234 $this->getIteratorInstance()->createFileHeader();
236 // And pre-allocate a bit
237 $this->getIteratorInstance()->preAllocateFile('file_stack');
240 // Load the file header
241 $this->readFileHeader();
243 // Count all entries in file
244 $this->getIteratorInstance()->analyzeFile();
247 * Get stack index instance. This can be used for faster
248 * "defragmentation" and startup.
250 $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
253 $this->setIndexInstance($indexInstance);
256 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
260 * Adds a value to given stack
262 * @param $stackerName Name of the stack
263 * @param $value Value to add to this stacker
265 * @throws FullStackerException If the stack is full
266 * @throws InvalidArgumentException If a parameter is not valid
267 * @throws InvalidArgumentException Not all variable types are wanted here
269 protected function addValue (string $stackerName, $value) {
270 // Validate parameter
271 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true)));
272 if (empty($stackerName)) {
273 // No empty stack name
274 throw new InvalidArgumentException('Parameter "stackerName" is empty');
275 } elseif ($this->isStackFull($stackerName)) {
277 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
278 } elseif (is_resource($value) || is_object($value)) {
280 throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($value)));
284 * Now add the value to the file stack which returns gap position, a
285 * hash and length of the raw data.
287 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
289 // Add the hash and gap position to the index
290 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
293 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
297 * Get last value from named stacker
299 * @param $stackerName Name of the stack
300 * @return $value Value of last added value
301 * @throws InvalidArgumentException If a parameter is not valid
302 * @throws BadMethodCallException If the stack is empty
304 protected function getLastValue (string $stackerName) {
305 // Validate parameter
306 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
307 if (empty($stackerName)) {
308 // No empty stack name
309 throw new InvalidArgumentException('Parameter "stackerName" is empty');
310 } elseif ($this->isStackEmpty($stackerName)) {
311 // Throw an exception
312 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
315 // Now get the last value
316 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
320 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
325 * Get first value from named stacker
327 * @param $stackerName Name of the stack
328 * @return $value Value of last added value
329 * @throws InvalidArgumentException If a parameter is not valid
330 * @throws BadMethodCallException If the stack is empty
332 protected function getFirstValue (string $stackerName) {
333 // Validate parameter
334 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
335 if (empty($stackerName)) {
336 // No empty stack name
337 throw new InvalidArgumentException('Parameter "stackerName" is empty');
338 } elseif ($this->isStackEmpty($stackerName)) {
339 // Throw an exception
340 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
343 // Now get the first value
344 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
348 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
353 * "Pops" last entry from stack
355 * @param $stackerName Name of the stack
356 * @return $value Value "poped" from array
357 * @throws InvalidArgumentException If a parameter is not valid
358 * @throws BadMethodCallException If the stack is empty
360 protected function popLast (string $stackerName) {
361 // Validate parameter
362 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
363 if (empty($stackerName)) {
364 // No empty stack name
365 throw new InvalidArgumentException('Parameter "stackerName" is empty');
366 } elseif ($this->isStackEmpty($stackerName)) {
367 // Throw an exception
368 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
371 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
372 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
377 * "Pops" first entry from stack
379 * @param $stackerName Name of the stack
380 * @return $value Value "shifted" from array
381 * @throws InvalidArgumentException If a parameter is not valid
382 * @throws BadMethodCallException If the named stacker is empty
384 protected function popFirst (string $stackerName) {
385 // Validate parameter
386 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
387 if (empty($stackerName)) {
388 // No empty stack name
389 throw new InvalidArgumentException('Parameter "stackerName" is empty');
390 } elseif ($this->isStackEmpty($stackerName)) {
391 // Throw an exception
392 throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
395 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
396 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
401 * Checks whether the given stack is full
403 * @param $stackerName Name of the stack
404 * @return $isFull Whether the stack is full
405 * @throws InvalidArgumentException If a parameter is not valid
407 protected function isStackFull (string $stackerName) {
408 // Validate parameter
409 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
410 if (empty($stackerName)) {
411 // No empty stack name
412 throw new InvalidArgumentException('Parameter "stackerName" is empty');
415 // @TODO Please implement this, returning false
416 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
420 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isFull=%d - EXIT!', intval($isFull)));
425 * Checks whether the given stack is empty
427 * @param $stackerName Name of the stack
428 * @return $isEmpty Whether the stack is empty
429 * @throws InvalidArgumentException If a parameter is not valid
430 * @throws BadMethodCallException If given stack is missing
432 public function isStackEmpty (string $stackerName) {
433 // Validate parameter
434 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
435 if (empty($stackerName)) {
436 // No empty stack name
437 throw new InvalidArgumentException('Parameter "stackerName" is empty');
440 // So, is the stack empty?
441 $isEmpty = (($this->getStackCount($stackerName)) == 0);
444 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isEmpty=%d - EXIT!', intval($isEmpty)));
449 * Initializes given stacker
451 * @param $stackerName Name of the stack
452 * @param $forceReInit Force re-initialization
454 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
456 public function initStack (string $stackerName, bool $forceReInit = false) {
457 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
461 * Initializes all stacks
464 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
466 public function initStacks (array $stacks, bool $forceReInit = false) {
467 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
471 * Checks whether the given stack is initialized (set in array $stackers)
473 * @param $stackerName Name of the stack
474 * @return $isInitialized Whether the stack is initialized
475 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
477 public function isStackInitialized (string $stackerName) {
478 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
482 * Determines whether the EOF has been reached
484 * @return $isEndOfFileReached Whether the EOF has been reached
485 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
487 public function isEndOfFileReached () {
488 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
492 * Getter for size of given stack (array count)
494 * @param $stackerName Name of the stack
495 * @return $count Size of stack (array count)
497 public function getStackCount (string $stackerName) {
498 // Now, simply return the found count value, this must be up-to-date then!
499 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
500 $count = $this->getIteratorInstance()->getCounter();
503 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: count=%d - EXIT!', $count));
508 * Calculates minimum length for one entry/block
510 * @return $length Minimum length for one entry/block
512 public function calculateMinimumBlockLength () {
514 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
516 // Length of entry group
517 BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
519 self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
521 strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
524 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - EXIT!', $length));
529 * Initializes counter for valid entries, arrays for damaged entries and
530 * an array for gap seek positions. If you call this method on your own,
531 * please re-analyze the file structure. So you are better to call
532 * analyzeFile() instead of this method.
535 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
537 public function initCountersGapsArray () {
538 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
542 * Getter for header size
544 * @return $totalEntries Size of file header
545 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
547 public final function getHeaderSize () {
548 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
552 * Setter for header size
554 * @param $headerSize Size of file header
556 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
558 public final function setHeaderSize (int $headerSize) {
559 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
563 * Getter for header array
565 * @return $totalEntries Size of file header
566 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
568 public final function getHeader () {
569 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
575 * @param $header Array for a file header
577 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
579 public final function setHeader (array $header) {
580 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
584 * Updates seekPosition attribute from file to avoid to much access on file.
587 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
589 public function updateSeekPosition () {
590 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
594 * Getter for total entries
596 * @return $totalEntries Total entries in this file
597 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
599 public final function getCounter () {
600 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
604 * Writes data at given position
606 * @param $seekPosition Seek position
607 * @param $data Data to be written
608 * @param $flushHeader Whether to flush the header (default: flush)
610 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
612 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
613 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader)));
614 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
618 * Writes given value to the file and returns a hash and gap position for it
620 * @param $groupId Group identifier
621 * @param $value Value to be added to the stack
622 * @return $data Hash and gap position
623 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
625 public function writeValueToFile (string $groupId, $value) {
626 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true)));
627 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
631 * Searches for next suitable gap the given length of data can fit in
632 * including padding bytes.
634 * @param $length Length of raw data
635 * @return $seekPosition Found next gap's seek position
636 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
638 public function searchNextGap (int $length) {
639 // Not supported here
640 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - CALLED!', $length));
641 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
645 * "Getter" for file size
647 * @return $fileSize Size of currently loaded file
649 public function getFileSize () {
650 // Call iterator's method
651 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!');
652 $size = $this->getIteratorInstance()->getFileSize();
655 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: size=%d - EXIT!', $size));
660 * Writes given raw data to the file and returns a gap position and length
662 * @param $groupId Group identifier
663 * @param $hash Hash from encoded value
664 * @param $encoded Encoded value to be written to the file
665 * @return $data Gap position and length of the raw data
667 public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
668 // Raw data been written to the file
669 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
670 $rawData = sprintf('%s%s%s%s%s',
672 BaseBinaryFile::SEPARATOR_GROUP_HASH,
674 BaseBinaryFile::SEPARATOR_HASH_VALUE,
678 // Search for next free gap
679 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d', $groupId, $hash, strlen($rawData)));
680 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
682 // Gap position cannot be smaller than header length + 1
683 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: gapPosition=%d', $gapPosition));
684 if ($gapPosition <= $this->getIteratorInstance()->getHeaderSize()) {
685 // Improper gap position
686 throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is not larger than headerSize=%d',
687 gettype($gapPosition),
689 $this->getIteratorInstance()->getHeaderSize()
693 // Then write the data at that gap
694 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
695 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
697 // Return gap position, hash and length of raw data
698 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
700 self::ARRAY_NAME_GAP_POSITION => $gapPosition,
701 self::ARRAY_NAME_HASH => $hash,
702 self::ARRAY_NAME_DATA_LENGTH => strlen($rawData),