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