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