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\Iterator\Filesystem\SeekableWritableFileIterator;
11 use Org\Mxchange\CoreFramework\Stacker\BaseStacker;
17 * A general file-based stack class
19 * @author Roland Haeder <webmaster@ship-simu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 abstract class BaseFileStack extends BaseStacker {
40 * Magic for this stack
42 const STACK_MAGIC = 'STACKv0.1';
45 * Name of array index for gap position
47 const ARRAY_INDEX_GAP_POSITION = 'gap';
50 * Name of array index for hash
52 const ARRAY_INDEX_HASH = 'hash';
55 * Name of array index for length of raw data
57 const ARRAY_INDEX_DATA_LENGTH = 'length';
60 * Protected constructor
62 * @param $className Name of the class
65 protected function __construct ($className) {
66 // Call parent constructor
67 parent::__construct($className);
71 * Reads the file header
74 * @todo To hard assertions here, better rewrite them to exceptions
76 public function readFileHeader () {
77 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
79 // First rewind to beginning as the header sits at the beginning ...
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('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize()));
86 // Have all requested bytes been read?
87 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
88 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
90 // Last character must be the separator
91 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1)))));
92 assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
93 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
95 // Okay, then remove it
96 $data = substr($data, 0, -1);
98 // And update seek position
99 $this->getIteratorInstance()->updateSeekPosition();
106 * 2 => current seek position
108 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
111 $this->getIteratorInstance()->setHeader($header);
113 // Check if the array has only 3 elements
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, true)));
115 assert(count($header) == 3);
116 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
119 assert($header[0] == self::STACK_MAGIC);
120 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
122 // Check length of count and seek position
123 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
124 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
125 assert(strlen($header[2]) == BaseBinaryFile::LENGTH_POSITION);
126 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
128 // Decode count and seek position
129 $header[1] = hex2bin($header[1]);
130 $header[2] = hex2bin($header[2]);
132 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
136 * Flushes the file header
140 public function flushFileHeader () {
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
143 // Put all informations together
144 $header = sprintf('%s%s%s%s%s%s',
148 // Separator magic<->count
149 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
151 // Total entries (will be zero) and pad it to 20 chars
152 str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
154 // Separator count<->seek position
155 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
157 // Position (will be zero)
158 str_pad($this->dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
160 // Separator position<->entries
161 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
164 // Write it to disk (header is always at seek position 0)
165 $this->getIteratorInstance()->writeData(0, $header, false);
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
171 * Initializes this file-based stack.
173 * @param $fileInfoInstance An instance of a SplFileInfo class
174 * @param $type Type of this stack (e.g. url_source for URL sources)
176 * @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.
178 protected function initFileStack (SplFileInfo $fileInfoInstance, $type) {
179 // Get a stack file instance
180 $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
182 // Get iterator instance
183 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
185 // Is the instance implementing the right interface?
186 assert($iteratorInstance instanceof SeekableWritableFileIterator);
189 $this->setIteratorInstance($iteratorInstance);
191 // Calculate header size
192 $this->getIteratorInstance()->setHeaderSize(
193 strlen(self::STACK_MAGIC) +
194 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
195 BaseBinaryFile::LENGTH_COUNT +
196 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
197 BaseBinaryFile::LENGTH_POSITION +
198 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
201 // Init counters and gaps array
202 $this->getIteratorInstance()->initCountersGapsArray();
204 // Is the file's header initialized?
205 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
206 // No, then create it (which may pre-allocate the stack)
207 $this->getIteratorInstance()->createFileHeader();
209 // And pre-allocate a bit
210 $this->getIteratorInstance()->preAllocateFile('file_stack');
213 // Load the file header
214 $this->readFileHeader();
216 // Count all entries in file
217 $this->getIteratorInstance()->analyzeFile();
220 * Get stack index instance. This can be used for faster
221 * "defragmentation" and startup.
223 $indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
226 $this->setIndexInstance($indexInstance);
230 * Adds a value to given stack
232 * @param $stackerName Name of the stack
233 * @param $value Value to add to this stacker
235 * @throws FullStackerException If the stack is full
237 protected function addValue ($stackerName, $value) {
239 if ($this->isStackFull($stackerName)) {
241 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
245 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true));
247 // No objects/resources are allowed as their serialization takes to long
248 assert(!is_object($value));
249 assert(!is_resource($value));
252 * Now add the value to the file stack which returns gap position, a
253 * hash and length of the raw data.
255 $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value);
257 // Add the hash and gap position to the index
258 $this->getIndexInstance()->addHashToIndex($stackerName, $data);
262 * Get last value from named stacker
264 * @param $stackerName Name of the stack
265 * @return $value Value of last added value
266 * @throws EmptyStackerException If the stack is empty
268 protected function getLastValue ($stackerName) {
269 // Is the stack not yet initialized or full?
270 if ($this->isStackEmpty($stackerName)) {
271 // Throw an exception
272 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
275 // Now get the last value
276 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
284 * Get first value from named stacker
286 * @param $stackerName Name of the stack
287 * @return $value Value of last added value
288 * @throws EmptyStackerException If the stack is empty
290 protected function getFirstValue ($stackerName) {
291 // Is the stack not yet initialized or full?
292 if ($this->isStackEmpty($stackerName)) {
293 // Throw an exception
294 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
297 // Now get the first value
298 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
306 * "Pops" last entry from stack
308 * @param $stackerName Name of the stack
309 * @return $value Value "poped" from array
310 * @throws EmptyStackerException If the stack is empty
312 protected function popLast ($stackerName) {
313 // Is the stack not yet initialized or full?
314 if ($this->isStackEmpty($stackerName)) {
315 // Throw an exception
316 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
319 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
320 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
325 * "Pops" first entry from stack
327 * @param $stackerName Name of the stack
328 * @return $value Value "shifted" from array
329 * @throws EmptyStackerException If the named stacker is empty
331 protected function popFirst ($stackerName) {
332 // Is the stack not yet initialized or full?
333 if ($this->isStackEmpty($stackerName)) {
334 // Throw an exception
335 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
338 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
339 /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName);
344 * Checks whether the given stack is full
346 * @param $stackerName Name of the stack
347 * @return $isFull Whether the stack is full
349 protected function isStackFull ($stackerName) {
350 // File-based stacks will only run full if the disk space is low.
351 // @TODO Please implement this, returning false
359 * Checks whether the given stack is empty
361 * @param $stackerName Name of the stack
362 * @return $isEmpty Whether the stack is empty
363 * @throws NoStackerException If given stack is missing
365 public function isStackEmpty ($stackerName) {
366 // So, is the stack empty?
367 $isEmpty = (($this->getStackCount($stackerName)) == 0);
374 * Initializes given stacker
376 * @param $stackerName Name of the stack
377 * @param $forceReInit Force re-initialization
379 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
381 public function initStack ($stackerName, $forceReInit = false) {
382 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
386 * Initializes all stacks
389 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
391 public function initStacks (array $stacks, $forceReInit = false) {
392 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
396 * Checks whether the given stack is initialized (set in array $stackers)
398 * @param $stackerName Name of the stack
399 * @return $isInitialized Whether the stack is initialized
400 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
402 public function isStackInitialized ($stackerName) {
403 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
407 * Determines whether the EOF has been reached
409 * @return $isEndOfFileReached Whether the EOF has been reached
410 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
412 public function isEndOfFileReached () {
413 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
417 * Getter for size of given stack (array count)
419 * @param $stackerName Name of the stack
420 * @return $count Size of stack (array count)
422 public function getStackCount ($stackerName) {
423 // Now, simply return the found count value, this must be up-to-date then!
424 return $this->getIteratorInstance()->getCounter();
428 * Calculates minimum length for one entry/block
430 * @return $length Minimum length for one entry/block
432 public function calculateMinimumBlockLength () {
435 // Length of entry group
436 BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) +
438 self::getHashLength() + strlen(chr(BaseBinaryFile::SEPARATOR_HASH_VALUE)) + 1 +
440 strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
447 * Initializes counter for valid entries, arrays for damaged entries and
448 * an array for gap seek positions. If you call this method on your own,
449 * please re-analyze the file structure. So you are better to call
450 * analyzeFile() instead of this method.
453 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
455 public function initCountersGapsArray () {
456 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
460 * Getter for header size
462 * @return $totalEntries Size of file header
463 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
465 public final function getHeaderSize () {
466 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
470 * Setter for header size
472 * @param $headerSize Size of file header
474 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
476 public final function setHeaderSize ($headerSize) {
477 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
481 * Getter for header array
483 * @return $totalEntries Size of file header
484 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
486 public final function getHeader () {
487 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
493 * @param $header Array for a file header
495 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
497 public final function setHeader (array $header) {
498 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
502 * Updates seekPosition attribute from file to avoid to much access on file.
505 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
507 public function updateSeekPosition () {
508 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
512 * Getter for total entries
514 * @return $totalEntries Total entries in this file
515 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
517 public final function getCounter () {
518 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
522 * Writes data at given position
524 * @param $seekPosition Seek position
525 * @param $data Data to be written
526 * @param $flushHeader Whether to flush the header (default: flush)
528 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
530 public function writeData ($seekPosition, $data, $flushHeader = true) {
531 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader)));
532 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
536 * Writes given value to the file and returns a hash and gap position for it
538 * @param $groupId Group identifier
539 * @param $value Value to be added to the stack
540 * @return $data Hash and gap position
541 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
543 public function writeValueToFile ($groupId, $value) {
544 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,value[%s]=%s', __METHOD__, __LINE__, $groupId, gettype($value), print_r($value, true)));
545 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
549 * Searches for next suitable gap the given length of data can fit in
550 * including padding bytes.
552 * @param $length Length of raw data
553 * @return $seekPosition Found next gap's seek position
554 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
556 public function searchNextGap ($length) {
557 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] length=%s', __METHOD__, __LINE__, $length));
558 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
562 * "Getter" for file size
564 * @return $fileSize Size of currently loaded file
566 public function getFileSize () {
567 // Call iterator's method
568 return $this->getIteratorInstance()->getFileSize();
572 * Writes given raw data to the file and returns a gap position and length
574 * @param $groupId Group identifier
575 * @param $hash Hash from encoded value
576 * @param $encoded Encoded value to be written to the file
577 * @return $data Gap position and length of the raw data
579 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
581 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,encoded()=%d - CALLED!', __METHOD__, __LINE__, $groupId, $hash, strlen($encoded)));
583 // Raw data been written to the file
584 $rawData = sprintf('%s%s%s%s%s',
586 BaseBinaryFile::SEPARATOR_GROUP_HASH,
588 BaseBinaryFile::SEPARATOR_HASH_VALUE,
593 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
595 // Search for next free gap
596 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
598 // Gap position cannot be smaller than header length + 1
599 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
602 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $hash, $gapPosition));
604 // Then write the data at that gap
605 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
608 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d - EXIT!', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
610 // Return gap position, hash and length of raw data
612 self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
613 self::ARRAY_INDEX_HASH => $hash,
614 self::ARRAY_INDEX_DATA_LENGTH => strlen($rawData)