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\FrameworkInterface;
10 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
11 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
12 use Org\Mxchange\CoreFramework\Utils\Crypto\CryptoUtils;
13 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
16 use \BadMethodCallException;
17 use \InvalidArgumentException;
23 * @author Roland Haeder <webmaster@ship-simu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.ship-simu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class StackFile extends BaseBinaryFile implements FileStacker {
44 * Protected constructor
48 private function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
54 * Creates an instance of this File class and prepares it for usage
56 * @param $infoInstance An instance of a SplFileInfo class
57 * @param $stackInstance An instance of a StackableFile class
58 * @return $stackFileInstance An instance of this File class
60 public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
62 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
63 $stackFileInstance = new StackFile();
65 // Set stack instance here for callbacks
66 $stackFileInstance->setStackInstance($stackInstance);
68 // Init this abstract file
69 $stackFileInstance->initFile($infoInstance);
71 // Init counters and gaps array
72 $stackFileInstance->initCountersGapsArray();
74 // Return the prepared instance
75 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackFileInstance=%s - EXIT!', $stackFileInstance->__toString()));
76 return $stackFileInstance;
80 * Flushes the file header
83 * @throws BadMethodCallException If this->stackInstance is not properly set
85 public function flushFileHeader () {
87 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
88 if (!($this->getStackInstance() instanceof StackableFIle)) {
89 // Index instance not set
90 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
93 // Call block instance
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Invoking this->indexInstance->flushFileHeader() ...');
95 $this->getStackInstance()->flushFileHeader();
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
102 * Pre-allocates file (if enabled) with some space for later faster write access.
104 * @param $type Type of the file
106 * @throws InvalidArgumentException If a parameter is empty
107 * @throws BadMethodCallException If this->stackInstance is not properly set
109 public function preAllocateFile (string $type) {
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: type=%s - CALLED!', $type));
114 throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
115 } elseif (!($this->getStackInstance() instanceof StackableFile) && !($this->getStackInstance() instanceof StackableFile)) {
116 // Index instance not set
117 throw new BadMethodCallException('this->stackInstance[] and this->pointerInstance are not properly set.');
121 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Pre-allocating file ...');
123 // Calculate minimum block length and get file size
124 $minimumBlockLength = $this->getStackInstance()->calculateMinimumBlockLength();
126 // Call protected method
127 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
134 * Checks wether the current entry is valid (not at the end of the file).
135 * This method will return true if an emptied (nulled) entry has been found.
137 * @return $isValid Whether the next entry is valid
138 * @throws UnexpectedValueException If some value is not expected
139 * @throws BadMethodCallException If this->stackInstance is not properly set
141 public function isValid () {
143 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
144 if (!($this->getStackInstance() instanceof StackableFile)) {
145 // Index instance not set
146 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
149 // Get length from index
150 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Invoking this->stackInstance->calculateMinimumBlockLength() ...');
151 $length = $this->getStackInstance()->calculateMinimumBlockLength();
153 // Call protected method
154 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Invoking this->isValidByLength(%d) ...', $length));
155 $isValid = $this->isValidByLength($length);
158 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: isValid=%d - EXIT!', intval($isValid)));
163 * Reads the file header
166 * @throws LogicException If both instances are not set
168 public function readFileHeader () {
169 // Call stacke instance
170 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Invoking this->stackInstance->readStackHeader() - CALLED!');
171 $this->getStackInstance()->readStackHeader();
174 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
178 * Reads next "block" of bytes into $currentBlock field. THis method loads
179 * the whole file into memory when the file is just freshly initialized
180 * (only zeros in it).
184 protected function readNextBlock () {
185 // First calculate minimum block length
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
187 $length = $this->getStackInstance()->calculateMinimumBlockLength();
189 // Call protected method
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Invoking parent::readNextBlockByLength(%d) ...', $length));
191 parent::readNextBlockByLength($length);
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
198 * Writes given value to the file and returns a hash and gap position for it
200 * @param $stackName Group identifier
201 * @param $value Value to be added to the stack
202 * @return $data Hash and gap position
203 * @throws InvalidArgumentException If a parameter is not valid
205 public function writeValueToFile (string $stackName, $value) {
206 // Validate parameter
207 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
208 if (empty($stackName)) {
210 throw new InvalidArgumentException('Parameter "stackName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
211 } elseif (is_object($value) || is_resource($value)) {
213 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
216 // Encode/convert the value into a "binary format"
217 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Invoking StringUtils::encodeData(value[]=%s) ...', gettype($value)));
218 $encoded = StringUtils::encodeData($value);
220 // Get a strong hash for the "encoded" data
221 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Invoking CryptoUtils::hash(%s) ...', $encoded));
222 $hash = CryptoUtils::hash($encoded);
224 // Then write it to the next free gap
225 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Invoking this->stackInstance->writeDataToFreeGap(%s,%s,%s) ...', $stackName, $hash, $encoded));
226 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
229 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: data[]=%s - EXIT!', gettype($data)));
234 * Writes given raw data to the file and returns a gap position and length
236 * @param $stackName Group identifier
237 * @param $hash Hash from encoded value
238 * @param $encoded Encoded value to be written to the file
239 * @return $data Gap position and length of the raw data
240 * @throws UnsupportedOperationException If this method is called
242 public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
243 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
244 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);