Continued:
[core.git] / inc / main / classes / index / class_BaseIndex.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Index;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8
9 /**
10  * A general index class
11  *
12  * @author              Roland Haeder <webmaster@ship-simu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.ship-simu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31 class BaseIndex extends BaseFrameworkSystem {
32         /**
33          * Magic for this index
34          */
35         const INDEX_MAGIC = 'INDEXv0.1';
36
37         /**
38          * Separator group->hash
39          */
40         const SEPARATOR_GROUP_HASH = 0x01;
41
42         /**
43          * Separator hash->gap position
44          */
45         const SEPARATOR_HASH_GAP_POSITION = 0x02;
46
47         /**
48          * Separator gap position->length
49          */
50         const SEPARATOR_GAP_LENGTH = 0x03;
51
52         /**
53          * Protected constructor
54          *
55          * @param       $className      Name of the class
56          * @return      void
57          */
58         protected function __construct ($className) {
59                 // Call parent constructor
60                 parent::__construct($className);
61         }
62
63         /**
64          * Reads the file header
65          *
66          * @return      void
67          */
68         public function readFileHeader () {
69                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
70
71                 // First rewind to beginning as the header sits at the beginning ...
72                 $this->getIteratorInstance()->rewind();
73
74                 // Then read it (see constructor for calculation)
75                 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
76                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize()));
77
78                 // Have all requested bytes been read?
79                 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
80                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
81
82                 // Last character must be the separator
83                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1)))));
84                 assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
85                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
86
87                 // Okay, then remove it
88                 $data = substr($data, 0, -1);
89
90                 // And update seek position
91                 $this->getIteratorInstance()->updateSeekPosition();
92
93                 /*
94                  * Now split it:
95                  *
96                  * 0 => magic
97                  * 1 => total entries
98                  */
99                 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
100
101                 // Set it here
102                 $this->getIteratorInstance()->setHeader($header);
103
104                 // Check if the array has only 3 elements
105                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, TRUE)));
106                 assert(count($header) == 2);
107                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
108
109                 // Check magic
110                 assert($header[0] == self::INDEX_MAGIC);
111                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
112
113                 // Check length of count
114                 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
115                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
116
117                 // Decode count
118                 $header[1] = hex2bin($header[1]);
119
120                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
121         }
122
123         /**
124          * Flushes the file header
125          *
126          * @return      void
127          */
128         public function flushFileHeader () {
129                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
130
131                 // Put all informations together
132                 $header = sprintf('%s%s%s%s',
133                         // Magic
134                         self::INDEX_MAGIC,
135
136                         // Separator header data
137                         chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
138
139                         // Total entries
140                         str_pad($this->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                 $this->getIteratorInstance()->writeData(0, $header, FALSE);
148
149                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
150         }
151
152         /**
153          * Initializes this index
154          *
155          * @param       $fileName       File name of this index
156          * @return      void
157          * @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.
158          */
159         protected function initIndex ($fileName) {
160                 // Append index file extension
161                 $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
162
163                 // Get a file i/o pointer instance for index file
164                 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName, $this));
165
166                 // Get iterator instance
167                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
168
169                 // Is the instance implementing the right interface?
170                 assert($iteratorInstance instanceof SeekableWritableFileIterator);
171
172                 // Set iterator here
173                 $this->setIteratorInstance($iteratorInstance);
174
175                 // Calculate header size
176                 $this->getIteratorInstance()->setHeaderSize(
177                         strlen(self::INDEX_MAGIC) +
178                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
179                         BaseBinaryFile::LENGTH_COUNT +
180                         strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
181                 );
182
183                 // Init counters and gaps array
184                 $this->getIteratorInstance()->initCountersGapsArray();
185
186                 // Is the file's header initialized?
187                 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
188                         // No, then create it (which may pre-allocate the index)
189                         $this->getIteratorInstance()->createFileHeader();
190
191                         // And pre-allocate a bit
192                         $this->getIteratorInstance()->preAllocateFile('index');
193                 } // END - if
194
195                 // Load the file header
196                 $this->readFileHeader();
197
198                 // Count all entries in file
199                 $this->getIteratorInstance()->analyzeFile();
200         }
201
202         /**
203          * Calculates minimum length for one entry/block
204          *
205          * @return      $length         Minimum length for one entry/block
206          */
207         public function calculateMinimumBlockLength () {
208                 // Calulcate it
209                 $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
210
211                 // Return it
212                 return $length;
213         }
214
215         /**
216          * Determines whether the EOF has been reached
217          *
218          * @return      $isEndOfFileReached             Whether the EOF has been reached
219          * @throws      UnsupportedOperationException   If this method is called
220          */
221         public function isEndOfFileReached () {
222                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
223         }
224
225         /**
226          * Getter for file name
227          *
228          * @return      $fileName       The current file name
229          * @throws      UnsupportedOperationException   If this method is called
230          */
231         public function getFileName () {
232                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
233         }
234
235         /**
236          * Initializes counter for valid entries, arrays for damaged entries and
237          * an array for gap seek positions. If you call this method on your own,
238          * please re-analyze the file structure. So you are better to call
239          * analyzeFile() instead of this method.
240          *
241          * @return      void
242          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
243          */
244         public function initCountersGapsArray () {
245                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
246         }
247
248         /**
249          * Getter for header size
250          *
251          * @return      $totalEntries   Size of file header
252          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
253          */
254         public final function getHeaderSize () {
255                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
256         }
257
258         /**
259          * Setter for header size
260          *
261          * @param       $headerSize             Size of file header
262          * @return      void
263          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
264          */
265         public final function setHeaderSize ($headerSize) {
266                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
267         }
268
269         /**
270          * Getter for header array
271          *
272          * @return      $totalEntries   Size of file header
273          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
274          */
275         public final function getHeader () {
276                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
277         }
278
279         /**
280          * Setter for header
281          *
282          * @param       $header         Array for a file header
283          * @return      void
284          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
285          */
286         public final function setHeader (array $header) {
287                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
288         }
289
290         /**
291          * Updates seekPosition attribute from file to avoid to much access on file.
292          *
293          * @return      void
294          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
295          */
296         public function updateSeekPosition () {
297                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
298         }
299
300         /**
301          * Getter for total entries
302          *
303          * @return      $totalEntries   Total entries in this file
304          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
305          */
306         public final function getCounter () {
307                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
308         }
309
310         /**
311          * "Getter" for file size
312          *
313          * @return      $fileSize       Size of currently loaded file
314          */
315         public function getFileSize () {
316                 // Call iterator's method
317                 return $this->getIteratorInstance()->getFileSize();
318         }
319
320         /**
321          * Writes data at given position
322          *
323          * @param       $seekPosition   Seek position
324          * @param       $data                   Data to be written
325          * @param       $flushHeader    Whether to flush the header (default: flush)
326          * @return      void
327          * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
328          */
329         public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
330                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader)));
331                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
332         }
333
334         /**
335          * Writes given value to the file and returns a hash and gap position for it
336          *
337          * @param       $groupId        Group identifier
338          * @param       $value          Value to be added to the stack
339          * @return      $data           Hash and gap position
340          * @throws      UnsupportedOperationException   If this method is called
341          */
342         public function writeValueToFile ($groupId, $value) {
343                 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
344                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
345         }
346
347         /**
348          * Writes given raw data to the file and returns a gap position and length
349          *
350          * @param       $groupId        Group identifier
351          * @param       $hash           Hash from encoded value
352          * @param       $encoded        Encoded value to be written to the file
353          * @return      $data           Gap position and length of the raw data
354          */
355         public function writeDataToFreeGap ($groupId, $hash, $encoded) {
356                 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
357                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
358         }
359
360 }