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\String\StringUtils;
14 use \BadMethodCallException;
15 use \InvalidArgumentException;
21 * @author Roland Haeder <webmaster@ship-simu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.ship-simu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class StackFile extends BaseBinaryFile implements FileStacker {
42 * Protected constructor
46 private function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this File class and prepares it for usage
54 * @param $infoInstance An instance of a SplFileInfo class
55 * @param $stackInstance An instance of a StackableFile class
56 * @return $stackFileInstance An instance of this File class
58 public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
60 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
61 $stackFileInstance = new StackFile();
63 // Set stack instance here for callbacks
64 $stackFileInstance->setStackInstance($stackInstance);
66 // Init this abstract file
67 $stackFileInstance->initFile($infoInstance);
69 // Init counters and gaps array
70 $stackFileInstance->initCountersGapsArray();
72 // Return the prepared instance
73 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackFileInstance=%s - EXIT!', $stackFileInstance->__toString()));
74 return $stackFileInstance;
78 * Flushes the file header
81 * @throws BadMethodCallException If this->stackInstance is not properly set
83 public function flushFileHeader () {
85 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
86 if (!($this->getStackInstance() instanceof StackableFIle)) {
87 // Index instance not set
88 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
91 // Call block instance
92 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->indexInstance->flushFileHeader() ...');
93 $this->getStackInstance()->flushFileHeader();
96 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
100 * Pre-allocates file (if enabled) with some space for later faster write access.
102 * @param $type Type of the file
104 * @throws InvalidArgumentException If a parameter is empty
105 * @throws BadMethodCallException If this->stackInstance is not properly set
107 public function preAllocateFile (string $type) {
109 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: type=%s - CALLED!', $type));
112 throw new InvalidArgumentException('Parameter "type" is empty');
113 } elseif (!($this->getStackInstance() instanceof StackableFile) && !($this->getStackInstance() instanceof StackableFile)) {
114 // Index instance not set
115 throw new BadMethodCallException('this->stackInstance[] and this->pointerInstance are not properly set.');
119 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Pre-allocating file ...');
121 // Calculate minimum block length and get file size
122 $minimumBlockLength = $this->getStackInstance()->calculateMinimumBlockLength();
124 // Call protected method
125 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
132 * Checks wether the current entry is valid (not at the end of the file).
133 * This method will return true if an emptied (nulled) entry has been found.
135 * @return $isValid Whether the next entry is valid
136 * @throws UnexpectedValueException If some value is not expected
137 * @throws BadMethodCallException If this->stackInstance is not properly set
139 public function isValid () {
141 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: CALLED!');
142 if (!($this->getStackInstance() instanceof StackableFile)) {
143 // Index instance not set
144 throw new BadMethodCallException('this->stackInstance[] is not properly set.');
147 // Get length from index
148 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->calculateMinimumBlockLength() ...');
149 $length = $this->getStackInstance()->calculateMinimumBlockLength();
151 // Call protected method
152 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->isValidByLength(%d) ...', $length));
153 $isValid = $this->isValidByLength($length);
156 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: isValid=%d - EXIT!', intval($isValid)));
161 * Writes given value to the file and returns a hash and gap position for it
163 * @param $stackName Group identifier
164 * @param $value Value to be added to the stack
165 * @return $data Hash and gap position
166 * @throws InvalidArgumentException If a parameter is not valid
168 public function writeValueToFile (string $stackName, $value) {
169 // Validate parameter
170 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
171 if (empty($stackName)) {
173 throw new InvalidArgumentException('Parameter "stackName" is empty');
174 } elseif (is_object($value) || is_resource($value)) {
176 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
179 // Encode/convert the value into a "binary format"
180 $encoded = StringUtils::encodeData($value);
182 // Get a strong hash for the "encoded" data
183 $hash = self::hash($encoded);
185 // Then write it to the next free gap
186 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
193 * Writes given raw data to the file and returns a gap position and length
195 * @param $stackName Group identifier
196 * @param $hash Hash from encoded value
197 * @param $encoded Encoded value to be written to the file
198 * @return $data Gap position and length of the raw data
199 * @throws UnsupportedOperationException If this method is called
201 public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
202 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
203 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);