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 magic->count
33 const SEPARATOR_MAGIC_COUNT = 0x01;
36 * Separator count->position
38 const SEPARATOR_COUNT_SEEK_POS = 0x02;
41 * Separator position->entries
43 const SEPARATOR_SEEK_POS_ENTRIES = 0x03;
46 * Separator hash->name
48 const SEPARATOR_HASH_NAME = 0x04;
53 const COUNT_NAME = 10;
58 const COUNT_LENGTH = 20;
63 const COUNT_POSITION = 20;
66 * Counter for total entries
68 private $totalEntries = 0;
71 * Current seek position
73 private $seekPosition = 0;
76 * Protected constructor
78 * @param $className Name of the class
81 protected function __construct ($className) {
82 // Call parent constructor
83 parent::__construct($className);
87 * Getter for total entries
89 * @return $totalEntries Total entries in this stack
91 private function getCounter () {
93 return $this->totalEntries;
101 private function incrementCounter () {
103 $this->totalEntries++;
107 * Getter for seek position
109 * @return $seekPosition Current seek position (stored here in object)
111 private function getSeekPosition () {
113 return $this->seekPosition;
117 * Setter for seek position
119 * @param $seekPosition Current seek position (stored here in object)
122 private function setSeekPosition ($seekPosition) {
124 $this->seekPosition = $seekPosition;
128 * Updates seekPosition attribute from file to avoid to much access on file.
132 private function updateSeekPosition () {
133 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
135 // Get key (= seek position)
136 $seekPosition = $this->getIteratorInstance()->key();
137 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
140 $this->setSeekPosition($seekPosition);
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
146 * Checks whether the file header is initialized
148 * @return $isInitialized Whether the file header is initialized
150 private function isFileHeaderInitialized () {
151 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
152 // Default is not initialized
153 $isInitialized = FALSE;
155 // Is the file initialized?
156 if ($this->isFileInitialized()) {
157 // Some bytes has been written, so rewind to start of it.
158 $rewindStatus = $this->getIteratorInstance()->rewind();
159 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
161 // Is the rewind() call successfull?
162 if ($rewindStatus != 1) {
163 // Something bad happened
164 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
168 $this->readFileHeader();
172 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
173 return $isInitialized;
177 * Checks whether the file-based stack has been initialized
179 * @return $isInitialized Whether the file's size is zero
181 private function isFileInitialized () {
182 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
184 // Get it from iterator which holds the pointer instance. If FALSE is returned
185 $fileSize = $this->getIteratorInstance()->size();
186 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
189 * The returned file size should not be FALSE or NULL as this means
190 * that the pointer class does not work correctly.
192 assert(is_int($fileSize));
194 // Is more than 0 returned?
195 $isInitialized = ($fileSize > 0);
198 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
199 return $isInitialized;
203 * Creates the file-stack's header
207 private function createFileHeader () {
208 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
209 // The file's header should not be initialized here
210 assert(!$this->isFileHeaderInitialized());
213 $this->flushFileHeader();
215 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
219 * Flushes the file header
223 private function flushFileHeader () {
224 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
226 // Put all informations together
227 $header = sprintf('%s%s%s%s%s%s',
231 // Separator magic<->count
232 chr(self::SEPARATOR_MAGIC_COUNT),
234 // Total entries (will be zero) and pad it to 20 chars
235 str_pad($this->dec2hex($this->getCounter()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
237 // Separator count<->seek position
238 chr(self::SEPARATOR_COUNT_SEEK_POS),
240 // Position (will be zero)
241 str_pad($this->dec2hex($this->getSeekPosition(), 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
243 // Separator position<->entries
244 chr(self::SEPARATOR_SEEK_POS_ENTRIES)
247 // Write it to disk (header is always at seek position 0)
248 $this->getIteratorInstance()->writeAtPosition(0, $header);
250 // Update seek position
251 $this->updateSeekPosition();
253 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
257 * Pre-allocates file (if enabled) with some space for later faster write access.
261 private function preAllocateFile () {
262 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
265 if ($this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_enabled') != 'Y') {
267 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating stack file.', __METHOD__, __LINE__));
269 // Don't continue here.
274 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating stack file ...', __METHOD__, __LINE__));
277 * Calculate minimum length for one entry:
278 * minimum length = hash length + separator + name + minimum entry size = ?? + 1 + 10 + 1 = ??
280 $minLengthEntry = self::getHashLength() + strlen(self::SEPARATOR_HASH_NAME) + self::COUNT_NAME + 1;
281 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
283 // Calulcate seek position
284 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry('file_stack_pre_allocate_count');
285 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
287 // Now seek to the position
288 $seekStatus = $this->getIteratorInstance()->writeAtPosition($seekPosition, chr(0));
291 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
293 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
297 * Initializes this file-based stack.
299 * @param $fileName File name of this stack
302 protected function initFileStack ($fileName) {
303 // Get a file i/o pointer instance
304 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
306 // Get iterator instance
307 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
309 // Is the instance implementing the right interface?
310 assert($iteratorInstance instanceof SeekableWritableFileIterator);
313 $this->setIteratorInstance($iteratorInstance);
315 // Is the file's header initialized?
316 if ($this->isFileHeaderInitialized()) {
318 $this->loadFileHeader();
320 // No, then create it (which may pre-allocate the stack)
321 $this->createFileHeader();
323 // And pre-allocate a bit
324 $this->preAllocateFile();
329 * Initializes given stacker
331 * @param $stackerName Name of the stack
332 * @param $forceReInit Force re-initialization
334 * @throws AlreadyInitializedStackerException If the stack is already initialized
336 public function initStack ($stackerName, $forceReInit = FALSE) {
337 // Is the stack already initialized?
338 if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) {
339 // Then throw the exception
340 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
343 // Initialize the given stack
344 $this->partialStub('stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit));
348 * Checks whether the given stack is initialized (set in array $stackers)
350 * @param $stackerName Name of the stack
351 * @return $isInitialized Whether the stack is initialized
353 public function isStackInitialized ($stackerName) {
355 $this->partialStub('stackerName=' . $stackerName);
356 $isInitialized = TRUE;
359 return $isInitialized;
363 * Getter for size of given stack (array count)
365 * @param $stackerName Name of the stack
366 * @return $count Size of stack (array count)
367 * @throws NoStackerException If given stack is missing
369 public function getStackCount ($stackerName) {
370 // Is the stack not yet initialized?
371 if (!$this->isStackInitialized($stackerName)) {
372 // Throw an exception
373 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
376 // Now, count the array of entries
377 $this->partialStub('stackerName=' . $stackerName);
385 * Adds a value to given stack
387 * @param $stackerName Name of the stack
388 * @param $value Value to add to this stacker
390 * @throws FullStackerException Thrown if the stack is full
392 protected function addValue ($stackerName, $value) {
393 // Is the stack not yet initialized or full?
394 if (!$this->isStackInitialized($stackerName)) {
396 $this->initStack($stackerName);
397 } elseif ($this->isStackFull($stackerName)) {
399 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
402 // Now add the value to the stack
403 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
407 * Get last value from named stacker
409 * @param $stackerName Name of the stack
410 * @return $value Value of last added value
411 * @throws NoStackerException If the named stacker was not found
412 * @throws EmptyStackerException If the named stacker is empty
414 protected function getLastValue ($stackerName) {
415 // Is the stack not yet initialized or full?
416 if (!$this->isStackInitialized($stackerName)) {
417 // Throw an exception
418 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
419 } elseif ($this->isStackEmpty($stackerName)) {
420 // Throw an exception
421 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
424 // Now get the last value
425 $this->partialStub('stackerName=' . $stackerName);
433 * Get first value from named stacker
435 * @param $stackerName Name of the stack
436 * @return $value Value of last added value
437 * @throws NoStackerException If the named stacker was not found
438 * @throws EmptyStackerException If the named stacker is empty
440 protected function getFirstValue ($stackerName) {
441 // Is the stack not yet initialized or full?
442 if (!$this->isStackInitialized($stackerName)) {
443 // Throw an exception
444 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
445 } elseif ($this->isStackEmpty($stackerName)) {
446 // Throw an exception
447 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
450 // Now get the first value
451 $this->partialStub('stackerName=' . $stackerName);
459 * "Pops" last entry from stack
461 * @param $stackerName Name of the stack
462 * @return $value Value "poped" from array
463 * @throws NoStackerException If the named stacker was not found
464 * @throws EmptyStackerException If the named stacker is empty
466 protected function popLast ($stackerName) {
467 // Is the stack not yet initialized or full?
468 if (!$this->isStackInitialized($stackerName)) {
469 // Throw an exception
470 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
471 } elseif ($this->isStackEmpty($stackerName)) {
472 // Throw an exception
473 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
476 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
477 $this->partialStub('stackerName=' . $stackerName);
482 * "Pops" first entry from stack
484 * @param $stackerName Name of the stack
485 * @return $value Value "shifted" from array
486 * @throws NoStackerException If the named stacker was not found
487 * @throws EmptyStackerException If the named stacker is empty
489 protected function popFirst ($stackerName) {
490 // Is the stack not yet initialized or full?
491 if (!$this->isStackInitialized($stackerName)) {
492 // Throw an exception
493 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
494 } elseif ($this->isStackEmpty($stackerName)) {
495 // Throw an exception
496 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
499 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
500 $this->partialStub('stackerName=' . $stackerName);