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