]> git.mxchange.org Git - core.git/blob - framework/main/classes/index/file/class_BaseFileIndex.php
Continued:
[core.git] / framework / main / classes / index / file / class_BaseFileIndex.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Index\File;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
8 use Org\Mxchange\CoreFramework\Index\BaseIndex;
9 use Org\Mxchange\CoreFramework\Index\Indexable;
10 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
11
12 // Import SPL stuff
13 use \OutOfBoundsException;
14 use \SplFileInfo;
15 use \UnexpectedValueException;
16
17 /**
18  * A general file-based index class
19  *
20  * @author              Roland Haeder <webmaster@ship-simu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.ship-simu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program. If not, see <http://www.gnu.org/licenses/>.
38  */
39 abstract class BaseFileIndex extends BaseIndex implements FileIndexer {
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Reads the file header
52          *
53          * @return      void
54          * @throws      UnexpectedValueException        If header length or count of elements is invalid
55          */
56         public function readIndexHeader () {
57                 // First rewind to beginning as the header sits at the beginning ...
58                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: CALLED!');
59                 $this->getIteratorInstance()->rewind();
60
61                 // Then read it (see constructor for calculation)
62                 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
63
64                 // Have all requested bytes been read?
65                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
66                 if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
67                         // Invalid header length
68                         throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d',
69                                 strlen($data),
70                                 $data,
71                                 $this->getIteratorInstance()->getHeaderSize()
72                         ));
73                 } elseif (empty(trim($data, chr(0)))) {
74                         // Empty file header
75                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: File header is empty - EXIT!');
76                         return;
77                 } elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
78                         // Bad last character
79                         throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"',
80                                 $data,
81                                 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
82                         ));
83                 }
84
85                 // Okay, then remove it
86                 $data = substr($data, 0, -1);
87
88                 // And update seek position
89                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->updateSeekPosition() ...');
90                 $this->getIteratorInstance()->updateSeekPosition();
91
92                 /*
93                  * Now split it:
94                  *
95                  * 0 => magic
96                  * 1 => total entries
97                  */
98                 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
99
100                 // Check if the array has only 3 elements
101                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header()=%d', count($header)));
102                 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header(%d)=%s', count($header), print_r($header, true)));
103                 if (count($header) != 2) {
104                         // Bad header
105                         throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header)));
106                 } elseif ($header[0] !== Indexable::INDEX_MAGIC) {
107                         // Magic must be in first element
108                         throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], Indexable::INDEX_MAGIC));
109                 } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
110                         // Length of total entries not matching
111                         throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BaseBinaryFile::LENGTH_COUNT));
112                 }
113
114                 // Decode count
115                 $header[1] = hex2bin($header[1]);
116
117                 // Set it here
118                 $this->getIteratorInstance()->setHeader($header);
119
120                 // Trace message
121                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: EXIT!');
122         }
123
124         /**
125          * Flushes the file header
126          *
127          * @return      void
128          */
129         public function flushFileHeader () {
130                 // Put all informations together
131                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: CALLED!');
132                 $header = sprintf('%s%s%s%s',
133                         // Magic
134                         Indexable::INDEX_MAGIC,
135
136                         // Separator header data
137                         chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
138
139                         // Total entries
140                         str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
141
142                         // Separator header<->entries
143                         chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
144                 );
145
146                 // Write it to disk (header is always at seek position 0)
147                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header));
148                 $this->getIteratorInstance()->writeAtPosition(0, $header);
149
150                 // Trace message
151                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: EXIT!');
152         }
153
154         /**
155          * Initializes this index
156          *
157          * @param       $fileInfoInstance       An instance of a SplFileInfo class
158          * @return      void
159          * @todo        Currently the index file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole index file.
160          */
161         protected function initIndex (SplFileInfo $fileInfoInstance) {
162                 // Get a file i/o pointer instance for index file
163                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance));
164                 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this));
165
166                 // Get iterator instance
167                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', [$fileInstance]);
168
169                 // Set iterator here
170                 $this->setIteratorInstance($iteratorInstance);
171
172                 // Calculate header size
173                 $headerSize = (
174                         strlen(Indexable::INDEX_MAGIC) +
175                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
176                         BaseBinaryFile::LENGTH_COUNT +
177                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
178                 );
179
180                 // Set it
181                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Setting headerSize=%d ...', $headerSize));
182                 $this->getIteratorInstance()->setHeaderSize($headerSize);
183
184                 // Init counters and gaps array
185                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->initCountersGapsArray() ...');
186                 $this->getIteratorInstance()->initCountersGapsArray();
187
188                 // Default is not created
189                 $created = false;
190
191                 // Is the file's header initialized?
192                 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
193                         // First pre-allocate a bit
194                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->preAllocateFile(index) ...');
195                         $this->getIteratorInstance()->preAllocateFile('index');
196
197                         // Then write file header
198                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->createFileHeader() ...');
199                         $this->getIteratorInstance()->createFileHeader();
200
201                         // Mark as freshly created
202                         $created = true;
203                 }
204
205                 // Load the file header
206                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->readIndexHeader() ...');
207                 $this->readIndexHeader();
208
209                 // Freshly created?
210                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: created=%d', intval($created)));
211                 if (!$created) {
212                         // Analyze file structure
213                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->analyzeFileStructure() ...');
214                         $this->getIteratorInstance()->analyzeFileStructure();
215                 }
216
217                 // Trace message
218                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: EXIT!');
219         }
220
221         /**
222          * Calculates minimum length for one entry/block
223          *
224          * @return      $length         Minimum length for one entry/block
225          */
226         public function calculateMinimumBlockLength () {
227                 // Is it "cached"?
228                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: CALLED!');
229                 if (self::$minimumBlockLength == 0) {
230                         // Calulcate it
231                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calculating ...');
232                         self::$minimumBlockLength = (
233                                 // Type
234                                 BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) +
235                                 // Position
236                                 BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES))
237                         );
238                 }
239
240                 // Return it
241                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength));
242                 return self::$minimumBlockLength;
243         }
244
245         /**
246          * "Getter" for file size
247          *
248          * @return      $fileSize       Size of currently loaded file
249          */
250         public function getFileSize () {
251                 // Call iterator's method
252                 return $this->getIteratorInstance()->getFileSize();
253         }
254
255         /**
256          * Searches for next suitable gap the given length of data can fit in
257          * including padding bytes.
258          *
259          * @param       $length                 Length of raw data
260          * @return      $seekPosition   Found next gap's seek position
261          * @throws      InvalidArgumentException        If the parameter is not valid
262          * @todo        Unfinished work
263          */
264         public function searchNextGap (int $length) {
265                 // Validate parameter
266                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: length=%d - CALLED!', $length));
267                 if ($length <= 0) {
268                         // Throw IAE
269                         throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
270                 }
271
272                 // Partial stub!
273                 $this->partialStub('length=' . $length);
274         }
275
276         /**
277          * Writes at given position by seeking to it.
278          *
279          * @param       $seekPosition   Seek position in file
280          * @param       $dataStream             Data to be written
281          * @return      mixed                   Number of writes bytes or false on error
282          * @throws      OutOfBoundsException    If the position is not seekable
283          * @throws      InvalidArgumentException        If a parameter is not valid
284          */
285         public function writeAtPosition (int $seekPosition, string $dataStream) {
286                 // Validate parameter
287                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
288                 if ($seekPosition < 0) {
289                         // Invalid seek position
290                         throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition));
291                 } elseif (empty($dataStream)) {
292                         // Empty dataStream
293                         throw new InvalidArgumentException('Parameter "dataStream" is empty');
294                 }
295
296                 // Call iterated object's method
297                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
298                 $status = $this->getIteratorInstance()->writeAtPosition($seekPosition, $dataStream);
299
300                 // Return status
301                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: status[%s]=%d - EXIT!', gettype($status), $status));
302                 return $status;
303         }
304
305         /**
306          * Checks if this index has been fully and properly loaded.
307          *
308          * @return      $isLoaded       Whether this index has been loaded
309          */
310         public function isIndexLoaded () {
311                 // Trace message
312                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: CALLED!');
313                 /* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: this=%s', __METHOD__, __LINE__, print_r($this, true)));
314         }
315
316 }