3 * A general index class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseIndex extends BaseFrameworkSystem {
26 * Magic for this index
28 const INDEX_MAGIC = 'INDEXv0.1';
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
40 // Calculate header size
42 strlen(self::INDEX_MAGIC) +
43 strlen(self::SEPARATOR_HEADER_DATA) +
45 strlen(self::SEPARATOR_HEADER_ENTRIES)
48 // Init counters and gaps array
49 $this->initCountersGapsArray();
53 * Reads the file header
57 protected function readFileHeader () {
58 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
60 // First rewind to beginning as the header sits at the beginning ...
61 $this->getIteratorInstance()->rewind();
63 // Then read it (see constructor for calculation)
64 $data = $this->getIteratorInstance()->read($this->getHeaderSize());
65 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getHeaderSize()));
67 // Have all requested bytes been read?
68 assert(strlen($data) == $this->getHeaderSize());
69 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
71 // Last character must be the separator
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1)))));
73 assert(substr($data, -1, 1) == chr(self::SEPARATOR_HEADER_ENTRIES));
74 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
76 // Okay, then remove it
77 $data = substr($data, 0, -1);
79 // And update seek position
80 $this->updateSeekPosition();
88 $header = explode(chr(self::SEPARATOR_HEADER_DATA), $data);
91 $this->setHeader($header);
93 // Check if the array has only 3 elements
94 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, TRUE)));
95 assert(count($header) == 2);
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
99 assert($header[0] == self::INDEX_MAGIC);
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
102 // Check length of count
103 assert(strlen($header[1]) == self::LENGTH_COUNT);
104 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
107 $header[1] = hex2bin($header[1]);
109 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
113 * Flushes the file header
117 protected function flushFileHeader () {
118 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
120 // Put all informations together
121 $header = sprintf('%s%s%s%s',
125 // Separator header data
126 chr(self::SEPARATOR_HEADER_DATA),
129 str_pad($this->dec2hex($this->getCounter()), self::LENGTH_COUNT, '0', STR_PAD_LEFT),
131 // Separator header<->entries
132 chr(self::SEPARATOR_HEADER_ENTRIES)
135 // Write it to disk (header is always at seek position 0)
136 $this->writeData(0, $header, FALSE);
138 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
142 * Analyzes entries in index file. This will count all found (and valid)
143 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
144 * only gaps are found, the file is considered as "virgin" (no entries).
148 private function analyzeFile () {
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
151 // Make sure the file is initialized
152 assert($this->isFileInitialized());
154 // Init counters and gaps array
155 $this->initCountersGapsArray();
157 // Output message (as this may take some time)
158 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
160 // First rewind to the begining
161 $this->getIteratorInstance()->rewind();
163 // Then try to load all entries
164 while ($this->getIteratorInstance()->valid()) {
166 $this->getIteratorInstance()->next();
169 $current = $this->getIteratorInstance()->current();
172 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE)));
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
179 * Initializes this index
181 * @param $fileName File name of this index
183 * @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.
185 protected function initIndex ($fileName) {
186 // Append index file extension
187 $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
189 // Get a file i/o pointer instance for index file
190 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName));
192 // Get iterator instance
193 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance, $this));
195 // Is the instance implementing the right interface?
196 assert($iteratorInstance instanceof SeekableWritableFileIterator);
199 $this->setIteratorInstance($iteratorInstance);
201 // Is the file's header initialized?
202 if (!$this->isFileHeaderInitialized()) {
203 // No, then create it (which may pre-allocate the index)
204 $this->createFileHeader();
206 // And pre-allocate a bit
207 $this->preAllocateFile('index');
210 // Load the file header
211 $this->readFileHeader();
213 // Count all entries in file
214 $this->analyzeFile();
218 * Calculates minimum length for one entry/block
220 * @return $length Minimum length for one entry/block
222 public function caluclateMinimumBlockLength () {
224 // @TODO Not finished yet