3 namespace Org\Mxchange\CoreFramework\Index;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
12 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
16 use \UnexpectedValueException;
19 * A general index class
21 * @author Roland Haeder <webmaster@ship-simu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.ship-simu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseIndex extends BaseFrameworkSystem implements Indexable {
45 * Minimum block length
47 private static $minimumBlockLength = 0;
50 * Protected constructor
52 * @param $className Name of the class
55 protected function __construct (string $className) {
56 // Call parent constructor
57 parent::__construct($className);
61 * Reads the file header
64 * @throws UnexpectedValueException If header length or count of elements is invalid
66 public function readFileHeader () {
67 // First rewind to beginning as the header sits at the beginning ...
68 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
69 $this->getIteratorInstance()->rewind();
71 // Then read it (see constructor for calculation)
72 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
74 // Have all requested bytes been read?
75 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
76 if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
77 // Invalid header length
78 throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d',
81 $this->getIteratorInstance()->getHeaderSize()
83 } elseif (empty(trim($data, chr(0)))) {
85 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: File header is empty - EXIT!');
87 } elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
89 throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"',
91 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
95 // Okay, then remove it
96 $data = substr($data, 0, -1);
98 // And update seek position
99 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->updateSeekPosition() ...');
100 $this->getIteratorInstance()->updateSeekPosition();
108 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
110 // Check if the array has only 3 elements
111 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header()=%d', count($header)));
112 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header(%d)=%s', count($header), print_r($header, true)));
113 if (count($header) != 2) {
115 throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header)));
116 } elseif ($header[0] !== Indexable::INDEX_MAGIC) {
117 // Magic must be in first element
118 throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], Indexable::INDEX_MAGIC));
119 } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
120 // Length of total entries not matching
121 throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BaseBinaryFile::LENGTH_COUNT));
125 $header[1] = hex2bin($header[1]);
128 $this->getIteratorInstance()->setHeader($header);
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
135 * Flushes the file header
139 public function flushFileHeader () {
140 // Put all informations together
141 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
142 $header = sprintf('%s%s%s%s',
144 Indexable::INDEX_MAGIC,
146 // Separator header data
147 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
150 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
152 // Separator header<->entries
153 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
156 // Write it to disk (header is always at seek position 0)
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header));
158 $this->getIteratorInstance()->writeAtPosition(0, $header);
161 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
165 * Initializes this index
167 * @param $fileInfoInstance An instance of a SplFileInfo class
169 * @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.
171 protected function initIndex (SplFileInfo $fileInfoInstance) {
172 // Get a file i/o pointer instance for index file
173 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance));
174 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this));
176 // Get iterator instance
177 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', [$fileInstance]);
180 $this->setIteratorInstance($iteratorInstance);
182 // Calculate header size
184 strlen(Indexable::INDEX_MAGIC) +
185 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
186 BaseBinaryFile::LENGTH_COUNT +
187 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
191 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Setting headerSize=%d ...', $headerSize));
192 $this->getIteratorInstance()->setHeaderSize($headerSize);
194 // Init counters and gaps array
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->initCountersGapsArray() ...');
196 $this->getIteratorInstance()->initCountersGapsArray();
198 // Default is not created
201 // Is the file's header initialized?
202 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
203 // First pre-allocate a bit
204 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->preAllocateFile(index) ...');
205 $this->getIteratorInstance()->preAllocateFile('index');
207 // Then write file header
208 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->createFileHeader() ...');
209 $this->getIteratorInstance()->createFileHeader();
211 // Mark as freshly created
215 // Load the file header
216 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->readFileHeader() ...');
217 $this->readFileHeader();
220 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: created=%d', intval($created)));
222 // Analyze file structure
223 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->analyzeFileStructure() ...');
224 $this->getIteratorInstance()->analyzeFileStructure();
228 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
232 * Calculates minimum length for one entry/block
234 * @return $length Minimum length for one entry/block
236 public function calculateMinimumBlockLength () {
238 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
239 if (self::$minimumBlockLength == 0) {
241 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calculating ...');
242 self::$minimumBlockLength = (
244 BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) +
246 BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES))
251 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: self::minimumBlockLength=%d - EXIT!', self::$minimumBlockLength));
252 return self::$minimumBlockLength;
256 * Determines whether the EOF has been reached
258 * @return $isEndOfFileReached Whether the EOF has been reached
259 * @throws UnsupportedOperationException If this method is called
261 public function isEndOfFileReached () {
262 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
266 * Initializes counter for valid entries, arrays for damaged entries and
267 * an array for gap seek positions. If you call this method on your own,
268 * please re-analyze the file structure. So you are better to call
269 * analyzeFileStructure() instead of this method.
272 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
274 public function initCountersGapsArray () {
275 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
279 * Getter for header size
281 * @return $totalEntries Size of file header
282 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
284 public final function getHeaderSize () {
285 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
289 * Setter for header size
291 * @param $headerSize Size of file header
293 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
295 public final function setHeaderSize (int $headerSize) {
296 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
300 * Getter for header array
302 * @return $totalEntries Size of file header
303 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
305 public final function getHeader () {
306 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
312 * @param $header Array for a file header
314 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
316 public final function setHeader (array $header) {
317 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
321 * Updates seekPosition attribute from file to avoid to much access on file.
324 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
326 public function updateSeekPosition () {
327 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
331 * Getter for total entries
333 * @return $totalEntries Total entries in this file
334 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
336 public final function getCounter () {
337 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
341 * "Getter" for file size
343 * @return $fileSize Size of currently loaded file
345 public function getFileSize () {
346 // Call iterator's method
347 return $this->getIteratorInstance()->getFileSize();
351 * Writes data at given position
353 * @param $seekPosition Seek position
354 * @param $data Data to be written
355 * @param $flushHeader Whether to flush the header (default: flush)
357 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
359 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
360 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s,data[]=%s,flushHeader=%d - CALLED!', $seekPosition, gettype($data), intval($flushHeader)));
361 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
365 * Writes given value to the file and returns a hash and gap position for it
367 * @param $groupId Group identifier
368 * @param $value Value to be added to the stack
369 * @return $data Hash and gap position
370 * @throws UnsupportedOperationException If this method is called
372 public function writeValueToFile (string $groupId, $value) {
373 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,value[%s]=%s - CALLED!', $groupId, gettype($value), print_r($value, true)));
374 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
378 * Writes given raw data to the file and returns a gap position and length
380 * @param $groupId Group identifier
381 * @param $hash Hash from encoded value
382 * @param $encoded Encoded value to be written to the file
383 * @return $data Gap position and length of the raw data
385 public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
386 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
387 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);