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