]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/binary/stack/class_StackFile.php
42a2412f007c4e28bb154f726f33fad5f2496ae9
[core.git] / framework / main / classes / file_directories / binary / stack / class_StackFile.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack\File;
4
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;
13
14 // Import SPL stuff
15 use \BadMethodCallException;
16 use \InvalidArgumentException;
17 use \SplFileInfo;
18
19 /**
20  * A stack file class
21  *
22  * @author              Roland Haeder <webmaster@ship-simu.org>
23  * @version             0.0.0
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
27  *
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.
32  *
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.
37  *
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/>.
40  */
41 class StackFile extends BaseBinaryFile implements FileStacker {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         private function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of this File class and prepares it for usage
54          *
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
58          */
59         public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
60                 // Get a new instance
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();
63
64                 // Set stack instance here for callbacks
65                 $stackFileInstance->setStackInstance($stackInstance);
66
67                 // Init this abstract file
68                 $stackFileInstance->initFile($infoInstance);
69
70                 // Init counters and gaps array
71                 $stackFileInstance->initCountersGapsArray();
72
73                 // Return the prepared instance
74                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackFileInstance=%s - EXIT!', $stackFileInstance->__toString()));
75                 return $stackFileInstance;
76         }
77
78         /**
79          * Flushes the file header
80          *
81          * @return      void
82          * @throws      BadMethodCallException  If this->stackInstance is not properly set
83          */
84         public function flushFileHeader () {
85                 // Validate call
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.');
90                 }
91
92                 // Call block instance
93                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->indexInstance->flushFileHeader() ...');
94                 $this->getStackInstance()->flushFileHeader();
95
96                 // Trace message
97                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
98         }
99
100         /**
101          * Pre-allocates file (if enabled) with some space for later faster write access.
102          *
103          * @param       $type   Type of the file
104          * @return      void
105          * @throws      InvalidArgumentException        If a parameter is empty
106          * @throws      BadMethodCallException  If this->stackInstance is not properly set
107          */
108         public function preAllocateFile (string $type) {
109                 // Is it enabled?
110                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: type=%s - CALLED!', $type));
111                 if (empty($type)) {
112                         // Empty 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.');
117                 }
118
119                 // Message to user
120                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Pre-allocating file ...');
121
122                 // Calculate minimum block length and get file size
123                 $minimumBlockLength = $this->getStackInstance()->calculateMinimumBlockLength();
124
125                 // Call protected method
126                 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
127
128                 // Trace message
129                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
130         }
131
132         /**
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.
135          *
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
139          */
140         public function isValid () {
141                 // Validate call
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.');
146                 }
147
148                 // Get length from index
149                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->calculateMinimumBlockLength() ...');
150                 $length = $this->getStackInstance()->calculateMinimumBlockLength();
151
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);
155
156                 // Return result
157                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: isValid=%d - EXIT!', intval($isValid)));
158                 return $isValid;
159         }
160
161         /**
162          * Writes given value to the file and returns a hash and gap position for it
163          *
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
168          */
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)) {
173                         // Throw IAE
174                         throw new InvalidArgumentException('Parameter "stackName" is empty');
175                 } elseif (is_object($value) || is_resource($value)) {
176                         // Not wanted here
177                         throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
178                 }
179
180                 // Encode/convert the value into a "binary format"
181                 $encoded = StringUtils::encodeData($value);
182
183                 // Get a strong hash for the "encoded" data
184                 $hash = CryptoUtils::hash($encoded);
185
186                 // Then write it to the next free gap
187                 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
188
189                 // Return info
190                 return $data;
191         }
192
193         /**
194          * Writes given raw data to the file and returns a gap position and length
195          *
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
201          */
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);
205         }
206
207 }