3 namespace CoreFramework\Index\Stack;
5 // Import framework stuff
6 use CoreFramework\Index\BaseIndex;
7 use CoreFramework\Index\Indexable;
8 use CoreFramework\Registry\Registerable;
9 use CoreFramework\Stack\Index\IndexableStack;
12 * A FileStack index class
14 * @author Roland Haeder <webmaster@ship-simu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.ship-simu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of this Index class and prepares it for usage
47 * @param $fileName Name of the file stack to create an index file for
48 * @return $indexInstance An instance of this Index class
50 public final static function createFileStackIndex ($fileName) {
52 $indexInstance = new FileStackIndex();
55 $indexInstance->initIndex($fileName);
57 // Return the prepared instance
58 return $indexInstance;
62 * Adds given hash to an index file
64 * @param $groupId Name of stack to add hash for
65 * @param $data Hash and gap position to be added to the index
68 public function addHashToIndex ($groupId, array $data) {
70 /* 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]));
72 // Raw data been written to the file
73 $rawData = sprintf('%s%s%s%s%s%s%s',
75 self::SEPARATOR_GROUP_HASH,
76 hex2bin($data[BaseFileStack::ARRAY_INDEX_HASH]),
77 self::SEPARATOR_HASH_GAP_POSITION,
78 $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION],
79 self::SEPARATOR_GAP_LENGTH,
80 $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH]
84 /* 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)));
86 // Search for next free gap
87 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
89 // Gap position cannot be smaller than header length + 1
90 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
93 /* 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));
95 // Then write the data at that gap
96 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
99 /* 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)));
103 * Searches for next suitable gap the given length of data can fit in
104 * including padding bytes.
106 * @param $length Length of raw data
107 * @return $seekPosition Found next gap's seek position
109 public function searchNextGap ($length) {
110 $this->partialStub('length=' . $length);