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