3 namespace Org\Mxchange\CoreFramework\Filesystem\Index;
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;
13 use \BadMethodCallException;
14 use \InvalidArgumentException;
20 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
39 class IndexFile extends BaseBinaryFile implements IndexableFile {
41 * Protected constructor
45 private function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this File class and prepares it for usage
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
57 public final static function createIndexFile (SplFileInfo $fileInfoInstance, Indexable $indexInstance) {
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();
62 // Set file instance here for callbacks
63 $indexFileInstance->setIndexInstance($indexInstance);
65 // Expand file name with .idx
66 $indexInfoInstance = new SplFileInfo(sprintf('%s.idx', $fileInfoInstance->__toString()));
68 // Init this abstract file
69 $indexFileInstance->initFile($indexInfoInstance);
71 // Init counters and gaps array
72 $indexFileInstance->initCountersGapsArray();
74 // Return the prepared instance
75 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: indexFileInstance=%s - EXIT!', $indexFileInstance->__toString()));
76 return $indexFileInstance;
80 * Flushes the file header
83 * @throws BadMethodCallException If this->indexInstance is not properly set
85 public function flushFileHeader () {
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.');
93 // Call block instance
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->flushFileHeader() ...');
95 $this->getIndexInstance()->flushFileHeader();
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
102 * Pre-allocates file (if enabled) with some space for later faster write access.
104 * @param $type Type of the file
106 * @throws InvalidArgumentException If a parameter is empty
107 * @throws BadMethodCallException If this->indexInstance is not properly set
109 public function preAllocateFile (string $type) {
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: type=%s - CALLED!', $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.');
121 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Pre-allocating file ...');
123 // Calculate minimum block length
124 $minimumBlockLength = $this->getIndexInstance()->calculateMinimumBlockLength();
126 // Call protected method
127 $this->preAllocateFileByTypeLength($type, $minimumBlockLength);
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
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.
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
141 public function isValid () {
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.');
149 // Get length from index
150 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->calculateMinimumBlockLength() ...');
151 $length = $this->getIndexInstance()->calculateMinimumBlockLength();
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);
158 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: isValid=%d - EXIT!', intval($isValid)));
163 * Writes given value to the file and returns a hash and gap position for it
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
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)) {
175 throw new InvalidArgumentException('Parameter "stackName" is empty');
176 } elseif (is_object($value) || is_resource($value)) {
178 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
181 // Encode/convert the value into a "binary format"
182 $encoded = StringUtils::encodeData($value);
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);
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);
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: data[]=%s - EXIT!', gettype($data)));
198 * Writes given raw data to the file and returns a gap position and length
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
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);