3 * A general file-based stack class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 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 BaseFileStack extends BaseStacker {
26 * Magic for this stack
28 const STACK_MAGIC = 'STACKv0.1';
31 * Separator for header data
33 const SEPARATOR_HEADER_DATA = 0x01;
36 * Separator header->entries
38 const SEPARATOR_HEADER_ENTRIES = 0x02;
41 * Separator hash->name
43 const SEPARATOR_HASH_NAME = 0x03;
48 const LENGTH_NAME = 10;
53 const LENGTH_COUNT = 20;
58 const LENGTH_POSITION = 20;
61 * Counter for total entries
63 private $totalEntries = 0;
66 * Current seek position
68 private $seekPosition = 0;
73 private $headerSize = 0;
78 private $header = array();
81 * Protected constructor
83 * @param $className Name of the class
86 protected function __construct ($className) {
87 // Call parent constructor
88 parent::__construct($className);
90 // Calculate header size
93 strlen(self::SEPARATOR_HEADER_DATA) +
95 strlen(self::SEPARATOR_HEADER_DATA) +
96 self::LENGTH_POSITION +
97 strlen(self::SEPARATOR_HEADER_ENTRIES)
102 * Getter for total entries
104 * @return $totalEntries Total entries in this stack
106 private function getCounter () {
108 return $this->totalEntries;
116 private function incrementCounter () {
118 $this->totalEntries++;
122 * Getter for seek position
124 * @return $seekPosition Current seek position (stored here in object)
126 private function getSeekPosition () {
128 return $this->seekPosition;
132 * Setter for seek position
134 * @param $seekPosition Current seek position (stored here in object)
137 private function setSeekPosition ($seekPosition) {
139 $this->seekPosition = $seekPosition;
143 * Updates seekPosition attribute from file to avoid to much access on file.
147 private function updateSeekPosition () {
148 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
150 // Get key (= seek position)
151 $seekPosition = $this->getIteratorInstance()->key();
152 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
155 $this->setSeekPosition($seekPosition);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
161 * Reads the file header
165 private function readFileHeader () {
166 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
168 // First rewind to beginning as the header sits at the beginning ...
169 $this->getIteratorInstance()->rewind();
171 // Then read it (see constructor for calculation)
172 $data = $this->getIteratorInstance()->read($this->headerLength);
174 // Last character must be the separator
175 assert(substr($data, -1, 1) == self::SEPARATOR_HEADER_ENTRIES);
177 // Okay, then remove it
178 $data = substr($data, 0, -1);
180 // And update seek position
181 $this->updateSeekPosition();
188 * 2 => Current seek position
190 $this->header = explode(self::SEPARATOR_HEADER_DATA, $data);
192 // Check if the array has only 3 elements
193 assert(count($this->header) == 3);
196 assert($this->header[0] == self::MAGIC);
198 // Check length of count and seek position
199 assert(strlen($this->header[1]) == self::LENGTH_COUNT);
200 assert(strlen($this->header[2]) == self::LENGTH_POSITION);
202 // Decode count and seek position
203 $this->header[1] = hex2bin($this->header[1]);
204 $this->header[2] = hex2bin($this->header[2]);
206 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
210 * Checks whether the file header is initialized
212 * @return $isInitialized Whether the file header is initialized
214 private function isFileHeaderInitialized () {
215 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
216 // Default is not initialized
217 $isInitialized = FALSE;
219 // Is the file initialized?
220 if ($this->isFileInitialized()) {
221 // Some bytes has been written, so rewind to start of it.
222 $rewindStatus = $this->getIteratorInstance()->rewind();
223 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
225 // Is the rewind() call successfull?
226 if ($rewindStatus != 1) {
227 // Something bad happened
228 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
232 $this->readFileHeader();
234 // Check if its format is valid.
238 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
239 return $isInitialized;
243 * Checks whether the file-based stack has been initialized
245 * @return $isInitialized Whether the file's size is zero
247 private function isFileInitialized () {
248 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
250 // Get it from iterator which holds the pointer instance. If FALSE is returned
251 $fileSize = $this->getIteratorInstance()->size();
252 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
255 * The returned file size should not be FALSE or NULL as this means
256 * that the pointer class does not work correctly.
258 assert(is_int($fileSize));
260 // Is more than 0 returned?
261 $isInitialized = ($fileSize > 0);
264 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
265 return $isInitialized;
269 * Creates the file-stack's header
273 private function createFileHeader () {
274 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
275 // The file's header should not be initialized here
276 assert(!$this->isFileHeaderInitialized());
278 // Simple flush file header which will create it.
279 $this->flushFileHeader();
281 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
285 * Flushes the file header
289 private function flushFileHeader () {
290 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
292 // Put all informations together
293 $header = sprintf('%s%s%s%s%s%s',
297 // Separator magic<->count
298 chr(self::SEPARATOR_HEADER_DATA),
300 // Total entries (will be zero) and pad it to 20 chars
301 str_pad($this->dec2hex($this->getCounter()), self::LENGTH_COUNT, '0', STR_PAD_LEFT),
303 // Separator count<->seek position
304 chr(self::SEPARATOR_HEADER_DATA),
306 // Position (will be zero)
307 str_pad($this->dec2hex($this->getSeekPosition(), 2), self::LENGTH_POSITION, '0', STR_PAD_LEFT),
309 // Separator position<->entries
310 chr(self::SEPARATOR_HEADER_ENTRIES)
313 // Write it to disk (header is always at seek position 0)
314 $this->writeData(0, $header);
316 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
320 * Writes data at given position
322 * @param $seekPosition Seek position
323 * @param $data Data to be written
326 private function writeData ($seekPosition, $data) {
327 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
329 // Write data at given position
330 $this->getIteratorInstance()->writeAtPosition($seekPosition, $data);
332 // Update seek position
333 $this->updateSeekPosition();
335 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
339 * Pre-allocates file (if enabled) with some space for later faster write access.
343 private function preAllocateFile () {
344 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
347 if ($this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_enabled') != 'Y') {
349 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating stack file.', __METHOD__, __LINE__));
351 // Don't continue here.
356 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating stack file ...', __METHOD__, __LINE__));
359 * Calculate minimum length for one entry:
360 * minimum length = hash length + separator + name + minimum entry size = ?? + 1 + 10 + 1 = ??
362 $minLengthEntry = self::getHashLength() + strlen(self::SEPARATOR_HASH_NAME) + self::LENGTH_NAME + 1;
363 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
365 // Calulcate seek position
366 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_count');
367 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
369 // Now simply write a NUL there. This will pre-allocate the file.
370 $this->writeData($seekPosition, chr(0));
372 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
376 * Initializes this file-based stack.
378 * @param $fileName File name of this stack
381 protected function initFileStack ($fileName) {
382 // Get a file i/o pointer instance
383 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
385 // Get iterator instance
386 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
388 // Is the instance implementing the right interface?
389 assert($iteratorInstance instanceof SeekableWritableFileIterator);
392 $this->setIteratorInstance($iteratorInstance);
394 // Is the file's header initialized?
395 if ($this->isFileHeaderInitialized()) {
397 $this->loadFileHeader();
399 // No, then create it (which may pre-allocate the stack)
400 $this->createFileHeader();
402 // And pre-allocate a bit
403 $this->preAllocateFile();
408 * Initializes given stacker
410 * @param $stackerName Name of the stack
411 * @param $forceReInit Force re-initialization
413 * @throws AlreadyInitializedStackerException If the stack is already initialized
415 public function initStack ($stackerName, $forceReInit = FALSE) {
416 // Is the stack already initialized?
417 if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) {
418 // Then throw the exception
419 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
422 // Initialize the given stack
423 $this->partialStub('stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit));
427 * Checks whether the given stack is initialized (set in array $stackers)
429 * @param $stackerName Name of the stack
430 * @return $isInitialized Whether the stack is initialized
432 public function isStackInitialized ($stackerName) {
434 $this->partialStub('stackerName=' . $stackerName);
435 $isInitialized = TRUE;
438 return $isInitialized;
442 * Getter for size of given stack (array count)
444 * @param $stackerName Name of the stack
445 * @return $count Size of stack (array count)
446 * @throws NoStackerException If given stack is missing
448 public function getStackCount ($stackerName) {
449 // Is the stack not yet initialized?
450 if (!$this->isStackInitialized($stackerName)) {
451 // Throw an exception
452 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
455 // Now, count the array of entries
456 $this->partialStub('stackerName=' . $stackerName);
464 * Adds a value to given stack
466 * @param $stackerName Name of the stack
467 * @param $value Value to add to this stacker
469 * @throws FullStackerException Thrown if the stack is full
471 protected function addValue ($stackerName, $value) {
472 // Is the stack not yet initialized or full?
473 if (!$this->isStackInitialized($stackerName)) {
475 $this->initStack($stackerName);
476 } elseif ($this->isStackFull($stackerName)) {
478 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
481 // Now add the value to the stack
482 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
486 * Get last value from named stacker
488 * @param $stackerName Name of the stack
489 * @return $value Value of last added value
490 * @throws NoStackerException If the named stacker was not found
491 * @throws EmptyStackerException If the named stacker is empty
493 protected function getLastValue ($stackerName) {
494 // Is the stack not yet initialized or full?
495 if (!$this->isStackInitialized($stackerName)) {
496 // Throw an exception
497 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
498 } elseif ($this->isStackEmpty($stackerName)) {
499 // Throw an exception
500 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
503 // Now get the last value
504 $this->partialStub('stackerName=' . $stackerName);
512 * Get first value from named stacker
514 * @param $stackerName Name of the stack
515 * @return $value Value of last added value
516 * @throws NoStackerException If the named stacker was not found
517 * @throws EmptyStackerException If the named stacker is empty
519 protected function getFirstValue ($stackerName) {
520 // Is the stack not yet initialized or full?
521 if (!$this->isStackInitialized($stackerName)) {
522 // Throw an exception
523 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
524 } elseif ($this->isStackEmpty($stackerName)) {
525 // Throw an exception
526 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
529 // Now get the first value
530 $this->partialStub('stackerName=' . $stackerName);
538 * "Pops" last entry from stack
540 * @param $stackerName Name of the stack
541 * @return $value Value "poped" from array
542 * @throws NoStackerException If the named stacker was not found
543 * @throws EmptyStackerException If the named stacker is empty
545 protected function popLast ($stackerName) {
546 // Is the stack not yet initialized or full?
547 if (!$this->isStackInitialized($stackerName)) {
548 // Throw an exception
549 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
550 } elseif ($this->isStackEmpty($stackerName)) {
551 // Throw an exception
552 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
555 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
556 $this->partialStub('stackerName=' . $stackerName);
561 * "Pops" first entry from stack
563 * @param $stackerName Name of the stack
564 * @return $value Value "shifted" from array
565 * @throws NoStackerException If the named stacker was not found
566 * @throws EmptyStackerException If the named stacker is empty
568 protected function popFirst ($stackerName) {
569 // Is the stack not yet initialized or full?
570 if (!$this->isStackInitialized($stackerName)) {
571 // Throw an exception
572 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
573 } elseif ($this->isStackEmpty($stackerName)) {
574 // Throw an exception
575 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
578 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
579 $this->partialStub('stackerName=' . $stackerName);