3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Stack\Stackable;
9 * A Stackable file interface
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 interface StackableFile extends Stackable {
32 * Magic for this stack
34 const STACK_MAGIC = 'STACKv0.1';
37 * Name of array index for gap position
39 const ARRAY_NAME_GAP_POSITION = 'gap';
42 * Name of array index for hash
44 const ARRAY_NAME_HASH = 'hash';
47 * Name of array index for length of raw data
49 const ARRAY_NAME_DATA_LENGTH = 'length';
52 * Seeks to given position
54 * @param $seekPosition Seek position in file
55 * @return $status Status of this operation
57 function seek (int $seekPosition);
62 * @return $size Size (in bytes) of file
67 * Reads the file header
70 * @todo To hard assertions here, better rewrite them to exceptions
71 * @throws UnexpectedValueException If header is not proper length
72 * @throws InvalidMagicException If a bad magic was found
74 function readFileHeader ();
77 * Flushes the file header
81 function flushFileHeader ();
84 * Determines whether the EOF has been reached
86 * @return $isEndOfFileReached Whether the EOF has been reached
87 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
89 function isEndOfFileReached ();
92 * Calculates minimum length for one entry/block
94 * @return $length Minimum length for one entry/block
96 function calculateMinimumBlockLength ();
99 * Initializes counter for valid entries, arrays for damaged entries and
100 * an array for gap seek positions. If you call this method on your own,
101 * please re-analyze the file structure. So you are better to call
102 * analyzeFileStructure() instead of this method.
105 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
107 function initCountersGapsArray ();
110 * Getter for header size
112 * @return $totalEntries Size of file header
113 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
115 function getHeaderSize ();
118 * Setter for header size
120 * @param $headerSize Size of file header
122 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
124 function setHeaderSize (int $headerSize);
127 * Getter for header array
129 * @return $totalEntries Size of file header
130 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
132 function getHeader ();
137 * @param $header Array for a file header
139 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
141 function setHeader (array $header);
144 * Updates seekPosition attribute from file to avoid to much access on file.
147 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
149 function updateSeekPosition ();
152 * Getter for total entries
154 * @return $totalEntries Total entries in this file
155 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
157 function getCounter ();
160 * Writes data at given position
162 * @param $seekPosition Seek position
163 * @param $data Data to be written
164 * @param $flushHeader Whether to flush the header (default: flush)
166 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
168 function writeData (int $seekPosition, string $data, bool $flushHeader = true);
171 * Writes at given position by seeking to it.
173 * @param $seekPosition Seek position in file
174 * @param $dataStream Data to be written
175 * @return mixed Number of writes bytes or false on error
176 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
178 function writeAtPosition (int $seekPosition, string $dataStream);
181 * Writes given value to the file and returns a hash and gap position for it
183 * @param $groupId Group identifier
184 * @param $value Value to be added to the stack
185 * @return $data Hash and gap position
186 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
188 function writeValueToFile (string $groupId, $value);
191 * Searches for next suitable gap the given length of data can fit in
192 * including padding bytes.
194 * @param $length Length of raw data
195 * @return $seekPosition Found next gap's seek position
196 * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
198 function searchNextGap (int $length);
201 * "Getter" for file size
203 * @return $fileSize Size of currently loaded file
205 function getFileSize ();
208 * Writes given raw data to the file and returns a gap position and length
210 * @param $groupId Group identifier
211 * @param $hash Hash from encoded value
212 * @param $encoded Encoded value to be written to the file
213 * @return $data Gap position and length of the raw data
215 function writeDataToFreeGap (string $groupId, string $hash, string $encoded);