3 namespace Org\Mxchange\CoreFramework\Stacker\Filesystem;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Filesystem\Stack\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\Index\Indexable;
11 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
12 use Org\Mxchange\CoreFramework\Stacker\BaseStacker;
18 * A general file-based stack class
20 * @author Roland Haeder <webmaster@ship-simu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.ship-simu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseFileStack extends BaseStacker {
41 * Magic for this stack
43 const STACK_MAGIC = 'STACKv0.1';
46 * Name of array index for gap position
48 const ARRAY_INDEX_GAP_POSITION = 'gap';
51 * Name of array index for hash
53 const ARRAY_INDEX_HASH = 'hash';
56 * Name of array index for length of raw data
58 const ARRAY_INDEX_DATA_LENGTH = 'length';
61 * An instance of an Indexable class
63 private $indexInstance = NULL;
66 * Protected constructor
68 * @param $className Name of the class
71 protected function __construct (string $className) {
72 // Call parent constructor
73 parent::__construct($className);
77 * Setter for Indexable instance
79 * @param $indexInstance An instance of an Indexable class
82 protected final function setIndexInstance (Indexable $indexInstance) {
83 $this->indexInstance = $indexInstance;
87 * Getter for Indexable instance
89 * @return $indexInstance An instance of an Indexable class
91 public final function getIndexInstance () {
92 return $this->indexInstance;
96 * Reads the file header
99 * @todo To hard assertions here, better rewrite them to exceptions
101 public function readFileHeader () {
102 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
104 // First rewind to beginning as the header sits at the beginning ...
105 $this->getIteratorInstance()->rewind();
107 // Then read it (see constructor for calculation)
108 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
109 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
111 // Have all requested bytes been read?
112 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
113 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
115 // Last character must be the separator
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1)))));
117 assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
118 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
120 // Okay, then remove it
121 $data = substr($data, 0, -1);
123 // And update seek position
124 $this->getIteratorInstance()->updateSeekPosition();
131 * 2 => current seek position
133 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
136 $this->getIteratorInstance()->setHeader($header);
138 // Check if the array has only 3 elements
139 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
140 assert(count($header) == 3);
141 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
144 assert($header[0] == self::STACK_MAGIC);
145 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
147 // Check length of count and seek position
148 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
150 assert(strlen($header[2]) == BaseBinaryFile::LENGTH_POSITION);
151 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
153 // Decode count and seek position
154 $header[1] = hex2bin($header[1]);
155 $header[2] = hex2bin($header[2]);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
161 * Flushes the file header
165 public function flushFileHeader () {
166 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
168 // Put all informations together
169 $header = sprintf('%s%s%s%s%s%s',
173 // Separator magic<->count
174 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
176 // Total entries (will be zero) and pad it to 20 chars
177 str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
179 // Separator count<->seek position
180 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
182 // Position (will be zero)
183 str_pad($this->dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
185 // Separator position<->entries
186 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
189 // Write it to disk (header is always at seek position 0)
190 $this->getIteratorInstance()->writeData(0, $header, false);
192 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
196 * Initializes this file-based stack.
198 * @param $fileInfoInstance An instance of a SplFileInfo class
199 * @param $type Type of this stack (e.g. url_source for URL sources)
201 * @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.
203 protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) {
204 // Get a stack file instance
205 $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
207 // Get iterator instance
208 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
210 // Is the instance implementing the right interface?
211 assert($iteratorInstance instanceof SeekableWritableFileIterator);
214 $this->setIteratorInstance($iteratorInstance);
216 // Calculate header size
217 $this->getIteratorInstance()->setHeaderSize(
218 strlen(self::STACK_MAGIC) +
219 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
220 BaseBinaryFile::LENGTH_COUNT +
221 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
222 BaseBinaryFile::LENGTH_POSITION +
223 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
226 // Init counters and gaps array
227 $this->getIteratorInstance()->initCountersGapsArray();
229 // Is the file's header initialized?
230 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
231 // No, then create it (which may pre-allocate the stack)
232 $this->getIteratorInstance()->createFileHeader();
234 // And pre-allocate a bit
235 $this->getIteratorInstance()->preAllocateFile('file_stack');
238 // Load the file header
239 $this->readFileHeader();
241 // Count all entries in file
242 $this->getIteratorInstance()->analyzeFile();
245 * Get stack index instance. This can be used for faster
246 * "defragmentation" and startup.
248 $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
251 $this->setIndexInstance($indexInstance);
255 * Adds a value to given stack
257 * @param $stackerName Name of the stack
258 * @param $value Value to add to this stacker
260 * @throws FullStackerException If the stack is full
262 protected function addValue (string $stackerName, $value) {
264 if ($this->isStackFull($stackerName)) {
266 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
269 // No objects/resources are allowed as their serialization takes to long
270 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true));
271 assert(!is_object($value));
272 assert(!is_resource($value));
275 * Now add the value to the file stack which returns gap position, a
276 * hash and length of the raw data.
278 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
280 // Add the hash and gap position to the index
281 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
285 * Get last value from named stacker
287 * @param $stackerName Name of the stack
288 * @return $value Value of last added value
289 * @throws EmptyStackerException If the stack is empty
291 protected function getLastValue (string $stackerName) {
292 // Is the stack not yet initialized or full?
293 if ($this->isStackEmpty($stackerName)) {
294 // Throw an exception
295 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
298 // Now get the last value
299 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
307 * Get first value from named stacker
309 * @param $stackerName Name of the stack
310 * @return $value Value of last added value
311 * @throws EmptyStackerException If the stack is empty
313 protected function getFirstValue (string $stackerName) {
314 // Is the stack not yet initialized or full?
315 if ($this->isStackEmpty($stackerName)) {
316 // Throw an exception
317 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
320 // Now get the first value
321 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
329 * "Pops" last entry from stack
331 * @param $stackerName Name of the stack
332 * @return $value Value "poped" from array
333 * @throws EmptyStackerException If the stack is empty
335 protected function popLast (string $stackerName) {
336 // Is the stack not yet initialized or full?
337 if ($this->isStackEmpty($stackerName)) {
338 // Throw an exception
339 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
342 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
343 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
348 * "Pops" first entry from stack
350 * @param $stackerName Name of the stack
351 * @return $value Value "shifted" from array
352 * @throws EmptyStackerException If the named stacker is empty
354 protected function popFirst (string $stackerName) {
355 // Is the stack not yet initialized or full?
356 if ($this->isStackEmpty($stackerName)) {
357 // Throw an exception
358 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
361 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
362 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
367 * Checks whether the given stack is full
369 * @param $stackerName Name of the stack
370 * @return $isFull Whether the stack is full
372 protected function isStackFull (string $stackerName) {
373 // File-based stacks will only run full if the disk space is low.
374 // @TODO Please implement this, returning false
382 * Checks whether the given stack is empty
384 * @param $stackerName Name of the stack
385 * @return $isEmpty Whether the stack is empty
386 * @throws NoStackerException If given stack is missing
388 public function isStackEmpty (string $stackerName) {
389 // So, is the stack empty?
390 $isEmpty = (($this->getStackCount($stackerName)) == 0);
397 * Initializes given stacker
399 * @param $stackerName Name of the stack
400 * @param $forceReInit Force re-initialization
402 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
404 public function initStack (string $stackerName, bool $forceReInit = false) {
405 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
409 * Initializes all stacks
412 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
414 public function initStacks (array $stacks, bool $forceReInit = false) {
415 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
419 * Checks whether the given stack is initialized (set in array $stackers)
421 * @param $stackerName Name of the stack
422 * @return $isInitialized Whether the stack is initialized
423 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
425 public function isStackInitialized (string $stackerName) {
426 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
430 * Determines whether the EOF has been reached
432 * @return $isEndOfFileReached Whether the EOF has been reached
433 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
435 public function isEndOfFileReached () {
436 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
440 * Getter for size of given stack (array count)
442 * @param $stackerName Name of the stack
443 * @return $count Size of stack (array count)
445 public function getStackCount (string $stackerName) {
446 // Now, simply return the found count value, this must be up-to-date then!
447 return $this->getIteratorInstance()->getCounter();
451 * Calculates minimum length for one entry/block
453 * @return $length Minimum length for one entry/block
455 public function calculateMinimumBlockLength () {
458 // Length of entry group
459 BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
461 self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
463 strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
470 * Initializes counter for valid entries, arrays for damaged entries and
471 * an array for gap seek positions. If you call this method on your own,
472 * please re-analyze the file structure. So you are better to call
473 * analyzeFile() instead of this method.
476 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
478 public function initCountersGapsArray () {
479 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
483 * Getter for header size
485 * @return $totalEntries Size of file header
486 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
488 public final function getHeaderSize () {
489 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
493 * Setter for header size
495 * @param $headerSize Size of file header
497 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
499 public final function setHeaderSize (int $headerSize) {
500 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
504 * Getter for header array
506 * @return $totalEntries Size of file header
507 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
509 public final function getHeader () {
510 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
516 * @param $header Array for a file header
518 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
520 public final function setHeader (array $header) {
521 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
525 * Updates seekPosition attribute from file to avoid to much access on file.
528 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
530 public function updateSeekPosition () {
531 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
535 * Getter for total entries
537 * @return $totalEntries Total entries in this file
538 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
540 public final function getCounter () {
541 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
545 * Writes data at given position
547 * @param $seekPosition Seek position
548 * @param $data Data to be written
549 * @param $flushHeader Whether to flush the header (default: flush)
551 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
553 public function writeData ($seekPosition, $data, bool $flushHeader = true) {
554 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader)));
555 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
559 * Writes given value to the file and returns a hash and gap position for it
561 * @param $groupId Group identifier
562 * @param $value Value to be added to the stack
563 * @return $data Hash and gap position
564 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
566 public function writeValueToFile ($groupId, $value) {
567 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true)));
568 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
572 * Searches for next suitable gap the given length of data can fit in
573 * including padding bytes.
575 * @param $length Length of raw data
576 * @return $seekPosition Found next gap's seek position
577 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
579 public function searchNextGap (int $length) {
580 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%s', $length));
581 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
585 * "Getter" for file size
587 * @return $fileSize Size of currently loaded file
589 public function getFileSize () {
590 // Call iterator's method
591 return $this->getIteratorInstance()->getFileSize();
595 * Writes given raw data to the file and returns a gap position and length
597 * @param $groupId Group identifier
598 * @param $hash Hash from encoded value
599 * @param $encoded Encoded value to be written to the file
600 * @return $data Gap position and length of the raw data
602 public function writeDataToFreeGap ($groupId, string $hash, $encoded) {
603 // Raw data been written to the file
604 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
605 $rawData = sprintf('%s%s%s%s%s',
607 BaseBinaryFile::SEPARATOR_GROUP_HASH,
609 BaseBinaryFile::SEPARATOR_HASH_VALUE,
613 // Search for next free gap
614 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d', $groupId, $hash, strlen($rawData)));
615 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
617 // Gap position cannot be smaller than header length + 1
618 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
620 // Then write the data at that gap
621 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
622 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
624 // Return gap position, hash and length of raw data
625 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
627 self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
628 self::ARRAY_INDEX_HASH => $hash,
629 self::ARRAY_INDEX_DATA_LENGTH => strlen($rawData),