Continued with renaming-season:
[core.git] / framework / main / classes / index / file_stack / class_FileStackIndex.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Index\Stack;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7
8 /**
9  * A FileStack index class
10  *
11  * @author              Roland Haeder <webmaster@ship-simu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this Index class and prepares it for usage
43          *
44          * @param       $fileName               Name of the file stack to create an index file for
45          * @return      $indexInstance  An instance of this Index class
46          */
47         public final static function createFileStackIndex ($fileName) {
48                 // Get a new instance
49                 $indexInstance = new FileStackIndex();
50
51                 // Initialize index
52                 $indexInstance->initIndex($fileName);
53
54                 // Return the prepared instance
55                 return $indexInstance;
56         }
57
58         /**
59          * Adds given hash to an index file
60          *
61          * @param       $groupId        Name of stack to add hash for
62          * @param       $data           Hash and gap position to be added to the index
63          * @return      void
64          */
65         public function addHashToIndex ($groupId, array $data) {
66                 // Debug message
67                 /* 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]));
68
69                 // Raw data been written to the file
70                 $rawData = sprintf('%s%s%s%s%s%s%s',
71                         $groupId,
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]
78                 );
79
80                 // Debug message
81                 /* 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)));
82
83                 // Search for next free gap
84                 $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
85
86                 // Gap position cannot be smaller than header length + 1
87                 assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
88
89                 // Debug message
90                 /* 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));
91
92                 // Then write the data at that gap
93                 $this->getIteratorInstance()->writeData($gapPosition, $rawData);
94
95                 // Debug message
96                 /* 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)));
97         }
98
99         /**
100          * Searches for next suitable gap the given length of data can fit in
101          * including padding bytes.
102          *
103          * @param       $length                 Length of raw data
104          * @return      $seekPosition   Found next gap's seek position
105          */
106         public function searchNextGap ($length) {
107                 $this->partialStub('length=' . $length);
108         }
109
110 }