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 * Separator for header data
33 const SEPARATOR_HEADER_DATA = 0x01;
36 * Separator header->entries
38 const SEPARATOR_HEADER_ENTRIES = 0x02;
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
50 // Calculate header size
52 strlen(self::INDEX_MAGIC) +
53 strlen(self::SEPARATOR_HEADER_DATA) +
55 strlen(self::SEPARATOR_HEADER_ENTRIES)
58 // Init counters and gaps array
59 $this->initCountersGapsArray();
63 * Reads the file header
67 private 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->headerSize);
75 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->headerSize));
77 // Have all requested bytes been read?
78 assert(strlen($data) == $this->headerSize);
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(self::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->updateSeekPosition();
98 $this->header = explode(chr(self::SEPARATOR_HEADER_DATA), $data);
100 // Check if the array has only 3 elements
101 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($this->header), print_r($this->header, TRUE)));
102 assert(count($this->header) == 2);
103 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
106 assert($this->header[0] == self::INDEX_MAGIC);
107 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
109 // Check length of count
110 assert(strlen($this->header[1]) == self::LENGTH_COUNT);
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
114 $this->header[1] = hex2bin($this->header[1]);
116 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
120 * Flushes the file header
124 private function flushFileHeader () {
125 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
127 // Put all informations together
128 $header = sprintf('%s%s',
132 // Separator position<->entries
133 chr(self::SEPARATOR_HEADER_ENTRIES)
136 // Write it to disk (header is always at seek position 0)
137 $this->writeData(0, $header);
139 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
143 * Analyzes entries in index file. This will count all found (and valid)
144 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
145 * only gaps are found, the file is considered as "virgin" (no entries).
149 private function analyzeFile () {
150 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
152 // Make sure the file is initialized
153 assert($this->isFileInitialized());
155 // Init counters and gaps array
156 $this->initGapsArray();
158 // Output message (as this may take some time)
159 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
165 * Initializes this index
167 * @param $fileName File name of this index
170 protected function initIndex ($fileName) {
171 // Get a file i/o pointer instance for index file
172 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
174 // Get iterator instance
175 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
177 // Is the instance implementing the right interface?
178 assert($iteratorInstance instanceof SeekableWritableFileIterator);
181 $this->setIteratorInstance($iteratorInstance);
183 // Is the file's header initialized?
184 if (!$this->isFileHeaderInitialized()) {
185 // No, then create it (which may pre-allocate the index)
186 $this->createFileHeader();
188 // And pre-allocate a bit
189 $this->preAllocateFile('index');
192 // Load the file header
193 $this->readFileHeader();
195 // Count all entries in file
196 $this->analyzeFile();