3 namespace Org\Mxchange\CoreFramework\Index;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\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 {
45 * Magic for this index
47 const INDEX_MAGIC = 'INDEXv0.1';
50 * Separator group->hash
52 const SEPARATOR_GROUP_HASH = 0x01;
55 * Separator hash->gap position
57 const SEPARATOR_HASH_GAP_POSITION = 0x02;
60 * Separator gap position->length
62 const SEPARATOR_GAP_LENGTH = 0x03;
65 * Protected constructor
67 * @param $className Name of the class
70 protected function __construct (string $className) {
71 // Call parent constructor
72 parent::__construct($className);
76 * Reads the file header
79 * @throws UnexpectedValueException If header length is invalid
81 public function readFileHeader () {
82 // First rewind to beginning as the header sits at the beginning ...
83 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
84 $this->getIteratorInstance()->rewind();
86 // Then read it (see constructor for calculation)
87 $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
89 // Have all requested bytes been read?
90 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
91 if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
92 // Invalid header length
93 throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d',
96 $this->getIteratorInstance()->getHeaderSize()
98 } elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
100 throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"',
102 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
106 // Okay, then remove it
107 $data = substr($data, 0, -1);
109 // And update seek position
110 $this->getIteratorInstance()->updateSeekPosition();
118 $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
121 $this->getIteratorInstance()->setHeader($header);
123 // Check if the array has only 3 elements
124 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('header(%d)=%s', count($header), print_r($header, true)));
125 assert(count($header) == 2);
128 assert($header[0] == self::INDEX_MAGIC);
130 // Check length of count
131 assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
134 $header[1] = hex2bin($header[1]);
137 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
141 * Flushes the file header
145 public function flushFileHeader () {
146 // Put all informations together
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
148 $header = sprintf('%s%s%s%s',
152 // Separator header data
153 chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
156 str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
158 // Separator header<->entries
159 chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)
162 // Write it to disk (header is always at seek position 0)
163 $this->getIteratorInstance()->writeData(0, $header, false);
166 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
170 * Initializes this index
172 * @param $fileInfoInstance An instance of a SplFileInfo class
174 * @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.
176 protected function initIndex (SplFileInfo $fileInfoInstance) {
177 // Get a file i/o pointer instance for index file
178 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance));
179 $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this));
181 // Get iterator instance
182 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance));
185 $this->setIteratorInstance($iteratorInstance);
187 // Calculate header size
188 $this->getIteratorInstance()->setHeaderSize(
189 strlen(self::INDEX_MAGIC) +
190 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
191 BaseBinaryFile::LENGTH_COUNT +
192 strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
195 // Init counters and gaps array
196 $this->getIteratorInstance()->initCountersGapsArray();
198 // Is the file's header initialized?
199 if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
200 // No, then create it (which may pre-allocate the index)
201 $this->getIteratorInstance()->createFileHeader();
203 // And pre-allocate a bit
204 $this->getIteratorInstance()->preAllocateFile('index');
207 // Load the file header
208 $this->readFileHeader();
210 // Count all entries in file
211 $this->getIteratorInstance()->analyzeFile();
214 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
218 * Calculates minimum length for one entry/block
220 * @return $length Minimum length for one entry/block
222 public function calculateMinimumBlockLength () {
224 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: CALLED!');
225 $length = BaseBinaryFile::LENGTH_TYPE + strlen(chr(BaseBinaryFile::SEPARATOR_TYPE_POSITION)) + BaseBinaryFile::LENGTH_POSITION + strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES));
228 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: length=%d - EXIT!', $length));
233 * Determines whether the EOF has been reached
235 * @return $isEndOfFileReached Whether the EOF has been reached
236 * @throws UnsupportedOperationException If this method is called
238 public function isEndOfFileReached () {
239 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
243 * Initializes counter for valid entries, arrays for damaged entries and
244 * an array for gap seek positions. If you call this method on your own,
245 * please re-analyze the file structure. So you are better to call
246 * analyzeFile() instead of this method.
249 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
251 public function initCountersGapsArray () {
252 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
256 * Getter for header size
258 * @return $totalEntries Size of file header
259 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
261 public final function getHeaderSize () {
262 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
266 * Setter for header size
268 * @param $headerSize Size of file header
270 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
272 public final function setHeaderSize (int $headerSize) {
273 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
277 * Getter for header array
279 * @return $totalEntries Size of file header
280 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
282 public final function getHeader () {
283 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
289 * @param $header Array for a file header
291 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
293 public final function setHeader (array $header) {
294 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
298 * Updates seekPosition attribute from file to avoid to much access on file.
301 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
303 public function updateSeekPosition () {
304 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
308 * Getter for total entries
310 * @return $totalEntries Total entries in this file
311 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
313 public final function getCounter () {
314 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
318 * "Getter" for file size
320 * @return $fileSize Size of currently loaded file
322 public function getFileSize () {
323 // Call iterator's method
324 return $this->getIteratorInstance()->getFileSize();
328 * Writes data at given position
330 * @param $seekPosition Seek position
331 * @param $data Data to be written
332 * @param $flushHeader Whether to flush the header (default: flush)
334 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
336 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
337 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s,data[]=%s,flushHeader=%d - CALLED!', $seekPosition, gettype($data), intval($flushHeader)));
338 throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
342 * Writes given value to the file and returns a hash and gap position for it
344 * @param $groupId Group identifier
345 * @param $value Value to be added to the stack
346 * @return $data Hash and gap position
347 * @throws UnsupportedOperationException If this method is called
349 public function writeValueToFile (string $groupId, $value) {
350 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,value[%s]=%s - CALLED!', $groupId, gettype($value), print_r($value, true)));
351 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
355 * Writes given raw data to the file and returns a gap position and length
357 * @param $groupId Group identifier
358 * @param $hash Hash from encoded value
359 * @param $encoded Encoded value to be written to the file
360 * @return $data Gap position and length of the raw data
362 public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
363 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
364 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);