3 namespace CoreFramework\Index;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A general index class
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseIndex extends BaseFrameworkSystem {
32 * Magic for this index
34 const INDEX_MAGIC = 'INDEXv0.1';
37 * Separator group->hash
39 const SEPARATOR_GROUP_HASH = 0x01;
42 * Separator hash->gap position
44 const SEPARATOR_HASH_GAP_POSITION = 0x02;
47 * Separator gap position->length
49 const SEPARATOR_GAP_LENGTH = 0x03;
52 * Protected constructor
54 * @param $className Name of the class
57 protected function __construct ($className) {
58 // Call parent constructor
59 parent::__construct($className);
63 * Reads the file header
67 public function readFileHeader () {
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
70 // First rewind to beginning as the header sits at the beginning ...
71 $this->getIteratorInstance()->rewind();
73 // Then read it (see constructor for calculation)
74 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
75 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize()));
77 // Have all requested bytes been read?
78 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
81 // Last character must be the separator
82 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1)))));
83 assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
84 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
86 // Okay, then remove it
87 $data = substr($data, 0, -1);
89 // And update seek position
90 $this->getIteratorInstance()->updateSeekPosition();
98 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
101 $this->getIteratorInstance()->setHeader($header);
103 // Check if the array has only 3 elements
104 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, TRUE)));
105 assert(count($header) == 2);
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
109 assert($header[0] == self::INDEX_MAGIC);
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
112 // Check length of count
113 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
117 $header[1] = hex2bin($header[1]);
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
123 * Flushes the file header
127 public function flushFileHeader () {
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
130 // Put all informations together
131 $header = sprintf('%s%s%s%s',
135 // Separator header data
136 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
139 str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
141 // Separator header<->entries
142 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
145 // Write it to disk (header is always at seek position 0)
146 $this->getIteratorInstance()->writeData(0, $header, FALSE);
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
152 * Initializes this index
154 * @param $fileName File name of this index
156 * @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 protected function initIndex ($fileName) {
159 // Append index file extension
160 $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
162 // Get a file i/o pointer instance for index file
163 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName, $this));
165 // Get iterator instance
166 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
168 // Is the instance implementing the right interface?
169 assert($iteratorInstance instanceof SeekableWritableFileIterator);
172 $this->setIteratorInstance($iteratorInstance);
174 // Calculate header size
175 $this->getIteratorInstance()->setHeaderSize(
176 strlen(self::INDEX_MAGIC) +
177 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
178 BaseBinaryFile::LENGTH_COUNT +
179 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
182 // Init counters and gaps array
183 $this->getIteratorInstance()->initCountersGapsArray();
185 // Is the file's header initialized?
186 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
187 // No, then create it (which may pre-allocate the index)
188 $this->getIteratorInstance()->createFileHeader();
190 // And pre-allocate a bit
191 $this->getIteratorInstance()->preAllocateFile('index');
194 // Load the file header
195 $this->readFileHeader();
197 // Count all entries in file
198 $this->getIteratorInstance()->analyzeFile();
202 * Calculates minimum length for one entry/block
204 * @return $length Minimum length for one entry/block
206 public function calculateMinimumBlockLength () {
208 $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
215 * Determines whether the EOF has been reached
217 * @return $isEndOfFileReached Whether the EOF has been reached
218 * @throws UnsupportedOperationException If this method is called
220 public function isEndOfFileReached () {
221 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
225 * Getter for file name
227 * @return $fileName The current file name
228 * @throws UnsupportedOperationException If this method is called
230 public function getFileName () {
231 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
235 * Initializes counter for valid entries, arrays for damaged entries and
236 * an array for gap seek positions. If you call this method on your own,
237 * please re-analyze the file structure. So you are better to call
238 * analyzeFile() instead of this method.
241 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
243 public function initCountersGapsArray () {
244 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
248 * Getter for header size
250 * @return $totalEntries Size of file header
251 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
253 public final function getHeaderSize () {
254 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
258 * Setter for header size
260 * @param $headerSize Size of file header
262 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
264 public final function setHeaderSize ($headerSize) {
265 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
269 * Getter for header array
271 * @return $totalEntries Size of file header
272 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
274 public final function getHeader () {
275 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
281 * @param $header Array for a file header
283 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
285 public final function setHeader (array $header) {
286 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
290 * Updates seekPosition attribute from file to avoid to much access on file.
293 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
295 public function updateSeekPosition () {
296 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
300 * Getter for total entries
302 * @return $totalEntries Total entries in this file
303 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
305 public final function getCounter () {
306 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
310 * "Getter" for file size
312 * @return $fileSize Size of currently loaded file
314 public function getFileSize () {
315 // Call iterator's method
316 return $this->getIteratorInstance()->getFileSize();
320 * Writes data at given position
322 * @param $seekPosition Seek position
323 * @param $data Data to be written
324 * @param $flushHeader Whether to flush the header (default: flush)
326 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
328 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
329 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader)));
330 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
334 * Writes given value to the file and returns a hash and gap position for it
336 * @param $groupId Group identifier
337 * @param $value Value to be added to the stack
338 * @return $data Hash and gap position
339 * @throws UnsupportedOperationException If this method is called
341 public function writeValueToFile ($groupId, $value) {
342 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
343 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
347 * Writes given raw data to the file and returns a gap position and length
349 * @param $groupId Group identifier
350 * @param $hash Hash from encoded value
351 * @param $encoded Encoded value to be written to the file
352 * @return $data Gap position and length of the raw data
354 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
355 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
356 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);