]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/binary/index/class_IndexFile.php
fd07bd6457277db073a2997c06d6726194fae3e4
[core.git] / framework / main / classes / file_directories / binary / index / class_IndexFile.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Index;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
8 use Org\Mxchange\CoreFramework\Filesystem\Index\IndexableFile;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Index\Indexable;
11
12 // Import SPL stuff
13 use \BadMethodCallException;
14 use \InvalidArgumentException;
15 use \SplFileInfo;
16
17 /**
18  * An index file class
19  *
20  * @author              Roland Haeder <webmaster@ship-simu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.ship-simu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program. If not, see <http://www.gnu.org/licenses/>.
38  */
39 class IndexFile extends BaseBinaryFile implements IndexableFile {
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         private function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this File class and prepares it for usage
52          *
53          * @param       $fileInfoInstance       An instance of a SplFileInfo class
54          * @param       $indexInstance  An instance of a Indexable class
55          * @return      $indexFileInstance      An instance of an IndexableFile class
56          */
57         public final static function createIndexFile (SplFileInfo $fileInfoInstance, Indexable $indexInstance) {
58                 // Get a new instance
59                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInfoInstance[%s]=%s,indexInstance=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $indexInstance->__toString()));
60                 $indexFileInstance = new IndexFile();
61
62                 // Set file instance here for callbacks
63                 $indexFileInstance->setIndexInstance($indexInstance);
64
65                 // Expand file name with .idx
66                 $indexInfoInstance = new SplFileInfo(sprintf('%s.idx', $fileInfoInstance->__toString()));
67
68                 // Init this abstract file
69                 $indexFileInstance->initFile($indexInfoInstance);
70
71                 // Init counters and gaps array
72                 $indexFileInstance->initCountersGapsArray();
73
74                 // Return the prepared instance
75                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: indexFileInstance=%s - EXIT!', $indexFileInstance->__toString()));
76                 return $indexFileInstance;
77         }
78
79         /**
80          * Flushes the file header
81          *
82          * @return      void
83          * @throws      BadMethodCallException  If this->indexInstance is not properly set
84          */
85         public function flushFileHeader () {
86                 // Validate call
87                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: CALLED!');
88                 if (!($this->getIndexInstance() instanceof Indexable)) {
89                         // Index instance not set
90                         throw new BadMethodCallException('this->indexInstance[] is not properly set.');
91                 }
92
93                 // Call block instance
94                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->flushFileHeader() ...');
95                 $this->getIndexInstance()->flushFileHeader();
96
97                 // Trace message
98                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
99         }
100
101         /**
102          * Pre-allocates file (if enabled) with some space for later faster write access.
103          *
104          * @param       $type   Type of the file
105          * @return      void
106          * @throws      InvalidArgumentException        If a parameter is empty
107          * @throws      BadMethodCallException  If this->indexInstance is not properly set
108          */
109         public function preAllocateFile (string $type) {
110                 // Is it enabled?
111                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: type=%s - CALLED!', $type));
112                 if (empty($type)) {
113                         // Empty type
114                         throw new InvalidArgumentException('Parameter "type" is empty');
115                 } elseif (!($this->getIndexInstance() instanceof Indexable)) {
116                         // Index instance not set
117                         throw new BadMethodCallException('this->indexInstance[] is not properly set.');
118                 }
119
120                 // Message to user
121                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Pre-allocating file ...');
122
123                 // Calculate minimum block length
124                 $minimumBlockLength = $this->getIndexInstance()->calculateMinimumBlockLength();
125
126                 // Call protected method
127                 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
128
129                 // Trace message
130                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
131         }
132
133         /**
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.
136          *
137          * @return      $isValid        Whether the next entry is valid
138          * @throws      UnexpectedValueException        If some value is not expected
139          * @throws      BadMethodCallException  If this->indexInstance is not properly set
140          */
141         public function isValid () {
142                 // Validate call
143                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: CALLED!');
144                 if (!($this->getIndexInstance() instanceof Indexable)) {
145                         // Index instance not set
146                         throw new BadMethodCallException('this->indexInstance[] is not properly set.');
147                 }
148
149                 // Get length from index
150                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->calculateMinimumBlockLength() ...');
151                 $length = $this->getIndexInstance()->calculateMinimumBlockLength();
152
153                 // Call protected method
154                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: Calling this->isValidByLength(%d) ...', $length));
155                 $isValid = $this->isValidByLength($length);
156
157                 // Return result
158                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: isValid=%d - EXIT!', intval($isValid)));
159                 return $isValid;
160         }
161
162         /**
163          * Writes given value to the file and returns a hash and gap position for it
164          *
165          * @param       $stackName      Group identifier
166          * @param       $value          Value to be added to the stack
167          * @return      $data           Hash and gap position
168          * @throws      InvalidArgumentException        If a parameter is not valid
169          */
170         public function writeValueToFile (string $stackName, $value) {
171                 // Validate parameter
172                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
173                 if (empty($stackName)) {
174                         // Throw IAE
175                         throw new InvalidArgumentException('Parameter "stackName" is empty');
176                 } elseif (is_object($value) || is_resource($value)) {
177                         // Not wanted here
178                         throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
179                 }
180
181                 // Encode/convert the value into a "binary format"
182                 $encoded = StringUtils::encodeData($value);
183
184                 // Get a strong hash for the "encoded" data
185                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: encoded=%s', $encoded));
186                 $hash = self::hash($encoded);
187
188                 // Then write it to the next free gap
189                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: hash=%s', $hash));
190                 $data = $this->getIndexInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
191
192                 // Return info
193                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: data[]=%s - EXIT!', gettype($data)));
194                 return $data;
195         }
196
197         /**
198          * Writes given raw data to the file and returns a gap position and length
199          *
200          * @param       $stackName      Group identifier
201          * @param       $hash           Hash from encoded value
202          * @param       $encoded        Encoded value to be written to the file
203          * @return      $data           Gap position and length of the raw data
204          * @throws      UnsupportedOperationException   If this method is called
205          */
206         public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
207                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
208                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
209         }
210
211 }