3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\Stack\FileStacker;
8 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
11 use Org\Mxchange\CoreFramework\Utils\Crypto\CryptoUtils;
12 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
15 use \BadMethodCallException;
16 use \InvalidArgumentException;
22 * @author Roland Haeder <webmaster@ship-simu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.ship-simu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class StackFile extends BaseBinaryFile implements FileStacker {
43 * Protected constructor
47 private function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this File class and prepares it for usage
55 * @param $infoInstance An instance of a SplFileInfo class
56 * @param $stackInstance An instance of a StackableFile class
57 * @return $stackFileInstance An instance of this File class
59 public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
61 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
62 $stackFileInstance = new StackFile();
64 // Set stack instance here for callbacks
65 $stackFileInstance->setStackInstance($stackInstance);
67 // Init this abstract file
68 $stackFileInstance->initFile($infoInstance);
70 // Init counters and gaps array
71 $stackFileInstance->initCountersGapsArray();
73 // Return the prepared instance
74 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackFileInstance=%s - EXIT!', $stackFileInstance->__toString()));
75 return $stackFileInstance;
79 * Flushes the file header
82 * @throws BadMethodCallException If this->stackInstance is not properly set
84 public function flushFileHeader () {
86 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
87 if (!($this->getStackInstance() instanceof StackableFIle)) {
88 // Index instance not set
89 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
92 // Call block instance
93 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->indexInstance->flushFileHeader() ...');
94 $this->getStackInstance()->flushFileHeader();
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
101 * Pre-allocates file (if enabled) with some space for later faster write access.
103 * @param $type Type of the file
105 * @throws InvalidArgumentException If a parameter is empty
106 * @throws BadMethodCallException If this->stackInstance is not properly set
108 public function preAllocateFile (string $type) {
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: type=%s - CALLED!', $type));
113 throw new InvalidArgumentException('Parameter "type" is empty');
114 } elseif (!($this->getStackInstance() instanceof StackableFile) && !($this->getStackInstance() instanceof StackableFile)) {
115 // Index instance not set
116 throw new BadMethodCallException('this->stackInstance[] and this->pointerInstance are not properly set.');
120 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Pre-allocating file ...');
122 // Calculate minimum block length and get file size
123 $minimumBlockLength = $this->getStackInstance()->calculateMinimumBlockLength();
125 // Call protected method
126 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
133 * Checks wether the current entry is valid (not at the end of the file).
134 * This method will return true if an emptied (nulled) entry has been found.
136 * @return $isValid Whether the next entry is valid
137 * @throws UnexpectedValueException If some value is not expected
138 * @throws BadMethodCallException If this->stackInstance is not properly set
140 public function isValid () {
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
143 if (!($this->getStackInstance() instanceof StackableFile)) {
144 // Index instance not set
145 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
148 // Get length from index
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->calculateMinimumBlockLength() ...');
150 $length = $this->getStackInstance()->calculateMinimumBlockLength();
152 // Call protected method
153 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->isValidByLength(%d) ...', $length));
154 $isValid = $this->isValidByLength($length);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: isValid=%d - EXIT!', intval($isValid)));
162 * Reads the file header
165 * @throws LogicException If both instances are not set
167 public function readFileHeader () {
168 // Call stacke instance
169 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->readStackHeader() - CALLED!');
170 $this->getStackInstance()->readStackHeader();
173 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
177 * Reads next "block" of bytes into $currentBlock field. THis method loads
178 * the whole file into memory when the file is just freshly initialized
179 * (only zeros in it).
183 protected function readNextBlock () {
184 // First calculate minimum block length
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
186 $length = $this->getStackInstance()->calculateMinimumBlockLength();
188 // Call protected method
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling parent::readNextBlockByLength(%d) ...', $length));
190 parent::readNextBlockByLength($length);
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
197 * Writes given value to the file and returns a hash and gap position for it
199 * @param $stackName Group identifier
200 * @param $value Value to be added to the stack
201 * @return $data Hash and gap position
202 * @throws InvalidArgumentException If a parameter is not valid
204 public function writeValueToFile (string $stackName, $value) {
205 // Validate parameter
206 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
207 if (empty($stackName)) {
209 throw new InvalidArgumentException('Parameter "stackName" is empty');
210 } elseif (is_object($value) || is_resource($value)) {
212 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
215 // Encode/convert the value into a "binary format"
216 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling StringUtils::encodeData(value[]=%s) ...', gettype($value)));
217 $encoded = StringUtils::encodeData($value);
219 // Get a strong hash for the "encoded" data
220 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling CryptoUtils::hash(%s) ...', $encoded));
221 $hash = CryptoUtils::hash($encoded);
223 // Then write it to the next free gap
224 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->stackInstance->writeDataToFreeGap(%s,%s,%s) ...', $stackName, $hash, $encoded));
225 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
228 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: data[]=%s - EXIT!', gettype($data)));
233 * Writes given raw data to the file and returns a gap position and length
235 * @param $stackName Group identifier
236 * @param $hash Hash from encoded value
237 * @param $encoded Encoded value to be written to the file
238 * @return $data Gap position and length of the raw data
239 * @throws UnsupportedOperationException If this method is called
241 public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
242 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
243 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);