3 namespace Org\Mxchange\CoreFramework\Index\Stack;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Index\BaseIndex;
7 use Org\Mxchange\CoreFramework\Index\Indexable;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Stacker\Filesystem\BaseFileStacker;
10 use Org\Mxchange\CoreFramework\Stacker\Index\IndexableStack;
13 * A FileStack index class
15 * @author Roland Haeder <webmaster@ship-simu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.ship-simu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this Index class and prepares it for usage
48 * @param $fileName Name of the file stack to create an index file for
49 * @return $indexInstance An instance of this Index class
51 public final static function createFileStackIndex ($fileName) {
53 $indexInstance = new FileStackIndex();
56 $indexInstance->initIndex($fileName);
58 // Return the prepared instance
59 return $indexInstance;
63 * Adds given hash to an index file
65 * @param $groupId Name of stack to add hash for
66 * @param $data Hash and gap position to be added to the index
69 public function addHashToIndex ($groupId, array $data) {
71 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gap=%d,length=%d - CALLED!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH]));
73 // Raw data been written to the file
74 $rawData = sprintf('%s%s%s%s%s%s%s',
76 self::SEPARATOR_GROUP_HASH,
77 hex2bin($data[BaseFileStack::ARRAY_INDEX_HASH]),
78 self::SEPARATOR_HASH_GAP_POSITION,
79 $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION],
80 self::SEPARATOR_GAP_LENGTH,
81 $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH]
85 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData)));
87 // Search for next free gap
88 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
90 // Gap position cannot be smaller than header length + 1
91 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $gapPosition));
96 // Then write the data at that gap
97 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
100 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d - EXIT!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData)));
104 * Searches for next suitable gap the given length of data can fit in
105 * including padding bytes.
107 * @param $length Length of raw data
108 * @return $seekPosition Found next gap's seek position
110 public function searchNextGap ($length) {
111 $this->partialStub('length=' . $length);