3 namespace CoreFramework\Index;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * A general index class
12 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
31 class BaseIndex extends BaseFrameworkSystem {
33 * Magic for this index
35 const INDEX_MAGIC = 'INDEXv0.1';
38 * Separator group->hash
40 const SEPARATOR_GROUP_HASH = 0x01;
43 * Separator hash->gap position
45 const SEPARATOR_HASH_GAP_POSITION = 0x02;
48 * Separator gap position->length
50 const SEPARATOR_GAP_LENGTH = 0x03;
53 * Protected constructor
55 * @param $className Name of the class
58 protected function __construct ($className) {
59 // Call parent constructor
60 parent::__construct($className);
64 * Reads the file header
68 public function readFileHeader () {
69 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
71 // First rewind to beginning as the header sits at the beginning ...
72 $this->getIteratorInstance()->rewind();
74 // Then read it (see constructor for calculation)
75 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
76 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize()));
78 // Have all requested bytes been read?
79 assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
80 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
82 // Last character must be the separator
83 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->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__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
87 // Okay, then remove it
88 $data = substr($data, 0, -1);
90 // And update seek position
91 $this->getIteratorInstance()->updateSeekPosition();
99 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
102 $this->getIteratorInstance()->setHeader($header);
104 // Check if the array has only 3 elements
105 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->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__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
110 assert($header[0] == self::INDEX_MAGIC);
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
113 // Check length of count
114 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
115 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
118 $header[1] = hex2bin($header[1]);
120 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
124 * Flushes the file header
128 public function flushFileHeader () {
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
131 // Put all informations together
132 $header = sprintf('%s%s%s%s',
136 // Separator header data
137 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
140 str_pad($this->dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
142 // Separator header<->entries
143 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
146 // Write it to disk (header is always at seek position 0)
147 $this->getIteratorInstance()->writeData(0, $header, FALSE);
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
153 * Initializes this index
155 * @param $fileName File name of this index
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.
159 protected function initIndex ($fileName) {
160 // Append index file extension
161 $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
163 // Get a file i/o pointer instance for index file
164 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName, $this));
166 // Get iterator instance
167 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
169 // Is the instance implementing the right interface?
170 assert($iteratorInstance instanceof SeekableWritableFileIterator);
173 $this->setIteratorInstance($iteratorInstance);
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))
183 // Init counters and gaps array
184 $this->getIteratorInstance()->initCountersGapsArray();
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();
191 // And pre-allocate a bit
192 $this->getIteratorInstance()->preAllocateFile('index');
195 // Load the file header
196 $this->readFileHeader();
198 // Count all entries in file
199 $this->getIteratorInstance()->analyzeFile();
203 * Calculates minimum length for one entry/block
205 * @return $length Minimum length for one entry/block
207 public function calculateMinimumBlockLength () {
209 $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
216 * Determines whether the EOF has been reached
218 * @return $isEndOfFileReached Whether the EOF has been reached
219 * @throws UnsupportedOperationException If this method is called
221 public function isEndOfFileReached () {
222 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
226 * Getter for file name
228 * @return $fileName The current file name
229 * @throws UnsupportedOperationException If this method is called
231 public function getFileName () {
232 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
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.
242 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
244 public function initCountersGapsArray () {
245 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
249 * Getter for header size
251 * @return $totalEntries Size of file header
252 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
254 public final function getHeaderSize () {
255 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
259 * Setter for header size
261 * @param $headerSize Size of file header
263 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
265 public final function setHeaderSize ($headerSize) {
266 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
270 * Getter for header array
272 * @return $totalEntries Size of file header
273 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
275 public final function getHeader () {
276 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
282 * @param $header Array for a file header
284 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
286 public final function setHeader (array $header) {
287 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
291 * Updates seekPosition attribute from file to avoid to much access on file.
294 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
296 public function updateSeekPosition () {
297 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
301 * Getter for total entries
303 * @return $totalEntries Total entries in this file
304 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
306 public final function getCounter () {
307 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
311 * "Getter" for file size
313 * @return $fileSize Size of currently loaded file
315 public function getFileSize () {
316 // Call iterator's method
317 return $this->getIteratorInstance()->getFileSize();
321 * Writes data at given position
323 * @param $seekPosition Seek position
324 * @param $data Data to be written
325 * @param $flushHeader Whether to flush the header (default: flush)
327 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
329 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
330 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->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);
335 * Writes given value to the file and returns a hash and gap position for it
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
342 public function writeValueToFile ($groupId, $value) {
343 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
344 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
348 * Writes given raw data to the file and returns a gap position and length
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
355 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
356 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] groupId=' . $groupId . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
357 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);