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 - 2021 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 * Writes given value to the file and returns a hash and gap position for it
164 * @param $stackName Group identifier
165 * @param $value Value to be added to the stack
166 * @return $data Hash and gap position
167 * @throws InvalidArgumentException If a parameter is not valid
169 public function writeValueToFile (string $stackName, $value) {
170 // Validate parameter
171 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
172 if (empty($stackName)) {
174 throw new InvalidArgumentException('Parameter "stackName" is empty');
175 } elseif (is_object($value) || is_resource($value)) {
177 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
180 // Encode/convert the value into a "binary format"
181 $encoded = StringUtils::encodeData($value);
183 // Get a strong hash for the "encoded" data
184 $hash = CryptoUtils::hash($encoded);
186 // Then write it to the next free gap
187 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
194 * Writes given raw data to the file and returns a gap position and length
196 * @param $stackName Group identifier
197 * @param $hash Hash from encoded value
198 * @param $encoded Encoded value to be written to the file
199 * @return $data Gap position and length of the raw data
200 * @throws UnsupportedOperationException If this method is called
202 public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
203 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
204 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);